Database connections actually work now
Jeremy D

Jeremy D commited on 2011-11-06 12:40:25
Showing 1 changed files, with 124 additions and 43 deletions.

... ...
@@ -31,9 +31,8 @@ class tbg_converter
31 31
 	protected $main_credits = 'TBG converter © Joshua Dickerson & Jeremy Darwood';
32 32
 
33 33
 	// The host, user, and pass must be on the same server
34
-	protected $db_driver = 'mysql';
35
-	protected $db_host = 'localhost';
36
-	protected $db_user = '';
34
+	protected $db_dsn = 'mysql:host=localhost;dbname=tbg3';
35
+	protected $db_user = 'tb3';
37 36
 	protected $db_pass = '';
38 37
 
39 38
 	protected $mbt_db_name = 'mantis';
... ...
@@ -160,7 +159,6 @@ class tbg_converter
160 159
 	*/
161 160
 	public function doConversion()
162 161
 	{
163
-		$this->setDatabasePrefix();
164 162
 		$this->getDatabaseConnection();
165 163
 
166 164
 		// Now restart.
... ...
@@ -210,7 +208,7 @@ class tbg_converter
210 208
 	public function converterSettings()
211 209
 	{
212 210
 		return array(
213
-			'tbg_loc' => array('name' => 'The Bug Genie location', 'type' => 'text', 'required' => true, 'default' => dirname(__FILE__), 'validate' => 'return file_exists($data) && file_exists($data . \'/index.php\');'),
211
+			'tbg_loc' => array('name' => 'The Bug Genie Core location', 'type' => 'text', 'required' => true, 'default' => dirname(__FILE__), 'validate' => 'return file_exists($data) && file_exists($data . \'/b2db_bootstrap.inc.php\');'),
214 212
 			// @TODO: Make this validate the password.
215 213
 			'tbg_db_pass' => array('name' => 'The Bug Genie database password', 'type' => 'password', 'required' => true, 'validate' => 'return true;',),
216 214
 		);
... ...
@@ -233,7 +231,7 @@ class tbg_converter
233 231
 		}
234 232
 
235 233
 		// Load some from the url.
236
-		$this->step = (int) $_GET['step'];
234
+		$this->step = isset($_GET['step']) ? (int) $_GET['step'] : 1;
237 235
 
238 236
 		$this->substep = isset($_GET['substep']) ? (int) $_GET['substep'] : 0;
239 237
 	}
... ...
@@ -274,22 +272,70 @@ class tbg_converter
274 272
 		return null;
275 273
 	}
276 274
 
275
+	/**
276
+	 * Sets the database username from bootstrap.
277
+	 * @param string $db_user = The database user name.
278
+	 */
279
+	public function setUname($db_user)
280
+	{
281
+		$this->db_user = $db_user;
282
+
283
+		return true;
284
+	}
285
+
286
+	/**
287
+	 * Sets the database password from bootstrap.
288
+	 * @param string $db_pass = The database user's password.
289
+	 */
290
+	public function setPasswd($db_pass)
291
+	{
292
+		$this->db_pass = $db_pass;
293
+
294
+		return true;
295
+	}
296
+
277 297
 	/**
278 298
 	 * Set the prefix that will be used prior to every reference of a table
279 299
 	 */
280
-	public function setTBGDatabasePrefix()
300
+	public function setTablePrefix()
281 301
 	{
282 302
 		$this->tbg_db_prefix = $this->tbg_db_name . '.' . $this->tbg_db_table_prefix;
283 303
 
284 304
 		return true;
285 305
 	}
306
+
286 307
 	/**
308
+	 * Set the DSN for our database connect
309
+	 * @param string $db_dsn = The DSN used for database connections.
310
+	 */
311
+	public function setDSN($db_dsn)
312
+	{
313
+		$this->db_dsn = $db_dsn;
314
+
315
+		return true;
316
+	}
317
+
287 318
 
319
+	/**
288 320
 	 * Establish a database connection
289 321
 	 */
290 322
 	function getDatabaseConnection()
291 323
 	{
292
-		$this->db = new PDO ($this->db_driver . ':host=' . $this->db_host, $this->db_user, $this->db_pass);
324
+		// Lets locate TBG Core
325
+		require_once($this->tbg_loc . '/b2db_bootstrap.inc.php');
326
+
327
+		// We first try to setup TBG connection.
328
+		try
329
+		{
330
+			$this->tbg_db = new PDO ($this->db_dsn, $this->db_user, $this->db_pass);
331
+		}
332
+		catch (PDOException $e)
333
+		{
334
+			exit('Connection failed' . $e->getMessage());
335
+		}
336
+
337
+		// Set the prefixes.
338
+		$this->setTablePrefix();
293 339
 
294 340
 		return true;
295 341
 	}
... ...
@@ -308,7 +354,7 @@ class tbg_converter
308 354
 
309 355
 		// All of the possible characters we will allow.
310 356
 		$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*_=+.,?/[]{}|';
311
-		$chars_len = strlen($chars);
357
+		$chars_len = strlen($chars) - 1;
312 358
 
313 359
 		$random_string = '';
314 360
 
... ...
@@ -396,7 +442,7 @@ class tbg_converter
396 442
 		// @ !!! Will need to strip doStep from the function name and cast as a int for security.
397 443
 		$real_step = str_replace('doStep', '', $function);
398 444
 
399
-		if (!is_int($real_step) || (int) $real_step == 0)
445
+		if (!is_int($real_step) && (int) $real_step == 0)
400 446
 		{
401 447
 			debug_print_backtrace();
402 448
 			exit;
... ...
@@ -404,9 +450,9 @@ class tbg_converter
404 450
 
405 451
 		// If ajax, return some data.
406 452
 		$data = array(
407
-			'step' => $function,
453
+			'step' => $real_step,
408 454
 			'time' => time(),
409
-			'substep' => getSubStep($function),
455
+			'substep' => $this->substep,
410 456
 		);
411 457
 
412 458
 		if ($this->is_json)
... ...
@@ -439,7 +485,6 @@ class mbt_to_tbg extends tbg_converter
439 485
 	 */
440 486
 	public function setDatabasePrefix()
441 487
 	{
442
-		$this->setTBGDatabasePrefix();
443 488
 		$this->mbt_db_prefix = $this->mbt_db_name . '.' . $this->mbt_db_table_prefix;
444 489
 	}
445 490
 
... ...
@@ -457,6 +502,41 @@ class mbt_to_tbg extends tbg_converter
457 502
 		);
458 503
 	}
459 504
 
505
+	/**
506
+	 * Establish a database connection
507
+	 */
508
+	function getDatabaseConnection()
509
+	{
510
+		global $g_database_name;
511
+
512
+		// First load TBG.
513
+		parent::getDatabaseConnection();
514
+
515
+		// Now lets load up Mantis stuff.
516
+		define('ON', true);
517
+		define('OFF', false);
518
+		define('PHPMAILER_METHOD_MAIL', 'mail');
519
+		define('DATABASE', 'mysql');
520
+		define('NOBODY', 'mantis');
521
+		require_once($this->mantis_loc . '/config_inc.php');
522
+
523
+		// Save the mantis info.
524
+		$this->mbt_db_name = $g_database_name;
525
+		$this->mbt_db_table_prefix = isset($g_db_table_prefix) ? $g_db_table_prefix . '_' : 'mantis_';
526
+
527
+		// Try to start a mantis connection.
528
+		try
529
+		{
530
+			$this->mantis_db = new PDO ($g_db_type . ':host=' . $g_hostname . ';dbname=' . $g_database_name, $g_db_username, $g_db_password);
531
+		}
532
+		catch (PDOException $e)
533
+		{
534
+			exit('Connection failed' . $e->getMessage());
535
+		}
536
+
537
+		$this->setDatabasePrefix();
538
+	}
539
+
460 540
 	/**
461 541
 	 * Sets the list types in TBG to be like MBT
462 542
 	 * 
... ...
@@ -519,18 +599,19 @@ class mbt_to_tbg extends tbg_converter
519 599
 		$query = '
520 600
 			SELECT
521 601
 				id, username, username AS buddyname, realname, email, password,
522
-				enabled, last_vist AS lastseen, date_created AS joined
602
+				enabled, last_visit AS lastseen, date_created AS joined
523 603
 			FROM
524 604
 				' . $this->mbt_db_prefix . 'user_table
525
-			LIMIT ' . $substep . ' ' . $step_size;
605
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
526 606
 
527 607
 		// We could let this save up a few inserts then do them at once, but are we really worried about saving queries here?
528 608
 		$i = 0;
529
-		foreach ($this->db->query($query) as $row)
609
+
610
+		foreach ($this->mantis_db->query($query) as $row)
530 611
 		{
531 612
 			$password = $this->getRandomString();
532 613
 
533
-			$this->db->query('
614
+			$this->tbg_db->query('
534 615
 				INSERT INTO ' . $this->tbg_db_prefix . 'users (id, username, buddyname, realname, email, password, enabled, lastseen, joined)
535 616
 				VALUES (' . $row['id'] . ', "' . $row['username'] . '", "' . $row['buddyname'] . '", "' . $row['realname'] . '", "' . $row['email'] . '", "' . $password . '", ' . $row['enabled'] . ', ' . $row['username'] . ', ' . $row['joined'] . ')');
536 617
 
... ...
@@ -554,12 +635,12 @@ class mbt_to_tbg extends tbg_converter
554 635
 				id, name, description,
555 636
 				(CASE WHEN enabled = 0 THEN 1 ELSE 0) AS locked
556 637
 				FROM ' . $this->mbt_db_prefix . 'project_table
557
-			LIMIT ' . $substep . ' ' . $step_size;
638
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
558 639
 
559 640
 		$i = 0;
560
-		foreach ($this->db->query($query) as $row)
641
+		foreach ($this->mantis_db->query($query) as $row)
561 642
 		{
562
-			$this->db->query('
643
+			$this->tbg_db->query('
563 644
 				INSERT INTO ' . $this->tbg_db_prefix . 'projects (id, name, locked, description)
564 645
 				VALUES (' . $row['id'] . ', "' . $row['name'] . '", ' . $row['locked'] . ', "' . $row['description'] . '")');
565 646
 
... ...
@@ -582,12 +663,12 @@ class mbt_to_tbg extends tbg_converter
582 663
 			SELECT
583 664
 				name
584 665
 				FROM ' . $this->mbt_db_prefix . 'category_table
585
-			LIMIT ' . $substep . ' ' . $step_size;
666
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
586 667
 
587 668
 		$i = 0;
588
-		foreach ($this->db->query($query) as $row)
669
+		foreach ($this->mantis_db->query($query) as $row)
589 670
 		{
590
-			$this->db->query('
671
+			$this->tbg_db->query('
591 672
 				INSERT INTO ' . $this->tbg_db_prefix . 'listtypes (name, itemtype, scope)
592 673
 				VALUES (' . $row['name'] . ', "category", 1)');
593 674
 
... ...
@@ -612,22 +693,22 @@ class mbt_to_tbg extends tbg_converter
612 693
 				id, name
613 694
 				FROM ' . $this->mbt_db_prefix . 'builds';
614 695
 		$builds = array();
615
-		foreach ($this->db->query($sql) as $row)
696
+		foreach ($this->tbg_db->query($sql) as $row)
616 697
 			$builds[$row['name']] = $row['id'];
617 698
 
618 699
 		$query = '
619 700
 			SELECT
620 701
 				id, project_id, version, released AS isreleased
621 702
 				FROM ' . $this->mbt_db_prefix . 'project_version_table
622
-			LIMIT ' . $substep . ' ' . $step_size;
703
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
623 704
 
624 705
 		$i = 0;
625
-		foreach ($this->db->query($query) as $row)
706
+		foreach ($this->mantis_db->query($query) as $row)
626 707
 		{
627 708
 			if (isset($builds[$version]))
628 709
 				continue;
629 710
 
630
-			$this->db->query('
711
+			$this->tbg_db->query('
631 712
 				INSERT INTO (' . $this->tbg_db_prefix . 'builds (name) VALUES (' . $row['version'] . ')');
632 713
 
633 714
 			$builds[$row['version']] = $this->db->lastInsertId();
... ...
@@ -654,7 +735,7 @@ class mbt_to_tbg extends tbg_converter
654 735
 				id, name
655 736
 				FROM ' . $this->mbt_db_prefix . 'builds';
656 737
 		$builds = array();
657
-		foreach ($this->db->query($query) as $row)
738
+		foreach ($this->tbg_db->query($query) as $row)
658 739
 			$builds[$row['name']] = $row['id'];
659 740
 
660 741
 		$query = '
... ...
@@ -673,19 +754,19 @@ class mbt_to_tbg extends tbg_converter
673 754
 					version
674 755
 				FROM ' . $this->mbt_db_prefix . 'bug_table AS bt
675 756
 					LEFT JOIN ' . $this->mbt_db_prefix . 'bug_text_table AS btt ON (btt.id = bt.bug_text_id)
676
-			LIMIT ' . $substep . ' ' . $step_size;
757
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
677 758
 
678 759
 		$i = 0;
679
-		foreach ($this->db->query($sql) as $row)
760
+		foreach ($this->mantis_db->query($sql) as $row)
680 761
 		{
681
-			$this->db->query('
762
+			$this->tbg_db->query('
682 763
 				INSERT INTO ' . $this->tbg_db_prefix . 'issues (id, project_id, title, assigned_to, duplicate_of, posted, last_updated, state, category, resolution, priority, severity, reproducability)
683 764
 				VALUES (' . $row['id'] . ', ' . $row['project_id'] . ', "' . $row['title'] . '", ' . $row['assigned_to'] . ', ' . $row['duplicate_of'] . ', ' . $row['posted'] . ', ' . $row['last_updated'] . ', ' . $row['state'] . ', ' . $row['category'] . ', ' . $row['category'] . ', ' . $row['resolution'] . ', ' . $row['priority'] . ', ' . $row['severity'] . ', ' . $row['reproducability'] . ')
684 765
 			');
685 766
 
686 767
 			if (!isset($builds[$row['version']]))
687 768
 			{
688
-				$this->db->query('
769
+				$this->tbg_db->query('
689 770
 					INSERT INTO (' . $this->tbg_db_prefix . 'builds (name) VALUES (' . $row['version'] . ')');
690 771
 
691 772
 				$builds[$row['version']] = $this->db->lastInsertId();	
... ...
@@ -693,7 +774,7 @@ class mbt_to_tbg extends tbg_converter
693 774
 
694 775
 			$affect_id = $builds[$row['version']];
695 776
 
696
-			$this->db->query('
777
+			$this->tbg_db->query('
697 778
 				INSERT INTO (' . $this->tbg_db_prefix . 'issueaffectsbuild (id, build) VALUES(' . $row['id'] . ', ' . $affect_id . ')');
698 779
 
699 780
 			++$i;
... ...
@@ -717,12 +798,12 @@ class mbt_to_tbg extends tbg_converter
717 798
 				bn.reporter_id AS updated_by, bn.reporter_id AS posted_by, bnt.note AS content
718 799
 				FROM ' . $this->mbt_db_prefix . 'bugnote_table AS bn
719 800
 					INNER JOIN ' . $this->mbt_db_prefix . 'butnote_text_table AS bnt ON (bn.id = bnt.bug_text_id)
720
-			LIMIT ' . $substep . ' ' . $step_size;
801
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
721 802
 
722 803
 		$i = 0;
723
-		foreach ($this->db->query($query) as $row)
804
+		foreach ($this->mantis_db->query($query) as $row)
724 805
 		{
725
-			$this->db->query('
806
+			$this->tbg_db->query('
726 807
 				INSERT INTO ' . $this->tbg_db_prefix . 'comments (id, target_id, updated, posted, updated_by, posted_by, content)
727 808
 				VALUES (' . $row['id'] . ', ' . $row['target_id'] . ', ' . $row['updated'] . ', ' . $row['posted'] . ', ' . $row['updated_by'] . ', ' . $row['posted_by'] . ', "' . $row['content'] . '")');
728 809
 
... ...
@@ -745,12 +826,12 @@ class mbt_to_tbg extends tbg_converter
745 826
 			SELECT
746 827
 				source_bug_id AS parent_id, destination_bug_id AS child_id
747 828
 				FROM ' . $this->mbt_db_prefix . 'bug_relationships_table
748
-			LIMIT ' . $substep . ' ' . $step_size;
829
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
749 830
 
750 831
 		$i = 0;
751
-		foreach ($this->db->query($query) as $row)
832
+		foreach ($this->mantis_db->query($query) as $row)
752 833
 		{
753
-			$this->db->query('
834
+			$this->tbg_db->query('
754 835
 				INSERT INTO ' . $this->tbg_db_prefix . 'issuerelations (parent_id, child_id)
755 836
 				VALUES (' . $row['parent_id'] . ', ' . $row['child_id'] . ')');
756 837
 
... ...
@@ -781,12 +862,12 @@ class mbt_to_tbg extends tbg_converter
781 862
 				id, user_id AS uid, 1 AS scope, filename AS real_filename, filename AS original_filename,
782 863
 				file_type AS content_type, content, date_added AS uploaded_at, description
783 864
 				FROM ' . $this->mbt_db_prefix . 'bug_file_table
784
-			LIMIT ' . $substep . ' ' . $step_size;
865
+			LIMIT ' . $step_size . ' OFFSET ' . $substep;
785 866
 
786 867
 		$i = 0;
787
-		foreach ($this->db->query($query) as $row)
868
+		foreach ($this->mantis_db->query($query) as $row)
788 869
 		{
789
-			$this->db->query('
870
+			$this->tbg_db->query('
790 871
 				INSERT INTO ' . $this->tbg_db_prefix . 'files (id, uid, scope, real_filename, original_filename, content_type, content, uploaded_at, description)
791 872
 				VALUES (' . $row['id'] . ', ' . $row['uid'] . ', ' . $row['scope'] . ', "' . $row['real_filename'] . '", "' . $row['original_filename'] . '", "' . $row['content_type'] . '", "' . $row['content'] . '", ' . $row['uploaded_at'] . ', "' . $row['description'] . '")');
792 873
 
... ...
@@ -1260,7 +1341,7 @@ class tbg_converter_wrapper
1260 1341
 			elseif ($data['type'] == 'password')
1261 1342
 				echo '<input type="password" name="', $key, '" />';
1262 1343
 			else
1263
-				echo '<input type="text" name="', $key, '"', isset($data['default']) ? ' value="' . $data['default'] . '"' : (isset($_POST[$key]) ? $_POST[$key] : ''), ' />';
1344
+				echo '<input type="text" name="', $key, '"', isset($_POST[$key]) ? ' value="' . $_POST[$key] . '"' : (isset($data['default']) ? $data['default'] : ''), ' />';
1264 1345
 
1265 1346
 			echo '</dd>';
1266 1347
 		}
1267 1348