Moved permissions to their own step
Jeremy D

Jeremy D commited on 2011-11-06 16:20:04
Showing 1 changed files, with 67 additions and 30 deletions.

... ...
@@ -489,12 +489,13 @@ class mbt_to_tbg extends tbg_converter
489 489
 		// Key => array('Descriptive', 'functionName', (int) step_size),
490 490
 		1 => array('Users', 'doStep1', 500),
491 491
 		array('Projects', 'doStep2', 500),
492
-		array('Categories', 'doStep3', 500),
493
-		array('Versions', 'doStep4', 500),
494
-		array('Issues', 'doStep5', 500),
495
-		array('Comments', 'doStep6', 500),
496
-		array('Relationships', 'doStep7', 500),
497
-		array('Attachments', 'doStep8', 100),
492
+		array('Project Permissions', 'doStep3', 1),
493
+		array('Categories', 'doStep4', 500),
494
+		array('Versions', 'doStep5', 500),
495
+		array('Issues', 'doStep6', 500),
496
+		array('Comments', 'doStep7', 500),
497
+		array('Relationships', 'doStep8', 500),
498
+		array('Attachments', 'doStep9', 100),
498 499
 	);
499 500
 	/**
500 501
 	 * Set the prefix that will be used prior to every reference of a table
... ...
@@ -656,24 +657,15 @@ class mbt_to_tbg extends tbg_converter
656 657
 		$i = 0;
657 658
 		foreach ($this->mantis_db->query($query) as $row)
658 659
 		{
659
-			$this->tbg_db->query('
660
-				REPLACE INTO ' . $this->tbg_db_prefix . 'projects (id, name, locked, description, scope, workflow_scheme_id, issuetype_scheme_id)
661
-				VALUES (' . $row['id'] . ', "' . $row['name'] . '", ' . $row['locked'] . ', "' . $row['description'] . '", 1, 1, 1)');
660
+			$key = strtr($row['name'], array(
661
+				' - ' => '-',
662
+				' ' => '_',
663
+			));
662 664
 
663
-			// Add the default permissions.
665
+				// We have to use ` on key otherwise mysql errors.
664 666
 				$this->tbg_db->query('
665
-				REPLACE INTO ' . $this->tbg_db_prefix . 'permissions (permission_type, target_id, allowed, module, uid, gid, tid, scope) VALUES
666
-					("canseeproject", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
667
-					("canseeprojecthierarchy", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
668
-					("canmanageproject", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
669
-					("page_project_allpages_access", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
670
-					("canvoteforissues", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
671
-					("canlockandeditlockedissues", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
672
-					("cancreateandeditissues", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
673
-					("caneditissue", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
674
-					("caneditissuecustomfields", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
675
-					("canaddextrainformationtoissues", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1),
676
-					("canpostseeandeditallcomments", ' . $row['id'] . ', 1, "core", 1, 0, 0, 1)');
667
+				REPLACE INTO ' . $this->tbg_db_prefix . 'projects (id, name, `key`, locked, description, scope, workflow_scheme_id, issuetype_scheme_id)
668
+				VALUES (' . $row['id'] . ', "' . $row['name'] . '", "' . $key . '", ' . $row['locked'] . ', "' . $row['description'] . '", 1, 1, 1)');
677 669
 
678 670
 			++$i;
679 671
 		}
... ...
@@ -682,18 +674,63 @@ class mbt_to_tbg extends tbg_converter
682 674
 	}
683 675
 
684 676
 	/**
685
-	* Convert Categories.
686
-	* WARNING: RUNNING THIS MULTIPLE TIMES MAY CAUSE DUPLICATE ENTRIES.
677
+	* Add in default permissions.
678
+	*
687 679
 	*/
688 680
 	function doStep3()
689 681
 	{
690
-		// This attempts to remove extras added by repeated conversions.
682
+		// Clean up the projects permissions index.
683
+		$query = '
684
+			SELECT id
685
+			FROM ' . $this->tbg_db_prefix . 'projects';
686
+
687
+		$projects = array();
688
+		foreach ($this->tbg_db->query($query) as $row)
689
+			$projects[] = $row['id'];
690
+
691
+		$this->tbg_db->query('
692
+				DELETE FROM ' . $this->tbg_db_prefix . 'permissions
693
+				WHERE target_id IN (' . implode(',', $projects) . ')');
694
+
695
+		// Lets avoid a trillionth id number.
696
+		$this->tbg_db->query('
697
+				ALTER TABLE ' . $this->tbg_db_prefix . 'permissions
698
+					AUTO_INCERMENT=1');
699
+
700
+		// We still have this handy!
701
+		foreach ($projects as $project_id)
702
+		{
703
+			$this->tbg_db->query('
704
+				INSERT INTO ' . $this->tbg_db_prefix . 'permissions (permission_type, target_id, allowed, module, uid, gid, tid, scope) VALUES
705
+					("canseeproject", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
706
+					("canseeprojecthierarchy", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
707
+					("canmanageproject", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
708
+					("page_project_allpages_access", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
709
+					("canvoteforissues", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
710
+					("canlockandeditlockedissues", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
711
+					("cancreateandeditissues", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
712
+					("caneditissue", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
713
+					("caneditissuecustomfields", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
714
+					("canaddextrainformationtoissues", ' . $project_id . ', 1, "core", 1, 0, 0, 1),
715
+					("canpostseeandeditallcomments", ' . $project_id . ', 1, "core", 1, 0, 0, 1)');
716
+		}
717
+
718
+		// We do this quickly to prevent issues with the next step.
691 719
 		$this->tbg_db->query('
692 720
 			DELETE FROM ' . $this->tbg_db_prefix . 'listtypes
693 721
 			WHERE id NOT IN (1,2,3)
694 722
 				AND itemtype = "category"');
695 723
 
724
+		// We are done.
725
+		return 2;
726
+	}
696 727
 
728
+	/**
729
+	* Convert Categories.
730
+	* WARNING: RUNNING THIS MULTIPLE TIMES MAY CAUSE DUPLICATE ENTRIES.
731
+	*/
732
+	function doStep4()
733
+	{
697 734
 		$step_size = 500;
698 735
 		$substep = $this->getSubStep(__FUNCTION__);
699 736
 
... ...
@@ -724,7 +761,7 @@ class mbt_to_tbg extends tbg_converter
724 761
 	* Convert Versions.
725 762
 	*
726 763
 	*/
727
-	function doStep4()
764
+	function doStep5()
728 765
 	{
729 766
 		$step_size = 500;
730 767
 		$substep = $this->getSubStep(__FUNCTION__);
... ...
@@ -766,7 +803,7 @@ class mbt_to_tbg extends tbg_converter
766 803
 	* Normally we want to fix them, but in this case we want to convert bugs.
767 804
 	*
768 805
 	*/
769
-	function doStep5()
806
+	function doStep6()
770 807
 	{
771 808
 		$step_size = 500;
772 809
 		$substep = $this->getSubStep(__FUNCTION__);
... ...
@@ -834,7 +871,7 @@ class mbt_to_tbg extends tbg_converter
834 871
 	* Bug Notes.
835 872
 	*
836 873
 	*/
837
-	function doStep6()
874
+	function doStep7()
838 875
 	{
839 876
 		$step_size = 500;
840 877
 		$substep = $this->getSubStep(__FUNCTION__);
... ...
@@ -864,7 +901,7 @@ class mbt_to_tbg extends tbg_converter
864 901
 	* Relationships are great.
865 902
 	*
866 903
 	*/
867
-	function doStep7()
904
+	function doStep8()
868 905
 	{
869 906
 		$step_size = 500;
870 907
 		$substep = $this->getSubStep(__FUNCTION__);
... ...
@@ -892,7 +929,7 @@ class mbt_to_tbg extends tbg_converter
892 929
 	* Attachments, the pain of our every existence)
893 930
 	*
894 931
 	*/
895
-	function doStep8()
932
+	function doStep9()
896 933
 	{
897 934
 		$step_size = 100;
898 935
 		$substep = $this->getSubStep(__FUNCTION__);
899 936