Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / Import.php
blobb3ca0416359e41dfd9d5da1a75c9c7156a18f5f8
1 <?php
2 /**
3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup SpecialPage
27 /**
28 * XML file reader for the page data importer
30 * implements Special:Import
31 * @ingroup SpecialPage
33 class WikiImporter {
34 private $reader = null;
35 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
36 private $mSiteInfoCallback, $mTargetNamespace, $mTargetRootPage, $mPageOutCallback;
37 private $mNoticeCallback, $mDebug;
38 private $mImportUploads, $mImageBasePath;
39 private $mNoUpdates = false;
41 /**
42 * Creates an ImportXMLReader drawing from the source provided
43 * @param ImportStreamSource $source
45 function __construct( ImportStreamSource $source ) {
46 $this->reader = new XMLReader();
48 if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) {
49 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
51 $id = UploadSourceAdapter::registerSource( $source );
52 if ( defined( 'LIBXML_PARSEHUGE' ) ) {
53 $this->reader->open( "uploadsource://$id", null, LIBXML_PARSEHUGE );
54 } else {
55 $this->reader->open( "uploadsource://$id" );
58 // Default callbacks
59 $this->setRevisionCallback( array( $this, "importRevision" ) );
60 $this->setUploadCallback( array( $this, 'importUpload' ) );
61 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
62 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
65 /**
66 * @return null|XMLReader
68 public function getReader() {
69 return $this->reader;
72 public function throwXmlError( $err ) {
73 $this->debug( "FAILURE: $err" );
74 wfDebug( "WikiImporter XML error: $err\n" );
77 public function debug( $data ) {
78 if ( $this->mDebug ) {
79 wfDebug( "IMPORT: $data\n" );
83 public function warn( $data ) {
84 wfDebug( "IMPORT: $data\n" );
87 public function notice( $msg /*, $param, ...*/ ) {
88 $params = func_get_args();
89 array_shift( $params );
91 if ( is_callable( $this->mNoticeCallback ) ) {
92 call_user_func( $this->mNoticeCallback, $msg, $params );
93 } else { # No ImportReporter -> CLI
94 echo wfMessage( $msg, $params )->text() . "\n";
98 /**
99 * Set debug mode...
100 * @param bool $debug
102 function setDebug( $debug ) {
103 $this->mDebug = $debug;
107 * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
108 * @param bool $noupdates
110 function setNoUpdates( $noupdates ) {
111 $this->mNoUpdates = $noupdates;
115 * Set a callback that displays notice messages
117 * @param callable $callback
118 * @return callable
120 public function setNoticeCallback( $callback ) {
121 return wfSetVar( $this->mNoticeCallback, $callback );
125 * Sets the action to perform as each new page in the stream is reached.
126 * @param callable $callback
127 * @return callable
129 public function setPageCallback( $callback ) {
130 $previous = $this->mPageCallback;
131 $this->mPageCallback = $callback;
132 return $previous;
136 * Sets the action to perform as each page in the stream is completed.
137 * Callback accepts the page title (as a Title object), a second object
138 * with the original title form (in case it's been overridden into a
139 * local namespace), and a count of revisions.
141 * @param callable $callback
142 * @return callable
144 public function setPageOutCallback( $callback ) {
145 $previous = $this->mPageOutCallback;
146 $this->mPageOutCallback = $callback;
147 return $previous;
151 * Sets the action to perform as each page revision is reached.
152 * @param callable $callback
153 * @return callable
155 public function setRevisionCallback( $callback ) {
156 $previous = $this->mRevisionCallback;
157 $this->mRevisionCallback = $callback;
158 return $previous;
162 * Sets the action to perform as each file upload version is reached.
163 * @param callable $callback
164 * @return callable
166 public function setUploadCallback( $callback ) {
167 $previous = $this->mUploadCallback;
168 $this->mUploadCallback = $callback;
169 return $previous;
173 * Sets the action to perform as each log item reached.
174 * @param callable $callback
175 * @return callable
177 public function setLogItemCallback( $callback ) {
178 $previous = $this->mLogItemCallback;
179 $this->mLogItemCallback = $callback;
180 return $previous;
184 * Sets the action to perform when site info is encountered
185 * @param callable $callback
186 * @return callable
188 public function setSiteInfoCallback( $callback ) {
189 $previous = $this->mSiteInfoCallback;
190 $this->mSiteInfoCallback = $callback;
191 return $previous;
195 * Set a target namespace to override the defaults
196 * @param null|int $namespace
197 * @return bool
199 public function setTargetNamespace( $namespace ) {
200 if ( is_null( $namespace ) ) {
201 // Don't override namespaces
202 $this->mTargetNamespace = null;
203 } elseif ( $namespace >= 0 ) {
204 // @todo FIXME: Check for validity
205 $this->mTargetNamespace = intval( $namespace );
206 } else {
207 return false;
212 * Set a target root page under which all pages are imported
213 * @param null|string $rootpage
214 * @return Status
216 public function setTargetRootPage( $rootpage ) {
217 $status = Status::newGood();
218 if ( is_null( $rootpage ) ) {
219 // No rootpage
220 $this->mTargetRootPage = null;
221 } elseif ( $rootpage !== '' ) {
222 $rootpage = rtrim( $rootpage, '/' ); //avoid double slashes
223 $title = Title::newFromText( $rootpage, !is_null( $this->mTargetNamespace )
224 ? $this->mTargetNamespace
225 : NS_MAIN
228 if ( !$title || $title->isExternal() ) {
229 $status->fatal( 'import-rootpage-invalid' );
230 } else {
231 if ( !MWNamespace::hasSubpages( $title->getNamespace() ) ) {
232 global $wgContLang;
234 $displayNSText = $title->getNamespace() == NS_MAIN
235 ? wfMessage( 'blanknamespace' )->text()
236 : $wgContLang->getNsText( $title->getNamespace() );
237 $status->fatal( 'import-rootpage-nosubpage', $displayNSText );
238 } else {
239 // set namespace to 'all', so the namespace check in processTitle() can passed
240 $this->setTargetNamespace( null );
241 $this->mTargetRootPage = $title->getPrefixedDBkey();
245 return $status;
249 * @param string $dir
251 public function setImageBasePath( $dir ) {
252 $this->mImageBasePath = $dir;
256 * @param bool $import
258 public function setImportUploads( $import ) {
259 $this->mImportUploads = $import;
263 * Default per-revision callback, performs the import.
264 * @param WikiRevision $revision
265 * @return bool
267 public function importRevision( $revision ) {
268 if ( !$revision->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
269 $this->notice( 'import-error-bad-location',
270 $revision->getTitle()->getPrefixedText(),
271 $revision->getID(),
272 $revision->getModel(),
273 $revision->getFormat() );
275 return false;
278 try {
279 $dbw = wfGetDB( DB_MASTER );
280 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
281 } catch ( MWContentSerializationException $ex ) {
282 $this->notice( 'import-error-unserialize',
283 $revision->getTitle()->getPrefixedText(),
284 $revision->getID(),
285 $revision->getModel(),
286 $revision->getFormat() );
289 return false;
293 * Default per-revision callback, performs the import.
294 * @param WikiRevision $revision
295 * @return bool
297 public function importLogItem( $revision ) {
298 $dbw = wfGetDB( DB_MASTER );
299 return $dbw->deadlockLoop( array( $revision, 'importLogItem' ) );
303 * Dummy for now...
304 * @param WikiRevision $revision
305 * @return bool
307 public function importUpload( $revision ) {
308 $dbw = wfGetDB( DB_MASTER );
309 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
313 * Mostly for hook use
314 * @param Title $title
315 * @param string $origTitle
316 * @param int $revCount
317 * @param int $sRevCount
318 * @param array $pageInfo
319 * @return bool
321 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
322 $args = func_get_args();
323 return wfRunHooks( 'AfterImportPage', $args );
327 * Alternate per-revision callback, for debugging.
328 * @param WikiRevision $revision
330 public function debugRevisionHandler( &$revision ) {
331 $this->debug( "Got revision:" );
332 if ( is_object( $revision->title ) ) {
333 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
334 } else {
335 $this->debug( "-- Title: <invalid>" );
337 $this->debug( "-- User: " . $revision->user_text );
338 $this->debug( "-- Timestamp: " . $revision->timestamp );
339 $this->debug( "-- Comment: " . $revision->comment );
340 $this->debug( "-- Text: " . $revision->text );
344 * Notify the callback function when a new "<page>" is reached.
345 * @param Title $title
347 function pageCallback( $title ) {
348 if ( isset( $this->mPageCallback ) ) {
349 call_user_func( $this->mPageCallback, $title );
354 * Notify the callback function when a "</page>" is closed.
355 * @param Title $title
356 * @param Title $origTitle
357 * @param int $revCount
358 * @param int $sucCount Number of revisions for which callback returned true
359 * @param array $pageInfo Associative array of page information
361 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
362 if ( isset( $this->mPageOutCallback ) ) {
363 $args = func_get_args();
364 call_user_func_array( $this->mPageOutCallback, $args );
369 * Notify the callback function of a revision
370 * @param WikiRevision $revision
371 * @return bool|mixed
373 private function revisionCallback( $revision ) {
374 if ( isset( $this->mRevisionCallback ) ) {
375 return call_user_func_array( $this->mRevisionCallback,
376 array( $revision, $this ) );
377 } else {
378 return false;
383 * Notify the callback function of a new log item
384 * @param WikiRevision $revision
385 * @return bool|mixed
387 private function logItemCallback( $revision ) {
388 if ( isset( $this->mLogItemCallback ) ) {
389 return call_user_func_array( $this->mLogItemCallback,
390 array( $revision, $this ) );
391 } else {
392 return false;
397 * Retrieves the contents of the named attribute of the current element.
398 * @param string $attr The name of the attribute
399 * @return string The value of the attribute or an empty string if it is not set in the current element.
401 public function nodeAttribute( $attr ) {
402 return $this->reader->getAttribute( $attr );
406 * Shouldn't something like this be built-in to XMLReader?
407 * Fetches text contents of the current element, assuming
408 * no sub-elements or such scary things.
409 * @return string
410 * @access private
412 public function nodeContents() {
413 if ( $this->reader->isEmptyElement ) {
414 return "";
416 $buffer = "";
417 while ( $this->reader->read() ) {
418 switch ( $this->reader->nodeType ) {
419 case XmlReader::TEXT:
420 case XmlReader::SIGNIFICANT_WHITESPACE:
421 $buffer .= $this->reader->value;
422 break;
423 case XmlReader::END_ELEMENT:
424 return $buffer;
428 $this->reader->close();
429 return '';
433 * Primary entry point
434 * @throws MWException
435 * @return bool
437 public function doImport() {
438 // Calls to reader->read need to be wrapped in calls to
439 // libxml_disable_entity_loader() to avoid local file
440 // inclusion attacks (bug 46932).
441 $oldDisable = libxml_disable_entity_loader( true );
442 $this->reader->read();
444 if ( $this->reader->name != 'mediawiki' ) {
445 libxml_disable_entity_loader( $oldDisable );
446 throw new MWException( "Expected <mediawiki> tag, got " .
447 $this->reader->name );
449 $this->debug( "<mediawiki> tag is correct." );
451 $this->debug( "Starting primary dump processing loop." );
453 $keepReading = $this->reader->read();
454 $skip = false;
455 while ( $keepReading ) {
456 $tag = $this->reader->name;
457 $type = $this->reader->nodeType;
459 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', array( $this ) ) ) {
460 // Do nothing
461 } elseif ( $tag == 'mediawiki' && $type == XmlReader::END_ELEMENT ) {
462 break;
463 } elseif ( $tag == 'siteinfo' ) {
464 $this->handleSiteInfo();
465 } elseif ( $tag == 'page' ) {
466 $this->handlePage();
467 } elseif ( $tag == 'logitem' ) {
468 $this->handleLogItem();
469 } elseif ( $tag != '#text' ) {
470 $this->warn( "Unhandled top-level XML tag $tag" );
472 $skip = true;
475 if ( $skip ) {
476 $keepReading = $this->reader->next();
477 $skip = false;
478 $this->debug( "Skip" );
479 } else {
480 $keepReading = $this->reader->read();
484 libxml_disable_entity_loader( $oldDisable );
485 return true;
489 * @return bool
490 * @throws MWException
492 private function handleSiteInfo() {
493 // Site info is useful, but not actually used for dump imports.
494 // Includes a quick short-circuit to save performance.
495 if ( !$this->mSiteInfoCallback ) {
496 $this->reader->next();
497 return true;
499 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
502 private function handleLogItem() {
503 $this->debug( "Enter log item handler." );
504 $logInfo = array();
506 // Fields that can just be stuffed in the pageInfo object
507 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
508 'logtitle', 'params' );
510 while ( $this->reader->read() ) {
511 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
512 $this->reader->name == 'logitem' ) {
513 break;
516 $tag = $this->reader->name;
518 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag', array(
519 $this, $logInfo
520 ) ) ) {
521 // Do nothing
522 } elseif ( in_array( $tag, $normalFields ) ) {
523 $logInfo[$tag] = $this->nodeContents();
524 } elseif ( $tag == 'contributor' ) {
525 $logInfo['contributor'] = $this->handleContributor();
526 } elseif ( $tag != '#text' ) {
527 $this->warn( "Unhandled log-item XML tag $tag" );
531 $this->processLogItem( $logInfo );
535 * @param array $logInfo
536 * @return bool|mixed
538 private function processLogItem( $logInfo ) {
539 $revision = new WikiRevision;
541 $revision->setID( $logInfo['id'] );
542 $revision->setType( $logInfo['type'] );
543 $revision->setAction( $logInfo['action'] );
544 $revision->setTimestamp( $logInfo['timestamp'] );
545 $revision->setParams( $logInfo['params'] );
546 $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
547 $revision->setNoUpdates( $this->mNoUpdates );
549 if ( isset( $logInfo['comment'] ) ) {
550 $revision->setComment( $logInfo['comment'] );
553 if ( isset( $logInfo['contributor']['ip'] ) ) {
554 $revision->setUserIP( $logInfo['contributor']['ip'] );
556 if ( isset( $logInfo['contributor']['username'] ) ) {
557 $revision->setUserName( $logInfo['contributor']['username'] );
560 return $this->logItemCallback( $revision );
563 private function handlePage() {
564 // Handle page data.
565 $this->debug( "Enter page handler." );
566 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
568 // Fields that can just be stuffed in the pageInfo object
569 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
571 $skip = false;
572 $badTitle = false;
574 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
575 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
576 $this->reader->name == 'page' ) {
577 break;
580 $tag = $this->reader->name;
582 if ( $badTitle ) {
583 // The title is invalid, bail out of this page
584 $skip = true;
585 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this,
586 &$pageInfo ) ) ) {
587 // Do nothing
588 } elseif ( in_array( $tag, $normalFields ) ) {
589 // An XML snippet:
590 // <page>
591 // <id>123</id>
592 // <title>Page</title>
593 // <redirect title="NewTitle"/>
594 // ...
595 // Because the redirect tag is built differently, we need special handling for that case.
596 if ( $tag == 'redirect' ) {
597 $pageInfo[$tag] = $this->nodeAttribute( 'title' );
598 } else {
599 $pageInfo[$tag] = $this->nodeContents();
600 if ( $tag == 'title' ) {
601 $title = $this->processTitle( $pageInfo['title'] );
603 if ( !$title ) {
604 $badTitle = true;
605 $skip = true;
608 $this->pageCallback( $title );
609 list( $pageInfo['_title'], $origTitle ) = $title;
612 } elseif ( $tag == 'revision' ) {
613 $this->handleRevision( $pageInfo );
614 } elseif ( $tag == 'upload' ) {
615 $this->handleUpload( $pageInfo );
616 } elseif ( $tag != '#text' ) {
617 $this->warn( "Unhandled page XML tag $tag" );
618 $skip = true;
622 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
623 $pageInfo['revisionCount'],
624 $pageInfo['successfulRevisionCount'],
625 $pageInfo );
629 * @param array $pageInfo
631 private function handleRevision( &$pageInfo ) {
632 $this->debug( "Enter revision handler" );
633 $revisionInfo = array();
635 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' );
637 $skip = false;
639 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
640 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
641 $this->reader->name == 'revision' ) {
642 break;
645 $tag = $this->reader->name;
647 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', array(
648 $this, $pageInfo, $revisionInfo
649 ) ) ) {
650 // Do nothing
651 } elseif ( in_array( $tag, $normalFields ) ) {
652 $revisionInfo[$tag] = $this->nodeContents();
653 } elseif ( $tag == 'contributor' ) {
654 $revisionInfo['contributor'] = $this->handleContributor();
655 } elseif ( $tag != '#text' ) {
656 $this->warn( "Unhandled revision XML tag $tag" );
657 $skip = true;
661 $pageInfo['revisionCount']++;
662 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
663 $pageInfo['successfulRevisionCount']++;
668 * @param array $pageInfo
669 * @param array $revisionInfo
670 * @return bool|mixed
672 private function processRevision( $pageInfo, $revisionInfo ) {
673 $revision = new WikiRevision;
675 if ( isset( $revisionInfo['id'] ) ) {
676 $revision->setID( $revisionInfo['id'] );
678 if ( isset( $revisionInfo['model'] ) ) {
679 $revision->setModel( $revisionInfo['model'] );
681 if ( isset( $revisionInfo['format'] ) ) {
682 $revision->setFormat( $revisionInfo['format'] );
684 $revision->setTitle( $pageInfo['_title'] );
686 if ( isset( $revisionInfo['text'] ) ) {
687 $handler = $revision->getContentHandler();
688 $text = $handler->importTransform(
689 $revisionInfo['text'],
690 $revision->getFormat() );
692 $revision->setText( $text );
694 if ( isset( $revisionInfo['timestamp'] ) ) {
695 $revision->setTimestamp( $revisionInfo['timestamp'] );
696 } else {
697 $revision->setTimestamp( wfTimestampNow() );
700 if ( isset( $revisionInfo['comment'] ) ) {
701 $revision->setComment( $revisionInfo['comment'] );
704 if ( isset( $revisionInfo['minor'] ) ) {
705 $revision->setMinor( true );
707 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
708 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
710 if ( isset( $revisionInfo['contributor']['username'] ) ) {
711 $revision->setUserName( $revisionInfo['contributor']['username'] );
713 $revision->setNoUpdates( $this->mNoUpdates );
715 return $this->revisionCallback( $revision );
719 * @param array $pageInfo
720 * @return mixed
722 private function handleUpload( &$pageInfo ) {
723 $this->debug( "Enter upload handler" );
724 $uploadInfo = array();
726 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
727 'src', 'size', 'sha1base36', 'archivename', 'rel' );
729 $skip = false;
731 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
732 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
733 $this->reader->name == 'upload' ) {
734 break;
737 $tag = $this->reader->name;
739 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', array(
740 $this, $pageInfo
741 ) ) ) {
742 // Do nothing
743 } elseif ( in_array( $tag, $normalFields ) ) {
744 $uploadInfo[$tag] = $this->nodeContents();
745 } elseif ( $tag == 'contributor' ) {
746 $uploadInfo['contributor'] = $this->handleContributor();
747 } elseif ( $tag == 'contents' ) {
748 $contents = $this->nodeContents();
749 $encoding = $this->reader->getAttribute( 'encoding' );
750 if ( $encoding === 'base64' ) {
751 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
752 $uploadInfo['isTempSrc'] = true;
754 } elseif ( $tag != '#text' ) {
755 $this->warn( "Unhandled upload XML tag $tag" );
756 $skip = true;
760 if ( $this->mImageBasePath && isset( $uploadInfo['rel'] ) ) {
761 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
762 if ( file_exists( $path ) ) {
763 $uploadInfo['fileSrc'] = $path;
764 $uploadInfo['isTempSrc'] = false;
768 if ( $this->mImportUploads ) {
769 return $this->processUpload( $pageInfo, $uploadInfo );
774 * @param string $contents
775 * @return string
777 private function dumpTemp( $contents ) {
778 $filename = tempnam( wfTempDir(), 'importupload' );
779 file_put_contents( $filename, $contents );
780 return $filename;
784 * @param array $pageInfo
785 * @param array $uploadInfo
786 * @return mixed
788 private function processUpload( $pageInfo, $uploadInfo ) {
789 $revision = new WikiRevision;
790 $text = isset( $uploadInfo['text'] ) ? $uploadInfo['text'] : '';
792 $revision->setTitle( $pageInfo['_title'] );
793 $revision->setID( $pageInfo['id'] );
794 $revision->setTimestamp( $uploadInfo['timestamp'] );
795 $revision->setText( $text );
796 $revision->setFilename( $uploadInfo['filename'] );
797 if ( isset( $uploadInfo['archivename'] ) ) {
798 $revision->setArchiveName( $uploadInfo['archivename'] );
800 $revision->setSrc( $uploadInfo['src'] );
801 if ( isset( $uploadInfo['fileSrc'] ) ) {
802 $revision->setFileSrc( $uploadInfo['fileSrc'],
803 !empty( $uploadInfo['isTempSrc'] ) );
805 if ( isset( $uploadInfo['sha1base36'] ) ) {
806 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
808 $revision->setSize( intval( $uploadInfo['size'] ) );
809 $revision->setComment( $uploadInfo['comment'] );
811 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
812 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
814 if ( isset( $uploadInfo['contributor']['username'] ) ) {
815 $revision->setUserName( $uploadInfo['contributor']['username'] );
817 $revision->setNoUpdates( $this->mNoUpdates );
819 return call_user_func( $this->mUploadCallback, $revision );
823 * @return array
825 private function handleContributor() {
826 $fields = array( 'id', 'ip', 'username' );
827 $info = array();
829 while ( $this->reader->read() ) {
830 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
831 $this->reader->name == 'contributor' ) {
832 break;
835 $tag = $this->reader->name;
837 if ( in_array( $tag, $fields ) ) {
838 $info[$tag] = $this->nodeContents();
842 return $info;
846 * @param string $text
847 * @return array|bool
849 private function processTitle( $text ) {
850 global $wgCommandLineMode;
852 $workTitle = $text;
853 $origTitle = Title::newFromText( $workTitle );
855 if ( !is_null( $this->mTargetNamespace ) && !is_null( $origTitle ) ) {
856 # makeTitleSafe, because $origTitle can have a interwiki (different setting of interwiki map)
857 # and than dbKey can begin with a lowercase char
858 $title = Title::makeTitleSafe( $this->mTargetNamespace,
859 $origTitle->getDBkey() );
860 } else {
861 if ( !is_null( $this->mTargetRootPage ) ) {
862 $workTitle = $this->mTargetRootPage . '/' . $workTitle;
864 $title = Title::newFromText( $workTitle );
867 if ( is_null( $title ) ) {
868 # Invalid page title? Ignore the page
869 $this->notice( 'import-error-invalid', $workTitle );
870 return false;
871 } elseif ( $title->isExternal() ) {
872 $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
873 return false;
874 } elseif ( !$title->canExist() ) {
875 $this->notice( 'import-error-special', $title->getPrefixedText() );
876 return false;
877 } elseif ( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
878 # Do not import if the importing wiki user cannot edit this page
879 $this->notice( 'import-error-edit', $title->getPrefixedText() );
880 return false;
881 } elseif ( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
882 # Do not import if the importing wiki user cannot create this page
883 $this->notice( 'import-error-create', $title->getPrefixedText() );
884 return false;
887 return array( $title, $origTitle );
891 /** This is a horrible hack used to keep source compatibility */
892 class UploadSourceAdapter {
893 /** @var array */
894 private static $sourceRegistrations = array();
896 /** @var string */
897 private $mSource;
899 /** @var string */
900 private $mBuffer;
902 /** @var int */
903 private $mPosition;
906 * @param ImportStreamSource $source
907 * @return string
909 static function registerSource( ImportStreamSource $source ) {
910 $id = wfRandomString();
912 self::$sourceRegistrations[$id] = $source;
914 return $id;
918 * @param string $path
919 * @param string $mode
920 * @param array $options
921 * @param string $opened_path
922 * @return bool
924 function stream_open( $path, $mode, $options, &$opened_path ) {
925 $url = parse_url( $path );
926 $id = $url['host'];
928 if ( !isset( self::$sourceRegistrations[$id] ) ) {
929 return false;
932 $this->mSource = self::$sourceRegistrations[$id];
934 return true;
938 * @param int $count
939 * @return string
941 function stream_read( $count ) {
942 $return = '';
943 $leave = false;
945 while ( !$leave && !$this->mSource->atEnd() &&
946 strlen( $this->mBuffer ) < $count ) {
947 $read = $this->mSource->readChunk();
949 if ( !strlen( $read ) ) {
950 $leave = true;
953 $this->mBuffer .= $read;
956 if ( strlen( $this->mBuffer ) ) {
957 $return = substr( $this->mBuffer, 0, $count );
958 $this->mBuffer = substr( $this->mBuffer, $count );
961 $this->mPosition += strlen( $return );
963 return $return;
967 * @param string $data
968 * @return bool
970 function stream_write( $data ) {
971 return false;
975 * @return mixed
977 function stream_tell() {
978 return $this->mPosition;
982 * @return bool
984 function stream_eof() {
985 return $this->mSource->atEnd();
989 * @return array
991 function url_stat() {
992 $result = array();
994 $result['dev'] = $result[0] = 0;
995 $result['ino'] = $result[1] = 0;
996 $result['mode'] = $result[2] = 0;
997 $result['nlink'] = $result[3] = 0;
998 $result['uid'] = $result[4] = 0;
999 $result['gid'] = $result[5] = 0;
1000 $result['rdev'] = $result[6] = 0;
1001 $result['size'] = $result[7] = 0;
1002 $result['atime'] = $result[8] = 0;
1003 $result['mtime'] = $result[9] = 0;
1004 $result['ctime'] = $result[10] = 0;
1005 $result['blksize'] = $result[11] = 0;
1006 $result['blocks'] = $result[12] = 0;
1008 return $result;
1013 * @todo document (e.g. one-sentence class description).
1014 * @ingroup SpecialPage
1016 class WikiRevision {
1017 /** @todo Unused? */
1018 private $importer = null;
1020 /** @var Title */
1021 public $title = null;
1023 /** @var int */
1024 private $id = 0;
1026 /** @var string */
1027 public $timestamp = "20010115000000";
1030 * @var int
1031 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
1032 public $user = 0;
1034 /** @var string */
1035 public $user_text = "";
1037 /** @var string */
1038 protected $model = null;
1040 /** @var string */
1041 protected $format = null;
1043 /** @var string */
1044 public $text = "";
1046 /** @var int */
1047 protected $size;
1049 /** @var Content */
1050 protected $content = null;
1052 /** @var ContentHandler */
1053 protected $contentHandler = null;
1055 /** @var string */
1056 public $comment = "";
1058 /** @var bool */
1059 protected $minor = false;
1061 /** @var string */
1062 protected $type = "";
1064 /** @var string */
1065 protected $action = "";
1067 /** @var string */
1068 protected $params = "";
1070 /** @var string */
1071 protected $fileSrc = '';
1073 /** @var bool|string */
1074 protected $sha1base36 = false;
1077 * @var bool
1078 * @todo Unused?
1080 private $isTemp = false;
1082 /** @var string */
1083 protected $archiveName = '';
1085 protected $filename;
1087 /** @var mixed */
1088 protected $src;
1090 /** @todo Unused? */
1091 private $fileIsTemp;
1093 /** @var bool */
1094 private $mNoUpdates = false;
1097 * @param Title $title
1098 * @throws MWException
1100 function setTitle( $title ) {
1101 if ( is_object( $title ) ) {
1102 $this->title = $title;
1103 } elseif ( is_null( $title ) ) {
1104 throw new MWException( "WikiRevision given a null title in import. "
1105 . "You may need to adjust \$wgLegalTitleChars." );
1106 } else {
1107 throw new MWException( "WikiRevision given non-object title in import." );
1112 * @param int $id
1114 function setID( $id ) {
1115 $this->id = $id;
1119 * @param string $ts
1121 function setTimestamp( $ts ) {
1122 # 2003-08-05T18:30:02Z
1123 $this->timestamp = wfTimestamp( TS_MW, $ts );
1127 * @param string $user
1129 function setUsername( $user ) {
1130 $this->user_text = $user;
1134 * @param string $ip
1136 function setUserIP( $ip ) {
1137 $this->user_text = $ip;
1141 * @param string $model
1143 function setModel( $model ) {
1144 $this->model = $model;
1148 * @param string $format
1150 function setFormat( $format ) {
1151 $this->format = $format;
1155 * @param string $text
1157 function setText( $text ) {
1158 $this->text = $text;
1162 * @param string $text
1164 function setComment( $text ) {
1165 $this->comment = $text;
1169 * @param bool $minor
1171 function setMinor( $minor ) {
1172 $this->minor = (bool)$minor;
1176 * @param mixed $src
1178 function setSrc( $src ) {
1179 $this->src = $src;
1183 * @param string $src
1184 * @param bool $isTemp
1186 function setFileSrc( $src, $isTemp ) {
1187 $this->fileSrc = $src;
1188 $this->fileIsTemp = $isTemp;
1192 * @param string $sha1base36
1194 function setSha1Base36( $sha1base36 ) {
1195 $this->sha1base36 = $sha1base36;
1199 * @param string $filename
1201 function setFilename( $filename ) {
1202 $this->filename = $filename;
1206 * @param string $archiveName
1208 function setArchiveName( $archiveName ) {
1209 $this->archiveName = $archiveName;
1213 * @param int $size
1215 function setSize( $size ) {
1216 $this->size = intval( $size );
1220 * @param string $type
1222 function setType( $type ) {
1223 $this->type = $type;
1227 * @param string $action
1229 function setAction( $action ) {
1230 $this->action = $action;
1234 * @param array $params
1236 function setParams( $params ) {
1237 $this->params = $params;
1241 * @param bool $noupdates
1243 public function setNoUpdates( $noupdates ) {
1244 $this->mNoUpdates = $noupdates;
1248 * @return Title
1250 function getTitle() {
1251 return $this->title;
1255 * @return int
1257 function getID() {
1258 return $this->id;
1262 * @return string
1264 function getTimestamp() {
1265 return $this->timestamp;
1269 * @return string
1271 function getUser() {
1272 return $this->user_text;
1276 * @return string
1278 * @deprecated Since 1.21, use getContent() instead.
1280 function getText() {
1281 ContentHandler::deprecated( __METHOD__, '1.21' );
1283 return $this->text;
1287 * @return ContentHandler
1289 function getContentHandler() {
1290 if ( is_null( $this->contentHandler ) ) {
1291 $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
1294 return $this->contentHandler;
1298 * @return Content
1300 function getContent() {
1301 if ( is_null( $this->content ) ) {
1302 $handler = $this->getContentHandler();
1303 $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
1306 return $this->content;
1310 * @return string
1312 function getModel() {
1313 if ( is_null( $this->model ) ) {
1314 $this->model = $this->getTitle()->getContentModel();
1317 return $this->model;
1321 * @return string
1323 function getFormat() {
1324 if ( is_null( $this->format ) ) {
1325 $this->format = $this->getContentHandler()->getDefaultFormat();
1328 return $this->format;
1332 * @return string
1334 function getComment() {
1335 return $this->comment;
1339 * @return bool
1341 function getMinor() {
1342 return $this->minor;
1346 * @return mixed
1348 function getSrc() {
1349 return $this->src;
1353 * @return bool|string
1355 function getSha1() {
1356 if ( $this->sha1base36 ) {
1357 return wfBaseConvert( $this->sha1base36, 36, 16 );
1359 return false;
1363 * @return string
1365 function getFileSrc() {
1366 return $this->fileSrc;
1370 * @return bool
1372 function isTempSrc() {
1373 return $this->isTemp;
1377 * @return mixed
1379 function getFilename() {
1380 return $this->filename;
1384 * @return string
1386 function getArchiveName() {
1387 return $this->archiveName;
1391 * @return mixed
1393 function getSize() {
1394 return $this->size;
1398 * @return string
1400 function getType() {
1401 return $this->type;
1405 * @return string
1407 function getAction() {
1408 return $this->action;
1412 * @return string
1414 function getParams() {
1415 return $this->params;
1419 * @return bool
1421 function importOldRevision() {
1422 $dbw = wfGetDB( DB_MASTER );
1424 # Sneak a single revision into place
1425 $user = User::newFromName( $this->getUser() );
1426 if ( $user ) {
1427 $userId = intval( $user->getId() );
1428 $userText = $user->getName();
1429 $userObj = $user;
1430 } else {
1431 $userId = 0;
1432 $userText = $this->getUser();
1433 $userObj = new User;
1436 // avoid memory leak...?
1437 $linkCache = LinkCache::singleton();
1438 $linkCache->clear();
1440 $page = WikiPage::factory( $this->title );
1441 $page->loadPageData( 'fromdbmaster' );
1442 if ( !$page->exists() ) {
1443 # must create the page...
1444 $pageId = $page->insertOn( $dbw );
1445 $created = true;
1446 $oldcountable = null;
1447 } else {
1448 $pageId = $page->getId();
1449 $created = false;
1451 $prior = $dbw->selectField( 'revision', '1',
1452 array( 'rev_page' => $pageId,
1453 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
1454 'rev_user_text' => $userText,
1455 'rev_comment' => $this->getComment() ),
1456 __METHOD__
1458 if ( $prior ) {
1459 // @todo FIXME: This could fail slightly for multiple matches :P
1460 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
1461 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
1462 return false;
1464 $oldcountable = $page->isCountable();
1467 # @todo FIXME: Use original rev_id optionally (better for backups)
1468 # Insert the row
1469 $revision = new Revision( array(
1470 'title' => $this->title,
1471 'page' => $pageId,
1472 'content_model' => $this->getModel(),
1473 'content_format' => $this->getFormat(),
1474 //XXX: just set 'content' => $this->getContent()?
1475 'text' => $this->getContent()->serialize( $this->getFormat() ),
1476 'comment' => $this->getComment(),
1477 'user' => $userId,
1478 'user_text' => $userText,
1479 'timestamp' => $this->timestamp,
1480 'minor_edit' => $this->minor,
1481 ) );
1482 $revision->insertOn( $dbw );
1483 $changed = $page->updateIfNewerOn( $dbw, $revision );
1485 if ( $changed !== false && !$this->mNoUpdates ) {
1486 wfDebug( __METHOD__ . ": running updates\n" );
1487 $page->doEditUpdates(
1488 $revision,
1489 $userObj,
1490 array( 'created' => $created, 'oldcountable' => $oldcountable )
1494 return true;
1497 function importLogItem() {
1498 $dbw = wfGetDB( DB_MASTER );
1499 # @todo FIXME: This will not record autoblocks
1500 if ( !$this->getTitle() ) {
1501 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1502 $this->timestamp . "\n" );
1503 return;
1505 # Check if it exists already
1506 // @todo FIXME: Use original log ID (better for backups)
1507 $prior = $dbw->selectField( 'logging', '1',
1508 array( 'log_type' => $this->getType(),
1509 'log_action' => $this->getAction(),
1510 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1511 'log_namespace' => $this->getTitle()->getNamespace(),
1512 'log_title' => $this->getTitle()->getDBkey(),
1513 'log_comment' => $this->getComment(),
1514 #'log_user_text' => $this->user_text,
1515 'log_params' => $this->params ),
1516 __METHOD__
1518 // @todo FIXME: This could fail slightly for multiple matches :P
1519 if ( $prior ) {
1520 wfDebug( __METHOD__
1521 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
1522 . $this->timestamp . "\n" );
1523 return;
1525 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1526 $data = array(
1527 'log_id' => $log_id,
1528 'log_type' => $this->type,
1529 'log_action' => $this->action,
1530 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1531 'log_user' => User::idFromName( $this->user_text ),
1532 #'log_user_text' => $this->user_text,
1533 'log_namespace' => $this->getTitle()->getNamespace(),
1534 'log_title' => $this->getTitle()->getDBkey(),
1535 'log_comment' => $this->getComment(),
1536 'log_params' => $this->params
1538 $dbw->insert( 'logging', $data, __METHOD__ );
1542 * @return bool
1544 function importUpload() {
1545 # Construct a file
1546 $archiveName = $this->getArchiveName();
1547 if ( $archiveName ) {
1548 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
1549 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1550 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1551 } else {
1552 $file = wfLocalFile( $this->getTitle() );
1553 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
1554 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
1555 $archiveName = $file->getTimestamp() . '!' . $file->getName();
1556 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1557 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1558 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
1561 if ( !$file ) {
1562 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
1563 return false;
1566 # Get the file source or download if necessary
1567 $source = $this->getFileSrc();
1568 $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0;
1569 if ( !$source ) {
1570 $source = $this->downloadSource();
1571 $flags |= File::DELETE_SOURCE;
1573 if ( !$source ) {
1574 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
1575 return false;
1577 $sha1 = $this->getSha1();
1578 if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
1579 if ( $flags & File::DELETE_SOURCE ) {
1580 # Broken file; delete it if it is a temporary file
1581 unlink( $source );
1583 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
1584 return false;
1587 $user = User::newFromName( $this->user_text );
1589 # Do the actual upload
1590 if ( $archiveName ) {
1591 $status = $file->uploadOld( $source, $archiveName,
1592 $this->getTimestamp(), $this->getComment(), $user, $flags );
1593 } else {
1594 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
1595 $flags, false, $this->getTimestamp(), $user );
1598 if ( $status->isGood() ) {
1599 wfDebug( __METHOD__ . ": Successful\n" );
1600 return true;
1601 } else {
1602 wfDebug( __METHOD__ . ': failed: ' . $status->getXml() . "\n" );
1603 return false;
1608 * @return bool|string
1610 function downloadSource() {
1611 global $wgEnableUploads;
1612 if ( !$wgEnableUploads ) {
1613 return false;
1616 $tempo = tempnam( wfTempDir(), 'download' );
1617 $f = fopen( $tempo, 'wb' );
1618 if ( !$f ) {
1619 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1620 return false;
1623 // @todo FIXME!
1624 $src = $this->getSrc();
1625 $data = Http::get( $src );
1626 if ( !$data ) {
1627 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1628 fclose( $f );
1629 unlink( $tempo );
1630 return false;
1633 fwrite( $f, $data );
1634 fclose( $f );
1636 return $tempo;
1642 * @todo document (e.g. one-sentence class description).
1643 * @ingroup SpecialPage
1645 class ImportStringSource {
1646 function __construct( $string ) {
1647 $this->mString = $string;
1648 $this->mRead = false;
1652 * @return bool
1654 function atEnd() {
1655 return $this->mRead;
1659 * @return bool|string
1661 function readChunk() {
1662 if ( $this->atEnd() ) {
1663 return false;
1665 $this->mRead = true;
1666 return $this->mString;
1671 * @todo document (e.g. one-sentence class description).
1672 * @ingroup SpecialPage
1674 class ImportStreamSource {
1675 function __construct( $handle ) {
1676 $this->mHandle = $handle;
1680 * @return bool
1682 function atEnd() {
1683 return feof( $this->mHandle );
1687 * @return string
1689 function readChunk() {
1690 return fread( $this->mHandle, 32768 );
1694 * @param string $filename
1695 * @return Status
1697 static function newFromFile( $filename ) {
1698 wfSuppressWarnings();
1699 $file = fopen( $filename, 'rt' );
1700 wfRestoreWarnings();
1701 if ( !$file ) {
1702 return Status::newFatal( "importcantopen" );
1704 return Status::newGood( new ImportStreamSource( $file ) );
1708 * @param string $fieldname
1709 * @return Status
1711 static function newFromUpload( $fieldname = "xmlimport" ) {
1712 $upload =& $_FILES[$fieldname];
1714 if ( $upload === null || !$upload['name'] ) {
1715 return Status::newFatal( 'importnofile' );
1717 if ( !empty( $upload['error'] ) ) {
1718 switch ( $upload['error'] ) {
1719 case 1:
1720 # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1721 return Status::newFatal( 'importuploaderrorsize' );
1722 case 2:
1723 # The uploaded file exceeds the MAX_FILE_SIZE directive that
1724 # was specified in the HTML form.
1725 return Status::newFatal( 'importuploaderrorsize' );
1726 case 3:
1727 # The uploaded file was only partially uploaded
1728 return Status::newFatal( 'importuploaderrorpartial' );
1729 case 6:
1730 # Missing a temporary folder.
1731 return Status::newFatal( 'importuploaderrortemp' );
1732 # case else: # Currently impossible
1736 $fname = $upload['tmp_name'];
1737 if ( is_uploaded_file( $fname ) ) {
1738 return ImportStreamSource::newFromFile( $fname );
1739 } else {
1740 return Status::newFatal( 'importnofile' );
1745 * @param string $url
1746 * @param string $method
1747 * @return Status
1749 static function newFromURL( $url, $method = 'GET' ) {
1750 wfDebug( __METHOD__ . ": opening $url\n" );
1751 # Use the standard HTTP fetch function; it times out
1752 # quicker and sorts out user-agent problems which might
1753 # otherwise prevent importing from large sites, such
1754 # as the Wikimedia cluster, etc.
1755 $data = Http::request( $method, $url, array( 'followRedirects' => true ) );
1756 if ( $data !== false ) {
1757 $file = tmpfile();
1758 fwrite( $file, $data );
1759 fflush( $file );
1760 fseek( $file, 0 );
1761 return Status::newGood( new ImportStreamSource( $file ) );
1762 } else {
1763 return Status::newFatal( 'importcantopen' );
1768 * @param string $interwiki
1769 * @param string $page
1770 * @param bool $history
1771 * @param bool $templates
1772 * @param int $pageLinkDepth
1773 * @return Status
1775 public static function newFromInterwiki( $interwiki, $page, $history = false,
1776 $templates = false, $pageLinkDepth = 0
1778 if ( $page == '' ) {
1779 return Status::newFatal( 'import-noarticle' );
1781 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
1782 if ( is_null( $link ) || !$link->isExternal() ) {
1783 return Status::newFatal( 'importbadinterwiki' );
1784 } else {
1785 $params = array();
1786 if ( $history ) {
1787 $params['history'] = 1;
1789 if ( $templates ) {
1790 $params['templates'] = 1;
1792 if ( $pageLinkDepth ) {
1793 $params['pagelink-depth'] = $pageLinkDepth;
1795 $url = $link->getFullURL( $params );
1796 # For interwikis, use POST to avoid redirects.
1797 return ImportStreamSource::newFromURL( $url, "POST" );