3 * XML file reader for the page data importer
9 * implements Special:Import
10 * @ingroup SpecialPage
13 private $reader = null;
14 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
15 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
19 * Creates an ImportXMLReader drawing from the source provided
21 function __construct( $source ) {
22 $this->reader
= new XMLReader2();
24 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
25 $id = UploadSourceAdapter
::registerSource( $source );
26 $this->reader
->open( "uploadsource://$id" );
29 $this->setRevisionCallback( array( $this, "importRevision" ) );
30 $this->setUploadCallback( array( $this, 'importUpload' ) );
31 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
32 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
35 private function throwXmlError( $err ) {
36 $this->debug( "FAILURE: $err" );
37 wfDebug( "WikiImporter XML error: $err\n" );
40 private function debug( $data ) {
42 wfDebug( "IMPORT: $data\n" );
46 private function warn( $data ) {
47 wfDebug( "IMPORT: $data\n" );
50 private function notice( $data ) {
51 global $wgCommandLineMode;
52 if( $wgCommandLineMode ) {
56 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
63 function setDebug( $debug ) {
64 $this->mDebug
= $debug;
68 * Sets the action to perform as each new page in the stream is reached.
69 * @param $callback callback
72 public function setPageCallback( $callback ) {
73 $previous = $this->mPageCallback
;
74 $this->mPageCallback
= $callback;
79 * Sets the action to perform as each page in the stream is completed.
80 * Callback accepts the page title (as a Title object), a second object
81 * with the original title form (in case it's been overridden into a
82 * local namespace), and a count of revisions.
84 * @param $callback callback
87 public function setPageOutCallback( $callback ) {
88 $previous = $this->mPageOutCallback
;
89 $this->mPageOutCallback
= $callback;
94 * Sets the action to perform as each page revision is reached.
95 * @param $callback callback
98 public function setRevisionCallback( $callback ) {
99 $previous = $this->mRevisionCallback
;
100 $this->mRevisionCallback
= $callback;
105 * Sets the action to perform as each file upload version is reached.
106 * @param $callback callback
109 public function setUploadCallback( $callback ) {
110 $previous = $this->mUploadCallback
;
111 $this->mUploadCallback
= $callback;
116 * Sets the action to perform as each log item reached.
117 * @param $callback callback
120 public function setLogItemCallback( $callback ) {
121 $previous = $this->mLogItemCallback
;
122 $this->mLogItemCallback
= $callback;
127 * Sets the action to perform when site info is encountered
128 * @param $callback callback
131 public function setSiteInfoCallback( $callback ) {
132 $previous = $this->mSiteInfoCallback
;
133 $this->mSiteInfoCallback
= $callback;
138 * Set a target namespace to override the defaults
140 public function setTargetNamespace( $namespace ) {
141 if( is_null( $namespace ) ) {
142 // Don't override namespaces
143 $this->mTargetNamespace
= null;
144 } elseif( $namespace >= 0 ) {
145 // FIXME: Check for validity
146 $this->mTargetNamespace
= intval( $namespace );
153 * Default per-revision callback, performs the import.
154 * @param $revision WikiRevision
156 public function importRevision( $revision ) {
157 $dbw = wfGetDB( DB_MASTER
);
158 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
162 * Default per-revision callback, performs the import.
163 * @param $rev WikiRevision
165 public function importLogItem( $rev ) {
166 $dbw = wfGetDB( DB_MASTER
);
167 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
173 public function importUpload( $revision ) {
174 //$dbw = wfGetDB( DB_MASTER );
175 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
180 * Mostly for hook use
182 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
183 $args = func_get_args();
184 return wfRunHooks( 'AfterImportPage', $args );
188 * Alternate per-revision callback, for debugging.
189 * @param $revision WikiRevision
191 public function debugRevisionHandler( &$revision ) {
192 $this->debug( "Got revision:" );
193 if( is_object( $revision->title
) ) {
194 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
196 $this->debug( "-- Title: <invalid>" );
198 $this->debug( "-- User: " . $revision->user_text
);
199 $this->debug( "-- Timestamp: " . $revision->timestamp
);
200 $this->debug( "-- Comment: " . $revision->comment
);
201 $this->debug( "-- Text: " . $revision->text
);
205 * Notify the callback function when a new <page> is reached.
206 * @param $title Title
208 function pageCallback( $title ) {
209 if( isset( $this->mPageCallback
) ) {
210 call_user_func( $this->mPageCallback
, $title );
215 * Notify the callback function when a </page> is closed.
216 * @param $title Title
217 * @param $origTitle Title
218 * @param $revCount Integer
219 * @param $sucCount Int: number of revisions for which callback returned true
220 * @param $pageInfo Array: associative array of page information
222 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
223 if( isset( $this->mPageOutCallback
) ) {
224 $args = func_get_args();
225 call_user_func_array( $this->mPageOutCallback
, $args );
230 * Notify the callback function of a revision
231 * @param $revision A WikiRevision object
233 private function revisionCallback( $revision ) {
234 if ( isset( $this->mRevisionCallback
) ) {
235 return call_user_func_array( $this->mRevisionCallback
,
236 array( $revision, $this ) );
243 * Notify the callback function of a new log item
244 * @param $revision A WikiRevision object
246 private function logItemCallback( $revision ) {
247 if ( isset( $this->mLogItemCallback
) ) {
248 return call_user_func_array( $this->mLogItemCallback
,
249 array( $revision, $this ) );
256 * Shouldn't something like this be built-in to XMLReader?
257 * Fetches text contents of the current element, assuming
258 * no sub-elements or such scary things.
262 private function nodeContents() {
263 return $this->reader
->nodeContents();
268 /** Left in for debugging */
269 private function dumpElement() {
270 static $lookup = null;
272 $xmlReaderConstants = array(
287 "SIGNIFICANT_WHITESPACE",
294 foreach( $xmlReaderConstants as $name ) {
295 $lookup[constant("XmlReader::$name")] = $name;
300 $lookup[$this->reader
->nodeType
],
307 * Primary entry point
309 public function doImport() {
310 $this->reader
->read();
312 if ( $this->reader
->name
!= 'mediawiki' ) {
313 throw new MWException( "Expected <mediawiki> tag, got ".
314 $this->reader
->name
);
316 $this->debug( "<mediawiki> tag is correct." );
318 $this->debug( "Starting primary dump processing loop." );
320 $keepReading = $this->reader
->read();
322 while ( $keepReading ) {
323 $tag = $this->reader
->name
;
324 $type = $this->reader
->nodeType
;
326 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this->reader
) ) {
328 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
330 } elseif ( $tag == 'siteinfo' ) {
331 $this->handleSiteInfo();
332 } elseif ( $tag == 'page' ) {
334 } elseif ( $tag == 'logitem' ) {
335 $this->handleLogItem();
336 } elseif ( $tag != '#text' ) {
337 $this->warn( "Unhandled top-level XML tag $tag" );
343 $keepReading = $this->reader
->next();
345 $this->debug( "Skip" );
347 $keepReading = $this->reader
->read();
354 private function handleSiteInfo() {
355 // Site info is useful, but not actually used for dump imports.
356 // Includes a quick short-circuit to save performance.
357 if ( ! $this->mSiteInfoCallback
) {
358 $this->reader
->next();
361 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
364 private function handleLogItem() {
365 $this->debug( "Enter log item handler." );
368 // Fields that can just be stuffed in the pageInfo object
369 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
370 'logtitle', 'params' );
372 while ( $this->reader
->read() ) {
373 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
374 $this->reader
->name
== 'logitem') {
378 $tag = $this->reader
->name
;
380 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
381 $this->reader
, $logInfo ) ) {
383 } elseif ( in_array( $tag, $normalFields ) ) {
384 $logInfo[$tag] = $this->nodeContents();
385 } elseif ( $tag == 'contributor' ) {
386 $logInfo['contributor'] = $this->handleContributor();
387 } elseif ( $tag != '#text' ) {
388 $this->warn( "Unhandled log-item XML tag $tag" );
392 $this->processLogItem( $logInfo );
395 private function processLogItem( $logInfo ) {
396 $revision = new WikiRevision
;
398 $revision->setID( $logInfo['id'] );
399 $revision->setType( $logInfo['type'] );
400 $revision->setAction( $logInfo['action'] );
401 $revision->setTimestamp( $logInfo['timestamp'] );
402 $revision->setParams( $logInfo['params'] );
403 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
405 if ( isset( $logInfo['comment'] ) ) {
406 $revision->setComment( $logInfo['comment'] );
409 if ( isset( $logInfo['contributor']['ip'] ) ) {
410 $revision->setUserIP( $logInfo['contributor']['ip'] );
412 if ( isset( $logInfo['contributor']['username'] ) ) {
413 $revision->setUserName( $logInfo['contributor']['username'] );
416 return $this->logItemCallback( $revision );
419 private function handlePage() {
421 $this->debug( "Enter page handler." );
422 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
424 // Fields that can just be stuffed in the pageInfo object
425 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
430 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
431 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
432 $this->reader
->name
== 'page') {
436 $tag = $this->reader
->name
;
439 // The title is invalid, bail out of this page
441 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this->reader
,
444 } elseif ( in_array( $tag, $normalFields ) ) {
445 $pageInfo[$tag] = $this->nodeContents();
446 if ( $tag == 'title' ) {
447 $title = $this->processTitle( $pageInfo['title'] );
454 $this->pageCallback( $title );
455 list( $pageInfo['_title'], $origTitle ) = $title;
457 } elseif ( $tag == 'revision' ) {
458 $this->handleRevision( $pageInfo );
459 } elseif ( $tag == 'upload' ) {
460 $this->handleUpload( $pageInfo );
461 } elseif ( $tag != '#text' ) {
462 $this->warn( "Unhandled page XML tag $tag" );
467 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
468 $pageInfo['revisionCount'],
469 $pageInfo['successfulRevisionCount'],
473 private function handleRevision( &$pageInfo ) {
474 $this->debug( "Enter revision handler" );
475 $revisionInfo = array();
477 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
481 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
482 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
483 $this->reader
->name
== 'revision') {
487 $tag = $this->reader
->name
;
489 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this->reader
,
490 $pageInfo, $revisionInfo ) ) {
492 } elseif ( in_array( $tag, $normalFields ) ) {
493 $revisionInfo[$tag] = $this->nodeContents();
494 } elseif ( $tag == 'contributor' ) {
495 $revisionInfo['contributor'] = $this->handleContributor();
496 } elseif ( $tag != '#text' ) {
497 $this->warn( "Unhandled revision XML tag $tag" );
502 $pageInfo['revisionCount']++
;
503 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
504 $pageInfo['successfulRevisionCount']++
;
508 private function processRevision( $pageInfo, $revisionInfo ) {
509 $revision = new WikiRevision
;
511 $revision->setID( $revisionInfo['id'] );
512 $revision->setText( $revisionInfo['text'] );
513 $revision->setTitle( $pageInfo['_title'] );
514 $revision->setTimestamp( $revisionInfo['timestamp'] );
516 if ( isset( $revisionInfo['comment'] ) ) {
517 $revision->setComment( $revisionInfo['comment'] );
520 if ( isset( $revisionInfo['minor'] ) )
521 $revision->setMinor( true );
523 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
524 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
526 if ( isset( $revisionInfo['contributor']['username'] ) ) {
527 $revision->setUserName( $revisionInfo['contributor']['username'] );
530 return $this->revisionCallback( $revision );
533 private function handleUpload( &$pageInfo ) {
534 $this->debug( "Enter upload handler" );
535 $uploadInfo = array();
537 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
542 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
543 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
544 $this->reader
->name
== 'upload') {
548 $tag = $this->reader
->name
;
550 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this->reader
,
551 $pageInfo, $revisionInfo ) ) {
553 } elseif ( in_array( $tag, $normalFields ) ) {
554 $uploadInfo[$tag] = $this->nodeContents();
555 } elseif ( $tag == 'contributor' ) {
556 $uploadInfo['contributor'] = $this->handleContributor();
557 } elseif ( $tag != '#text' ) {
558 $this->warn( "Unhandled upload XML tag $tag" );
563 return $this->processUpload( $pageInfo, $uploadInfo );
566 private function processUpload( $pageInfo, $uploadInfo ) {
567 $revision = new WikiRevision
;
569 $revision->setTitle( $pageInfo['_title'] );
570 $revision->setID( $uploadInfo['id'] );
571 $revision->setTimestamp( $uploadInfo['timestamp'] );
572 $revision->setText( $uploadInfo['text'] );
573 $revision->setFilename( $uploadInfo['filename'] );
574 $revision->setSrc( $uploadInfo['src'] );
575 $revision->setSize( intval( $uploadInfo['size'] ) );
576 $revision->setComment( $uploadInfo['comment'] );
578 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
579 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
581 if ( isset( $uploadInfo['contributor']['username'] ) ) {
582 $revision->setUserName( $revisionInfo['contributor']['username'] );
585 return $this->uploadCallback( $revision );
588 private function handleContributor() {
589 $fields = array( 'id', 'ip', 'username' );
592 while ( $this->reader
->read() ) {
593 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
594 $this->reader
->name
== 'contributor') {
598 $tag = $this->reader
->name
;
600 if ( in_array( $tag, $fields ) ) {
601 $info[$tag] = $this->nodeContents();
608 private function processTitle( $text ) {
610 $origTitle = Title
::newFromText( $workTitle );
612 if( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
613 $title = Title
::makeTitle( $this->mTargetNamespace
,
614 $origTitle->getDBkey() );
616 $title = Title
::newFromText( $workTitle );
619 if( is_null( $title ) ) {
620 // Invalid page title? Ignore the page
621 $this->notice( "Skipping invalid page title '$workTitle'" );
622 } elseif( $title->getInterwiki() != '' ) {
623 $this->notice( "Skipping interwiki page title '$workTitle'" );
627 return array( $origTitle, $title );
631 /** This is a horrible hack used to keep source compatibility */
632 class UploadSourceAdapter
{
633 static $sourceRegistrations = array();
639 static function registerSource( $source ) {
640 $id = wfGenerateToken();
642 self
::$sourceRegistrations[$id] = $source;
647 function stream_open( $path, $mode, $options, &$opened_path ) {
648 $url = parse_url($path);
651 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
655 $this->mSource
= self
::$sourceRegistrations[$id];
660 function stream_read( $count ) {
664 while ( !$leave && !$this->mSource
->atEnd() &&
665 strlen($this->mBuffer
) < $count ) {
666 $read = $this->mSource
->readChunk();
668 if ( !strlen($read) ) {
672 $this->mBuffer
.= $read;
675 if ( strlen($this->mBuffer
) ) {
676 $return = substr( $this->mBuffer
, 0, $count );
677 $this->mBuffer
= substr( $this->mBuffer
, $count );
680 $this->mPosition +
= strlen($return);
685 function stream_write( $data ) {
689 function stream_tell() {
690 return $this->mPosition
;
693 function stream_eof() {
694 return $this->mSource
->atEnd();
697 function url_stat() {
700 $result['dev'] = $result[0] = 0;
701 $result['ino'] = $result[1] = 0;
702 $result['mode'] = $result[2] = 0;
703 $result['nlink'] = $result[3] = 0;
704 $result['uid'] = $result[4] = 0;
705 $result['gid'] = $result[5] = 0;
706 $result['rdev'] = $result[6] = 0;
707 $result['size'] = $result[7] = 0;
708 $result['atime'] = $result[8] = 0;
709 $result['mtime'] = $result[9] = 0;
710 $result['ctime'] = $result[10] = 0;
711 $result['blksize'] = $result[11] = 0;
712 $result['blocks'] = $result[12] = 0;
718 class XMLReader2
extends XMLReader
{
719 function nodeContents() {
720 if( $this->isEmptyElement
) {
724 while( $this->read() ) {
725 switch( $this->nodeType
) {
726 case XmlReader
::TEXT
:
727 case XmlReader
::SIGNIFICANT_WHITESPACE
:
728 $buffer .= $this->value
;
730 case XmlReader
::END_ELEMENT
:
734 return $this->close();