changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / t_convertdb.php
1 <?php
2 /*
3    database conversion util
4    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
5    If you need to special settings to the ewiki database you're converting
6    to, then please insert all required define(); statements above the first
7    include line, so you can override the settings from your main config.php
8    script.
9 */
10
11
12 include("t_config.php");
13 ?>
14 <html>
15 <head>
16  <title>database conversion (wiki engine transition)</title>
17  <link rel="stylesheet" type="text/css" href="t_config.css">
18 </head>
19 <body bgcolor="#ffffff" text="#000000">
20 <h1>database conversion</h1>
21 <?php
22
23  if (empty($_REQUEST["convert"])
24  and empty($_REQUEST["readtest"])
25  and empty($_REQUEST["writetest"])) {
26
27 ?>
28   This tool can be used to convert an existing (foreign) wiki database with
29   all its pages to the ewiki database and page format. It is very experimental
30   and you should consider using the export utility of your previous Wiki with
31   <tt>ewikictl</tt> afterwards to reimport it here.
32
33   <br>
34  <small>
35   <br>
36   Some of the databases you can export from need more parameters, and if you
37   transfer data from one SQL-based database scheme to another, you need to
38   have source and target tables in the same database (cannot cross-convert
39   from/to different servers/dbms). And the target tables for foreign schemes
40   must already exist.
41
42   <br>
43   <br>
44   You must have your target ewiki database and plugin already defined
45   in your <tt>config.php</tt>. If you want a special target, then you must
46   edit <i>this</i> script (<tt title="<?php echo __FILE__; ?>">t_convertdb.php</tt>)
47   and uncomment the other targets and insert correct define() or assignment
48   statements to make it work yourself.
49  </small>
50
51   <br>
52   <br>
53   <form action="t_convertdb.php" method="POST">
54     <table border="0" width="100%"> 
55     <tr>
56     <td width="50%" valign="top" class="add-border-right">
57
58       <h4>convert from</h4>
59       <label for="from_type">database type</label>
60       <select name="from_type" id="from_type">
61        <option value="pmwiki10" selected>PmWiki 0.6.x/1.0.x</option>
62        <option value="phpwiki13">PhpWiki 1.3.x</option>
63        <option value="ewiki">configured $ewiki_db</option>
64        <option value="mysql">ErfurtWiki/MySQL</option>
65        <option value="flat">ErfurtWiki/FlatFiles</option>
66        <option value="fast">ErfurtWiki/FastFiles</option>
67        <option value="dzf2">ErfurtWiki/dzf2</option>
68        <option value="dba">ErfurtWiki/dba+dbm</option>
69        <option value="zip">ErfurtWiki/zip</option>
70        <option value="anydb">ErfurtWiki/anydb+sql</option>
71        <option value="xmlrpc">XML-RPC remote database</option>
72       </select>
73       <br>
74
75       <br>
76       <input type="checkbox" name="from_convert_markup" value="1" id="from_cnv">
77       <label for="from_cnv">convert into ewiki markup</label>
78
79       <br>
80       <br>
81       <small>miscellaneous database access options</small>
82       <br>
83       <label for="from_o1">file / directory</label>
84       <input type="text" name="from_dir" id="from_o1" size="16" title="for dzf2, zip, PmWiki">
85       <br>
86       <label for="from_o2">subwiki identifier</label>
87       <input type="text" name="from_subwiki" value="Main" size="7" id="from_o2" title="for PmWiki, you can only import one of the various db fragments at a time">
88       <br>
89       <label for="from_o3">sql table name</label>
90       <input type="text" name="from_sqltable" id="from_o3" size="10" title="for most SQL databases">
91
92       <br>
93       <br>
94       <input type="submit" name="readtest" value="test read access">
95       
96       <br><br>
97       <br>
98      <small>
99       <u>notes</u>
100       <br>
101       - If you access the '<b>configured $ewiki_db</b>' then you do not need to
102         set any option, because this refers to whatever you configured in your
103         'config.php' script as default database. Always the best option.
104       <br>   
105       - For <b>PmWiki</b> you must set the 'subwiki identifier' (to "Main" at best), so
106         the conversion utility can extract one of the database fragments. You
107         also need to define the 'file/directory' setting to the exact position
108         of PmWikis 'wiki.d' or 'wikilib.d' data dirs (relative to the ewiki
109         installations base directory; and separate multiple dir names using
110         spaces, commas or colons).
111       <br>
112      </small>
113       
114
115     </td>
116     <td width="50%" valign="top">
117
118       <h4>into</h4>
119       <label for="to_type">destination</label>
120       <select name="to_type" id="to_type">
121        <option value="configured" selected>configured $ewiki_db</option>
122
123 <!-- can't use the following without extra config:
124        <option value="mysql">ErfurtWiki/MySQL</option>
125        <option value="flat">ErfurtWiki/FlatFiles</option>
126        <option value="fast">ErfurtWiki/FastFiles</option>
127        <option value="dzf2">ErfurtWiki/dzf2</option>
128        <option value="dba">ErfurtWiki/dba+dbm</option>
129        <option value="zip">ErfurtWiki/zip</option>
130        <option value="anydb">ErfurtWiki/anydb+sql</option>
131 -->       
132        <option value="phpwiki13">PhpWiki 1.3.x</option>
133 <!-- 
134        <option value="mysql">old PhpWiki 1.2</option>
135 -->
136       </select>
137       <br>
138
139       <br>
140       <input type="checkbox" name="to_convert_markup" value="1" id="to_cnv">
141       <label for="to_cnv">use wiki markup export filters</label>
142
143       <br>
144       <input type="checkbox" name="all_versions" value="1" checked="checked" id="all_vers">
145       <label for="all_vers">transfer all page versions/revisions</label>
146       <br>
147       <small>and</small>
148       <input type="checkbox" name="overwrite" value="1" id="to_over">
149       <label for="to_over">overwrite page versions that already exist in target database</label>
150       <small>(or copied page will simply be appended as newest)</small>
151
152       <br>
153       <br>
154       <input type="submit" name="convert" value="start transfer">
155
156     </td>
157     </tr>
158     </table>
159   </form>
160  
161 <?php
162
163  }
164
165  #------------------------------------------------------------------ go ---
166  else {
167  
168     #-- localize expected vars
169     $from_convert = $_REQUEST["from_convert_markup"];
170     $from_type = $_REQUEST["from_type"];
171     $to_type = $_REQUEST["to_type"];
172     $to_convert = $_REQUEST["to_convert_markup"];
173     $all_versions = $_REQUEST["all_versions"];
174     $overwrite = $_REQUEST["overwrite"];
175     $read = $_REQUEST["readtest"];
176     $write = $_REQUEST["convert"];
177     
178     
179     #-- database types and plugins
180     $t_scr = array(
181        "ewiki" => "",
182        "mysql" => "",
183        "pmwiki10" => "read_pmwiki1.php",
184        "phpwiki13" => "phpwiki13.php",
185        "dba" => "dba.php",
186        "flat" => "flat_files.php",
187        "fast" => "fast_files.php",
188        "dzf2" => "dzf2.php",
189        "zip" => "zip.php",
190        "anydb" => "anydb.php",
191        "xmlrpc" => "xmlrpc.php",
192     );
193     $t_api = array(
194        "ewiki" => "$",
195        "mysql" => "ewiki_database_mysql",
196        "pmwiki10" => "ewiki_database_pmwiki",
197        "phpwiki13" => "ewiki_database_phpwiki13",
198        "dba" => "ewiki_database_dba",
199        "flat" => "ewiki_database_files",
200        "fast" => "ewiki_database_files",
201        "dzf2" => "ewiki_database_DirZlibFiles2",
202        "zip" => "ewiki_database_zip",
203        "anydb" => "ewiki_database_anydb",
204        "xmlrpc" => "ewiki_database_xmlrpc",
205     );
206     $t_init = array(   // auto-create database scheme (::INIT)
207        "ewiki" => 1,
208        "mysql" => 1,
209        "dba" => 1,
210        "flat" => 1,
211        "fast" => 1,
212        "dzf2" => 1,
213        "anydb" => 1,
214     );
215     $t_in_convert = array(
216        "phpwiki13" => array("phpwiki.php"),
217        "usemod" => array("usemod.php"),
218     );
219     $t_out_convert = array(
220        "phpwiki13" => array(),  // nothing ready yet
221     );
222     
223     #-- preparations
224     define("PHPWIKI13_WRITEACCESS", 1);
225
226
227     #-- open databases
228     if ($script = $t_scr[$from_type]) include_once("plugins/db/$script");
229     $FROM = instantiate_db_api($t_api[$from_type]);
230     echo "- source database module loaded/instantiated<br>\n";
231     if ($read) {
232        read_test();
233     }
234     if ($script = $t_scr[$from_to]) include_once("plugins/db/$script");
235     $DEST = instantiate_db_api($t_api[$to_type]);
236     echo "- target database module loaded/instantiated<br>\n";
237     
238     
239     #-- set up options for $FROM database
240     if ($grp = $_REQUEST["from_subwiki"]) {
241        $FROM->group = $grp;
242     }
243     if ($dir = $_REQUEST["from_dir"]) {
244        $FROM->zip = $dir;
245        $FROM->dir = $dir;
246        $FROM->dirs = preg_split("/[\s:;,]+/", $dir);
247     }
248     if ($tbl = $_REQUEST["from_sqltable"]) {
249        $FROM->table = $tbl;
250     }
251     
252     
253     
254     #-- load markup conversion scripts
255     $ewiki_plugins["format_source"] = array();   // kill default ones
256     if ($from_convert)
257     if ($scr_a = $t_in_convert[$from_type]) {
258        foreach ($scr_a as $script) {
259           echo "- loading markup conversion module '$script'<br>\n";
260           include_once("../plugins/markup/$script");
261        }
262     }
263     $ewiki_plugins["markup_convert"] = array();
264     if ($to_convert)
265     if ($scr_a = $t_out_convert[$from_type]) {
266        foreach ($scr_a as $script) {
267           echo "- loading markup export plugin '$script'<br>\n";
268           include_once("plugins/markup/$script");
269        }
270     }
271     
272
273     #-- creating ewiki database
274     if ($t_init[$to_type]) {
275        echo "- <u>creating</u> destination target database scheme/whatever...<br>\n";
276        $DEST->INIT();
277     }
278     else {
279        echo "- cannot create database scheme for that target type [SKIPPED]<br>\n";
280     }
281     echo "<br>\n";
282
283
284     #-- start --------------------------------------------------------
285     echo "\n\n<br>\n<h4>copying pages:</h4>\n\n";
286
287     $result = $FROM->GETALL(array("flags", "version"));   // not "id"!, we're doing raw access here, and need to obey the internal rules
288     while ($row = $result->get()) {
289     
290        #-- run forever, if necessary
291        set_time_limit(2000);
292        
293        #-- page id
294        $id = $row["id"];
295        echo htmlentities("$id");
296
297
298        #-- read
299        $data = $FROM->GET($id);
300
301
302        #-- operation mode
303        if ($all_versions) {
304        
305           #-- copy all page versions
306           for ($ver=$data["version"]; $version>=1; $version--) {
307              if ($data = $FROM->GET($id, $version)) {
308                 markup_convert($data);
309                 if ($DEST->WRITE($data, $overwrite)) {
310                    echo ".$version";
311                 }
312                 else {
313                    echo "<b>.E</b>";
314                 }
315              }
316              
317           }
318        
319        }
320        else {
321        
322           #-- check for existence of current page
323           $old = $DEST->GET($id);
324           if (!$old["version"]) {
325              $data["version"] = 1;
326           }
327           elseif ($overwrite && $old) {
328              $data["version"] = $old["version"] + 1;
329           }
330           else {
331              continue;
332           }
333           
334           markup_convert($data);
335           if ($DEST->WRITE($data)) {
336              echo "[" . $data["version"] . "]";
337           }
338           else {
339              echo "<b>[ERROR]</b>";
340           }
341        
342        }
343
344
345        echo ",\n";
346     }
347
348     #-- fin
349     echo "<br><br>done\n";
350
351  }
352
353
354
355
356
357
358 #----------------------------------------------------------- utility code ---
359
360
361
362
363 #-- rewriting of page content to match target wiki engines markup
364 function markup_convert() {
365    global $ewiki_plugins;
366
367    #-- in-conversion
368    if ($pf_a = $ewiki_plugins["format_source"]) foreach ($pf_a as $pf) {
369       $pf($data["content"]);
370    }
371
372    #-- out-conversion
373    if ($pf_a = $ewiki_plugins["markup_convert"]) foreach ($pf_a as $pf) {
374       $pf($data["content"]);
375    }
376    
377    #-- update {refs}  (helpful for PmWiki, which does not have an equivalent)
378    ewiki_scan_wikiwords($data["content"], $ewiki_links, "_STRIP_EMAIL=1");
379    $data["refs"] = "\n\n".implode("\n", array_keys($ewiki_links))."\n\n";
380
381    //fin
382
383  
384
385 #-- submodule
386 function read_test() {
387    global $FROM;
388    echo "- now performing only an<br>\n<br>\n<h4>read test</h4>\n";
389    $all = $FROM->GETALL(array("version","flags"));
390    $list = array();
391    echo "- following pages were found in that database:<br>\n";
392    while ($row = $all->get()) {
393       echo $row["id"] . "[" . $row["version"] . "], ";
394       $list[] = $row["id"];
395    }
396    echo "\n<br><br>\n<h4>randomly chosen entry</h4>\n";
397    $id = $list[rand(0,count($list)-1)];
398    $row = $FROM->GET($id);
399    echo "<pre>";
400    print_r($row);
401    echo "</pre>\n\n<br>\n</body></html>";
402    die();
403 }
404
405
406
407 #-- create database object (or simply return reference to default $ewiki_db)
408 function & instantiate_db_api($class) {
409    global $ewiki_db;
410    if ((!$class) || ($class=="$")) {
411       if (!isset($ewiki_db)) {
412          $ewiki_db = & new ewiki_database_mysql();
413       }
414       $db = & $ewiki_db;
415    }
416    else {
417       $db = & new $class;
418    }
419    return($db);
420 }
421
422
423 ?>
424 </body></html>