FU r106752: added b/c code to FSRepo to make things easy for extensions (like Confirm...
[lhc/web/wiklou.git] / includes / Import.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup SpecialPage
25 */
26
27 /**
28 * XML file reader for the page data importer
29 *
30 * implements Special:Import
31 * @ingroup SpecialPage
32 */
33 class WikiImporter {
34 private $reader = null;
35 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
36 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
37 private $mDebug;
38 private $mImportUploads, $mImageBasePath;
39 private $mNoUpdates = false;
40
41 /**
42 * Creates an ImportXMLReader drawing from the source provided
43 * @param $source
44 */
45 function __construct( $source ) {
46 $this->reader = new XMLReader();
47
48 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
49 $id = UploadSourceAdapter::registerSource( $source );
50 if (defined( 'LIBXML_PARSEHUGE' ) ) {
51 $this->reader->open( "uploadsource://$id", null, LIBXML_PARSEHUGE );
52 } else {
53 $this->reader->open( "uploadsource://$id" );
54 }
55
56 // Default callbacks
57 $this->setRevisionCallback( array( $this, "importRevision" ) );
58 $this->setUploadCallback( array( $this, 'importUpload' ) );
59 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
60 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
61 }
62
63 private function throwXmlError( $err ) {
64 $this->debug( "FAILURE: $err" );
65 wfDebug( "WikiImporter XML error: $err\n" );
66 }
67
68 private function debug( $data ) {
69 if( $this->mDebug ) {
70 wfDebug( "IMPORT: $data\n" );
71 }
72 }
73
74 private function warn( $data ) {
75 wfDebug( "IMPORT: $data\n" );
76 }
77
78 private function notice( $data ) {
79 global $wgCommandLineMode;
80 if( $wgCommandLineMode ) {
81 print "$data\n";
82 } else {
83 global $wgOut;
84 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
85 }
86 }
87
88 /**
89 * Set debug mode...
90 * @param $debug bool
91 */
92 function setDebug( $debug ) {
93 $this->mDebug = $debug;
94 }
95
96 /**
97 * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
98 * @param $noupdates bool
99 */
100 function setNoUpdates( $noupdates ) {
101 $this->mNoUpdates = $noupdates;
102 }
103
104 /**
105 * Sets the action to perform as each new page in the stream is reached.
106 * @param $callback callback
107 * @return callback
108 */
109 public function setPageCallback( $callback ) {
110 $previous = $this->mPageCallback;
111 $this->mPageCallback = $callback;
112 return $previous;
113 }
114
115 /**
116 * Sets the action to perform as each page in the stream is completed.
117 * Callback accepts the page title (as a Title object), a second object
118 * with the original title form (in case it's been overridden into a
119 * local namespace), and a count of revisions.
120 *
121 * @param $callback callback
122 * @return callback
123 */
124 public function setPageOutCallback( $callback ) {
125 $previous = $this->mPageOutCallback;
126 $this->mPageOutCallback = $callback;
127 return $previous;
128 }
129
130 /**
131 * Sets the action to perform as each page revision is reached.
132 * @param $callback callback
133 * @return callback
134 */
135 public function setRevisionCallback( $callback ) {
136 $previous = $this->mRevisionCallback;
137 $this->mRevisionCallback = $callback;
138 return $previous;
139 }
140
141 /**
142 * Sets the action to perform as each file upload version is reached.
143 * @param $callback callback
144 * @return callback
145 */
146 public function setUploadCallback( $callback ) {
147 $previous = $this->mUploadCallback;
148 $this->mUploadCallback = $callback;
149 return $previous;
150 }
151
152 /**
153 * Sets the action to perform as each log item reached.
154 * @param $callback callback
155 * @return callback
156 */
157 public function setLogItemCallback( $callback ) {
158 $previous = $this->mLogItemCallback;
159 $this->mLogItemCallback = $callback;
160 return $previous;
161 }
162
163 /**
164 * Sets the action to perform when site info is encountered
165 * @param $callback callback
166 * @return callback
167 */
168 public function setSiteInfoCallback( $callback ) {
169 $previous = $this->mSiteInfoCallback;
170 $this->mSiteInfoCallback = $callback;
171 return $previous;
172 }
173
174 /**
175 * Set a target namespace to override the defaults
176 * @param $namespace
177 * @return bool
178 */
179 public function setTargetNamespace( $namespace ) {
180 if( is_null( $namespace ) ) {
181 // Don't override namespaces
182 $this->mTargetNamespace = null;
183 } elseif( $namespace >= 0 ) {
184 // @todo FIXME: Check for validity
185 $this->mTargetNamespace = intval( $namespace );
186 } else {
187 return false;
188 }
189 }
190
191 /**
192 * @param $dir
193 */
194 public function setImageBasePath( $dir ) {
195 $this->mImageBasePath = $dir;
196 }
197
198 /**
199 * @param $import
200 */
201 public function setImportUploads( $import ) {
202 $this->mImportUploads = $import;
203 }
204
205 /**
206 * Default per-revision callback, performs the import.
207 * @param $revision WikiRevision
208 * @return bool
209 */
210 public function importRevision( $revision ) {
211 $dbw = wfGetDB( DB_MASTER );
212 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
213 }
214
215 /**
216 * Default per-revision callback, performs the import.
217 * @param $rev WikiRevision
218 * @return bool
219 */
220 public function importLogItem( $rev ) {
221 $dbw = wfGetDB( DB_MASTER );
222 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
223 }
224
225 /**
226 * Dummy for now...
227 * @param $revision
228 * @return bool
229 */
230 public function importUpload( $revision ) {
231 $dbw = wfGetDB( DB_MASTER );
232 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
233 }
234
235 /**
236 * Mostly for hook use
237 * @param $title
238 * @param $origTitle
239 * @param $revCount
240 * @param $sRevCount
241 * @param $pageInfo
242 * @return
243 */
244 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
245 $args = func_get_args();
246 return wfRunHooks( 'AfterImportPage', $args );
247 }
248
249 /**
250 * Alternate per-revision callback, for debugging.
251 * @param $revision WikiRevision
252 */
253 public function debugRevisionHandler( &$revision ) {
254 $this->debug( "Got revision:" );
255 if( is_object( $revision->title ) ) {
256 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
257 } else {
258 $this->debug( "-- Title: <invalid>" );
259 }
260 $this->debug( "-- User: " . $revision->user_text );
261 $this->debug( "-- Timestamp: " . $revision->timestamp );
262 $this->debug( "-- Comment: " . $revision->comment );
263 $this->debug( "-- Text: " . $revision->text );
264 }
265
266 /**
267 * Notify the callback function when a new <page> is reached.
268 * @param $title Title
269 */
270 function pageCallback( $title ) {
271 if( isset( $this->mPageCallback ) ) {
272 call_user_func( $this->mPageCallback, $title );
273 }
274 }
275
276 /**
277 * Notify the callback function when a </page> is closed.
278 * @param $title Title
279 * @param $origTitle Title
280 * @param $revCount Integer
281 * @param $sucCount Int: number of revisions for which callback returned true
282 * @param $pageInfo Array: associative array of page information
283 */
284 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
285 if( isset( $this->mPageOutCallback ) ) {
286 $args = func_get_args();
287 call_user_func_array( $this->mPageOutCallback, $args );
288 }
289 }
290
291 /**
292 * Notify the callback function of a revision
293 * @param $revision A WikiRevision object
294 */
295 private function revisionCallback( $revision ) {
296 if ( isset( $this->mRevisionCallback ) ) {
297 return call_user_func_array( $this->mRevisionCallback,
298 array( $revision, $this ) );
299 } else {
300 return false;
301 }
302 }
303
304 /**
305 * Notify the callback function of a new log item
306 * @param $revision A WikiRevision object
307 */
308 private function logItemCallback( $revision ) {
309 if ( isset( $this->mLogItemCallback ) ) {
310 return call_user_func_array( $this->mLogItemCallback,
311 array( $revision, $this ) );
312 } else {
313 return false;
314 }
315 }
316
317 /**
318 * Shouldn't something like this be built-in to XMLReader?
319 * Fetches text contents of the current element, assuming
320 * no sub-elements or such scary things.
321 * @return string
322 * @access private
323 */
324 private function nodeContents() {
325 if( $this->reader->isEmptyElement ) {
326 return "";
327 }
328 $buffer = "";
329 while( $this->reader->read() ) {
330 switch( $this->reader->nodeType ) {
331 case XmlReader::TEXT:
332 case XmlReader::SIGNIFICANT_WHITESPACE:
333 $buffer .= $this->reader->value;
334 break;
335 case XmlReader::END_ELEMENT:
336 return $buffer;
337 }
338 }
339
340 $this->reader->close();
341 return '';
342 }
343
344 # --------------
345
346 /** Left in for debugging */
347 private function dumpElement() {
348 static $lookup = null;
349 if (!$lookup) {
350 $xmlReaderConstants = array(
351 "NONE",
352 "ELEMENT",
353 "ATTRIBUTE",
354 "TEXT",
355 "CDATA",
356 "ENTITY_REF",
357 "ENTITY",
358 "PI",
359 "COMMENT",
360 "DOC",
361 "DOC_TYPE",
362 "DOC_FRAGMENT",
363 "NOTATION",
364 "WHITESPACE",
365 "SIGNIFICANT_WHITESPACE",
366 "END_ELEMENT",
367 "END_ENTITY",
368 "XML_DECLARATION",
369 );
370 $lookup = array();
371
372 foreach( $xmlReaderConstants as $name ) {
373 $lookup[constant("XmlReader::$name")] = $name;
374 }
375 }
376
377 print( var_dump(
378 $lookup[$this->reader->nodeType],
379 $this->reader->name,
380 $this->reader->value
381 )."\n\n" );
382 }
383
384 /**
385 * Primary entry point
386 */
387 public function doImport() {
388 $this->reader->read();
389
390 if ( $this->reader->name != 'mediawiki' ) {
391 throw new MWException( "Expected <mediawiki> tag, got ".
392 $this->reader->name );
393 }
394 $this->debug( "<mediawiki> tag is correct." );
395
396 $this->debug( "Starting primary dump processing loop." );
397
398 $keepReading = $this->reader->read();
399 $skip = false;
400 while ( $keepReading ) {
401 $tag = $this->reader->name;
402 $type = $this->reader->nodeType;
403
404 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this ) ) {
405 // Do nothing
406 } elseif ( $tag == 'mediawiki' && $type == XmlReader::END_ELEMENT ) {
407 break;
408 } elseif ( $tag == 'siteinfo' ) {
409 $this->handleSiteInfo();
410 } elseif ( $tag == 'page' ) {
411 $this->handlePage();
412 } elseif ( $tag == 'logitem' ) {
413 $this->handleLogItem();
414 } elseif ( $tag != '#text' ) {
415 $this->warn( "Unhandled top-level XML tag $tag" );
416
417 $skip = true;
418 }
419
420 if ($skip) {
421 $keepReading = $this->reader->next();
422 $skip = false;
423 $this->debug( "Skip" );
424 } else {
425 $keepReading = $this->reader->read();
426 }
427 }
428
429 return true;
430 }
431
432 /**
433 * @return bool
434 * @throws MWException
435 */
436 private function handleSiteInfo() {
437 // Site info is useful, but not actually used for dump imports.
438 // Includes a quick short-circuit to save performance.
439 if ( ! $this->mSiteInfoCallback ) {
440 $this->reader->next();
441 return true;
442 }
443 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
444 }
445
446 private function handleLogItem() {
447 $this->debug( "Enter log item handler." );
448 $logInfo = array();
449
450 // Fields that can just be stuffed in the pageInfo object
451 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
452 'logtitle', 'params' );
453
454 while ( $this->reader->read() ) {
455 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
456 $this->reader->name == 'logitem') {
457 break;
458 }
459
460 $tag = $this->reader->name;
461
462 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
463 $this, $logInfo ) ) {
464 // Do nothing
465 } elseif ( in_array( $tag, $normalFields ) ) {
466 $logInfo[$tag] = $this->nodeContents();
467 } elseif ( $tag == 'contributor' ) {
468 $logInfo['contributor'] = $this->handleContributor();
469 } elseif ( $tag != '#text' ) {
470 $this->warn( "Unhandled log-item XML tag $tag" );
471 }
472 }
473
474 $this->processLogItem( $logInfo );
475 }
476
477 /**
478 * @param $logInfo
479 * @return bool|mixed
480 */
481 private function processLogItem( $logInfo ) {
482 $revision = new WikiRevision;
483
484 $revision->setID( $logInfo['id'] );
485 $revision->setType( $logInfo['type'] );
486 $revision->setAction( $logInfo['action'] );
487 $revision->setTimestamp( $logInfo['timestamp'] );
488 $revision->setParams( $logInfo['params'] );
489 $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
490 $revision->setNoUpdates( $this->mNoUpdates );
491
492 if ( isset( $logInfo['comment'] ) ) {
493 $revision->setComment( $logInfo['comment'] );
494 }
495
496 if ( isset( $logInfo['contributor']['ip'] ) ) {
497 $revision->setUserIP( $logInfo['contributor']['ip'] );
498 }
499 if ( isset( $logInfo['contributor']['username'] ) ) {
500 $revision->setUserName( $logInfo['contributor']['username'] );
501 }
502
503 return $this->logItemCallback( $revision );
504 }
505
506 private function handlePage() {
507 // Handle page data.
508 $this->debug( "Enter page handler." );
509 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
510
511 // Fields that can just be stuffed in the pageInfo object
512 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
513
514 $skip = false;
515 $badTitle = false;
516
517 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
518 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
519 $this->reader->name == 'page') {
520 break;
521 }
522
523 $tag = $this->reader->name;
524
525 if ( $badTitle ) {
526 // The title is invalid, bail out of this page
527 $skip = true;
528 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this,
529 &$pageInfo ) ) ) {
530 // Do nothing
531 } elseif ( in_array( $tag, $normalFields ) ) {
532 $pageInfo[$tag] = $this->nodeContents();
533 if ( $tag == 'title' ) {
534 $title = $this->processTitle( $pageInfo['title'] );
535
536 if ( !$title ) {
537 $badTitle = true;
538 $skip = true;
539 }
540
541 $this->pageCallback( $title );
542 list( $pageInfo['_title'], $origTitle ) = $title;
543 }
544 } elseif ( $tag == 'revision' ) {
545 $this->handleRevision( $pageInfo );
546 } elseif ( $tag == 'upload' ) {
547 $this->handleUpload( $pageInfo );
548 } elseif ( $tag != '#text' ) {
549 $this->warn( "Unhandled page XML tag $tag" );
550 $skip = true;
551 }
552 }
553
554 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
555 $pageInfo['revisionCount'],
556 $pageInfo['successfulRevisionCount'],
557 $pageInfo );
558 }
559
560 /**
561 * @param $pageInfo
562 */
563 private function handleRevision( &$pageInfo ) {
564 $this->debug( "Enter revision handler" );
565 $revisionInfo = array();
566
567 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
568
569 $skip = false;
570
571 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
572 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
573 $this->reader->name == 'revision') {
574 break;
575 }
576
577 $tag = $this->reader->name;
578
579 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this,
580 $pageInfo, $revisionInfo ) ) {
581 // Do nothing
582 } elseif ( in_array( $tag, $normalFields ) ) {
583 $revisionInfo[$tag] = $this->nodeContents();
584 } elseif ( $tag == 'contributor' ) {
585 $revisionInfo['contributor'] = $this->handleContributor();
586 } elseif ( $tag != '#text' ) {
587 $this->warn( "Unhandled revision XML tag $tag" );
588 $skip = true;
589 }
590 }
591
592 $pageInfo['revisionCount']++;
593 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
594 $pageInfo['successfulRevisionCount']++;
595 }
596 }
597
598 /**
599 * @param $pageInfo
600 * @param $revisionInfo
601 * @return bool|mixed
602 */
603 private function processRevision( $pageInfo, $revisionInfo ) {
604 $revision = new WikiRevision;
605
606 if( isset( $revisionInfo['id'] ) ) {
607 $revision->setID( $revisionInfo['id'] );
608 }
609 if ( isset( $revisionInfo['text'] ) ) {
610 $revision->setText( $revisionInfo['text'] );
611 }
612 $revision->setTitle( $pageInfo['_title'] );
613
614 if ( isset( $revisionInfo['timestamp'] ) ) {
615 $revision->setTimestamp( $revisionInfo['timestamp'] );
616 } else {
617 $revision->setTimestamp( wfTimestampNow() );
618 }
619
620 if ( isset( $revisionInfo['comment'] ) ) {
621 $revision->setComment( $revisionInfo['comment'] );
622 }
623
624 if ( isset( $revisionInfo['minor'] ) ) {
625 $revision->setMinor( true );
626 }
627 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
628 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
629 }
630 if ( isset( $revisionInfo['contributor']['username'] ) ) {
631 $revision->setUserName( $revisionInfo['contributor']['username'] );
632 }
633 $revision->setNoUpdates( $this->mNoUpdates );
634
635 return $this->revisionCallback( $revision );
636 }
637
638 /**
639 * @param $pageInfo
640 * @return mixed
641 */
642 private function handleUpload( &$pageInfo ) {
643 $this->debug( "Enter upload handler" );
644 $uploadInfo = array();
645
646 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
647 'src', 'size', 'sha1base36', 'archivename', 'rel' );
648
649 $skip = false;
650
651 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
652 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
653 $this->reader->name == 'upload') {
654 break;
655 }
656
657 $tag = $this->reader->name;
658
659 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this,
660 $pageInfo ) ) {
661 // Do nothing
662 } elseif ( in_array( $tag, $normalFields ) ) {
663 $uploadInfo[$tag] = $this->nodeContents();
664 } elseif ( $tag == 'contributor' ) {
665 $uploadInfo['contributor'] = $this->handleContributor();
666 } elseif ( $tag == 'contents' ) {
667 $contents = $this->nodeContents();
668 $encoding = $this->reader->getAttribute( 'encoding' );
669 if ( $encoding === 'base64' ) {
670 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
671 $uploadInfo['isTempSrc'] = true;
672 }
673 } elseif ( $tag != '#text' ) {
674 $this->warn( "Unhandled upload XML tag $tag" );
675 $skip = true;
676 }
677 }
678
679 if ( $this->mImageBasePath && isset( $uploadInfo['rel'] ) ) {
680 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
681 if ( file_exists( $path ) ) {
682 $uploadInfo['fileSrc'] = $path;
683 $uploadInfo['isTempSrc'] = false;
684 }
685 }
686
687 if ( $this->mImportUploads ) {
688 return $this->processUpload( $pageInfo, $uploadInfo );
689 }
690 }
691
692 /**
693 * @param $contents
694 * @return string
695 */
696 private function dumpTemp( $contents ) {
697 $filename = tempnam( wfTempDir(), 'importupload' );
698 file_put_contents( $filename, $contents );
699 return $filename;
700 }
701
702 /**
703 * @param $pageInfo
704 * @param $uploadInfo
705 * @return mixed
706 */
707 private function processUpload( $pageInfo, $uploadInfo ) {
708 $revision = new WikiRevision;
709 $text = isset( $uploadInfo['text'] ) ? $uploadInfo['text'] : '';
710
711 $revision->setTitle( $pageInfo['_title'] );
712 $revision->setID( $pageInfo['id'] );
713 $revision->setTimestamp( $uploadInfo['timestamp'] );
714 $revision->setText( $text );
715 $revision->setFilename( $uploadInfo['filename'] );
716 if ( isset( $uploadInfo['archivename'] ) ) {
717 $revision->setArchiveName( $uploadInfo['archivename'] );
718 }
719 $revision->setSrc( $uploadInfo['src'] );
720 if ( isset( $uploadInfo['fileSrc'] ) ) {
721 $revision->setFileSrc( $uploadInfo['fileSrc'],
722 !empty( $uploadInfo['isTempSrc'] ) );
723 }
724 if ( isset( $uploadInfo['sha1base36'] ) ) {
725 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
726 }
727 $revision->setSize( intval( $uploadInfo['size'] ) );
728 $revision->setComment( $uploadInfo['comment'] );
729
730 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
731 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
732 }
733 if ( isset( $uploadInfo['contributor']['username'] ) ) {
734 $revision->setUserName( $uploadInfo['contributor']['username'] );
735 }
736 $revision->setNoUpdates( $this->mNoUpdates );
737
738 return call_user_func( $this->mUploadCallback, $revision );
739 }
740
741 /**
742 * @return array
743 */
744 private function handleContributor() {
745 $fields = array( 'id', 'ip', 'username' );
746 $info = array();
747
748 while ( $this->reader->read() ) {
749 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
750 $this->reader->name == 'contributor') {
751 break;
752 }
753
754 $tag = $this->reader->name;
755
756 if ( in_array( $tag, $fields ) ) {
757 $info[$tag] = $this->nodeContents();
758 }
759 }
760
761 return $info;
762 }
763
764 /**
765 * @param $text string
766 * @return Array or false
767 */
768 private function processTitle( $text ) {
769 global $wgCommandLineMode;
770
771 $workTitle = $text;
772 $origTitle = Title::newFromText( $workTitle );
773
774 if( !is_null( $this->mTargetNamespace ) && !is_null( $origTitle ) ) {
775 $title = Title::makeTitle( $this->mTargetNamespace,
776 $origTitle->getDBkey() );
777 } else {
778 $title = Title::newFromText( $workTitle );
779 }
780
781 if( is_null( $title ) ) {
782 # Invalid page title? Ignore the page
783 $this->notice( "Skipping invalid page title '$workTitle'" );
784 return false;
785 } elseif( $title->getInterwiki() != '' ) {
786 $this->notice( "Skipping interwiki page title '$workTitle'" );
787 return false;
788 } elseif( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
789 # Do not import if the importing wiki user cannot edit this page
790 $this->notice( wfMessage( 'import-error-edit', $title->getText() )->text() );
791 return false;
792 } elseif( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
793 # Do not import if the importing wiki user cannot create this page
794 $this->notice( wfMessage( 'import-error-create', $title->getText() )->text() );
795 return false;
796 }
797
798 return array( $title, $origTitle );
799 }
800 }
801
802 /** This is a horrible hack used to keep source compatibility */
803 class UploadSourceAdapter {
804 static $sourceRegistrations = array();
805
806 private $mSource;
807 private $mBuffer;
808 private $mPosition;
809
810 /**
811 * @param $source
812 * @return string
813 */
814 static function registerSource( $source ) {
815 $id = wfGenerateToken();
816
817 self::$sourceRegistrations[$id] = $source;
818
819 return $id;
820 }
821
822 /**
823 * @param $path
824 * @param $mode
825 * @param $options
826 * @param $opened_path
827 * @return bool
828 */
829 function stream_open( $path, $mode, $options, &$opened_path ) {
830 $url = parse_url($path);
831 $id = $url['host'];
832
833 if ( !isset( self::$sourceRegistrations[$id] ) ) {
834 return false;
835 }
836
837 $this->mSource = self::$sourceRegistrations[$id];
838
839 return true;
840 }
841
842 /**
843 * @param $count
844 * @return string
845 */
846 function stream_read( $count ) {
847 $return = '';
848 $leave = false;
849
850 while ( !$leave && !$this->mSource->atEnd() &&
851 strlen($this->mBuffer) < $count ) {
852 $read = $this->mSource->readChunk();
853
854 if ( !strlen($read) ) {
855 $leave = true;
856 }
857
858 $this->mBuffer .= $read;
859 }
860
861 if ( strlen($this->mBuffer) ) {
862 $return = substr( $this->mBuffer, 0, $count );
863 $this->mBuffer = substr( $this->mBuffer, $count );
864 }
865
866 $this->mPosition += strlen($return);
867
868 return $return;
869 }
870
871 /**
872 * @param $data
873 * @return bool
874 */
875 function stream_write( $data ) {
876 return false;
877 }
878
879 /**
880 * @return mixed
881 */
882 function stream_tell() {
883 return $this->mPosition;
884 }
885
886 /**
887 * @return bool
888 */
889 function stream_eof() {
890 return $this->mSource->atEnd();
891 }
892
893 /**
894 * @return array
895 */
896 function url_stat() {
897 $result = array();
898
899 $result['dev'] = $result[0] = 0;
900 $result['ino'] = $result[1] = 0;
901 $result['mode'] = $result[2] = 0;
902 $result['nlink'] = $result[3] = 0;
903 $result['uid'] = $result[4] = 0;
904 $result['gid'] = $result[5] = 0;
905 $result['rdev'] = $result[6] = 0;
906 $result['size'] = $result[7] = 0;
907 $result['atime'] = $result[8] = 0;
908 $result['mtime'] = $result[9] = 0;
909 $result['ctime'] = $result[10] = 0;
910 $result['blksize'] = $result[11] = 0;
911 $result['blocks'] = $result[12] = 0;
912
913 return $result;
914 }
915 }
916
917 class XMLReader2 extends XMLReader {
918
919 /**
920 * @return bool|string
921 */
922 function nodeContents() {
923 if( $this->isEmptyElement ) {
924 return "";
925 }
926 $buffer = "";
927 while( $this->read() ) {
928 switch( $this->nodeType ) {
929 case XmlReader::TEXT:
930 case XmlReader::SIGNIFICANT_WHITESPACE:
931 $buffer .= $this->value;
932 break;
933 case XmlReader::END_ELEMENT:
934 return $buffer;
935 }
936 }
937 return $this->close();
938 }
939 }
940
941 /**
942 * @todo document (e.g. one-sentence class description).
943 * @ingroup SpecialPage
944 */
945 class WikiRevision {
946 var $importer = null;
947
948 /**
949 * @var Title
950 */
951 var $title = null;
952 var $id = 0;
953 var $timestamp = "20010115000000";
954 var $user = 0;
955 var $user_text = "";
956 var $text = "";
957 var $comment = "";
958 var $minor = false;
959 var $type = "";
960 var $action = "";
961 var $params = "";
962 var $fileSrc = '';
963 var $sha1base36 = false;
964 var $isTemp = false;
965 var $archiveName = '';
966 var $fileIsTemp;
967 private $mNoUpdates = false;
968
969 /**
970 * @param $title
971 * @throws MWException
972 */
973 function setTitle( $title ) {
974 if( is_object( $title ) ) {
975 $this->title = $title;
976 } elseif( is_null( $title ) ) {
977 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
978 } else {
979 throw new MWException( "WikiRevision given non-object title in import." );
980 }
981 }
982
983 /**
984 * @param $id
985 */
986 function setID( $id ) {
987 $this->id = $id;
988 }
989
990 /**
991 * @param $ts
992 */
993 function setTimestamp( $ts ) {
994 # 2003-08-05T18:30:02Z
995 $this->timestamp = wfTimestamp( TS_MW, $ts );
996 }
997
998 /**
999 * @param $user
1000 */
1001 function setUsername( $user ) {
1002 $this->user_text = $user;
1003 }
1004
1005 /**
1006 * @param $ip
1007 */
1008 function setUserIP( $ip ) {
1009 $this->user_text = $ip;
1010 }
1011
1012 /**
1013 * @param $text
1014 */
1015 function setText( $text ) {
1016 $this->text = $text;
1017 }
1018
1019 /**
1020 * @param $text
1021 */
1022 function setComment( $text ) {
1023 $this->comment = $text;
1024 }
1025
1026 /**
1027 * @param $minor
1028 */
1029 function setMinor( $minor ) {
1030 $this->minor = (bool)$minor;
1031 }
1032
1033 /**
1034 * @param $src
1035 */
1036 function setSrc( $src ) {
1037 $this->src = $src;
1038 }
1039
1040 /**
1041 * @param $src
1042 * @param $isTemp
1043 */
1044 function setFileSrc( $src, $isTemp ) {
1045 $this->fileSrc = $src;
1046 $this->fileIsTemp = $isTemp;
1047 }
1048
1049 /**
1050 * @param $sha1base36
1051 */
1052 function setSha1Base36( $sha1base36 ) {
1053 $this->sha1base36 = $sha1base36;
1054 }
1055
1056 /**
1057 * @param $filename
1058 */
1059 function setFilename( $filename ) {
1060 $this->filename = $filename;
1061 }
1062
1063 /**
1064 * @param $archiveName
1065 */
1066 function setArchiveName( $archiveName ) {
1067 $this->archiveName = $archiveName;
1068 }
1069
1070 /**
1071 * @param $size
1072 */
1073 function setSize( $size ) {
1074 $this->size = intval( $size );
1075 }
1076
1077 /**
1078 * @param $type
1079 */
1080 function setType( $type ) {
1081 $this->type = $type;
1082 }
1083
1084 /**
1085 * @param $action
1086 */
1087 function setAction( $action ) {
1088 $this->action = $action;
1089 }
1090
1091 /**
1092 * @param $params
1093 */
1094 function setParams( $params ) {
1095 $this->params = $params;
1096 }
1097
1098 /**
1099 * @param $noupdates
1100 */
1101 public function setNoUpdates( $noupdates ) {
1102 $this->mNoUpdates = $noupdates;
1103 }
1104
1105 /**
1106 * @return Title
1107 */
1108 function getTitle() {
1109 return $this->title;
1110 }
1111
1112 /**
1113 * @return int
1114 */
1115 function getID() {
1116 return $this->id;
1117 }
1118
1119 /**
1120 * @return string
1121 */
1122 function getTimestamp() {
1123 return $this->timestamp;
1124 }
1125
1126 /**
1127 * @return string
1128 */
1129 function getUser() {
1130 return $this->user_text;
1131 }
1132
1133 /**
1134 * @return string
1135 */
1136 function getText() {
1137 return $this->text;
1138 }
1139
1140 /**
1141 * @return string
1142 */
1143 function getComment() {
1144 return $this->comment;
1145 }
1146
1147 /**
1148 * @return bool
1149 */
1150 function getMinor() {
1151 return $this->minor;
1152 }
1153
1154 /**
1155 * @return mixed
1156 */
1157 function getSrc() {
1158 return $this->src;
1159 }
1160
1161 /**
1162 * @return bool|String
1163 */
1164 function getSha1() {
1165 if ( $this->sha1base36 ) {
1166 return wfBaseConvert( $this->sha1base36, 36, 16 );
1167 }
1168 return false;
1169 }
1170
1171 /**
1172 * @return string
1173 */
1174 function getFileSrc() {
1175 return $this->fileSrc;
1176 }
1177
1178 /**
1179 * @return bool
1180 */
1181 function isTempSrc() {
1182 return $this->isTemp;
1183 }
1184
1185 /**
1186 * @return mixed
1187 */
1188 function getFilename() {
1189 return $this->filename;
1190 }
1191
1192 /**
1193 * @return string
1194 */
1195 function getArchiveName() {
1196 return $this->archiveName;
1197 }
1198
1199 /**
1200 * @return mixed
1201 */
1202 function getSize() {
1203 return $this->size;
1204 }
1205
1206 /**
1207 * @return string
1208 */
1209 function getType() {
1210 return $this->type;
1211 }
1212
1213 /**
1214 * @return string
1215 */
1216 function getAction() {
1217 return $this->action;
1218 }
1219
1220 /**
1221 * @return string
1222 */
1223 function getParams() {
1224 return $this->params;
1225 }
1226
1227 /**
1228 * @return bool
1229 */
1230 function importOldRevision() {
1231 $dbw = wfGetDB( DB_MASTER );
1232
1233 # Sneak a single revision into place
1234 $user = User::newFromName( $this->getUser() );
1235 if( $user ) {
1236 $userId = intval( $user->getId() );
1237 $userText = $user->getName();
1238 $userObj = $user;
1239 } else {
1240 $userId = 0;
1241 $userText = $this->getUser();
1242 $userObj = new User;
1243 }
1244
1245 // avoid memory leak...?
1246 $linkCache = LinkCache::singleton();
1247 $linkCache->clear();
1248
1249 $page = WikiPage::factory( $this->title );
1250 if( !$page->exists() ) {
1251 # must create the page...
1252 $pageId = $page->insertOn( $dbw );
1253 $created = true;
1254 $oldcountable = null;
1255 } else {
1256 $pageId = $page->getId();
1257 $created = false;
1258
1259 $prior = $dbw->selectField( 'revision', '1',
1260 array( 'rev_page' => $pageId,
1261 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
1262 'rev_user_text' => $userText,
1263 'rev_comment' => $this->getComment() ),
1264 __METHOD__
1265 );
1266 if( $prior ) {
1267 // @todo FIXME: This could fail slightly for multiple matches :P
1268 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
1269 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
1270 return false;
1271 }
1272 $oldcountable = $page->isCountable();
1273 }
1274
1275 # @todo FIXME: Use original rev_id optionally (better for backups)
1276 # Insert the row
1277 $revision = new Revision( array(
1278 'page' => $pageId,
1279 'text' => $this->getText(),
1280 'comment' => $this->getComment(),
1281 'user' => $userId,
1282 'user_text' => $userText,
1283 'timestamp' => $this->timestamp,
1284 'minor_edit' => $this->minor,
1285 ) );
1286 $revision->insertOn( $dbw );
1287 $changed = $page->updateIfNewerOn( $dbw, $revision );
1288
1289 if ( $changed !== false && !$this->mNoUpdates ) {
1290 wfDebug( __METHOD__ . ": running updates\n" );
1291 $page->doEditUpdates( $revision, $userObj, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
1292 }
1293
1294 return true;
1295 }
1296
1297 /**
1298 * @return mixed
1299 */
1300 function importLogItem() {
1301 $dbw = wfGetDB( DB_MASTER );
1302 # @todo FIXME: This will not record autoblocks
1303 if( !$this->getTitle() ) {
1304 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1305 $this->timestamp . "\n" );
1306 return;
1307 }
1308 # Check if it exists already
1309 // @todo FIXME: Use original log ID (better for backups)
1310 $prior = $dbw->selectField( 'logging', '1',
1311 array( 'log_type' => $this->getType(),
1312 'log_action' => $this->getAction(),
1313 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1314 'log_namespace' => $this->getTitle()->getNamespace(),
1315 'log_title' => $this->getTitle()->getDBkey(),
1316 'log_comment' => $this->getComment(),
1317 #'log_user_text' => $this->user_text,
1318 'log_params' => $this->params ),
1319 __METHOD__
1320 );
1321 // @todo FIXME: This could fail slightly for multiple matches :P
1322 if( $prior ) {
1323 wfDebug( __METHOD__ . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
1324 $this->timestamp . "\n" );
1325 return;
1326 }
1327 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1328 $data = array(
1329 'log_id' => $log_id,
1330 'log_type' => $this->type,
1331 'log_action' => $this->action,
1332 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1333 'log_user' => User::idFromName( $this->user_text ),
1334 #'log_user_text' => $this->user_text,
1335 'log_namespace' => $this->getTitle()->getNamespace(),
1336 'log_title' => $this->getTitle()->getDBkey(),
1337 'log_comment' => $this->getComment(),
1338 'log_params' => $this->params
1339 );
1340 $dbw->insert( 'logging', $data, __METHOD__ );
1341 }
1342
1343 /**
1344 * @return bool
1345 */
1346 function importUpload() {
1347 # Construct a file
1348 $archiveName = $this->getArchiveName();
1349 if ( $archiveName ) {
1350 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
1351 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1352 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1353 } else {
1354 $file = wfLocalFile( $this->getTitle() );
1355 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
1356 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
1357 $archiveName = $file->getTimestamp() . '!' . $file->getName();
1358 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1359 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1360 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
1361 }
1362 }
1363 if( !$file ) {
1364 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
1365 return false;
1366 }
1367
1368 # Get the file source or download if necessary
1369 $source = $this->getFileSrc();
1370 $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0;
1371 if ( !$source ) {
1372 $source = $this->downloadSource();
1373 $flags |= File::DELETE_SOURCE;
1374 }
1375 if( !$source ) {
1376 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
1377 return false;
1378 }
1379 $sha1 = $this->getSha1();
1380 if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
1381 if ( $flags & File::DELETE_SOURCE ) {
1382 # Broken file; delete it if it is a temporary file
1383 unlink( $source );
1384 }
1385 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
1386 return false;
1387 }
1388
1389 $user = User::newFromName( $this->user_text );
1390
1391 # Do the actual upload
1392 if ( $archiveName ) {
1393 $status = $file->uploadOld( $source, $archiveName,
1394 $this->getTimestamp(), $this->getComment(), $user, $flags );
1395 } else {
1396 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
1397 $flags, false, $this->getTimestamp(), $user );
1398 }
1399
1400 if ( $status->isGood() ) {
1401 wfDebug( __METHOD__ . ": Succesful\n" );
1402 return true;
1403 } else {
1404 wfDebug( __METHOD__ . ': failed: ' . $status->getXml() . "\n" );
1405 return false;
1406 }
1407 }
1408
1409 /**
1410 * @return bool|string
1411 */
1412 function downloadSource() {
1413 global $wgEnableUploads;
1414 if( !$wgEnableUploads ) {
1415 return false;
1416 }
1417
1418 $tempo = tempnam( wfTempDir(), 'download' );
1419 $f = fopen( $tempo, 'wb' );
1420 if( !$f ) {
1421 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1422 return false;
1423 }
1424
1425 // @todo FIXME!
1426 $src = $this->getSrc();
1427 $data = Http::get( $src );
1428 if( !$data ) {
1429 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1430 fclose( $f );
1431 unlink( $tempo );
1432 return false;
1433 }
1434
1435 fwrite( $f, $data );
1436 fclose( $f );
1437
1438 return $tempo;
1439 }
1440
1441 }
1442
1443 /**
1444 * @todo document (e.g. one-sentence class description).
1445 * @ingroup SpecialPage
1446 */
1447 class ImportStringSource {
1448 function __construct( $string ) {
1449 $this->mString = $string;
1450 $this->mRead = false;
1451 }
1452
1453 /**
1454 * @return bool
1455 */
1456 function atEnd() {
1457 return $this->mRead;
1458 }
1459
1460 /**
1461 * @return bool|string
1462 */
1463 function readChunk() {
1464 if( $this->atEnd() ) {
1465 return false;
1466 }
1467 $this->mRead = true;
1468 return $this->mString;
1469 }
1470 }
1471
1472 /**
1473 * @todo document (e.g. one-sentence class description).
1474 * @ingroup SpecialPage
1475 */
1476 class ImportStreamSource {
1477 function __construct( $handle ) {
1478 $this->mHandle = $handle;
1479 }
1480
1481 /**
1482 * @return bool
1483 */
1484 function atEnd() {
1485 return feof( $this->mHandle );
1486 }
1487
1488 /**
1489 * @return string
1490 */
1491 function readChunk() {
1492 return fread( $this->mHandle, 32768 );
1493 }
1494
1495 /**
1496 * @param $filename string
1497 * @return Status
1498 */
1499 static function newFromFile( $filename ) {
1500 wfSuppressWarnings();
1501 $file = fopen( $filename, 'rt' );
1502 wfRestoreWarnings();
1503 if( !$file ) {
1504 return Status::newFatal( "importcantopen" );
1505 }
1506 return Status::newGood( new ImportStreamSource( $file ) );
1507 }
1508
1509 /**
1510 * @param $fieldname string
1511 * @return Status
1512 */
1513 static function newFromUpload( $fieldname = "xmlimport" ) {
1514 $upload =& $_FILES[$fieldname];
1515
1516 if( !isset( $upload ) || !$upload['name'] ) {
1517 return Status::newFatal( 'importnofile' );
1518 }
1519 if( !empty( $upload['error'] ) ) {
1520 switch($upload['error']){
1521 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1522 return Status::newFatal( 'importuploaderrorsize' );
1523 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1524 return Status::newFatal( 'importuploaderrorsize' );
1525 case 3: # The uploaded file was only partially uploaded
1526 return Status::newFatal( 'importuploaderrorpartial' );
1527 case 6: #Missing a temporary folder.
1528 return Status::newFatal( 'importuploaderrortemp' );
1529 # case else: # Currently impossible
1530 }
1531
1532 }
1533 $fname = $upload['tmp_name'];
1534 if( is_uploaded_file( $fname ) ) {
1535 return ImportStreamSource::newFromFile( $fname );
1536 } else {
1537 return Status::newFatal( 'importnofile' );
1538 }
1539 }
1540
1541 /**
1542 * @param $url
1543 * @param $method string
1544 * @return Status
1545 */
1546 static function newFromURL( $url, $method = 'GET' ) {
1547 wfDebug( __METHOD__ . ": opening $url\n" );
1548 # Use the standard HTTP fetch function; it times out
1549 # quicker and sorts out user-agent problems which might
1550 # otherwise prevent importing from large sites, such
1551 # as the Wikimedia cluster, etc.
1552 $data = Http::request( $method, $url, array( 'followRedirects' => true ) );
1553 if( $data !== false ) {
1554 $file = tmpfile();
1555 fwrite( $file, $data );
1556 fflush( $file );
1557 fseek( $file, 0 );
1558 return Status::newGood( new ImportStreamSource( $file ) );
1559 } else {
1560 return Status::newFatal( 'importcantopen' );
1561 }
1562 }
1563
1564 /**
1565 * @param $interwiki
1566 * @param $page
1567 * @param $history bool
1568 * @param $templates bool
1569 * @param $pageLinkDepth int
1570 * @return Status
1571 */
1572 public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
1573 if( $page == '' ) {
1574 return Status::newFatal( 'import-noarticle' );
1575 }
1576 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
1577 if( is_null( $link ) || $link->getInterwiki() == '' ) {
1578 return Status::newFatal( 'importbadinterwiki' );
1579 } else {
1580 $params = array();
1581 if ( $history ) $params['history'] = 1;
1582 if ( $templates ) $params['templates'] = 1;
1583 if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
1584 $url = $link->getFullUrl( $params );
1585 # For interwikis, use POST to avoid redirects.
1586 return ImportStreamSource::newFromURL( $url, "POST" );
1587 }
1588 }
1589 }