3 * implements Special:Import
6 class ImportXMLReader
{
7 private $reader = null;
8 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
9 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
12 function __construct( $source ) {
13 $this->reader
= new XMLReader();
15 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
16 $id = UploadSourceAdapter
::registerSource( $source );
17 $this->reader
->open( "uploadsource://$id" );
20 $this->setRevisionCallback( array( $this, "importRevision" ) );
21 $this->setUploadCallback( array( $this, 'importUpload' ) );
22 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
25 function throwXmlError( $err ) {
26 $this->debug( "FAILURE: $err" );
27 wfDebug( "WikiImporter XML error: $err\n" );
30 function debug( $data ) {
32 wfDebug( "IMPORT: $data\n" );
36 function warn( $data ) {
37 wfDebug( "IMPORT: $data\n" );
40 function notice( $data ) {
41 global $wgCommandLineMode;
42 if( $wgCommandLineMode ) {
46 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
53 function setDebug( $debug ) {
54 $this->mDebug
= $debug;
58 * Sets the action to perform as each new page in the stream is reached.
59 * @param $callback callback
62 function setPageCallback( $callback ) {
63 $previous = $this->mPageCallback
;
64 $this->mPageCallback
= $callback;
69 * Sets the action to perform as each page in the stream is completed.
70 * Callback accepts the page title (as a Title object), a second object
71 * with the original title form (in case it's been overridden into a
72 * local namespace), and a count of revisions.
74 * @param $callback callback
77 function setPageOutCallback( $callback ) {
78 $previous = $this->mPageOutCallback
;
79 $this->mPageOutCallback
= $callback;
84 * Sets the action to perform as each page revision is reached.
85 * @param $callback callback
88 function setRevisionCallback( $callback ) {
89 $previous = $this->mRevisionCallback
;
90 $this->mRevisionCallback
= $callback;
95 * Sets the action to perform as each file upload version is reached.
96 * @param $callback callback
99 function setUploadCallback( $callback ) {
100 $previous = $this->mUploadCallback
;
101 $this->mUploadCallback
= $callback;
106 * Sets the action to perform as each log item reached.
107 * @param $callback callback
110 function setLogItemCallback( $callback ) {
111 $previous = $this->mLogItemCallback
;
112 $this->mLogItemCallback
= $callback;
117 * Sets the action to perform when site info is encountered
118 * @param $callback callback
121 function setSiteInfoCallback( $callback ) {
122 $previous = $this->mSiteInfoCallback
;
123 $this->mSiteInfoCallback
= $callback;
128 * Set a target namespace to override the defaults
130 function setTargetNamespace( $namespace ) {
131 if( is_null( $namespace ) ) {
132 // Don't override namespaces
133 $this->mTargetNamespace
= null;
134 } elseif( $namespace >= 0 ) {
135 // FIXME: Check for validity
136 $this->mTargetNamespace
= intval( $namespace );
143 * Default per-revision callback, performs the import.
144 * @param $revision WikiRevision
147 function importRevision( $revision ) {
148 $dbw = wfGetDB( DB_MASTER
);
149 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
153 * Default per-revision callback, performs the import.
154 * @param $rev WikiRevision
157 function importLogItem( $rev ) {
158 $dbw = wfGetDB( DB_MASTER
);
159 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
165 function importUpload( $revision ) {
166 //$dbw = wfGetDB( DB_MASTER );
167 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
172 * Alternate per-revision callback, for debugging.
173 * @param $revision WikiRevision
176 function debugRevisionHandler( &$revision ) {
177 $this->debug( "Got revision:" );
178 if( is_object( $revision->title
) ) {
179 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
181 $this->debug( "-- Title: <invalid>" );
183 $this->debug( "-- User: " . $revision->user_text
);
184 $this->debug( "-- Timestamp: " . $revision->timestamp
);
185 $this->debug( "-- Comment: " . $revision->comment
);
186 $this->debug( "-- Text: " . $revision->text
);
190 * Notify the callback function when a new <page> is reached.
191 * @param $title Title
194 function pageCallback( $title ) {
195 if( is_callable( $this->mPageCallback
) ) {
196 call_user_func( $this->mPageCallback
, $title );
201 * Notify the callback function when a </page> is closed.
202 * @param $title Title
203 * @param $origTitle Title
204 * @param $revisionCount int
205 * @param $successCount Int: number of revisions for which callback returned true
208 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
209 if( is_callable( $this->mPageOutCallback
) ) {
210 call_user_func_array( $this->mPageOutCallback
,
211 array( $title, $origTitle, $revisionCount, $successCount ) );
215 function revisionCallback( $revision ) {
216 if ( is_callable( $this->mRevisionCallback
) ) {
217 return call_user_func_array( $this->mRevisionCallback
,
218 array( $revision, $this ) );
224 function logItemCallback( $revision ) {
225 if ( is_callable( $this->mLogItemCallback
) ) {
226 return call_user_func_array( $this->mLogItemCallback
,
227 array( $revision, $this ) );
234 * Shouldn't something like this be built-in to XMLReader?
235 * Fetches text contents of the current element, assuming
236 * no sub-elements or such scary things.
240 function nodeContents() {
241 if( $this->reader
->isEmptyElement
) {
245 while( $this->reader
->read() ) {
246 switch( $this->reader
->nodeType
) {
247 case XmlReader
::TEXT
:
248 case XmlReader
::SIGNIFICANT_WHITESPACE
:
249 $buffer .= $this->reader
->value
;
251 case XmlReader
::END_ELEMENT
:
255 return $this->close();
260 function dumpElement() {
261 static $lookup = null;
263 $xmlReaderConstants = array(
278 "SIGNIFICANT_WHITESPACE",
285 foreach( $xmlReaderConstants as $name ) {
286 $lookup[constant("XmlReader::$name")] = $name;
291 $lookup[$this->reader
->nodeType
],
297 function doImport() {
298 $this->reader
->read();
300 if ( $this->reader
->name
!= 'mediawiki' ) {
301 throw new MWException( "Expected <mediawiki> tag, got ".
302 $this->reader
->name
);
304 $this->debug( "<mediawiki> tag is correct." );
306 $this->debug( "Starting primary dump processing loop." );
308 $keepReading = $this->reader
->read();
310 while ( $keepReading ) {
311 $tag = $this->reader
->name
;
312 $type = $this->reader
->nodeType
;
314 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this->reader
) ) {
316 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
318 } elseif ( $tag == 'siteinfo' ) {
319 $this->handleSiteInfo();
320 } elseif ( $tag == 'page' ) {
322 } elseif ( $tag == 'logitem' ) {
323 $this->handleLogItem();
324 } elseif ( $tag != '#text' ) {
325 $this->warn( "Unhandled top-level XML tag $tag" );
331 $keepReading = $this->reader
->next();
333 $this->debug( "Skip" );
335 $keepReading = $this->reader
->read();
342 function handleSiteInfo() {
343 // Site info is useful, but not actually used for dump imports.
344 // Includes a quick short-circuit to save performance.
345 if ( ! $this->mSiteInfoCallback
) {
346 $this->reader
->next();
349 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
352 function handleLogItem() {
353 $this->debug( "Enter log item handler." );
356 // Fields that can just be stuffed in the pageInfo object
357 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
358 'logtitle', 'params' );
360 while ( $this->reader
->read() ) {
361 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
362 $this->reader
->name
== 'logitem') {
366 $tag = $this->reader
->name
;
368 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
369 $this->reader
, &$logInfo ) ) {
371 } if ( in_array( $tag, $normalFields ) ) {
372 $logInfo[$tag] = $this->nodeContents();
373 } elseif ( $tag == 'contributor' ) {
374 $logInfo['contributor'] = $this->handleContributor();
375 } elseif ( $tag != '#text' ) {
376 $this->warn( "Unhandled log-item XML tag $tag" );
380 $this->processLogItem( $logInfo );
383 function processLogItem( $logInfo ) {
384 $revision = new WikiRevision
;
386 $revision->setID( $logInfo['id'] );
387 $revision->setType( $logInfo['type'] );
388 $revision->setAction( $logInfo['action'] );
389 $revision->setTimestamp( $logInfo['timestamp'] );
390 $revision->setParams( $logInfo['params'] );
391 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
393 if ( isset( $logInfo['comment'] ) ) {
394 $revision->setComment( $logInfo['comment'] );
397 if ( isset( $logInfo['contributor']['ip'] ) ) {
398 $revision->setUserIP( $logInfo['contributor']['ip'] );
400 if ( isset( $logInfo['contributor']['username'] ) ) {
401 $revision->setUserName( $logInfo['contributor']['username'] );
404 return $this->logItemCallback( $revision );
407 function handlePage() {
409 $this->debug( "Enter page handler." );
410 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
412 // Fields that can just be stuffed in the pageInfo object
413 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
418 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
419 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
420 $this->reader
->name
== 'page') {
424 $tag = $this->reader
->name
;
427 // The title is invalid, bail out of this page
429 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', $this->reader
,
432 } if ( in_array( $tag, $normalFields ) ) {
433 $pageInfo[$tag] = $this->nodeContents();
434 if ( $tag == 'title' ) {
435 $title = $this->processTitle( $pageInfo['title'] );
442 $this->pageCallback( $title );
443 list( $pageInfo['_title'], $origTitle ) = $title;
445 } elseif ( $tag == 'revision' ) {
446 $this->handleRevision( $pageInfo );
447 } elseif ( $tag == 'upload' ) {
448 $this->handleUpload( $pageInfo );
449 } elseif ( $tag != '#text' ) {
450 $this->warn( "Unhandled page XML tag $tag" );
455 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
456 $pageInfo['revisionCount'],
457 $pageInfo['successfulRevisionCount'] );
460 function handleRevision( &$pageInfo ) {
461 $this->debug( "Enter revision handler" );
462 $revisionInfo = array();
464 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
468 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
469 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
470 $this->reader
->name
== 'revision') {
474 $tag = $this->reader
->name
;
476 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this->reader
,
477 &$pageInfo, &$revisionInfo ) ) {
479 } if ( in_array( $tag, $normalFields ) ) {
480 $revisionInfo[$tag] = $this->nodeContents();
481 } elseif ( $tag == 'contributor' ) {
482 $revisionInfo['contributor'] = $this->handleContributor();
483 } elseif ( $tag != '#text' ) {
484 $this->warn( "Unhandled revision XML tag $tag" );
489 $pageInfo['revisionCount']++
;
490 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
491 $pageInfo['successfulRevisionCount']++
;
495 function processRevision( $pageInfo, $revisionInfo ) {
496 $revision = new WikiRevision
;
498 $revision->setID( $revisionInfo['id'] );
499 $revision->setText( $revisionInfo['text'] );
500 $revision->setTitle( $pageInfo['_title'] );
501 $revision->setTimestamp( $revisionInfo['timestamp'] );
503 if ( isset( $revisionInfo['comment'] ) ) {
504 $revision->setComment( $revisionInfo['comment'] );
507 if ( isset( $revisionInfo['minor'] ) )
508 $revision->setMinor( true );
510 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
511 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
513 if ( isset( $revisionInfo['contributor']['username'] ) ) {
514 $revision->setUserName( $revisionInfo['contributor']['username'] );
517 return $this->revisionCallback( $revision );
520 function handleUpload( &$pageInfo ) {
521 $this->debug( "Enter upload handler" );
522 $uploadInfo = array();
524 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
529 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
530 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
531 $this->reader
->name
== 'upload') {
535 $tag = $this->reader
->name
;
537 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this->reader
,
538 &$pageInfo, &$revisionInfo ) ) {
540 } if ( in_array( $tag, $normalFields ) ) {
541 $uploadInfo[$tag] = $this->nodeContents();
542 } elseif ( $tag == 'contributor' ) {
543 $uploadInfo['contributor'] = $this->handleContributor();
544 } elseif ( $tag != '#text' ) {
545 $this->warn( "Unhandled upload XML tag $tag" );
550 return $this->processUpload( $pageInfo, $uploadInfo );
553 function processUpload( $pageInfo, $uploadInfo ) {
554 $revision = new WikiRevision
;
556 $revision->setTitle( $pageInfo['_title'] );
557 $revision->setID( $uploadInfo['id'] );
558 $revision->setTimestamp( $uploadInfo['timestamp'] );
559 $revision->setText( $uploadInfo['text'] );
560 $revision->setFilename( $uploadInfo['filename'] );
561 $revision->setSrc( $uploadInfo['src'] );
562 $revision->setSize( intval( $uploadInfo['size'] ) );
563 $revision->setComment( $uploadInfo['comment'] );
565 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
566 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
568 if ( isset( $uploadInfo['contributor']['username'] ) ) {
569 $revision->setUserName( $revisionInfo['contributor']['username'] );
572 return $this->uploadCallback( $revision );
575 function handleContributor() {
576 $fields = array( 'id', 'ip', 'username' );
579 while ( $this->reader
->read() ) {
580 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
581 $this->reader
->name
== 'contributor') {
585 $tag = $this->reader
->name
;
587 if ( in_array( $tag, $fields ) ) {
588 $info[$tag] = $this->nodeContents();
595 function processTitle( $text ) {
597 $origTitle = Title
::newFromText( $workTitle );
600 if( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
601 $title = Title
::makeTitle( $this->mTargetNamespace
,
602 $origTitle->getDBkey() );
604 $title = Title
::newFromText( $workTitle );
607 if( is_null( $title ) ) {
608 // Invalid page title? Ignore the page
609 $this->notice( "Skipping invalid page title '$workTitle'" );
610 } elseif( $title->getInterwiki() != '' ) {
611 $this->notice( "Skipping interwiki page title '$workTitle'" );
615 return array( $origTitle, $title );
619 class UploadSourceAdapter
{
620 static $sourceRegistrations = array();
626 static function registerSource( $source ) {
627 $id = wfGenerateToken();
629 self
::$sourceRegistrations[$id] = $source;
634 function stream_open( $path, $mode, $options, &$opened_path ) {
635 $url = parse_url($path);
638 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
642 $this->mSource
= self
::$sourceRegistrations[$id];
647 function stream_read( $count ) {
651 while ( !$leave && !$this->mSource
->atEnd() &&
652 count($this->mBuffer
) < $count ) {
653 $read = $this->mSource
->readChunk();
655 if ( !count($read) ) {
659 $this->mBuffer
.= $read;
662 if ( count($this->mBuffer
) ) {
663 $return = substr( $this->mBuffer
, 0, $count );
664 $this->mBuffer
= substr( $this->mBuffer
, $count );
667 $this->mPosition +
= strlen($return);
672 function stream_write( $data ) {
676 function stream_tell() {
677 return $this->mPosition
;
680 function stream_eof() {
681 return $this->mSource
->atEnd();
684 function url_stat() {
687 $result['dev'] = $result[0] = 0;
688 $result['ino'] = $result[1] = 0;
689 $result['mode'] = $result[2] = 0;
690 $result['nlink'] = $result[3] = 0;
691 $result['uid'] = $result[4] = 0;
692 $result['gid'] = $result[5] = 0;
693 $result['rdev'] = $result[6] = 0;
694 $result['size'] = $result[7] = 0;
695 $result['atime'] = $result[8] = 0;
696 $result['mtime'] = $result[9] = 0;
697 $result['ctime'] = $result[10] = 0;
698 $result['blksize'] = $result[11] = 0;
699 $result['blocks'] = $result[12] = 0;