* fixed table duplication for unit tests
[lhc/web/wiklou.git] / maintenance / importUseModWikipedia.php
1 <?php
2
3 /**
4 * A script to read a dump of the English Wikipedia from the UseModWiki period, and to
5 * generate an XML dump in MediaWiki format.
6 *
7 * Some relevant code was ported from UseModWiki 0.92.
8 *
9 */
10
11 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
12 require_once( dirname( __FILE__ ) .'/../includes/normal/UtfNormalUtil.php' );
13
14
15 class ImportUseModWikipedia extends Maintenance {
16 var $encodeMap, $decodeMap;
17
18 var $deepRenames = array(
19 'JimboWales' => 983862286,
20 'TexaS' => 983918410,
21 'HistoryOfUnitedStatesTalk' => 984795423,
22 'MetallicA' => 985128533,
23 'PythagoreanTheorem' => 985225545,
24 'TheCanonofScripture' => 985368223,
25 'TaoTehChing' => 985368222,
26 //'TheMostRemarkableFormulaInTheWorld' => 985368221,
27 'TheRecorder' => 985368220,
28 'GladstoneOregon' => 985368219,
29 'PacificBeach' => '?',
30 'AaRiver' => '?',
31 );
32
33 var $replacements = array();
34
35 var $renameTextLinksOps = array(
36 983846265 => array(
37 'TestIgnore' => 'IgnoreTest',
38 ),
39 983848080 => array(
40 'UnitedLocomotiveWorks' => 'Atlas Shrugged/United Locomotive Works'
41 ),
42 983856376 => array(
43 'WikiPedia' => 'Wikipedia',
44 ),
45 983896152 => array(
46 'John_F_Kennedy' => 'John_F._Kennedy',
47 ),
48 983905871 => array(
49 'LarrySanger' => 'Larry_Sanger'
50 ),
51 984697068 => array(
52 'UnitedStates' => 'United States',
53 ),
54 984792748 => array(
55 'LibertarianisM' => 'Libertarianism'
56 ),
57 985327832 => array(
58 'AnarchisM' => 'Anarchism',
59 ),
60 985290063 => array(
61 'HistoryOfUnitedStatesDiscussion' => 'History_Of_United_States_Discussion'
62 ),
63 985290091 => array(
64 'BritishEmpire' => 'British Empire'
65 ),
66 /*
67 985468958 => array(
68 'ScienceFiction' => 'Science fiction',
69 ),*/
70 );
71
72 /**
73 * Hack for observed substitution issues
74 */
75 var $skipSelfSubstitution = array(
76 'Pythagorean_Theorem',
77 'The_Most_Remarkable_Formula_In_The_World',
78 'Wine',
79 );
80
81 var $unixLineEndingsOps = array(
82 987743732 => 'Wikipedia_FAQ'
83 );
84
85 var $replacementsDone = array();
86
87 var $moveLog = array();
88 var $moveDests = array();
89 var $revId;
90
91 var $rc = array();
92 var $textCache = array();
93 var $blacklist = array();
94
95 var $FS, $FS1, $FS2, $FS3;
96 var $FreeLinkPattern, $UrlPattern, $LinkPattern, $InterLinkPattern;
97
98 var $cp1252Table = array(
99 0x80 => 0x20ac,
100 0x81 => 0x0081,
101 0x82 => 0x201a,
102 0x83 => 0x0192,
103 0x84 => 0x201e,
104 0x85 => 0x2026,
105 0x86 => 0x2020,
106 0x87 => 0x2021,
107 0x88 => 0x02c6,
108 0x89 => 0x2030,
109 0x8a => 0x0160,
110 0x8b => 0x2039,
111 0x8c => 0x0152,
112 0x8d => 0x008d,
113 0x8e => 0x017d,
114 0x8f => 0x008f,
115 0x90 => 0x0090,
116 0x91 => 0x2018,
117 0x92 => 0x2019,
118 0x93 => 0x201c,
119 0x94 => 0x201d,
120 0x95 => 0x2022,
121 0x96 => 0x2013,
122 0x97 => 0x2014,
123 0x98 => 0x02dc,
124 0x99 => 0x2122,
125 0x9a => 0x0161,
126 0x9b => 0x203a,
127 0x9c => 0x0153,
128 0x9d => 0x009d,
129 0x9e => 0x017e,
130 0x9f => 0x0178);
131
132 public function __construct() {
133 parent::__construct();
134 $this->addOption( 'datadir', 'the value of $DataDir from wiki.cgi', true, true );
135 $this->addOption( 'outfile', 'the name of the output XML file', true, true );
136 $this->initLinkPatterns();
137
138 $this->encodeMap = $this->decodeMap = array();
139
140 for ($source = 0; $source <= 0xff; $source++) {
141 if ( isset( $this->cp1252Table[$source] ) ) {
142 $dest = $this->cp1252Table[$source];
143 } else {
144 $dest = $source;
145 }
146 $sourceChar = chr( $source );
147 $destChar = codepointToUtf8( $dest );
148 $this->encodeMap[$sourceChar] = $destChar;
149 $this->decodeMap[$destChar] = $sourceChar;
150 }
151 }
152
153 function initLinkPatterns() {
154 # Field separators are used in the URL-style patterns below.
155 $this->FS = "\xb3"; # The FS character is a superscript "3"
156 $this->FS1 = $this->FS . "1"; # The FS values are used to separate fields
157 $this->FS2 = $this->FS . "2"; # in stored hashtables and other data structures.
158 $this->FS3 = $this->FS . "3"; # The FS character is not allowed in user data.
159
160 $UpperLetter = "[A-Z";
161 $LowerLetter = "[a-z";
162 $AnyLetter = "[A-Za-z";
163 $AnyLetter .= "_0-9";
164 $UpperLetter .= "]"; $LowerLetter .= "]"; $AnyLetter .= "]";
165
166 # Main link pattern: lowercase between uppercase, then anything
167 $LpA = $UpperLetter . "+" . $LowerLetter . "+" . $UpperLetter
168 . $AnyLetter . "*";
169 # Optional subpage link pattern: uppercase, lowercase, then anything
170 $LpB = $UpperLetter . "+" . $LowerLetter . "+" . $AnyLetter . "*";
171
172 # Loose pattern: If subpage is used, subpage may be simple name
173 $this->LinkPattern = "((?:(?:$LpA)?\\/$LpB)|$LpA)";
174 $QDelim = '(?:"")?'; # Optional quote delimiter (not in output)
175 $this->LinkPattern .= $QDelim;
176
177 # Inter-site convention: sites must start with uppercase letter
178 # (Uppercase letter avoids confusion with URLs)
179 $InterSitePattern = $UpperLetter . $AnyLetter . "+";
180 $this->InterLinkPattern = "((?:$InterSitePattern:[^\\]\\s\"<>{$this->FS}]+)$QDelim)";
181
182 $AnyLetter = "[-,. _0-9A-Za-z]";
183 $this->FreeLinkPattern = "($AnyLetter+)";
184 $this->FreeLinkPattern = "((?:(?:$AnyLetter+)?\\/)?$AnyLetter+)";
185 $this->FreeLinkPattern .= $QDelim;
186
187 # Url-style links are delimited by one of:
188 # 1. Whitespace (kept in output)
189 # 2. Left or right angle-bracket (< or >) (kept in output)
190 # 3. Right square-bracket (]) (kept in output)
191 # 4. A single double-quote (") (kept in output)
192 # 5. A $FS (field separator) character (kept in output)
193 # 6. A double double-quote ("") (removed from output)
194
195 $UrlProtocols = "http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|"
196 . "prospero|telnet|gopher";
197 $UrlProtocols .= '|file';
198 $this->UrlPattern = "((?:(?:$UrlProtocols):[^\\]\\s\"<>{$this->FS}]+)$QDelim)";
199 $ImageExtensions = "(gif|jpg|png|bmp|jpeg)";
200 $RFCPattern = "RFC\\s?(\\d+)";
201 $ISBNPattern = "ISBN:?([0-9- xX]{10,})";
202 }
203
204 function execute() {
205 $this->articleFileName = '/tmp/importUseMod.' . mt_rand( 0, 0x7ffffff ) . '.tmp';
206 $this->patchFileName = '/tmp/importUseMod.' . mt_rand( 0, 0x7ffffff ) . '.tmp';
207 $this->dataDir = $this->getOption( 'datadir' );
208 $this->outFile = fopen( $this->getOption( 'outfile' ), 'w' );
209 if ( !$this->outFile ) {
210 echo "Unable to open output file\n";
211 return 1;
212 }
213 $this->writeXmlHeader();
214 $this->readRclog();
215 $this->writeMoveLog();
216 $this->writeRevisions();
217 $this->reconcileCurrentRevs();
218 $this->writeXmlFooter();
219 unlink( $this->articleFileName );
220 unlink( $this->patchFileName );
221 return 0;
222 }
223
224 function writeXmlHeader() {
225 fwrite( $this->outFile, <<<EOT
226 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd" version="0.3" xml:lang="en">
227 <siteinfo>
228 <sitename>Wikipedia</sitename>
229 <base>http://www.wikipedia.com/</base>
230 <generator>MediaWiki 1.18alpha importUseModWikipedia.php</generator>
231 <case>case-sensitive</case>
232 <namespaces>
233 <namespace key="0" />
234 </namespaces>
235 </siteinfo>
236
237 EOT
238 );
239 }
240
241 function writeXmlFooter() {
242 fwrite( $this->outFile, "</mediawiki>\n" );
243 }
244
245 function readRclog() {
246 $rcFile = fopen( "{$this->dataDir}/rclog", 'r' );
247 while ( $line = fgets( $rcFile ) ) {
248 $bits = explode( $this->FS3, $line );
249 if ( count( $bits ) !== 7 ) {
250 echo "Error reading rclog\n";
251 return;
252 }
253 $params = array(
254 'timestamp' => $bits[0],
255 'rctitle' => $bits[1],
256 'summary' => $bits[2],
257 'minor' => $bits[3],
258 'host' => $bits[4],
259 'kind' => $bits[5],
260 'extra' => array()
261 );
262 $extraList = explode( $this->FS2, $bits[6] );
263
264 for ( $i = 0; $i < count( $extraList ); $i += 2 ) {
265 $params['extra'][$extraList[$i]] = $extraList[$i + 1];
266 }
267 $this->rc[$params['timestamp']][] = $params;
268 }
269 }
270
271 function writeMoveLog() {
272 $this->moveLog = array();
273 $deepRenames = $this->deepRenames;
274 echo "Calculating move log...\n";
275 $this->processDiffFile( array( $this, 'moveLogCallback' ) );
276
277 // We have the timestamp intervals, now make a guess at the actual timestamp
278 foreach ( $this->moveLog as $newTitle => $params ) {
279 // Is there a time specified?
280 $drTime = false;
281 if ( isset( $deepRenames[$params['old']] ) ) {
282 $drTime = $deepRenames[$params['old']];
283 if ( $drTime !== '?' ) {
284 if ( ( !isset( $params['endTime'] ) || $drTime < $params['endTime'] )
285 && $drTime > $params['startTime'] )
286 {
287 $this->moveLog[$newTitle]['timestamp'] = $drTime;
288 $this->moveLog[$newTitle]['deep'] = true;
289
290 echo "{$params['old']} -> $newTitle at $drTime\n";
291 unset( $deepRenames[$params['old']] );
292 continue;
293 } else {
294 echo "WARNING: deep rename time invalid: {$params['old']}\n";
295 unset( $deepRenames[$params['old']] );
296 }
297 }
298 }
299
300 // Guess that it is one second after the last edit to the page before it was moved
301 $this->moveLog[$newTitle]['timestamp'] = $params['startTime'] + 1;
302 if ( $drTime === '?' ) {
303 $this->moveLog[$newTitle]['deep'] = true;
304 unset( $deepRenames[$params['old']] );
305 }
306 if ( isset( $params['endTime'] ) ) {
307 $this->printLatin1( "{$params['old']} -> $newTitle between " .
308 "{$params['startTime']} and {$params['endTime']}\n" );
309 } else {
310 $this->printLatin1( "{$params['old']} -> $newTitle after " .
311 "{$params['startTime']}\n" );
312 }
313 }
314
315 // Write the move log to the XML file
316 $id = 1;
317 foreach ( $this->moveLog as $newTitle => $params ) {
318 $out = "<logitem>\n" .
319 $this->element( 'id', $id++ ) .
320 $this->element( 'timestamp', wfTimestamp( TS_ISO_8601, $params['timestamp'] ) ) .
321 "<contributor>\n" .
322 $this->element( 'username', 'UseModWiki admin' ) .
323 "</contributor>" .
324 $this->element( 'type', 'move' ) .
325 $this->element( 'action', 'move' ) .
326 $this->element( 'logtitle', $params['old'] ) .
327 "<params xml:space=\"preserve\">" .
328 htmlspecialchars( $this->encode( "{$newTitle}\n1" ) ) .
329 "</params>\n" .
330 "</logitem>\n";
331 fwrite( $this->outFile, $out );
332 }
333
334 // Check for remaining deep rename entries
335 if ( $deepRenames ) {
336 echo "WARNING: the following entries in \$this->deepRenames are " .
337 "invalid, since no such move exists:\n" .
338 implode( "\n", array_keys( $deepRenames ) ) .
339 "\n\n";
340 }
341
342 }
343
344 function element( $name, $value ) {
345 return "<$name>" . htmlspecialchars( $this->encode( $value ) ) . "</$name>\n";
346 }
347
348 function moveLogCallback( $entry ) {
349 $rctitle = $entry['rctitle'];
350 $title = $entry['title'];
351 $this->moveDests[$rctitle] = $title;
352
353 if ( $rctitle === $title ) {
354 if ( isset( $this->moveLog[$rctitle] )
355 && !isset( $this->moveLog[$rctitle]['endTime'] ) )
356 {
357 // This is the latest time that the page could have been moved
358 $this->moveLog[$rctitle]['endTime'] = $entry['timestamp'];
359 }
360 } else {
361 if ( !isset( $this->moveLog[$rctitle] ) ) {
362 // Initialise the move log entry
363 $this->moveLog[$rctitle] = array(
364 'old' => $title
365 );
366 }
367 // Update the earliest time the page could have been moved
368 $this->moveLog[$rctitle]['startTime'] = $entry['timestamp'];
369 }
370 }
371
372 function writeRevisions() {
373 $this->numGoodRevs = 0;
374 $this->revId = 1;
375 $this->processDiffFile( array( $this, 'revisionCallback' ) );
376 echo "\n\nImported {$this->numGoodRevs} out of {$this->numRevs}\n";
377 }
378
379 function revisionCallback( $params ) {
380 $title = $params['rctitle'];
381 $editTime = $params['timestamp'];
382
383 if ( isset( $this->blacklist[$title] ) ) {
384 return;
385 }
386 $this->doPendingOps( $editTime );
387
388 $origText = $this->getText( $title );
389 $text = $this->patch( $origText, $params['diff'] );
390 if ( $text === false ) {
391 echo "$editTime $title attempting resolution...\n";
392 $linkSubstitutes = $this->resolveFailedDiff( $origText, $params['diff'] );
393 if ( !$linkSubstitutes ) {
394 $this->printLatin1( "$editTime $title DIFF FAILED\n" );
395 $this->blacklist[$title] = true;
396 return;
397 }
398 $this->printLatin1( "$editTime $title requires substitutions:\n" );
399 $time = $editTime - 1;
400 foreach ( $linkSubstitutes as $old => $new ) {
401 $this->printLatin1( "SUBSTITUTE $old -> $new\n" );
402 $this->renameTextLinks( $old, $new, $time-- );
403 }
404 $origText = $this->getText( $title );
405 $text = $this->patch( $origText, $params['diff'] );
406 if ( $text === false ) {
407 $this->printLatin1( "$editTime $title STILL FAILS!\n" );
408 $this->blacklist[$title] = true;
409 return;
410 }
411
412 echo "\n";
413 }
414
415 $params['text'] = $text;
416 $this->saveRevision( $params );
417 $this->numGoodRevs++;
418 #$this->printLatin1( "$editTime $title\n" );
419 }
420
421 function doPendingOps( $editTime ) {
422 foreach ( $this->moveLog as $newTitle => $entry ) {
423 if ( $entry['timestamp'] <= $editTime ) {
424 unset( $this->moveLog[$newTitle] );
425 if ( isset( $entry['deep'] ) ) {
426 $this->renameTextLinks( $entry['old'], $newTitle, $entry['timestamp'] );
427 }
428 }
429 }
430
431 foreach ( $this->renameTextLinksOps as $renameTime => $replacements ) {
432 if ( $editTime >= $renameTime ) {
433 foreach ( $replacements as $old => $new ) {
434 $this->printLatin1( "SUBSTITUTE $old -> $new\n" );
435 $this->renameTextLinks( $old, $new, $renameTime );
436 }
437 unset( $this->renameTextLinksOps[$renameTime] );
438 }
439 }
440
441 foreach ( $this->unixLineEndingsOps as $fixTime => $title ) {
442 if ( $editTime >= $fixTime ) {
443 $this->printLatin1( "$fixTime $title FIXING LINE ENDINGS\n" );
444 $text = $this->getText( $title );
445 $text = str_replace( "\r", '', $text );
446 $this->saveRevision( array(
447 'rctitle' => $title,
448 'timestamp' => $fixTime,
449 'extra' => array( 'name' => 'UseModWiki admin' ),
450 'text' => $text,
451 'summary' => 'Fixing line endings',
452 ) );
453 unset( $this->unixLineEndingsOps[$fixTime] );
454 }
455 }
456 }
457
458 function patch( $source, $diff ) {
459 file_put_contents( $this->articleFileName, $source );
460 file_put_contents( $this->patchFileName, $diff );
461 $error = wfShellExec(
462 wfEscapeShellArg(
463 'patch',
464 '-n',
465 '-r', '-',
466 '--no-backup-if-mismatch',
467 '--binary',
468 $this->articleFileName,
469 $this->patchFileName
470 ) . ' 2>&1',
471 $status
472 );
473 $text = file_get_contents( $this->articleFileName );
474 if ( $status || $text === false ) {
475 return false;
476 } else {
477 return $text;
478 }
479 }
480
481 function resolveFailedDiff( $origText, $diff ) {
482 $context = array();
483 $diffLines = explode( "\n", $diff );
484 for ( $i = 0; $i < count( $diffLines ); $i++ ) {
485 $diffLine = $diffLines[$i];
486 if ( !preg_match( '/^(\d+)(?:,\d+)?[acd]\d+(?:,\d+)?$/', $diffLine, $m ) ) {
487 continue;
488 }
489
490 $sourceIndex = intval( $m[1] );
491 $i++;
492 while ( $i < count( $diffLines ) && substr( $diffLines[$i], 0, 1 ) === '<' ) {
493 $context[$sourceIndex - 1] = substr( $diffLines[$i], 2 );
494 $sourceIndex++;
495 $i++;
496 }
497 $i--;
498 }
499
500 $changedLinks = array();
501 $origLines = explode( "\n", $origText );
502 foreach ( $context as $i => $contextLine ) {
503 $origLine = isset( $origLines[$i] ) ? $origLines[$i] : '';
504 if ( $contextLine === $origLine ) {
505 continue;
506 }
507 $newChanges = $this->resolveTextChange( $origLine, $contextLine );
508 if ( is_array( $newChanges ) ) {
509 $changedLinks += $newChanges;
510 } else {
511 echo "Resolution failure on line " . ( $i + 1 ) . "\n";
512 $this->printLatin1( $newChanges );
513 }
514 }
515
516 return $changedLinks;
517 }
518
519 function resolveTextChange( $source, $dest ) {
520 $changedLinks = array();
521 $sourceLinks = $this->getLinkList( $source );
522 $destLinks = $this->getLinkList( $dest );
523 $newLinks = array_diff( $destLinks, $sourceLinks );
524 $removedLinks = array_diff( $sourceLinks, $destLinks );
525
526 // Match up the removed links with the new links
527 foreach ( $newLinks as $newLink ) {
528 $minDistance = 100000000;
529 $bestRemovedLink = false;
530 foreach ( $removedLinks as $removedLink ) {
531 $editDistance = levenshtein( $newLink, $removedLink );
532 if ( $editDistance < $minDistance ) {
533 $minDistance = $editDistance;
534 $bestRemovedLink = $removedLink;
535 }
536 }
537 if ( $bestRemovedLink !== false ) {
538 $changedLinks[$bestRemovedLink] = $newLink;
539 $newLinks = array_diff( $newLinks, array( $newLink ) );
540 $removedLinks = array_diff( $removedLinks, array( $bestRemovedLink ) );
541 }
542 }
543
544 $proposal = $source;
545 foreach ( $changedLinks as $removedLink => $newLink ) {
546 $proposal = $this->substituteTextLinks( $removedLink, $newLink, $proposal );
547 }
548 if ( $proposal !== $dest ) {
549 // Resolution failed
550 $msg = "Source line: $source\n" .
551 "Source links: " . implode( ', ', $sourceLinks ) . "\n" .
552 "Context line: $dest\n" .
553 "Context links: " . implode( ', ', $destLinks ) . "\n" .
554 "Proposal: $proposal\n";
555 return $msg;
556 }
557 return $changedLinks;
558 }
559
560 function processDiffFile( $callback ) {
561 $diffFile = fopen( "{$this->dataDir}/diff_log", 'r' );
562
563 $delimiter = "------\n";
564 file_put_contents( $this->articleFileName, "Describe the new page here.\n" );
565
566 $line = fgets( $diffFile );
567 $lineNum = 1;
568 if ( $line !== $delimiter ) {
569 echo "Invalid diff file\n";
570 return false;
571 }
572 $lastReportLine = 0;
573 $this->numRevs = 0;
574
575 while ( true ) {
576 $line = fgets( $diffFile );
577 $lineNum++;
578 if ( $line === false ) {
579 break;
580 }
581 if ( $lineNum > $lastReportLine + 1000 ) {
582 $lastReportLine = $lineNum;
583 fwrite( STDERR, "$lineNum \r" );
584 fflush( STDERR );
585 }
586 $line = trim( $line );
587 if ( !preg_match( '/^([^|]+)\|(\d+)$/', $line, $matches ) ) {
588 echo "Invalid header on line $lineNum\n";
589 return true;
590 }
591 list( , $title, $editTime ) = $matches;
592
593 $diff = '';
594 $diffStartLine = $lineNum;
595 while ( true ) {
596 $line = fgets( $diffFile );
597 $lineNum++;
598 if ( $line === $delimiter ) {
599 break;
600 }
601 if ( $line === false ) {
602 break 2;
603 }
604 $diff .= $line;
605 }
606
607 $this->numRevs++;
608
609 if ( !isset( $this->rc[$editTime] ) ) {
610 $this->printLatin1( "$editTime $title DELETED, skipping\n" );
611 continue;
612 }
613
614 if ( count( $this->rc[$editTime] ) == 1 ) {
615 $params = $this->rc[$editTime][0];
616 } else {
617 $params = false;
618 $candidates = '';
619 foreach ( $this->rc[$editTime] as $rc ) {
620 if ( $rc['rctitle'] === $title ) {
621 $params = $rc;
622 break;
623 }
624 if ( $candidates === '' ) {
625 $candidates = $rc['rctitle'];
626 } else {
627 $candidates .= ', ' . $rc['rctitle'];
628 }
629 }
630 if ( !$params ) {
631 $this->printLatin1( "$editTime $title ERROR cannot resolve rclog\n" );
632 $this->printLatin1( "$editTime $title CANDIDATES: $candidates\n" );
633 continue;
634 }
635 }
636 $params['diff'] = $diff;
637 $params['title'] = $title;
638 $params['diffStartLine'] = $diffStartLine;
639 call_user_func( $callback, $params );
640 }
641 echo "\n";
642
643 if ( !feof( $diffFile ) ) {
644 echo "Stopped at line $lineNum\n";
645 }
646 return true;
647 }
648
649 function reconcileCurrentRevs() {
650 foreach ( $this->textCache as $title => $text ) {
651 $fileName = "{$this->dataDir}/page/";
652 if ( preg_match( '/^[A-Z]/', $title, $m ) ) {
653 $fileName .= $m[0];
654 } else {
655 $fileName .= 'other';
656 }
657 $fileName .= "/$title.db";
658
659 if ( !file_exists( $fileName ) ) {
660 $this->printLatin1( "ERROR: Cannot find page file for {$title}\n" );
661 continue;
662 }
663
664 $fileContents = file_get_contents( $fileName );
665 $page = $this->unserializeUseMod( $fileContents, $this->FS1 );
666 $section = $this->unserializeUseMod( $page['text_default'], $this->FS2 );
667 $data = $this->unserializeUseMod( $section['data'], $this->FS3 );
668 $pageText = $data['text'];
669 if ( $text !== $pageText ) {
670 $substs = $this->resolveTextChange( $text, $pageText );
671 if ( is_array( $substs ) ) {
672 foreach ( $substs as $source => $dest ) {
673 if ( isset( $this->moveLog[$dest] ) ) {
674 $this->printLatin1( "ERROR: need deep rename: $source\n" );
675 } else {
676 $this->printLatin1( "ERROR: need substitute: $source -> $dest\n" );
677 }
678 }
679 } else {
680 $this->printLatin1( "ERROR: unresolved diff in $title:\n" );
681 wfSuppressWarnings();
682 $diff = xdiff_string_diff( $text, $pageText ) . '';
683 wfRestoreWarnings();
684 $this->printLatin1( "$diff\n" );
685 }
686 }
687 }
688 }
689
690 function makeTitle( $titleText ) {
691 return Title::newFromText( $this->encode( $titleText ) );
692 }
693
694 function getText( $titleText ) {
695 if ( !isset( $this->textCache[$titleText] ) ) {
696 return "Describe the new page here.\n";
697 } else {
698 return $this->textCache[$titleText];
699 }
700 }
701
702 function saveRevision( $params ) {
703 $this->textCache[$params['rctitle']] = $params['text'];
704
705 $out = "<page>\n" .
706 $this->element( 'title', $params['rctitle'] ) .
707 "<revision>\n" .
708 $this->element( 'id', $this->revId ++ ) .
709 $this->element( 'timestamp', wfTimestamp( TS_ISO_8601, $params['timestamp'] ) ) .
710 "<contributor>\n";
711 if ( isset( $params['extra']['name'] ) ) {
712 $out .= $this->element( 'username', $params['extra']['name'] );
713 }
714 if ( isset( $params['extra']['id'] ) ) {
715 $out .= $this->element( 'id', $params['extra']['id'] );
716 }
717 if ( isset( $params['host'] ) ) {
718 $out .= $this->element( 'ip', $params['host'] );
719 }
720 $out .=
721 "</contributor>\n" .
722 $this->element( 'comment', $params['summary'] ) .
723 "<text xml:space=\"preserve\">" .
724 htmlspecialchars( $this->encode( $params['text'] ) ) .
725 "</text>\n" .
726 "</revision>\n" .
727 "</page>\n";
728 fwrite( $this->outFile, $out );
729 }
730
731 function renameTextLinks( $old, $new, $timestamp ) {
732 $newWithUnderscores = $new;
733 $old = str_replace( '_', ' ', $old );
734 $new = str_replace( '_', ' ', $new );
735
736 foreach ( $this->textCache as $title => $oldText ) {
737 if ( $newWithUnderscores === $title
738 && in_array( $title, $this->skipSelfSubstitution ) )
739 {
740 // Hack to make Pythagorean_Theorem etc. work
741 continue;
742 }
743
744 $newText = $this->substituteTextLinks( $old, $new, $oldText );
745 if ( $oldText !== $newText ) {
746 $this->saveRevision( array(
747 'rctitle' => $title,
748 'timestamp' => $timestamp,
749 'text' => $newText,
750 'extra' => array( 'name' => 'Page move link fixup script' ),
751 'summary' => '',
752 'minor' => true
753 ) );
754 }
755 }
756 }
757
758 function substituteTextLinks( $old, $new, $text ) {
759 $this->saveUrl = array();
760 $this->old = $old;
761 $this->new = $new;
762
763 $text = str_replace( $this->FS, '', $text ); # Remove separators (paranoia)
764 $text = preg_replace_callback( '/(<pre>(.*?)<\/pre>)/is',
765 array( $this, 'storeRaw' ), $text );
766 $text = preg_replace_callback( '/(<code>(.*?)<\/code>)/is',
767 array( $this, 'storeRaw' ), $text );
768 $text = preg_replace_callback( '/(<nowiki>(.*?)<\/nowiki>)/s',
769 array( $this, 'storeRaw' ), $text );
770
771 $text = preg_replace_callback( "/\[\[{$this->FreeLinkPattern}\|([^\]]+)\]\]/",
772 array( $this, 'subFreeLink' ), $text );
773 $text = preg_replace_callback( "/\[\[{$this->FreeLinkPattern}\]\]/",
774 array( $this, 'subFreeLink' ), $text );
775 $text = preg_replace_callback( "/(\[{$this->UrlPattern}\s+([^\]]+?)\])/",
776 array( $this, 'storeRaw' ), $text );
777 $text = preg_replace_callback( "/(\[{$this->InterLinkPattern}\s+([^\]]+?)\])/",
778 array( $this, 'storeRaw' ), $text );
779 $text = preg_replace_callback( "/(\[?{$this->UrlPattern}\]?)/",
780 array( $this, 'storeRaw' ), $text );
781 $text = preg_replace_callback( "/(\[?{$this->InterLinkPattern}\]?)/",
782 array( $this, 'storeRaw' ), $text );
783 $text = preg_replace_callback( "/{$this->LinkPattern}/",
784 array( $this, 'subWikiLink' ), $text );
785
786 $text = preg_replace_callback( "/{$this->FS}(\d+){$this->FS}/",
787 array( $this, 'restoreRaw' ), $text ); # Restore saved text
788 return $text;
789 }
790
791 function getLinkList( $text ) {
792 $this->saveUrl = array();
793 $this->linkList = array();
794
795 $text = str_replace( $this->FS, '', $text ); # Remove separators (paranoia)
796 $text = preg_replace_callback( '/(<pre>(.*?)<\/pre>)/is',
797 array( $this, 'storeRaw' ), $text );
798 $text = preg_replace_callback( '/(<code>(.*?)<\/code>)/is',
799 array( $this, 'storeRaw' ), $text );
800 $text = preg_replace_callback( '/(<nowiki>(.*?)<\/nowiki>)/s',
801 array( $this, 'storeRaw' ), $text );
802
803 $text = preg_replace_callback( "/\[\[{$this->FreeLinkPattern}\|([^\]]+)\]\]/",
804 array( $this, 'storeLink' ), $text );
805 $text = preg_replace_callback( "/\[\[{$this->FreeLinkPattern}\]\]/",
806 array( $this, 'storeLink' ), $text );
807 $text = preg_replace_callback( "/(\[{$this->UrlPattern}\s+([^\]]+?)\])/",
808 array( $this, 'storeRaw' ), $text );
809 $text = preg_replace_callback( "/(\[{$this->InterLinkPattern}\s+([^\]]+?)\])/",
810 array( $this, 'storeRaw' ), $text );
811 $text = preg_replace_callback( "/(\[?{$this->UrlPattern}\]?)/",
812 array( $this, 'storeRaw' ), $text );
813 $text = preg_replace_callback( "/(\[?{$this->InterLinkPattern}\]?)/",
814 array( $this, 'storeRaw' ), $text );
815 $text = preg_replace_callback( "/{$this->LinkPattern}/",
816 array( $this, 'storeLink' ), $text );
817
818 return $this->linkList;
819 }
820
821 function storeRaw( $m ) {
822 $this->saveUrl[] = $m[1];
823 return $this->FS . (count( $this->saveUrl ) - 1) . $this->FS;
824 }
825
826 function subFreeLink( $m ) {
827 $link = $m[1];
828 if ( isset( $m[2] ) ) {
829 $name = $m[2];
830 } else {
831 $name = '';
832 }
833 $oldlink = $link;
834 $link = preg_replace( '/^\s+/', '', $link );
835 $link = preg_replace( '/\s+$/', '', $link );
836 if ( $link == $this->old ) {
837 $link = $this->new;
838 } else {
839 $link = $oldlink; # Preserve spaces if no match
840 }
841 $link = "[[$link";
842 if ( $name !== "" ) {
843 $link .= "|$name";
844 }
845 $link .= "]]";
846 return $this->storeRaw( array( 1 => $link ) );
847 }
848
849 function subWikiLink( $m ) {
850 $link = $m[1];
851 if ( $link == $this->old ) {
852 $link = $this->new;
853 if ( !preg_match( "/^{$this->LinkPattern}$/", $this->new ) ) {
854 $link = "[[$link]]";
855 }
856 }
857 return $this->storeRaw( array( 1 => $link ) );
858 }
859
860 function restoreRaw( $m ) {
861 return $this->saveUrl[$m[1]];
862 }
863
864 function storeLink( $m ) {
865 $this->linkList[] = $m[1];
866 return $this->storeRaw( $m );
867 }
868
869 function encode( $s ) {
870 return strtr( $s, $this->encodeMap );
871 }
872
873 function decode( $s ) {
874 return strtr( $s, $this->decodeMap );
875 }
876
877 function printLatin1( $s ) {
878 echo $this->encode( $s );
879 }
880
881 function unserializeUseMod( $s, $sep ) {
882 $parts = explode( $sep, $s );
883 $result = array();
884 for ( $i = 0; $i < count( $parts ); $i += 2 ) {
885 $result[$parts[$i]] = $parts[$i+1];
886 }
887 return $result;
888 }
889 }
890
891 $maintClass = 'ImportUseModWikipedia';
892 require_once( RUN_MAINTENANCE_IF_MAIN );