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