Moved /0 prefix tests to valid...it just means "everything"
[mediawiki.git] / maintenance / importUseModWiki.php
blob52a3d0e3b3c350920dbcef7431f576b6dc148197
1 <?php
3 /**
4 * Import data from a UseModWiki into a MediaWiki wiki
5 * 2003-02-09 Brion VIBBER <brion@pobox.com>
6 * Based loosely on Magnus's code from 2001-2002
8 * Updated limited version to get something working temporarily
9 * 2003-10-09
10 * Be sure to run the link & index rebuilding scripts!
12 * Some more munging for charsets etc
13 * 2003-11-28
15 * Partial fix for pages starting with lowercase letters (??)
16 * and CamelCase and /Subpage link conversion
17 * 2004-11-17
19 * Rewrite output to create Special:Export format for import
20 * instead of raw SQL. Should be 'future-proof' against future
21 * schema changes.
22 * 2005-03-14
24 * @todo document
25 * @file
26 * @ingroup Maintenance
29 if ( php_sapi_name() != 'cli' ) {
30 echo "Please customize the settings and run me from the command line.";
31 die( -1 );
34 /** Set these correctly! */
35 $wgImportEncoding = "CP1252"; /* We convert all to UTF-8 */
36 $wgRootDirectory = "/kalman/Projects/wiki2002/wiki/lib-http/db/wiki";
38 /* On a large wiki, you might run out of memory */
39 @ini_set( 'memory_limit', '40M' );
41 /* globals */
42 $wgFieldSeparator = "\xb3"; # Some wikis may use different char
43 $FS = $wgFieldSeparator ;
44 $FS1 = $FS . "1" ;
45 $FS2 = $FS . "2" ;
46 $FS3 = $FS . "3" ;
48 # Unicode sanitization tools
49 require_once( dirname( dirname( __FILE__ ) ) . '/includes/normal/UtfNormal.php' );
51 $usercache = array();
53 importPages();
55 # ------------------------------------------------------------------------------
57 function importPages()
59 global $wgRootDirectory;
61 $gt = '>';
62 echo <<<XML
63 <?xml version="1.0" encoding="UTF-8" ?$gt
64 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.1/"
65 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66 xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.1/
67 http://www.mediawiki.org/xml/export-0.1.xsd"
68 version="0.1"
69 xml:lang="en">
70 <!-- generated by importUseModWiki.php -->
72 XML;
73 $letters = array(
74 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
75 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
76 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'other' );
77 foreach ( $letters as $letter ) {
78 $dir = "$wgRootDirectory/page/$letter";
79 if ( is_dir( $dir ) )
80 importPageDirectory( $dir );
82 echo <<<XML
83 </mediawiki>
85 XML;
88 function importPageDirectory( $dir, $prefix = "" )
90 echo "\n<!-- Checking page directory " . xmlCommentSafe( $dir ) . " -->\n";
91 $mydir = opendir( $dir );
92 while ( $entry = readdir( $mydir ) ) {
93 $m = array();
94 if ( preg_match( '/^(.+)\.db$/', $entry, $m ) ) {
95 echo importPage( $prefix . $m[1] );
96 } else {
97 if ( is_dir( "$dir/$entry" ) ) {
98 if ( $entry != '.' && $entry != '..' ) {
99 importPageDirectory( "$dir/$entry", "$entry/" );
101 } else {
102 echo "<!-- File '" . xmlCommentSafe( $entry ) . "' doesn't seem to contain an article. Skipping. -->\n";
109 # ------------------------------------------------------------------------------
111 /* fetch_ functions
112 Grab a given item from the database
115 function useModFilename( $title ) {
116 $c = substr( $title, 0, 1 );
117 if ( preg_match( '/[A-Z]/i', $c ) ) {
118 return strtoupper( $c ) . "/$title";
120 return "other/$title";
123 function fetchPage( $title )
125 global $FS1, $FS2, $FS3, $wgRootDirectory;
127 $fname = $wgRootDirectory . "/page/" . useModFilename( $title ) . ".db";
128 if ( !file_exists( $fname ) ) {
129 echo "Couldn't open file '$fname' for page '$title'.\n";
130 die( -1 );
133 $page = splitHash( $FS1, file_get_contents( $fname ) );
134 $section = splitHash( $FS2, $page["text_default"] );
135 $text = splitHash( $FS3, $section["data"] );
137 return array2object( array( "text" => $text["text"] , "summary" => $text["summary"] ,
138 "minor" => $text["minor"] , "ts" => $section["ts"] ,
139 "username" => $section["username"] , "host" => $section["host"] ) );
142 function fetchKeptPages( $title )
144 global $FS1, $FS2, $FS3, $wgRootDirectory;
146 $fname = $wgRootDirectory . "/keep/" . useModFilename( $title ) . ".kp";
147 if ( !file_exists( $fname ) ) return array();
149 $keptlist = explode( $FS1, file_get_contents( $fname ) );
150 array_shift( $keptlist ); # Drop the junk at beginning of file
152 $revisions = array();
153 foreach ( $keptlist as $rev ) {
154 $section = splitHash( $FS2, $rev );
155 $text = splitHash( $FS3, $section["data"] );
156 if ( $text["text"] && $text["minor"] != "" && ( $section["ts"] * 1 > 0 ) ) {
157 array_push( $revisions, array2object( array ( "text" => $text["text"] , "summary" => $text["summary"] ,
158 "minor" => $text["minor"] , "ts" => $section["ts"] ,
159 "username" => $section["username"] , "host" => $section["host"] ) ) );
160 } else {
161 echo "<!-- skipped a bad old revision -->\n";
164 return $revisions;
167 function splitHash ( $sep , $str ) {
168 $temp = explode ( $sep , $str ) ;
169 $ret = array () ;
170 for ( $i = 0; $i + 1 < count ( $temp ) ; $i++ ) {
171 $ret[$temp[$i]] = $temp[++$i] ;
173 return $ret ;
177 /* import_ functions
178 Take a fetched item and produce SQL
181 function checkUserCache( $name, $host )
183 global $usercache;
185 if ( $name ) {
186 if ( in_array( $name, $usercache ) ) {
187 $userid = $usercache[$name];
188 } else {
189 # If we haven't imported user accounts
190 $userid = 0;
192 $username = str_replace( '_', ' ', $name );
193 } else {
194 $userid = 0;
195 $username = $host;
197 return array( $userid, $username );
200 function importPage( $title )
202 echo "\n<!-- Importing page " . xmlCommentSafe( $title ) . " -->\n";
203 $page = fetchPage( $title );
205 $newtitle = xmlsafe( str_replace( '_', ' ', recodeText( $title ) ) );
207 $munged = mungeFormat( $page->text );
208 if ( $munged != $page->text ) {
210 * Save a *new* revision with the conversion, and put the
211 * previous last version into the history.
213 $next = array2object( array(
214 'text' => $munged,
215 'minor' => 1,
216 'username' => 'Conversion script',
217 'host' => '127.0.0.1',
218 'ts' => time(),
219 'summary' => 'link fix',
220 ) );
221 $revisions = array( $page, $next );
222 } else {
224 * Current revision:
226 $revisions = array( $page );
228 $xml = <<<XML
229 <page>
230 <title>$newtitle</title>
232 XML;
234 # History
235 $revisions = array_merge( $revisions, fetchKeptPages( $title ) );
236 if ( count( $revisions ) == 0 ) {
237 return NULL; // Was "$sql", which does not appear to be defined.
240 foreach ( $revisions as $rev ) {
241 $text = xmlsafe( recodeText( $rev->text ) );
242 $minor = ( $rev->minor ? '<minor/>' : '' );
243 list( /* $userid */ , $username ) = checkUserCache( $rev->username, $rev->host );
244 $username = xmlsafe( recodeText( $username ) );
245 $timestamp = xmlsafe( timestamp2ISO8601( $rev->ts ) );
246 $comment = xmlsafe( recodeText( $rev->summary ) );
248 $xml .= <<<XML
249 <revision>
250 <timestamp>$timestamp</timestamp>
251 <contributor><username>$username</username></contributor>
252 $minor
253 <comment>$comment</comment>
254 <text>$text</text>
255 </revision>
257 XML;
259 $xml .= "</page>\n\n";
260 return $xml;
263 # Whee!
264 function recodeText( $string ) {
265 global $wgImportEncoding;
266 # For currently latin-1 wikis
267 $string = str_replace( "\r\n", "\n", $string );
268 $string = @iconv( $wgImportEncoding, "UTF-8", $string );
269 $string = wfMungeToUtf8( $string ); # Any old &#1234; stuff
270 return $string;
273 function wfUtf8Sequence( $codepoint ) {
274 if ( $codepoint < 0x80 ) return chr( $codepoint );
275 if ( $codepoint < 0x800 ) return chr( $codepoint >> 6 & 0x3f | 0xc0 ) .
276 chr( $codepoint & 0x3f | 0x80 );
277 if ( $codepoint < 0x10000 ) return chr( $codepoint >> 12 & 0x0f | 0xe0 ) .
278 chr( $codepoint >> 6 & 0x3f | 0x80 ) .
279 chr( $codepoint & 0x3f | 0x80 );
280 if ( $codepoint < 0x100000 ) return chr( $codepoint >> 18 & 0x07 | 0xf0 ) . # Double-check this
281 chr( $codepoint >> 12 & 0x3f | 0x80 ) .
282 chr( $codepoint >> 6 & 0x3f | 0x80 ) .
283 chr( $codepoint & 0x3f | 0x80 );
284 # Doesn't yet handle outside the BMP
285 return "&#$codepoint;";
288 function wfMungeToUtf8( $string ) {
289 $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
290 $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
291 # Should also do named entities here
292 return $string;
295 function timestamp2ISO8601( $ts ) {
296 # 2003-08-05T18:30:02Z
297 return gmdate( 'Y-m-d', $ts ) . 'T' . gmdate( 'H:i:s', $ts ) . 'Z';
300 function xmlsafe( $string ) {
302 * The page may contain old data which has not been properly normalized.
303 * Invalid UTF-8 sequences or forbidden control characters will make our
304 * XML output invalid, so be sure to strip them out.
306 $string = UtfNormal::cleanUp( $string );
308 $string = htmlspecialchars( $string );
309 return $string;
312 function xmlCommentSafe( $text ) {
313 return str_replace( '--', '\\-\\-', xmlsafe( recodeText( $text ) ) );
317 function array2object( $arr ) {
318 $o = (object)0;
319 foreach ( $arr as $x => $y ) {
320 $o->$x = $y;
322 return $o;
327 * Make CamelCase and /Talk links work
329 function mungeFormat( $text ) {
330 global $nowiki;
331 $nowiki = array();
332 $staged = preg_replace_callback(
333 '/(<nowiki>.*?<\\/nowiki>|(?:http|https|ftp):\\S+|\[\[[^]\\n]+]])/s',
334 'nowikiPlaceholder', $text );
336 # This is probably not 100% correct, I'm just
337 # glancing at the UseModWiki code.
338 $upper = "[A-Z]";
339 $lower = "[a-z_0-9]";
340 $any = "[A-Za-z_0-9]";
341 $camel = "(?:$upper+$lower+$upper+$any*)";
342 $subpage = "(?:\\/$any+)";
343 $substart = "(?:\\/$upper$any*)";
345 $munged = preg_replace( "/(?!\\[\\[)($camel$subpage*|$substart$subpage*)\\b(?!\\]\\]|>)/",
346 '[[$1]]', $staged );
348 $final = preg_replace( '/' . preg_quote( placeholder() ) . '/es',
349 'array_shift( $nowiki )', $munged );
350 return $final;
354 function placeholder( $x = null ) {
355 return '\xffplaceholder\xff';
358 function nowikiPlaceholder( $matches ) {
359 global $nowiki;
360 $nowiki[] = $matches[1];
361 return placeholder();