46 $this->reader =
new XMLReader();
48 stream_wrapper_register(
'uploadsource',
'UploadSourceAdapter' );
50 if ( defined(
'LIBXML_PARSEHUGE' ) ) {
51 $this->reader->open(
"uploadsource://$id",
null, LIBXML_PARSEHUGE );
53 $this->reader->open(
"uploadsource://$id" );
64 $this->
debug(
"FAILURE: $err" );
65 wfDebug(
"WikiImporter XML error: $err\n" );
68 private function debug( $data ) {
69 if ( $this->mDebug ) {
74 private function warn( $data ) {
82 if ( is_callable( $this->mNoticeCallback ) ) {
83 call_user_func( $this->mNoticeCallback, $msg,
$params );
102 $this->mNoUpdates = $noupdates;
112 return wfSetVar( $this->mNoticeCallback, $callback );
122 $this->mPageCallback = $callback;
137 $this->mPageOutCallback = $callback;
148 $this->mRevisionCallback = $callback;
159 $this->mUploadCallback = $callback;
170 $this->mLogItemCallback = $callback;
181 $this->mSiteInfoCallback = $callback;
191 if ( is_null( $namespace ) ) {
193 $this->mTargetNamespace =
null;
194 } elseif ( $namespace >= 0 ) {
196 $this->mTargetNamespace = intval( $namespace );
209 if ( is_null( $rootpage ) ) {
211 $this->mTargetRootPage =
null;
212 } elseif ( $rootpage !==
'' ) {
213 $rootpage = rtrim( $rootpage,
'/' );
216 $status->fatal(
'import-rootpage-invalid' );
224 $status->fatal(
'import-rootpage-nosubpage', $displayNSText );
228 $this->mTargetRootPage =
$title->getPrefixedDBkey();
239 $this->mImageBasePath =
$dir;
246 $this->mImportUploads = $import;
255 if ( !$revision->getContent()->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
256 $this->
notice(
'import-error-bad-location',
257 $revision->getTitle()->getPrefixedText(),
259 $revision->getModel(),
260 $revision->getFormat() );
267 return $dbw->deadlockLoop(
array( $revision,
'importOldRevision' ) );
269 $this->
notice(
'import-error-unserialize',
270 $revision->getTitle()->getPrefixedText(),
272 $revision->getModel(),
273 $revision->getFormat() );
286 return $dbw->deadlockLoop(
array(
$rev,
'importLogItem' ) );
296 return $dbw->deadlockLoop(
array( $revision,
'importUpload' ) );
309 $args = func_get_args();
318 $this->
debug(
"Got revision:" );
319 if ( is_object( $revision->title ) ) {
320 $this->
debug(
"-- Title: " . $revision->title->getPrefixedText() );
322 $this->
debug(
"-- Title: <invalid>" );
324 $this->
debug(
"-- User: " . $revision->user_text );
325 $this->
debug(
"-- Timestamp: " . $revision->timestamp );
326 $this->
debug(
"-- Comment: " . $revision->comment );
327 $this->
debug(
"-- Text: " . $revision->text );
335 if ( isset( $this->mPageCallback ) ) {
336 call_user_func( $this->mPageCallback,
$title );
349 if ( isset( $this->mPageOutCallback ) ) {
350 $args = func_get_args();
351 call_user_func_array( $this->mPageOutCallback,
$args );
361 if ( isset( $this->mRevisionCallback ) ) {
362 return call_user_func_array( $this->mRevisionCallback,
363 array( $revision, $this ) );
375 if ( isset( $this->mLogItemCallback ) ) {
376 return call_user_func_array( $this->mLogItemCallback,
377 array( $revision, $this ) );
391 if ( $this->reader->isEmptyElement ) {
395 while ( $this->reader->read() ) {
396 switch ( $this->reader->nodeType ) {
397 case XmlReader::TEXT:
398 case XmlReader::SIGNIFICANT_WHITESPACE:
399 $buffer .= $this->reader->value;
401 case XmlReader::END_ELEMENT:
406 $this->reader->close();
414 static $lookup =
null;
416 $xmlReaderConstants =
array(
431 "SIGNIFICANT_WHITESPACE",
438 foreach ( $xmlReaderConstants
as $name ) {
439 $lookup[constant(
"XmlReader::$name" )] =
$name;
444 $lookup[$this->reader->nodeType],
460 $oldDisable = libxml_disable_entity_loader(
true );
461 $this->reader->read();
463 if ( $this->reader->name !=
'mediawiki' ) {
464 libxml_disable_entity_loader( $oldDisable );
465 throw new MWException(
"Expected <mediawiki> tag, got " .
466 $this->reader->name );
468 $this->
debug(
"<mediawiki> tag is correct." );
470 $this->
debug(
"Starting primary dump processing loop." );
472 $keepReading = $this->reader->read();
474 while ( $keepReading ) {
475 $tag = $this->reader->name;
476 $type = $this->reader->nodeType;
480 } elseif ( $tag ==
'mediawiki' &&
$type == XmlReader::END_ELEMENT ) {
482 } elseif ( $tag ==
'siteinfo' ) {
484 } elseif ( $tag ==
'page' ) {
486 } elseif ( $tag ==
'logitem' ) {
488 } elseif ( $tag !=
'#text' ) {
489 $this->
warn(
"Unhandled top-level XML tag $tag" );
495 $keepReading = $this->reader->next();
497 $this->
debug(
"Skip" );
499 $keepReading = $this->reader->read();
503 libxml_disable_entity_loader( $oldDisable );
514 if ( ! $this->mSiteInfoCallback ) {
515 $this->reader->next();
518 throw new MWException(
"SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
522 $this->
debug(
"Enter log item handler." );
526 $normalFields =
array(
'id',
'comment',
'type',
'action',
'timestamp',
527 'logtitle',
'params' );
529 while ( $this->reader->read() ) {
530 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
531 $this->reader->name ==
'logitem' ) {
535 $tag = $this->reader->name;
541 } elseif ( in_array( $tag, $normalFields ) ) {
543 } elseif ( $tag ==
'contributor' ) {
545 } elseif ( $tag !=
'#text' ) {
546 $this->
warn(
"Unhandled log-item XML tag $tag" );
560 $revision->
setID( $logInfo[
'id'] );
561 $revision->setType( $logInfo[
'type'] );
562 $revision->setAction( $logInfo[
'action'] );
563 $revision->setTimestamp( $logInfo[
'timestamp'] );
564 $revision->setParams( $logInfo[
'params'] );
566 $revision->setNoUpdates( $this->mNoUpdates );
568 if ( isset( $logInfo[
'comment'] ) ) {
569 $revision->setComment( $logInfo[
'comment'] );
572 if ( isset( $logInfo[
'contributor'][
'ip'] ) ) {
573 $revision->setUserIP( $logInfo[
'contributor'][
'ip'] );
575 if ( isset( $logInfo[
'contributor'][
'username'] ) ) {
576 $revision->setUserName( $logInfo[
'contributor'][
'username'] );
584 $this->
debug(
"Enter page handler." );
585 $pageInfo =
array(
'revisionCount' => 0,
'successfulRevisionCount' => 0 );
588 $normalFields =
array(
'title',
'id',
'redirect',
'restrictions' );
593 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
594 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
595 $this->reader->name ==
'page' ) {
599 $tag = $this->reader->name;
607 } elseif ( in_array( $tag, $normalFields ) ) {
609 if ( $tag ==
'title' ) {
618 list( $pageInfo[
'_title'], $origTitle ) =
$title;
620 } elseif ( $tag ==
'revision' ) {
622 } elseif ( $tag ==
'upload' ) {
624 } elseif ( $tag !=
'#text' ) {
625 $this->
warn(
"Unhandled page XML tag $tag" );
631 $pageInfo[
'revisionCount'],
632 $pageInfo[
'successfulRevisionCount'],
640 $this->
debug(
"Enter revision handler" );
641 $revisionInfo =
array();
643 $normalFields =
array(
'id',
'timestamp',
'comment',
'minor',
'model',
'format',
'text' );
647 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
648 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
649 $this->reader->name ==
'revision' ) {
653 $tag = $this->reader->name;
656 $this, $pageInfo, $revisionInfo
659 } elseif ( in_array( $tag, $normalFields ) ) {
661 } elseif ( $tag ==
'contributor' ) {
663 } elseif ( $tag !=
'#text' ) {
664 $this->
warn(
"Unhandled revision XML tag $tag" );
669 $pageInfo[
'revisionCount']++;
671 $pageInfo[
'successfulRevisionCount']++;
683 if ( isset( $revisionInfo[
'id'] ) ) {
684 $revision->
setID( $revisionInfo[
'id'] );
686 if ( isset( $revisionInfo[
'text'] ) ) {
687 $revision->setText( $revisionInfo[
'text'] );
689 if ( isset( $revisionInfo[
'model'] ) ) {
690 $revision->setModel( $revisionInfo[
'model'] );
692 if ( isset( $revisionInfo[
'format'] ) ) {
693 $revision->setFormat( $revisionInfo[
'format'] );
695 $revision->setTitle( $pageInfo[
'_title'] );
697 if ( isset( $revisionInfo[
'timestamp'] ) ) {
698 $revision->setTimestamp( $revisionInfo[
'timestamp'] );
703 if ( isset( $revisionInfo[
'comment'] ) ) {
704 $revision->setComment( $revisionInfo[
'comment'] );
707 if ( isset( $revisionInfo[
'minor'] ) ) {
708 $revision->setMinor(
true );
710 if ( isset( $revisionInfo[
'contributor'][
'ip'] ) ) {
711 $revision->setUserIP( $revisionInfo[
'contributor'][
'ip'] );
713 if ( isset( $revisionInfo[
'contributor'][
'username'] ) ) {
714 $revision->setUserName( $revisionInfo[
'contributor'][
'username'] );
716 $revision->setNoUpdates( $this->mNoUpdates );
726 $this->
debug(
"Enter upload handler" );
727 $uploadInfo =
array();
729 $normalFields =
array(
'timestamp',
'comment',
'filename',
'text',
730 'src',
'size',
'sha1base36',
'archivename',
'rel' );
734 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
735 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
736 $this->reader->name ==
'upload' ) {
740 $tag = $this->reader->name;
746 } elseif ( in_array( $tag, $normalFields ) ) {
748 } elseif ( $tag ==
'contributor' ) {
750 } elseif ( $tag ==
'contents' ) {
752 $encoding = $this->reader->getAttribute(
'encoding' );
753 if ( $encoding ===
'base64' ) {
754 $uploadInfo[
'fileSrc'] = $this->
dumpTemp( base64_decode( $contents ) );
755 $uploadInfo[
'isTempSrc'] =
true;
757 } elseif ( $tag !=
'#text' ) {
758 $this->
warn(
"Unhandled upload XML tag $tag" );
763 if ( $this->mImageBasePath && isset( $uploadInfo[
'rel'] ) ) {
764 $path =
"{$this->mImageBasePath}/{$uploadInfo['rel']}";
765 if ( file_exists(
$path ) ) {
766 $uploadInfo[
'fileSrc'] =
$path;
767 $uploadInfo[
'isTempSrc'] =
false;
771 if ( $this->mImportUploads ) {
781 $filename = tempnam(
wfTempDir(),
'importupload' );
782 file_put_contents( $filename, $contents );
793 $text = isset( $uploadInfo[
'text'] ) ? $uploadInfo[
'text'] :
'';
795 $revision->
setTitle( $pageInfo[
'_title'] );
796 $revision->setID( $pageInfo[
'id'] );
797 $revision->setTimestamp( $uploadInfo[
'timestamp'] );
798 $revision->setText( $text );
799 $revision->setFilename( $uploadInfo[
'filename'] );
800 if ( isset( $uploadInfo[
'archivename'] ) ) {
801 $revision->setArchiveName( $uploadInfo[
'archivename'] );
803 $revision->setSrc( $uploadInfo[
'src'] );
804 if ( isset( $uploadInfo[
'fileSrc'] ) ) {
805 $revision->setFileSrc( $uploadInfo[
'fileSrc'],
806 !empty( $uploadInfo[
'isTempSrc'] ) );
808 if ( isset( $uploadInfo[
'sha1base36'] ) ) {
809 $revision->setSha1Base36( $uploadInfo[
'sha1base36'] );
811 $revision->setSize( intval( $uploadInfo[
'size'] ) );
812 $revision->setComment( $uploadInfo[
'comment'] );
814 if ( isset( $uploadInfo[
'contributor'][
'ip'] ) ) {
815 $revision->setUserIP( $uploadInfo[
'contributor'][
'ip'] );
817 if ( isset( $uploadInfo[
'contributor'][
'username'] ) ) {
818 $revision->setUserName( $uploadInfo[
'contributor'][
'username'] );
820 $revision->setNoUpdates( $this->mNoUpdates );
822 return call_user_func( $this->mUploadCallback, $revision );
829 $fields =
array(
'id',
'ip',
'username' );
832 while ( $this->reader->read() ) {
833 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
834 $this->reader->name ==
'contributor' ) {
838 $tag = $this->reader->name;
840 if ( in_array( $tag, $fields ) ) {
858 if ( !is_null( $this->mTargetNamespace ) && !is_null( $origTitle ) ) {
859 # makeTitleSafe, because $origTitle can have a interwiki (different setting of interwiki map)
860 # and than dbKey can begin with a lowercase char
862 $origTitle->getDBkey() );
864 if ( !is_null( $this->mTargetRootPage ) ) {
865 $workTitle = $this->mTargetRootPage .
'/' . $workTitle;
870 if ( is_null(
$title ) ) {
871 # Invalid page title? Ignore the page
872 $this->
notice(
'import-error-invalid', $workTitle );
874 } elseif (
$title->isExternal() ) {
875 $this->
notice(
'import-error-interwiki',
$title->getPrefixedText() );
877 } elseif ( !
$title->canExist() ) {
878 $this->
notice(
'import-error-special',
$title->getPrefixedText() );
881 # Do not import if the importing wiki user cannot edit this page
882 $this->
notice(
'import-error-edit',
$title->getPrefixedText() );
885 # Do not import if the importing wiki user cannot create this page
886 $this->
notice(
'import-error-create',
$title->getPrefixedText() );
909 self::$sourceRegistrations[$id] =
$source;
922 $url = parse_url(
$path );
925 if ( !isset( self::$sourceRegistrations[$id] ) ) {
929 $this->mSource = self::$sourceRegistrations[$id];
942 while ( !$leave && !$this->mSource->atEnd() &&
943 strlen( $this->mBuffer ) <
$count ) {
944 $read = $this->mSource->readChunk();
946 if ( !strlen( $read ) ) {
950 $this->mBuffer .= $read;
953 if ( strlen( $this->mBuffer ) ) {
954 $return = substr( $this->mBuffer, 0,
$count );
955 $this->mBuffer = substr( $this->mBuffer,
$count );
958 $this->mPosition += strlen( $return );
982 return $this->mSource->atEnd();
1015 if ( $this->isEmptyElement ) {
1019 while ( $this->read() ) {
1020 switch ( $this->nodeType ) {
1021 case XmlReader::TEXT:
1022 case XmlReader::SIGNIFICANT_WHITESPACE:
1025 case XmlReader::END_ELEMENT:
1029 return $this->close();
1069 if ( is_object(
$title ) ) {
1071 } elseif ( is_null(
$title ) ) {
1072 throw new MWException(
"WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
1074 throw new MWException(
"WikiRevision given non-object title in import." );
1089 # 2003-08-05T18:30:02Z
1097 $this->user_text =
$user;
1104 $this->user_text = $ip;
1132 $this->comment =
$text;
1139 $this->minor = (bool)
$minor;
1145 function setSrc( $src ) {
1154 $this->fileSrc = $src;
1169 $this->filename = $filename;
1183 $this->size = intval(
$size );
1211 $this->mNoUpdates = $noupdates;
1257 if ( is_null( $this->
content ) ) {
1274 if ( is_null( $this->model ) ) {
1275 $this->model = $this->
getTitle()->getContentModel();
1285 if ( is_null( $this->model ) ) {
1317 if ( $this->sha1base36 ) {
1341 return $this->filename;
1385 # Sneak a single revision into place
1388 $userId = intval(
$user->getId() );
1389 $userText =
$user->getName();
1394 $userObj =
new User;
1399 $linkCache->clear();
1402 if ( !$page->exists() ) {
1403 # must create the page...
1404 $pageId = $page->insertOn( $dbw );
1406 $oldcountable =
null;
1408 $pageId = $page->getId();
1411 $prior = $dbw->selectField(
'revision',
'1',
1412 array(
'rev_page' => $pageId,
1413 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
1414 'rev_user_text' => $userText,
1420 wfDebug( __METHOD__ .
": skipping existing revision for [[" .
1421 $this->
title->getPrefixedText() .
"]], timestamp " . $this->timestamp .
"\n" );
1424 $oldcountable = $page->isCountable();
1427 # @todo FIXME: Use original rev_id optionally (better for backups)
1430 'title' => $this->
title,
1432 'content_model' => $this->
getModel(),
1437 'user_text' => $userText,
1438 'timestamp' => $this->timestamp,
1439 'minor_edit' => $this->minor,
1441 $revision->insertOn( $dbw );
1442 $changed = $page->updateIfNewerOn( $dbw, $revision );
1444 if (
$changed !==
false && !$this->mNoUpdates ) {
1445 wfDebug( __METHOD__ .
": running updates\n" );
1446 $page->doEditUpdates( $revision, $userObj,
array(
'created' => $created,
'oldcountable' => $oldcountable ) );
1457 # @todo FIXME: This will not record autoblocks
1459 wfDebug( __METHOD__ .
": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1460 $this->timestamp .
"\n" );
1463 # Check if it exists already
1465 $prior = $dbw->selectField(
'logging',
'1',
1468 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1469 'log_namespace' => $this->
getTitle()->getNamespace(),
1470 'log_title' => $this->
getTitle()->getDBkey(),
1472 #'log_user_text' => $this->user_text,
1478 wfDebug( __METHOD__ .
": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
1479 $this->timestamp .
"\n" );
1482 $log_id = $dbw->nextSequenceValue(
'logging_log_id_seq' );
1484 'log_id' => $log_id,
1485 'log_type' => $this->
type,
1486 'log_action' => $this->
action,
1487 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1489 #'log_user_text' => $this->user_text,
1490 'log_namespace' => $this->
getTitle()->getNamespace(),
1491 'log_title' => $this->
getTitle()->getDBkey(),
1495 $dbw->insert(
'logging', $data, __METHOD__ );
1504 if ( $archiveName ) {
1505 wfDebug( __METHOD__ .
"Importing archived file as $archiveName\n" );
1510 wfDebug( __METHOD__ .
'Importing new file as ' .
$file->getName() .
"\n" );
1512 $archiveName =
$file->getTimestamp() .
'!' .
$file->getName();
1515 wfDebug( __METHOD__ .
"File already exists; importing as $archiveName\n" );
1523 # Get the file source or download if necessary
1531 wfDebug( __METHOD__ .
": Could not fetch remote file.\n" );
1535 if ( $sha1 && ( $sha1 !== sha1_file(
$source ) ) ) {
1537 # Broken file; delete it if it is a temporary file
1540 wfDebug( __METHOD__ .
": Corrupt file $source.\n" );
1546 # Do the actual upload
1555 if ( $status->isGood() ) {
1556 wfDebug( __METHOD__ .
": Successful\n" );
1559 wfDebug( __METHOD__ .
': failed: ' . $status->getXml() .
"\n" );
1569 if ( !$wgEnableUploads ) {
1573 $tempo = tempnam(
wfTempDir(),
'download' );
1574 $f = fopen( $tempo,
'wb' );
1576 wfDebug(
"IMPORT: couldn't write to temp file $tempo\n" );
1584 wfDebug(
"IMPORT: couldn't fetch source $src\n" );
1590 fwrite(
$f, $data );
1604 $this->mString = $string;
1605 $this->mRead =
false;
1612 return $this->mRead;
1619 if ( $this->
atEnd() ) {
1622 $this->mRead =
true;
1623 return $this->mString;
1633 $this->mHandle = $handle;
1640 return feof( $this->mHandle );
1647 return fread( $this->mHandle, 32768 );
1656 $file = fopen( $filename,
'rt' );
1669 $upload =& $_FILES[$fieldname];
1674 if ( !empty(
$upload[
'error'] ) ) {
1676 case 1: # The uploaded
file exceeds the upload_max_filesize directive
in php.ini.
1678 case 2: # The uploaded
file exceeds the MAX_FILE_SIZE directive
that was specified
in the HTML
form.
1680 case 3: # The uploaded
file was
only partially uploaded
1682 case 6: #Missing a temporary folder.
1684 # case else: # Currently impossible
1689 if ( is_uploaded_file(
$fname ) ) {
1701 static function newFromURL( $url, $method =
'GET' ) {
1702 wfDebug( __METHOD__ .
": opening $url\n" );
1703 # Use the standard HTTP fetch function; it times out
1704 # quicker and sorts out user-agent problems which might
1705 # otherwise prevent importing from large sites, such
1706 # as the Wikimedia cluster, etc.
1708 if ( $data !==
false ) {
1710 fwrite(
$file, $data );
1727 public static function newFromInterwiki( $interwiki, $page, $history =
false, $templates =
false, $pageLinkDepth = 0 ) {
1728 if ( $page ==
'' ) {
1732 if ( is_null(
$link ) || !
$link->isExternal() ) {
1742 if ( $pageLinkDepth ) {
1743 $params[
'pagelink-depth'] = $pageLinkDepth;
1746 # For interwikis, use POST to avoid redirects.