3 * implements Special:Import
7 private $reader = null;
8 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
9 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
13 * Creates an ImportXMLReader drawing from the source provided
15 function __construct( $source ) {
16 $this->reader
= new XMLReader2();
18 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
19 $id = UploadSourceAdapter
::registerSource( $source );
20 $this->reader
->open( "uploadsource://$id" );
23 $this->setRevisionCallback( array( $this, "importRevision" ) );
24 $this->setUploadCallback( array( $this, 'importUpload' ) );
25 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
26 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
29 private function throwXmlError( $err ) {
30 $this->debug( "FAILURE: $err" );
31 wfDebug( "WikiImporter XML error: $err\n" );
34 private function debug( $data ) {
36 wfDebug( "IMPORT: $data\n" );
40 private function warn( $data ) {
41 wfDebug( "IMPORT: $data\n" );
44 private function notice( $data ) {
45 global $wgCommandLineMode;
46 if( $wgCommandLineMode ) {
50 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
57 function setDebug( $debug ) {
58 $this->mDebug
= $debug;
62 * Sets the action to perform as each new page in the stream is reached.
63 * @param $callback callback
66 public function setPageCallback( $callback ) {
67 $previous = $this->mPageCallback
;
68 $this->mPageCallback
= $callback;
73 * Sets the action to perform as each page in the stream is completed.
74 * Callback accepts the page title (as a Title object), a second object
75 * with the original title form (in case it's been overridden into a
76 * local namespace), and a count of revisions.
78 * @param $callback callback
81 public function setPageOutCallback( $callback ) {
82 $previous = $this->mPageOutCallback
;
83 $this->mPageOutCallback
= $callback;
88 * Sets the action to perform as each page revision is reached.
89 * @param $callback callback
92 public function setRevisionCallback( $callback ) {
93 $previous = $this->mRevisionCallback
;
94 $this->mRevisionCallback
= $callback;
99 * Sets the action to perform as each file upload version is reached.
100 * @param $callback callback
103 public function setUploadCallback( $callback ) {
104 $previous = $this->mUploadCallback
;
105 $this->mUploadCallback
= $callback;
110 * Sets the action to perform as each log item reached.
111 * @param $callback callback
114 public function setLogItemCallback( $callback ) {
115 $previous = $this->mLogItemCallback
;
116 $this->mLogItemCallback
= $callback;
121 * Sets the action to perform when site info is encountered
122 * @param $callback callback
125 public function setSiteInfoCallback( $callback ) {
126 $previous = $this->mSiteInfoCallback
;
127 $this->mSiteInfoCallback
= $callback;
132 * Set a target namespace to override the defaults
134 public function setTargetNamespace( $namespace ) {
135 if( is_null( $namespace ) ) {
136 // Don't override namespaces
137 $this->mTargetNamespace
= null;
138 } elseif( $namespace >= 0 ) {
139 // FIXME: Check for validity
140 $this->mTargetNamespace
= intval( $namespace );
147 * Default per-revision callback, performs the import.
148 * @param $revision WikiRevision
150 public function importRevision( $revision ) {
151 $dbw = wfGetDB( DB_MASTER
);
152 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
156 * Default per-revision callback, performs the import.
157 * @param $rev WikiRevision
159 public function importLogItem( $rev ) {
160 $dbw = wfGetDB( DB_MASTER
);
161 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
167 public function importUpload( $revision ) {
168 //$dbw = wfGetDB( DB_MASTER );
169 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
174 * Mostly for hook use
176 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
177 $args = func_get_args();
178 return wfRunHooks( 'AfterImportPage', $args );
182 * Alternate per-revision callback, for debugging.
183 * @param $revision WikiRevision
185 public function debugRevisionHandler( &$revision ) {
186 $this->debug( "Got revision:" );
187 if( is_object( $revision->title
) ) {
188 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
190 $this->debug( "-- Title: <invalid>" );
192 $this->debug( "-- User: " . $revision->user_text
);
193 $this->debug( "-- Timestamp: " . $revision->timestamp
);
194 $this->debug( "-- Comment: " . $revision->comment
);
195 $this->debug( "-- Text: " . $revision->text
);
199 * Notify the callback function when a new <page> is reached.
200 * @param $title Title
202 function pageCallback( $title ) {
203 if( isset( $this->mPageCallback
) ) {
204 call_user_func( $this->mPageCallback
, $title );
209 * Notify the callback function when a </page> is closed.
210 * @param $title Title
211 * @param $origTitle Title
212 * @param $revisionCount int
213 * @param $successCount Int: number of revisions for which callback returned true
215 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
216 if( isset( $this->mPageOutCallback
) ) {
217 $args = func_get_args();
218 call_user_func_array( $this->mPageOutCallback
, $args );
223 * Notify the callback function of a revision
224 * @param $revision A WikiRevision object
226 private function revisionCallback( $revision ) {
227 if ( isset( $this->mRevisionCallback
) ) {
228 return call_user_func_array( $this->mRevisionCallback
,
229 array( $revision, $this ) );
236 * Notify the callback function of a new log item
237 * @param $revision A WikiRevision object
239 private function logItemCallback( $revision ) {
240 if ( isset( $this->mLogItemCallback
) ) {
241 return call_user_func_array( $this->mLogItemCallback
,
242 array( $revision, $this ) );
249 * Shouldn't something like this be built-in to XMLReader?
250 * Fetches text contents of the current element, assuming
251 * no sub-elements or such scary things.
255 private function nodeContents() {
256 return $this->reader
->nodeContents();
261 /** Left in for debugging */
262 private function dumpElement() {
263 static $lookup = null;
265 $xmlReaderConstants = array(
280 "SIGNIFICANT_WHITESPACE",
287 foreach( $xmlReaderConstants as $name ) {
288 $lookup[constant("XmlReader::$name")] = $name;
293 $lookup[$this->reader
->nodeType
],
300 * Primary entry point
302 public function doImport() {
303 $this->reader
->read();
305 if ( $this->reader
->name
!= 'mediawiki' ) {
306 throw new MWException( "Expected <mediawiki> tag, got ".
307 $this->reader
->name
);
309 $this->debug( "<mediawiki> tag is correct." );
311 $this->debug( "Starting primary dump processing loop." );
313 $keepReading = $this->reader
->read();
315 while ( $keepReading ) {
316 $tag = $this->reader
->name
;
317 $type = $this->reader
->nodeType
;
319 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this->reader
) ) {
321 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
323 } elseif ( $tag == 'siteinfo' ) {
324 $this->handleSiteInfo();
325 } elseif ( $tag == 'page' ) {
327 } elseif ( $tag == 'logitem' ) {
328 $this->handleLogItem();
329 } elseif ( $tag != '#text' ) {
330 $this->warn( "Unhandled top-level XML tag $tag" );
336 $keepReading = $this->reader
->next();
338 $this->debug( "Skip" );
340 $keepReading = $this->reader
->read();
347 private function handleSiteInfo() {
348 // Site info is useful, but not actually used for dump imports.
349 // Includes a quick short-circuit to save performance.
350 if ( ! $this->mSiteInfoCallback
) {
351 $this->reader
->next();
354 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
357 private function handleLogItem() {
358 $this->debug( "Enter log item handler." );
361 // Fields that can just be stuffed in the pageInfo object
362 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
363 'logtitle', 'params' );
365 while ( $this->reader
->read() ) {
366 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
367 $this->reader
->name
== 'logitem') {
371 $tag = $this->reader
->name
;
373 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
374 $this->reader
, $logInfo ) ) {
376 } elseif ( in_array( $tag, $normalFields ) ) {
377 $logInfo[$tag] = $this->nodeContents();
378 } elseif ( $tag == 'contributor' ) {
379 $logInfo['contributor'] = $this->handleContributor();
380 } elseif ( $tag != '#text' ) {
381 $this->warn( "Unhandled log-item XML tag $tag" );
385 $this->processLogItem( $logInfo );
388 private function processLogItem( $logInfo ) {
389 $revision = new WikiRevision
;
391 $revision->setID( $logInfo['id'] );
392 $revision->setType( $logInfo['type'] );
393 $revision->setAction( $logInfo['action'] );
394 $revision->setTimestamp( $logInfo['timestamp'] );
395 $revision->setParams( $logInfo['params'] );
396 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
398 if ( isset( $logInfo['comment'] ) ) {
399 $revision->setComment( $logInfo['comment'] );
402 if ( isset( $logInfo['contributor']['ip'] ) ) {
403 $revision->setUserIP( $logInfo['contributor']['ip'] );
405 if ( isset( $logInfo['contributor']['username'] ) ) {
406 $revision->setUserName( $logInfo['contributor']['username'] );
409 return $this->logItemCallback( $revision );
412 private function handlePage() {
414 $this->debug( "Enter page handler." );
415 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
417 // Fields that can just be stuffed in the pageInfo object
418 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
423 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
424 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
425 $this->reader
->name
== 'page') {
429 $tag = $this->reader
->name
;
432 // The title is invalid, bail out of this page
434 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this->reader
,
437 } elseif ( in_array( $tag, $normalFields ) ) {
438 $pageInfo[$tag] = $this->nodeContents();
439 if ( $tag == 'title' ) {
440 $title = $this->processTitle( $pageInfo['title'] );
447 $this->pageCallback( $title );
448 list( $pageInfo['_title'], $origTitle ) = $title;
450 } elseif ( $tag == 'revision' ) {
451 $this->handleRevision( $pageInfo );
452 } elseif ( $tag == 'upload' ) {
453 $this->handleUpload( $pageInfo );
454 } elseif ( $tag != '#text' ) {
455 $this->warn( "Unhandled page XML tag $tag" );
460 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
461 $pageInfo['revisionCount'],
462 $pageInfo['successfulRevisionCount'],
466 private function handleRevision( &$pageInfo ) {
467 $this->debug( "Enter revision handler" );
468 $revisionInfo = array();
470 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
474 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
475 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
476 $this->reader
->name
== 'revision') {
480 $tag = $this->reader
->name
;
482 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this->reader
,
483 $pageInfo, $revisionInfo ) ) {
485 } elseif ( in_array( $tag, $normalFields ) ) {
486 $revisionInfo[$tag] = $this->nodeContents();
487 } elseif ( $tag == 'contributor' ) {
488 $revisionInfo['contributor'] = $this->handleContributor();
489 } elseif ( $tag != '#text' ) {
490 $this->warn( "Unhandled revision XML tag $tag" );
495 $pageInfo['revisionCount']++
;
496 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
497 $pageInfo['successfulRevisionCount']++
;
501 private function processRevision( $pageInfo, $revisionInfo ) {
502 $revision = new WikiRevision
;
504 $revision->setID( $revisionInfo['id'] );
505 $revision->setText( $revisionInfo['text'] );
506 $revision->setTitle( $pageInfo['_title'] );
507 $revision->setTimestamp( $revisionInfo['timestamp'] );
509 if ( isset( $revisionInfo['comment'] ) ) {
510 $revision->setComment( $revisionInfo['comment'] );
513 if ( isset( $revisionInfo['minor'] ) )
514 $revision->setMinor( true );
516 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
517 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
519 if ( isset( $revisionInfo['contributor']['username'] ) ) {
520 $revision->setUserName( $revisionInfo['contributor']['username'] );
523 return $this->revisionCallback( $revision );
526 private function handleUpload( &$pageInfo ) {
527 $this->debug( "Enter upload handler" );
528 $uploadInfo = array();
530 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
535 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
536 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
537 $this->reader
->name
== 'upload') {
541 $tag = $this->reader
->name
;
543 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this->reader
,
544 $pageInfo, $revisionInfo ) ) {
546 } elseif ( in_array( $tag, $normalFields ) ) {
547 $uploadInfo[$tag] = $this->nodeContents();
548 } elseif ( $tag == 'contributor' ) {
549 $uploadInfo['contributor'] = $this->handleContributor();
550 } elseif ( $tag != '#text' ) {
551 $this->warn( "Unhandled upload XML tag $tag" );
556 return $this->processUpload( $pageInfo, $uploadInfo );
559 private function processUpload( $pageInfo, $uploadInfo ) {
560 $revision = new WikiRevision
;
562 $revision->setTitle( $pageInfo['_title'] );
563 $revision->setID( $uploadInfo['id'] );
564 $revision->setTimestamp( $uploadInfo['timestamp'] );
565 $revision->setText( $uploadInfo['text'] );
566 $revision->setFilename( $uploadInfo['filename'] );
567 $revision->setSrc( $uploadInfo['src'] );
568 $revision->setSize( intval( $uploadInfo['size'] ) );
569 $revision->setComment( $uploadInfo['comment'] );
571 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
572 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
574 if ( isset( $uploadInfo['contributor']['username'] ) ) {
575 $revision->setUserName( $revisionInfo['contributor']['username'] );
578 return $this->uploadCallback( $revision );
581 private function handleContributor() {
582 $fields = array( 'id', 'ip', 'username' );
585 while ( $this->reader
->read() ) {
586 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
587 $this->reader
->name
== 'contributor') {
591 $tag = $this->reader
->name
;
593 if ( in_array( $tag, $fields ) ) {
594 $info[$tag] = $this->nodeContents();
601 private function processTitle( $text ) {
603 $origTitle = Title
::newFromText( $workTitle );
606 if( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
607 $title = Title
::makeTitle( $this->mTargetNamespace
,
608 $origTitle->getDBkey() );
610 $title = Title
::newFromText( $workTitle );
613 if( is_null( $title ) ) {
614 // Invalid page title? Ignore the page
615 $this->notice( "Skipping invalid page title '$workTitle'" );
616 } elseif( $title->getInterwiki() != '' ) {
617 $this->notice( "Skipping interwiki page title '$workTitle'" );
621 return array( $origTitle, $title );
625 /** This is a horrible hack used to keep source compatibility */
626 class UploadSourceAdapter
{
627 static $sourceRegistrations = array();
633 static function registerSource( $source ) {
634 $id = wfGenerateToken();
636 self
::$sourceRegistrations[$id] = $source;
641 function stream_open( $path, $mode, $options, &$opened_path ) {
642 $url = parse_url($path);
645 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
649 $this->mSource
= self
::$sourceRegistrations[$id];
654 function stream_read( $count ) {
658 while ( !$leave && !$this->mSource
->atEnd() &&
659 strlen($this->mBuffer
) < $count ) {
660 $read = $this->mSource
->readChunk();
662 if ( !strlen($read) ) {
666 $this->mBuffer
.= $read;
669 if ( strlen($this->mBuffer
) ) {
670 $return = substr( $this->mBuffer
, 0, $count );
671 $this->mBuffer
= substr( $this->mBuffer
, $count );
674 $this->mPosition +
= strlen($return);
679 function stream_write( $data ) {
683 function stream_tell() {
684 return $this->mPosition
;
687 function stream_eof() {
688 return $this->mSource
->atEnd();
691 function url_stat() {
694 $result['dev'] = $result[0] = 0;
695 $result['ino'] = $result[1] = 0;
696 $result['mode'] = $result[2] = 0;
697 $result['nlink'] = $result[3] = 0;
698 $result['uid'] = $result[4] = 0;
699 $result['gid'] = $result[5] = 0;
700 $result['rdev'] = $result[6] = 0;
701 $result['size'] = $result[7] = 0;
702 $result['atime'] = $result[8] = 0;
703 $result['mtime'] = $result[9] = 0;
704 $result['ctime'] = $result[10] = 0;
705 $result['blksize'] = $result[11] = 0;
706 $result['blocks'] = $result[12] = 0;
712 class XMLReader2
extends XMLReader
{
713 function nodeContents() {
714 if( $this->isEmptyElement
) {
718 while( $this->read() ) {
719 switch( $this->nodeType
) {
720 case XmlReader
::TEXT
:
721 case XmlReader
::SIGNIFICANT_WHITESPACE
:
722 $buffer .= $this->value
;
724 case XmlReader
::END_ELEMENT
:
728 return $this->close();