r137: Adding datatools.php and datatools_store.php which are used to insert the defau...
[bugdar.git] / docs / datatools.php
1 <?php
2
3 // can load/export all of our common data bits
4 // SVN $Id$
5
6 chdir('./../');
7 require_once('./includes/init.php');
8
9 // ###################################################################
10 // list of things to export/import: array(displayname => array('table' => db table, 'rebuild' => rebuild function)
11
12 $THELIST = array(
13 'usergroups' => array(
14 'table' => 'usergroup',
15 'rebuild' => 'build_usergroups'
16 ),
17
18 'statuses' => array(
19 'table' => 'status',
20 'rebuild' => 'build_statuses'
21 ),
22
23 'severity' => array(
24 'table' => 'severity',
25 'rebuild' => 'build_severities'
26 ),
27
28 'priorities' => array(
29 'table' => 'priority',
30 'rebuild' => 'build_priorities'
31 ),
32
33 'resolutions' => array(
34 'table' => 'resolution',
35 'rebuild' => 'build_resolutions'
36 )
37 );
38
39 ?>
40
41 <h2>Data Tools</h2>
42
43 Here you can either export or import the standard data tables for BugStrike. These include things such as usergroups, priorities, etc. However phrases are not handled here, but instead with lang_file_to_db.php
44
45 <h3>[<strong><a href="datatools.php?do=export">Export</a></strong>] [<strong><a href="datatools.php?do=import">Import</a></strong>]</h3>
46
47 <hr />
48
49 <pre>
50
51 <?php
52
53 // ###################################################################
54
55 if ($_REQUEST['do'] == 'export')
56 {
57 foreach ($THELIST AS $display => $data)
58 {
59 $fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "$data[table] ORDER BY {$data[table]}id ASC");
60 while ($fitem = $db->fetch_array($fetch))
61 {
62 $exportlist["$display"][] = $fitem;
63 }
64 $db->free_result($fetch);
65
66 echo "Exported $display\n";
67 }
68
69 $phpfile = '<?' . 'php
70
71 // stores exported data data
72 // SVN $' . 'Id: $
73
74 $DATASTORE = "' . addslashes(serialize($exportlist)) . '";
75
76 ?' . '>';
77
78 if ($handle = fopen('./docs/datatools_store.php', 'w'))
79 {
80 if (fwrite($handle, $phpfile))
81 {
82 fclose($handle);
83 }
84 else
85 {
86 echo 'Could not write the file';
87 exit;
88 }
89 }
90 else
91 {
92 echo 'Could not open the file with mode "w"';
93 exit;
94 }
95
96 echo "Wrote the file\n";
97 }
98
99 // ###################################################################
100
101 if ($_REQUEST['do'] == 'import')
102 {
103 require('./docs/datatools_store.php');
104 $DATASTORE = unserialize(stripslashes($DATASTORE));
105
106 foreach ($THELIST AS $display => $data)
107 {
108 $db->query("TRUNCATE TABLE " . TABLE_PREFIX . "$data[table]");
109
110 $fields = array();
111 $values = array();
112 foreach ($DATASTORE["$display"] AS $mainarray)
113 {
114 $fields = $values = array();
115 foreach ($mainarray AS $field => $value)
116 {
117 $fields[] = $field;
118 $values[] = "'" . $bugsys->escape($value) . "'";
119 }
120 $query = "INSERT INTO " . TABLE_PREFIX . "$data[table] (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $values) . ")";
121 echo str_replace(array('>', '<'), array('&gt;', '&lt;'), $query) . "\n";
122 $db->query($query);
123 }
124
125 $data['rebuild']();
126 echo "Rebuilding $data[table]\n\n";
127 }
128 }
129
130 ?>