Happy 2009! Updating copyright years.
[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 foreach ($fetch as $fitem)
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 chmod('./docs/datatools_store.php', 0777);
90 }
91 else
92 {
93 echo 'Could not write the file';
94 exit;
95 }
96 }
97 else
98 {
99 echo 'Could not open the file with mode "w"';
100 exit;
101 }
102
103 echo "Wrote the file\n";
104 }
105
106 // ###################################################################
107
108 if ($_REQUEST['do'] == 'import')
109 {
110 require('./docs/datatools_store.php');
111 $DATASTORE = unserialize(stripslashes($DATASTORE));
112
113 foreach ($THELIST AS $display => $data)
114 {
115 $db->query("TRUNCATE TABLE " . TABLE_PREFIX . "$data[table]");
116
117 $fields = array();
118 $values = array();
119 foreach ($DATASTORE["$display"] AS $mainarray)
120 {
121 $fields = $values = array();
122 foreach ($mainarray AS $field => $value)
123 {
124 $fields[] = $field;
125 $values[] = "'" . $bugsys->escape($value) . "'";
126 }
127 $query = "INSERT INTO " . TABLE_PREFIX . "$data[table] (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $values) . ")";
128 echo str_replace(array('>', '<'), array('&gt;', '&lt;'), $query) . "\n";
129 $db->query($query);
130 }
131
132 $data['rebuild']();
133 echo "Rebuilding $data[table]\n\n";
134 }
135 }
136
137 // ###################################################################
138
139 if ($_REQUEST['do'] == 'view')
140 {
141 require_once('./docs/datatools_store.php');
142 $DATASTORE = unserialize(stripslashes($DATASTORE));
143
144 $build = '$data = array(';
145
146 foreach ($THELIST AS $display => $data)
147 {
148 $build .= "\n\t'$data[table]' => array(";
149 foreach ($DATASTORE["$display"] AS $mainarray)
150 {
151 $build .= "\n\t\tarray(";
152 foreach ($mainarray AS $field => $value)
153 {
154 $build .= "\n\t\t\t'" . addslashes($field) . "' => '" . addslashes($value) . "',";
155 }
156 $build = substr($build, 0, strlen($build) - 1);
157 $build .= "\n\t\t),";
158 }
159 $build = substr($build, 0, strlen($build) - 1);
160 $build .= "\n\t),";
161 }
162 $build = substr($build, 0, strlen($build) - 1);
163
164 $build .= "\n);";
165
166 echo '<textarea style="height: 500px; width: 100%">' . htmlspecialchars($build) . '</textarea>';
167 }
168
169 // ###################################################################
170
171 if ($_REQUEST['do'] == 'settings')
172 {
173 $fout = <<<FILE
174 <?php
175 /*=====================================================================*\
176 || ###################################################################
177 || # Bugdar
178 || # Copyright (c)2004-2009 Blue Static
179 || #
180 || # This program is free software; you can redistribute it and/or modify
181 || # it under the terms of the GNU General Public License as published by
182 || # the Free Software Foundation; version 2 of the License.
183 || #
184 || # This program is distributed in the hope that it will be useful, but
185 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
186 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
187 || # more details.
188 || #
189 || # You should have received a copy of the GNU General Public License along
190 || # with this program; if not, write to the Free Software Foundation, Inc.,
191 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
192 || ###################################################################
193 \*=====================================================================*/
194
195 // this stores all the settings for a new installation
196
197 \$settings = array(
198 FILE;
199
200 $settings = $db->query("SELECT * FROM " . TABLE_PREFIX . "setting");
201 foreach ($settings as $setting)
202 {
203 $fout .= "\n\t'$setting[varname]' => '" . str_replace("'", "\'", $setting['value']) . "',";
204 }
205
206 $fout .= <<<FILE
207
208 );
209
210 /*=====================================================================*\
211 || ###################################################################
212 || # \$HeadURL: \$
213 || # \$Id: \$
214 || ###################################################################
215 \*=====================================================================*/
216 ?>
217 FILE;
218
219 file_put_contents('install/settings.php', $fout);
220 chmod('install/settings.php', 0777);
221
222 echo 'Dumping current settings into install/settings.php';
223 }
224
225 ?>