File: 20050325-2.php

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (245 lines) | stat: -rwxr-xr-x 7,333 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#! /usr/bin/php4 -f
<?php
/**
 * GForge Group Role Generator
 *
 * Copyright 2004 GForge, LLC
 * http://gforge.org/
 *
 * @version   $Id: 20050325-2.php 5430 2006-04-28 12:37:49Z danper $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */

require_once('www/include/squal_pre.php');
require_once('common/tracker/ArtifactExtraField.class');
require_once('common/tracker/ArtifactExtraFieldElement.class');

// First of all, try to create the "alias" field if it doesn't exist
$res = db_query("SELECT alias FROM artifact_extra_field_list");
if (!$res) {		// error, the field doesn't exist
	$res = db_query("ALTER TABLE artifact_extra_field_list ADD COLUMN alias TEXT");
	if (!$res) {
		echo db_error();
		exit(16);
	}
} 

//
//  Set up this script to run as the site admin
//

$res = db_query("SELECT user_id FROM user_group WHERE admin_flags='A' AND group_id='1'");

if (!$res) {
    echo db_error();
    exit(1);
}

if (db_numrows($res) == 0) {
    // There are no Admins yet, aborting without failing
    echo "SUCCESS\n";
    exit(0);
}

$id=db_result($res,0,0);
session_set_new($id);

db_begin();

$res=db_query("SELECT group_id,group_artifact_id,use_resolution FROM artifact_group_list");

for ($i=0; $i<db_numrows($res); $i++) {

	$group_id=db_result($res,$i,'group_id');
	$gaid=db_result($res,$i,'group_artifact_id');
	$ur=db_result($res,$i,'use_resolution');

	$g = group_get_object($group_id);
	if (!$g || !is_object($g)) {
		echo "\nCould Not Get Group: $group_id";
		db_rollback();
		exit(2);
	}

	$at = new ArtifactType($g,$gaid);
	if (!$at || !is_object($at)) {
		echo "\nCould Not Get ArtifactType: $gaid";
		db_rollback();
		exit(3);
	}
	//
	//	Convert ArtifactCategory To Extra Field
	//
	$aef=new ArtifactExtraField($at);
	$aef->create('Category',ARTIFACT_EXTRAFIELDTYPE_SELECT,0,0);
	$catbox_id=$aef->getID();
	if (!$catbox_id) {
		echo "\nCould Not Get New Category Box ID: $gaid ".$aef->getErrorMessage();
		db_rollback();
		exit(4);
	}
	$resc=db_query("SELECT * FROM artifact_category WHERE group_artifact_id='$gaid'");
	for ($j=0; $j<db_numrows($resc); $j++) {
		$cat_id=db_result($resc,$j,'id');
		$cat_name=addslashes(db_result($resc,$j,'category_name'));
		if (strlen($cat_name) < 1) {
			$cat_name='[empty]';
		}

		$efe=new ArtifactExtraFieldElement($aef);
		#DEBUG
		#$efe->create($cat_name);
		if (!$efe->create($cat_name)) {
			echo 'Group: '.$group_id.' Could not create category element: '.$cat_name.' '.$efe->getErrorMessage();
			db_rollback();
			exit(5);
		}
		$efe_id=$efe->getID();
//echo 'Artifact Category: Group: '.$group_id;
//print_r($efe->data_array);
		if (!$efe_id) {
			#DEBUG echo "\nDid Not Get efe_id";
			echo "\nDid Not Get efe_id (group_id: $group_id)";
			db_rollback();
			exit(5);
		}
		$res2=db_query("INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
			SELECT artifact_id,$efe_id,$catbox_id FROM artifact 
			WHERE category_id='$cat_id'");
		if (!$res2) {
			echo "Could Not Insert AEFD for category " . db_error();
			db_rollback();
			exit(6);
		}
		$res3=db_query("UPDATE artifact_history SET old_value='$cat_name',field_name='Category'
			WHERE old_value='$cat_id' AND field_name='category_id'");
		if (!$res3) {
			echo "Could Not update history category " . db_error();
			db_rollback();
			exit(7);
		}
	}

	//
	//	Convert ArtifactGroup To Extra Field
	//
	$aef=new ArtifactExtraField($at);
	$aef->create('Group',ARTIFACT_EXTRAFIELDTYPE_SELECT,0,0);
	$groupbox_id=$aef->getID();
	if (!$groupbox_id) {
		echo "\nCould Not Get groupbox_id ".$aef->getErrorMessage();
		db_rollback();
		exit(8);
	}
	$resc=db_query("SELECT * FROM artifact_group WHERE group_artifact_id='$gaid'");
	for ($j=0; $j<db_numrows($resc); $j++) {
		$artgroup_id=db_result($resc,$j,'id');
		$group_name=addslashes(db_result($resc,$j,'group_name'));
		if (strlen($group_name) < 1) {
			$group_name='[empty]';
		}

		$efe=new ArtifactExtraFieldElement($aef);
		//$efe->create($group_name);
		if (!$efe->create($group_name)) {
			echo 'Group: '.$group_id.' Could not create group element: '.$group_name.' '.$efe->getErrorMessage();
			db_rollback();
			exit(5);
		}
//echo 'Artifact Group: Group: '.$group_id;
//print_r($efe->data_array);
		$efe_id=$efe->getID();
		if (!$efe_id) {
			echo "\nDid Not Get efe_id";
			db_rollback();
			exit(9);
		}
		$res2=db_query("INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
			SELECT artifact_id,$efe_id,$groupbox_id FROM artifact 
			WHERE artifact_group_id='$artgroup_id'");
		if (!$res2) {
			echo "Could Not Insert AEFD for artifactgroup " . db_error();
			db_rollback();
			exit(10);
		}
		$res3=db_query("UPDATE artifact_history SET old_value='$group_name',field_name='Group'
			WHERE old_value='$artgroup_id' AND field_name='artifact_group_id'");
		if (!$res3) {
			echo "Could Not update history artifactgroup " . db_error();
			db_rollback();
			exit(11);
		}
	}

	if ($ur) {
		//
		//	Convert ArtifactResolution To Extra Field
		//
		$aef=new ArtifactExtraField($at);
		$aef->create('Resolution',ARTIFACT_EXTRAFIELDTYPE_SELECT,0,0);
		$resolutionbox_id=$aef->getID();
		if (!$resolutionbox_id) {
			echo "\nCould Not Get resolutionbox_id ".$aef->getErrorMessage();
			db_rollback();
			exit(12);
		}
		$resc=db_query("SELECT * FROM artifact_resolution");
		for ($j=0; $j<db_numrows($resc); $j++) {
			$resolution_id=db_result($resc,$j,'id');
			$resolution_name=addslashes(db_result($resc,$j,'resolution_name'));
			if (strlen($resolution_name) < 1) {
				$resolution_name='[empty]';
			}
			$efe=new ArtifactExtraFieldElement($aef);
		//	$efe->create($resolution_name);
			if (!$efe->create($resolution_name)) {
				echo 'Group: '.$group_id.' Could not create resolution element: '.$resolution_name.' '.$efe->getErrorMessage();
				db_rollback();
				exit(5);
			}
//echo 'Artifact Group: Group: '.$group_id;
//print_r($efe->data_array);
			$efe_id=$efe->getID();
			if (!$efe_id) {
				echo "\nDid Not Get efe_id";
				db_rollback();
				exit(13);
			}
			$res2=db_query("INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
				SELECT artifact_id,$efe_id,$resolutionbox_id FROM artifact 
				WHERE resolution_id='$resolution_id'");
			if (!$res2) {
				echo "Could Not Insert AEFD for resolution " . db_error();
				db_rollback();
				exit(14);
			}
			$res3=db_query("UPDATE artifact_history SET old_value='$resolution_name',field_name='Resolution'
				WHERE old_value='$resolution_id' AND field_name='resolution_id'");
			if (!$res3) {
				echo "Could Not update history resolution " . db_error();
				db_rollback();
				exit(15);
			}
		}
	}
}
db_commit();

echo "SUCCESS";

?>