r1472: Fix a string offset bug due to those annoying uses of construct_user_display...
[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 'user help' => array(
39 'table' => 'fieldhelp',
40 'rebuild' => 'build_user_help',
41 'orderfree' => true
42 )
43 );
44
45 ?>
46
47 <h2>Data Tools</h2>
48
49 Here you can either export or import the standard data tables for Bugdar. These include things such as usergroups, priorities, etc.
50
51 <h3>[<strong><a href="datatools.php?do=export">Export</a></strong>] [<strong><a href="datatools.php?do=import">Import</a></strong>] [<strong><a href="datatools.php?do=view">View</a></strong>] [<strong><a href="datatools.php?do=settings">Dump Settings</a></strong>]</h3>
52
53 <hr />
54
55 <pre>
56
57 <?php
58
59 // ###################################################################
60
61 if ($_REQUEST['do'] == 'export')
62 {
63 foreach ($THELIST AS $display => $data)
64 {
65 $fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "$data[table]" . (!$data['orderfree'] ? " ORDER BY {$data[table]}id ASC" : ""));
66 while ($fitem = $db->fetch_array($fetch))
67 {
68 $exportlist["$display"][] = $fitem;
69 }
70 $db->free_result($fetch);
71
72 echo "Exported $display\n";
73 }
74
75 $phpfile = '<?' . 'php
76
77 // stores exported data data
78 // SVN $' . 'Id: $
79
80 $DATASTORE = "' . addslashes(serialize($exportlist)) . '";
81
82 ?' . '>';
83
84 if ($handle = fopen('./docs/datatools_store.php', 'w'))
85 {
86 if (fwrite($handle, $phpfile))
87 {
88 fclose($handle);
89 }
90 else
91 {
92 echo 'Could not write the file';
93 exit;
94 }
95 }
96 else
97 {
98 echo 'Could not open the file with mode "w"';
99 exit;
100 }
101
102 echo "Wrote the file\n";
103 }
104
105 // ###################################################################
106
107 if ($_REQUEST['do'] == 'import')
108 {
109 require('./docs/datatools_store.php');
110 $DATASTORE = unserialize(stripslashes($DATASTORE));
111
112 foreach ($THELIST AS $display => $data)
113 {
114 $db->query("TRUNCATE TABLE " . TABLE_PREFIX . "$data[table]");
115
116 $fields = array();
117 $values = array();
118 foreach ($DATASTORE["$display"] AS $mainarray)
119 {
120 $fields = $values = array();
121 foreach ($mainarray AS $field => $value)
122 {
123 $fields[] = $field;
124 $values[] = "'" . $bugsys->escape($value) . "'";
125 }
126 $query = "INSERT INTO " . TABLE_PREFIX . "$data[table] (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $values) . ")";
127 echo str_replace(array('>', '<'), array('&gt;', '&lt;'), $query) . "\n";
128 $db->query($query);
129 }
130
131 $data['rebuild']();
132 echo "Rebuilding $data[table]\n\n";
133 }
134 }
135
136 // ###################################################################
137
138 if ($_REQUEST['do'] == 'view')
139 {
140 require_once('./docs/datatools_store.php');
141 $DATASTORE = unserialize(stripslashes($DATASTORE));
142
143 $build = '$data = array(';
144
145 foreach ($THELIST AS $display => $data)
146 {
147 $build .= "\n\t'$data[table]' => array(";
148 foreach ($DATASTORE["$display"] AS $mainarray)
149 {
150 $build .= "\n\t\tarray(";
151 foreach ($mainarray AS $field => $value)
152 {
153 $build .= "\n\t\t\t'" . addslashes($field) . "' => '" . addslashes($value) . "',";
154 }
155 $build = substr($build, 0, strlen($build) - 1);
156 $build .= "\n\t\t),";
157 }
158 $build = substr($build, 0, strlen($build) - 1);
159 $build .= "\n\t),";
160 }
161 $build = substr($build, 0, strlen($build) - 1);
162
163 $build .= "\n);";
164
165 echo '<textarea style="height: 500px; width: 100%">' . htmlspecialchars($build) . '</textarea>';
166 }
167
168 // ###################################################################
169
170 if ($_REQUEST['do'] == 'settings')
171 {
172 $fout = <<<FILE
173 <?php
174 /*=====================================================================*\
175 || ###################################################################
176 || # Bugdar [#]version[#]
177 || # Copyright ©2002-[#]year[#] Blue Static
178 || #
179 || # This program is free software; you can redistribute it and/or modify
180 || # it under the terms of the GNU General Public License as published by
181 || # the Free Software Foundation; version [#]gpl[#] of the License.
182 || #
183 || # This program is distributed in the hope that it will be useful, but
184 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
185 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
186 || # more details.
187 || #
188 || # You should have received a copy of the GNU General Public License along
189 || # with this program; if not, write to the Free Software Foundation, Inc.,
190 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
191 || ###################################################################
192 \*=====================================================================*/
193
194 // this stores all the settings for a new installation
195
196 \$settings = array(
197 FILE;
198
199 $settings = $db->query("SELECT * FROM " . TABLE_PREFIX . "setting");
200 while ($setting = $db->fetch_array($settings))
201 {
202 $fout .= "\n\t'$setting[varname]' => '" . str_replace("'", "\'", $setting['value']) . "',";
203 }
204
205 $fout .= <<<FILE
206
207 );
208
209 /*=====================================================================*\
210 || ###################################################################
211 || # \$HeadURL: \$
212 || # \$Id: \$
213 || ###################################################################
214 \*=====================================================================*/
215 ?>
216 FILE;
217
218 file_put_contents('install/settings.php', $fout);
219
220 echo 'Dumping current settings into install/settings.php';
221 }
222
223 ?>