-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.php
407 lines (368 loc) · 14.4 KB
/
database.php
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
if (PHP_SAPI === 'cli') {
if (count($argv)==2) {
$userId = $argv[1];
}
} else {
if (isset($_GET['user'])) {
$userId = $_GET['user'];
}
}
if (!isset($userId)) {
die("You did not set the input User ID!\n");
}
require("sql.inc.php");
require_once("sql_export.inc.php");
// No HTML headers for CLI
/*
if (PHP_SAPI !== 'cli') {
header("Content-Type: text/sql");
header("Content-Disposition:attachment;filename=database_{$userId}.sql");
}
*/
/*
// Find relevant user
$query = "SELECT * FROM `users` where `id`='$userId'";
$result = mysql_query($query) or die("Query failed: ".mysql_error()."\n");
// Printing results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($row as $key=>$val) {
echo "$key => $val\n";
}
echo "\n\n";
}
*/
// Find projects used by user
$query = "SELECT `project_id` FROM `members` where `user_id`='$userId'";
$result = mysql_query($query) or die("Query members failed: ".mysql_error()."\n");
$projects = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$projects .= $row[0] . ",";
//echo "projects: {$row[0]}\n";
}
$projects = substr($projects, 0, -1);
if (!empty($projects)) {
// Find wikis by project
$query = "SELECT `id` FROM `wikis` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query wikis failed: ".mysql_error()."\n");
$wikis = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$wikis .= $row[0] . ",";
//echo "wikis: {$row[0]}\n";
}
$wikis = substr($wikis, 0, -1);
if (!empty($wikis)) {
// Find pages by wiki
$query = "SELECT `id` FROM `wiki_pages` where `wiki_id` IN ($wikis)";
$result = mysql_query($query) or die("Query wiki_pages failed: ".mysql_error()."\n");
$wiki_pages = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$wiki_pages .= $row[0] . ",";
//echo "wiki_pages: {$row[0]}\n";
}
$wiki_pages = substr($wiki_pages, 0, -1);
}
// Find issues by project
$query = "SELECT `id` FROM `issues` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query issues failed: ".mysql_error()."\n");
$issues = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$issues .= $row[0] . ",";
//echo "issues: {$row[0]}\n";
}
$issues = substr($issues, 0, -1);
// Find news by project
$query = "SELECT `id` FROM `news` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query news failed: ".mysql_error()."\n");
$news = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$news .= $row[0] . ",";
//echo "news: {$row[0]}\n";
}
$news = substr($news, 0, -1);
// Find repositories by project
$query = "SELECT `id` FROM `repositories` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query repositories failed: ".mysql_error()."\n");
$repositories = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$repositories .= $row[0] . ",";
//echo "repositories: {$row[0]}\n";
}
$repositories = substr($repositories, 0, -1);
}
if (!empty($repositories)) {
// Find changesets by project
$query = "SELECT `id` FROM `changesets` where `repository_id` IN ($repositories)";
$result = mysql_query($query) or die("Query changesets failed: ".mysql_error()."\n");
$changesets = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$changesets .= $row[0] . ",";
//echo "changesets: {$row[0]}\n";
}
$changesets = substr($changesets, 0, -1);
}
if (!empty($projects)) {
// Find versions by project
$query = "SELECT `id` FROM `versions` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query versions failed: ".mysql_error()."\n");
$versions = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$versions .= $row[0] . ",";
//echo "versions: {$row[0]}\n";
}
$versions = substr($versions, 0, -1);
// Find boards by project
$query = "SELECT `id` FROM `boards` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query boards failed: ".mysql_error()."\n");
$boards = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$boards .= $row[0] . ",";
//echo "boards: {$row[0]}\n";
}
$boards = substr($boards, 0, -1);
if (!empty($boards)) {
// Find messages by project
$query = "SELECT `id` FROM `messages` where `board_id` IN ($boards)";
$result = mysql_query($query) or die("Query messages failed: ".mysql_error()."\n");
$messages = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$messages .= $row[0] . ",";
//echo "messages: {$row[0]}\n";
}
$messages = substr($messages, 0, -1);
// Find watchers
$query = "SELECT `id` FROM `watchers` where ";
if (!empty($boards)) $query .= "(`watchable_type`='Board' AND `watchable_id` IN ($boards)) OR ";
if (!empty($messages)) $query .= "(`watchable_type`='Message' AND `watchable_id` IN ($messages)) OR ";
if (!empty($issues)) $query .= "(`watchable_type`='Issue' AND `watchable_id` IN ($issues)) OR ";
if (!empty($wikis)) $query .= "(`watchable_type`='WikiPage' AND `watchable_id` IN ($wikis)) OR ";
if (!empty($news)) $query .= "(`watchable_type`='News' AND `watchable_id` IN ($news)) OR ";
$query .= "FALSE";
$result = mysql_query($query) or die("Query watchers failed: ".mysql_error()."\n");
$watchers = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$watchers .= $row[0] . ",";
//echo "watchers: {$row[0]}\n";
}
$watchers = substr($watchers, 0, -1);
}
// Find documents by project
$query = "SELECT `id` FROM `documents` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query documents failed: ".mysql_error()."\n");
$documents = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$documents .= $row[0] . ",";
//echo "documents: {$row[0]}\n";
}
$documents = substr($documents, 0, -1);
// Find attachments
$query = "SELECT `id` FROM `attachments` where ";
if (!empty($documents)) $query .= "(`container_type`='Document' AND `container_id` IN ($documents)) OR ";
if (!empty($wikis)) $query .= "(`container_type`='WikiPage' AND `container_id` IN ($wikis)) OR ";
if (!empty($versions)) $query .= "(`container_type`='Version' AND `container_id` IN ($versions)) OR ";
if (!empty($projects)) $query .= "(`container_type`='Project' AND `container_id` IN ($projects)) OR ";
if (!empty($issues)) $query .= "(`container_type`='Issue' AND `container_id` IN ($issues)) OR ";
if (!empty($messages)) $query .= "(`container_type`='Message' AND `container_id` IN ($messages)) OR ";
$query .= "FALSE";
$result = mysql_query($query) or die("Query attachments failed: ".mysql_error()."\n");
$attachments = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$attachments .= $row[0] . ",";
//echo "attachments: {$row[0]}\n";
}
$attachments = substr($attachments, 0, -1);
// Find members used by project
// Find users used by project
$query = "SELECT `id`, `user_id` FROM `members` where `project_id` IN ($projects)";
$result = mysql_query($query) or die("Query members failed: ".mysql_error()."\n");
$members = "";
$users = "2,"; // We need the Anonymous user
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$members .= $row[0] . ",";
$users .= $row[1] . ",";
//echo "members: {$row[0]}\n";
//echo "users: {$row[1]}\n";
}
$members = substr($members, 0, -1);
$users = substr($users, 0, -1);
// Find journals
// Add contributing users that are not members
$query = "SELECT `id`, `user_id` FROM `journals` where ";
if (!empty($issues)) $query .= "(`journalized_type`='Issue' AND `journalized_id` IN ($issues)) OR ";
$query .= "FALSE";
$result = mysql_query($query) or die("Query journals failed: ".mysql_error()."\n");
$journals = "";
//$users is already initalised
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$journals .= $row[0] . ",";
//$users .= $row[1] . ",";
//echo "journals: {$row[0]}\n";
//echo "users: {$row[1]}\n";
}
$journals = substr($journals, 0, -1);
//if (substr($users,-1)==',') $users = substr($users, 0, -1);
// Find custom_values
$query = "SELECT `id` FROM `custom_values` where ";
if (!empty($issues)) $query .= "(`customized_type`='Issue' AND `customized_id` IN ($issues)) OR ";
if (!empty($users)) $query .= "(`customized_type`='Principal' AND `customized_id` IN ($users)) OR ";
if (!empty($projects)) $query .= "(`customized_type`='Project' AND `customized_id` IN ($projects)) OR ";
$query .= "FALSE";
$result = mysql_query($query) or die("Query custom_values failed: ".mysql_error()."\n");
$custom_values = "";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$custom_values .= $row[0] . ",";
//echo "custom_values: {$row[0]}\n";
}
$custom_values = substr($custom_values, 0, -1);
}
//Instantiate the SQL_Export class
$e = new SQL_Export($server, $username, $password, $db);
// Define the database tables
$tables = $e->get_tables();
// Open the output file
$database_file = "database_{$userId}.sql";
$fp = fopen($database_file, 'w');
# Now UTF-8 - Add byte order mark
fwrite($fp, pack("CCC",0xef,0xbb,0xbf));
//Run the export
foreach ($tables as $t) {
$header = $e->create_header($t);
$data = "";
switch ($t) {
case 'attachments':
if (!empty($attachments)) $data = $e->get_data($t, "`id` IN ($attachments)");
break;
case 'boards':
if (!empty($boards)) $data = $e->get_data($t, "`id` IN ($boards)");
break;
case 'changes':
if (!empty($changesets)) $data = $e->get_data($t, "`changeset_id` IN ($changesets)");
break;
case 'changesets':
if (!empty($changesets)) $data = $e->get_data($t, "`id` IN ($changesets)");
break;
case 'changesets_issues':
if (!empty($changesets) and !empty($issues)) $data = $e->get_data($t, "`changeset_id` IN ($changesets) OR `issue_id` IN ($issues)");
break;
case 'chart_done_ratios': # Plugin
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'chart_issue_statuses': # Plugin
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'chart_saved_conditions': # Plugin
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'chart_time_entries': # Plugin
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'comments':
if (!empty($news)) $data = $e->get_data($t, "`commented_id` IN ($news)");
break;
case 'custom_fields_projects':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'custom_values':
if (!empty($custom_values)) $data = $e->get_data($t, "`id` IN ($custom_values)");
break;
case 'documents':
if (!empty($documents)) $data = $e->get_data($t, "`id` IN ($documents)");
break;
case 'enabled_modules':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'enumerations':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects) OR `project_id` IS NULL");
break;
case 'gitosis_public_keys': # Plugin
$data = $e->get_data($t, "FALSE");
break;
case 'groups_users':
if (!empty($users)) $data = $e->get_data($t, "`user_id` IN ($users)");
break;
case 'issues':
if (!empty($issues)) $data = $e->get_data($t, "`id` IN ($issues)");
break;
case 'issue_categories':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'issue_checklists': # Plugin
if (!empty($issues)) $data = $e->get_data($t, "`id` IN ($issues)");
break;
case 'issue_relations':
if (!empty($issues)) $data = $e->get_data($t, "`issue_from_id` IN ($issues) OR `issue_to_id` IN ($issues)");
break;
case 'journals':
if (!empty($journals)) $data = $e->get_data($t, "`id` IN ($journals)");
break;
case 'journal_details':
if (!empty($journals)) $data = $e->get_data($t, "`journal_id` IN ($journals)");
break;
case 'members':
if (!empty($members)) $data = $e->get_data($t, "`id` IN ($members)");
break;
case 'member_roles':
if (!empty($members)) $data = $e->get_data($t, "`member_id` IN ($members)");
break;
case 'messages':
if (!empty($messages)) $data = $e->get_data($t, "`id` IN ($messages)");
break;
case 'news':
if (!empty($news)) $data = $e->get_data($t, "`id` IN ($news)");
break;
case 'projects':
if (!empty($projects)) $data = $e->get_data($t, "`id` IN ($projects)");
break;
case 'projects_trackers':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'queries':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'repositories':
if (!empty($repositories)) $data = $e->get_data($t, "`id` IN ($repositories)");
break;
case 'theme_changer_user_settings': # Plugin
if (!empty($users)) $data = $e->get_data($t, "`user_id` IN ($users)");
break;
case 'time_entries':
if (!empty($projects)) $data = $e->get_data($t, "`project_id` IN ($projects)");
break;
case 'tokens':
if (!empty($users)) $data = $e->get_data($t, "`user_id` IN ($users)");
break;
case 'users':
if (!empty($users)) $data = $e->get_data($t, "`id` IN ($users)");
break;
case 'user_preferences':
if (!empty($users)) $data = $e->get_data($t, "`user_id` IN ($users)");
break;
case 'versions':
if (!empty($versions)) $data = $e->get_data($t, "`id` IN ($versions)");
break;
case 'watchers':
if (!empty($watchers)) $data = $e->get_data($t, "`id` IN ($watchers)");
break;
case 'wikis':
if (!empty($wikis)) $data = $e->get_data($t, "`id` IN ($wikis)");
break;
case 'wiki_contents':
if (!empty($wiki_pages)) $data = $e->get_data($t, "`page_id` IN ($wiki_pages)");
break;
case 'wiki_content_versions':
if (!empty($wiki_pages)) $data = $e->get_data($t, "`page_id` IN ($wiki_pages)");
break;
case 'wiki_pages':
if (!empty($wikis)) $data = $e->get_data($t, "`wiki_id` IN ($wikis)");
break;
case 'wiki_redirects':
if (!empty($wikis)) $data = $e->get_data($t, "`wiki_id` IN ($wikis)");
break;
default:
$data = $e->get_data($t);
break;
}
fwrite($fp, "\n###################\n# Dumping table `$t`\n###################\n\n$header\n$data\n");
}
fclose($fp);
?>