3 * MediaWiki page data importer
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * http://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
24 * @ingroup SpecialPage
28 * XML file reader for the page data importer
30 * implements Special:Import
31 * @ingroup SpecialPage
34 private $reader = null;
35 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
36 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
40 * Creates an ImportXMLReader drawing from the source provided
42 function __construct( $source ) {
43 $this->reader
= new XMLReader();
45 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
46 $id = UploadSourceAdapter
::registerSource( $source );
47 $this->reader
->open( "uploadsource://$id" );
50 $this->setRevisionCallback( array( $this, "importRevision" ) );
51 $this->setUploadCallback( array( $this, 'importUpload' ) );
52 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
53 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
56 private function throwXmlError( $err ) {
57 $this->debug( "FAILURE: $err" );
58 wfDebug( "WikiImporter XML error: $err\n" );
61 private function debug( $data ) {
63 wfDebug( "IMPORT: $data\n" );
67 private function warn( $data ) {
68 wfDebug( "IMPORT: $data\n" );
71 private function notice( $data ) {
72 global $wgCommandLineMode;
73 if( $wgCommandLineMode ) {
77 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
84 function setDebug( $debug ) {
85 $this->mDebug
= $debug;
89 * Sets the action to perform as each new page in the stream is reached.
90 * @param $callback callback
93 public function setPageCallback( $callback ) {
94 $previous = $this->mPageCallback
;
95 $this->mPageCallback
= $callback;
100 * Sets the action to perform as each page in the stream is completed.
101 * Callback accepts the page title (as a Title object), a second object
102 * with the original title form (in case it's been overridden into a
103 * local namespace), and a count of revisions.
105 * @param $callback callback
108 public function setPageOutCallback( $callback ) {
109 $previous = $this->mPageOutCallback
;
110 $this->mPageOutCallback
= $callback;
115 * Sets the action to perform as each page revision is reached.
116 * @param $callback callback
119 public function setRevisionCallback( $callback ) {
120 $previous = $this->mRevisionCallback
;
121 $this->mRevisionCallback
= $callback;
126 * Sets the action to perform as each file upload version is reached.
127 * @param $callback callback
130 public function setUploadCallback( $callback ) {
131 $previous = $this->mUploadCallback
;
132 $this->mUploadCallback
= $callback;
137 * Sets the action to perform as each log item reached.
138 * @param $callback callback
141 public function setLogItemCallback( $callback ) {
142 $previous = $this->mLogItemCallback
;
143 $this->mLogItemCallback
= $callback;
148 * Sets the action to perform when site info is encountered
149 * @param $callback callback
152 public function setSiteInfoCallback( $callback ) {
153 $previous = $this->mSiteInfoCallback
;
154 $this->mSiteInfoCallback
= $callback;
159 * Set a target namespace to override the defaults
161 public function setTargetNamespace( $namespace ) {
162 if( is_null( $namespace ) ) {
163 // Don't override namespaces
164 $this->mTargetNamespace
= null;
165 } elseif( $namespace >= 0 ) {
166 // FIXME: Check for validity
167 $this->mTargetNamespace
= intval( $namespace );
174 * Default per-revision callback, performs the import.
175 * @param $revision WikiRevision
177 public function importRevision( $revision ) {
178 $dbw = wfGetDB( DB_MASTER
);
179 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
183 * Default per-revision callback, performs the import.
184 * @param $rev WikiRevision
186 public function importLogItem( $rev ) {
187 $dbw = wfGetDB( DB_MASTER
);
188 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
194 public function importUpload( $revision ) {
195 $revision->importUpload();
196 //$dbw = wfGetDB( DB_MASTER );
197 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
202 * Mostly for hook use
204 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
205 $args = func_get_args();
206 return wfRunHooks( 'AfterImportPage', $args );
210 * Alternate per-revision callback, for debugging.
211 * @param $revision WikiRevision
213 public function debugRevisionHandler( &$revision ) {
214 $this->debug( "Got revision:" );
215 if( is_object( $revision->title
) ) {
216 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
218 $this->debug( "-- Title: <invalid>" );
220 $this->debug( "-- User: " . $revision->user_text
);
221 $this->debug( "-- Timestamp: " . $revision->timestamp
);
222 $this->debug( "-- Comment: " . $revision->comment
);
223 $this->debug( "-- Text: " . $revision->text
);
227 * Notify the callback function when a new <page> is reached.
228 * @param $title Title
230 function pageCallback( $title ) {
231 if( isset( $this->mPageCallback
) ) {
232 call_user_func( $this->mPageCallback
, $title );
237 * Notify the callback function when a </page> is closed.
238 * @param $title Title
239 * @param $origTitle Title
240 * @param $revCount Integer
241 * @param $sucCount Int: number of revisions for which callback returned true
242 * @param $pageInfo Array: associative array of page information
244 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
245 if( isset( $this->mPageOutCallback
) ) {
246 $args = func_get_args();
247 call_user_func_array( $this->mPageOutCallback
, $args );
252 * Notify the callback function of a revision
253 * @param $revision A WikiRevision object
255 private function revisionCallback( $revision ) {
256 if ( isset( $this->mRevisionCallback
) ) {
257 return call_user_func_array( $this->mRevisionCallback
,
258 array( $revision, $this ) );
265 * Notify the callback function of a new log item
266 * @param $revision A WikiRevision object
268 private function logItemCallback( $revision ) {
269 if ( isset( $this->mLogItemCallback
) ) {
270 return call_user_func_array( $this->mLogItemCallback
,
271 array( $revision, $this ) );
278 * Shouldn't something like this be built-in to XMLReader?
279 * Fetches text contents of the current element, assuming
280 * no sub-elements or such scary things.
284 private function nodeContents() {
285 if( $this->reader
->isEmptyElement
) {
289 while( $this->reader
->read() ) {
290 switch( $this->reader
->nodeType
) {
291 case XmlReader
::TEXT
:
292 case XmlReader
::SIGNIFICANT_WHITESPACE
:
293 $buffer .= $this->reader
->value
;
295 case XmlReader
::END_ELEMENT
:
300 $this->reader
->close();
306 /** Left in for debugging */
307 private function dumpElement() {
308 static $lookup = null;
310 $xmlReaderConstants = array(
325 "SIGNIFICANT_WHITESPACE",
332 foreach( $xmlReaderConstants as $name ) {
333 $lookup[constant("XmlReader::$name")] = $name;
338 $lookup[$this->reader
->nodeType
],
345 * Primary entry point
347 public function doImport() {
348 $this->reader
->read();
350 if ( $this->reader
->name
!= 'mediawiki' ) {
351 throw new MWException( "Expected <mediawiki> tag, got ".
352 $this->reader
->name
);
354 $this->debug( "<mediawiki> tag is correct." );
356 $this->debug( "Starting primary dump processing loop." );
358 $keepReading = $this->reader
->read();
360 while ( $keepReading ) {
361 $tag = $this->reader
->name
;
362 $type = $this->reader
->nodeType
;
364 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this ) ) {
366 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
368 } elseif ( $tag == 'siteinfo' ) {
369 $this->handleSiteInfo();
370 } elseif ( $tag == 'page' ) {
372 } elseif ( $tag == 'logitem' ) {
373 $this->handleLogItem();
374 } elseif ( $tag != '#text' ) {
375 $this->warn( "Unhandled top-level XML tag $tag" );
381 $keepReading = $this->reader
->next();
383 $this->debug( "Skip" );
385 $keepReading = $this->reader
->read();
392 private function handleSiteInfo() {
393 // Site info is useful, but not actually used for dump imports.
394 // Includes a quick short-circuit to save performance.
395 if ( ! $this->mSiteInfoCallback
) {
396 $this->reader
->next();
399 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
402 private function handleLogItem() {
403 $this->debug( "Enter log item handler." );
406 // Fields that can just be stuffed in the pageInfo object
407 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
408 'logtitle', 'params' );
410 while ( $this->reader
->read() ) {
411 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
412 $this->reader
->name
== 'logitem') {
416 $tag = $this->reader
->name
;
418 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
419 $this, $logInfo ) ) {
421 } elseif ( in_array( $tag, $normalFields ) ) {
422 $logInfo[$tag] = $this->nodeContents();
423 } elseif ( $tag == 'contributor' ) {
424 $logInfo['contributor'] = $this->handleContributor();
425 } elseif ( $tag != '#text' ) {
426 $this->warn( "Unhandled log-item XML tag $tag" );
430 $this->processLogItem( $logInfo );
433 private function processLogItem( $logInfo ) {
434 $revision = new WikiRevision
;
436 $revision->setID( $logInfo['id'] );
437 $revision->setType( $logInfo['type'] );
438 $revision->setAction( $logInfo['action'] );
439 $revision->setTimestamp( $logInfo['timestamp'] );
440 $revision->setParams( $logInfo['params'] );
441 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
443 if ( isset( $logInfo['comment'] ) ) {
444 $revision->setComment( $logInfo['comment'] );
447 if ( isset( $logInfo['contributor']['ip'] ) ) {
448 $revision->setUserIP( $logInfo['contributor']['ip'] );
450 if ( isset( $logInfo['contributor']['username'] ) ) {
451 $revision->setUserName( $logInfo['contributor']['username'] );
454 return $this->logItemCallback( $revision );
457 private function handlePage() {
459 $this->debug( "Enter page handler." );
460 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
462 // Fields that can just be stuffed in the pageInfo object
463 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
468 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
469 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
470 $this->reader
->name
== 'page') {
474 $tag = $this->reader
->name
;
477 // The title is invalid, bail out of this page
479 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this,
482 } elseif ( in_array( $tag, $normalFields ) ) {
483 $pageInfo[$tag] = $this->nodeContents();
484 if ( $tag == 'title' ) {
485 $title = $this->processTitle( $pageInfo['title'] );
492 $this->pageCallback( $title );
493 list( $pageInfo['_title'], $origTitle ) = $title;
495 } elseif ( $tag == 'revision' ) {
496 $this->handleRevision( $pageInfo );
497 } elseif ( $tag == 'upload' ) {
498 $this->handleUpload( $pageInfo );
499 } elseif ( $tag != '#text' ) {
500 $this->warn( "Unhandled page XML tag $tag" );
505 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
506 $pageInfo['revisionCount'],
507 $pageInfo['successfulRevisionCount'],
511 private function handleRevision( &$pageInfo ) {
512 $this->debug( "Enter revision handler" );
513 $revisionInfo = array();
515 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
519 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
520 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
521 $this->reader
->name
== 'revision') {
525 $tag = $this->reader
->name
;
527 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this,
528 $pageInfo, $revisionInfo ) ) {
530 } elseif ( in_array( $tag, $normalFields ) ) {
531 $revisionInfo[$tag] = $this->nodeContents();
532 } elseif ( $tag == 'contributor' ) {
533 $revisionInfo['contributor'] = $this->handleContributor();
534 } elseif ( $tag != '#text' ) {
535 $this->warn( "Unhandled revision XML tag $tag" );
540 $pageInfo['revisionCount']++
;
541 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
542 $pageInfo['successfulRevisionCount']++
;
546 private function processRevision( $pageInfo, $revisionInfo ) {
547 $revision = new WikiRevision
;
549 if( isset( $revisionInfo['id'] ) ) {
550 $revision->setID( $revisionInfo['id'] );
552 if ( isset( $revisionInfo['text'] ) ) {
553 $revision->setText( $revisionInfo['text'] );
555 $revision->setTitle( $pageInfo['_title'] );
557 if ( isset( $revisionInfo['timestamp'] ) ) {
558 $revision->setTimestamp( $revisionInfo['timestamp'] );
560 $revision->setTimestamp( wfTimestampNow() );
563 if ( isset( $revisionInfo['comment'] ) ) {
564 $revision->setComment( $revisionInfo['comment'] );
567 if ( isset( $revisionInfo['minor'] ) ) {
568 $revision->setMinor( true );
570 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
571 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
573 if ( isset( $revisionInfo['contributor']['username'] ) ) {
574 $revision->setUserName( $revisionInfo['contributor']['username'] );
577 return $this->revisionCallback( $revision );
580 private function handleUpload( &$pageInfo ) {
581 $this->debug( "Enter upload handler" );
582 $uploadInfo = array();
584 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
589 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
590 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
591 $this->reader
->name
== 'upload') {
595 $tag = $this->reader
->name
;
597 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this,
600 } elseif ( in_array( $tag, $normalFields ) ) {
601 $uploadInfo[$tag] = $this->nodeContents();
602 } elseif ( $tag == 'contributor' ) {
603 $uploadInfo['contributor'] = $this->handleContributor();
604 } elseif ( $tag != '#text' ) {
605 $this->warn( "Unhandled upload XML tag $tag" );
610 return $this->processUpload( $pageInfo, $uploadInfo );
614 private function processUpload( $pageInfo, $uploadInfo ) {
615 $revision = new WikiRevision
;
616 $text = isset( $uploadInfo['text'] ) ?
$uploadInfo['text'] : '';
618 $revision->setTitle( $pageInfo['_title'] );
619 $revision->setID( $pageInfo['id'] );
620 $revision->setTimestamp( $uploadInfo['timestamp'] );
621 $revision->setText( $text );
622 $revision->setFilename( $uploadInfo['filename'] );
623 $revision->setSrc( $uploadInfo['src'] );
624 $revision->setSize( intval( $uploadInfo['size'] ) );
625 $revision->setComment( $uploadInfo['comment'] );
627 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
628 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
630 if ( isset( $uploadInfo['contributor']['username'] ) ) {
631 $revision->setUserName( $uploadInfo['contributor']['username'] );
634 return call_user_func( $this->mUploadCallback
, $revision );
637 private function handleContributor() {
638 $fields = array( 'id', 'ip', 'username' );
641 while ( $this->reader
->read() ) {
642 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
643 $this->reader
->name
== 'contributor') {
647 $tag = $this->reader
->name
;
649 if ( in_array( $tag, $fields ) ) {
650 $info[$tag] = $this->nodeContents();
657 private function processTitle( $text ) {
659 $origTitle = Title
::newFromText( $workTitle );
661 if( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
662 $title = Title
::makeTitle( $this->mTargetNamespace
,
663 $origTitle->getDBkey() );
665 $title = Title
::newFromText( $workTitle );
668 if( is_null( $title ) ) {
669 // Invalid page title? Ignore the page
670 $this->notice( "Skipping invalid page title '$workTitle'" );
672 } elseif( $title->getInterwiki() != '' ) {
673 $this->notice( "Skipping interwiki page title '$workTitle'" );
677 return array( $title, $origTitle );
681 /** This is a horrible hack used to keep source compatibility */
682 class UploadSourceAdapter
{
683 static $sourceRegistrations = array();
689 static function registerSource( $source ) {
690 $id = wfGenerateToken();
692 self
::$sourceRegistrations[$id] = $source;
697 function stream_open( $path, $mode, $options, &$opened_path ) {
698 $url = parse_url($path);
701 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
705 $this->mSource
= self
::$sourceRegistrations[$id];
710 function stream_read( $count ) {
714 while ( !$leave && !$this->mSource
->atEnd() &&
715 strlen($this->mBuffer
) < $count ) {
716 $read = $this->mSource
->readChunk();
718 if ( !strlen($read) ) {
722 $this->mBuffer
.= $read;
725 if ( strlen($this->mBuffer
) ) {
726 $return = substr( $this->mBuffer
, 0, $count );
727 $this->mBuffer
= substr( $this->mBuffer
, $count );
730 $this->mPosition +
= strlen($return);
735 function stream_write( $data ) {
739 function stream_tell() {
740 return $this->mPosition
;
743 function stream_eof() {
744 return $this->mSource
->atEnd();
747 function url_stat() {
750 $result['dev'] = $result[0] = 0;
751 $result['ino'] = $result[1] = 0;
752 $result['mode'] = $result[2] = 0;
753 $result['nlink'] = $result[3] = 0;
754 $result['uid'] = $result[4] = 0;
755 $result['gid'] = $result[5] = 0;
756 $result['rdev'] = $result[6] = 0;
757 $result['size'] = $result[7] = 0;
758 $result['atime'] = $result[8] = 0;
759 $result['mtime'] = $result[9] = 0;
760 $result['ctime'] = $result[10] = 0;
761 $result['blksize'] = $result[11] = 0;
762 $result['blocks'] = $result[12] = 0;
768 class XMLReader2
extends XMLReader
{
769 function nodeContents() {
770 if( $this->isEmptyElement
) {
774 while( $this->read() ) {
775 switch( $this->nodeType
) {
776 case XmlReader
::TEXT
:
777 case XmlReader
::SIGNIFICANT_WHITESPACE
:
778 $buffer .= $this->value
;
780 case XmlReader
::END_ELEMENT
:
784 return $this->close();
789 * @todo document (e.g. one-sentence class description).
790 * @ingroup SpecialPage
795 var $timestamp = "20010115000000";
805 function setTitle( $title ) {
806 if( is_object( $title ) ) {
807 $this->title
= $title;
808 } elseif( is_null( $title ) ) {
809 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
811 throw new MWException( "WikiRevision given non-object title in import." );
815 function setID( $id ) {
819 function setTimestamp( $ts ) {
820 # 2003-08-05T18:30:02Z
821 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
824 function setUsername( $user ) {
825 $this->user_text
= $user;
828 function setUserIP( $ip ) {
829 $this->user_text
= $ip;
832 function setText( $text ) {
836 function setComment( $text ) {
837 $this->comment
= $text;
840 function setMinor( $minor ) {
841 $this->minor
= (bool)$minor;
844 function setSrc( $src ) {
848 function setFilename( $filename ) {
849 $this->filename
= $filename;
852 function setSize( $size ) {
853 $this->size
= intval( $size );
856 function setType( $type ) {
860 function setAction( $action ) {
861 $this->action
= $action;
864 function setParams( $params ) {
865 $this->params
= $params;
868 function getTitle() {
876 function getTimestamp() {
877 return $this->timestamp
;
881 return $this->user_text
;
888 function getComment() {
889 return $this->comment
;
892 function getMinor() {
900 function getFilename() {
901 return $this->filename
;
912 function getAction() {
913 return $this->action
;
916 function getParams() {
917 return $this->params
;
920 function importOldRevision() {
921 $dbw = wfGetDB( DB_MASTER
);
923 # Sneak a single revision into place
924 $user = User
::newFromName( $this->getUser() );
926 $userId = intval( $user->getId() );
927 $userText = $user->getName();
930 $userText = $this->getUser();
933 // avoid memory leak...?
934 $linkCache = LinkCache
::singleton();
937 $article = new Article( $this->title
);
938 $pageId = $article->getId();
940 # must create the page...
941 $pageId = $article->insertOn( $dbw );
946 $prior = $dbw->selectField( 'revision', '1',
947 array( 'rev_page' => $pageId,
948 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
949 'rev_user_text' => $userText,
950 'rev_comment' => $this->getComment() ),
954 // FIXME: this could fail slightly for multiple matches :P
955 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
956 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
961 # FIXME: Use original rev_id optionally (better for backups)
963 $revision = new Revision( array(
965 'text' => $this->getText(),
966 'comment' => $this->getComment(),
968 'user_text' => $userText,
969 'timestamp' => $this->timestamp
,
970 'minor_edit' => $this->minor
,
972 $revId = $revision->insertOn( $dbw );
973 $changed = $article->updateIfNewerOn( $dbw, $revision );
975 # To be on the safe side...
976 $tempTitle = $GLOBALS['wgTitle'];
977 $GLOBALS['wgTitle'] = $this->title
;
980 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
981 Article
::onArticleCreate( $this->title
);
983 wfDebug( __METHOD__
. ": running create updates\n" );
984 $article->createUpdates( $revision );
986 } elseif( $changed ) {
987 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
988 Article
::onArticleEdit( $this->title
);
990 wfDebug( __METHOD__
. ": running edit updates\n" );
991 $article->editUpdates(
998 $GLOBALS['wgTitle'] = $tempTitle;
1003 function importLogItem() {
1004 $dbw = wfGetDB( DB_MASTER
);
1005 # FIXME: this will not record autoblocks
1006 if( !$this->getTitle() ) {
1007 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1008 $this->timestamp
. "\n" );
1011 # Check if it exists already
1012 // FIXME: use original log ID (better for backups)
1013 $prior = $dbw->selectField( 'logging', '1',
1014 array( 'log_type' => $this->getType(),
1015 'log_action' => $this->getAction(),
1016 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1017 'log_namespace' => $this->getTitle()->getNamespace(),
1018 'log_title' => $this->getTitle()->getDBkey(),
1019 'log_comment' => $this->getComment(),
1020 #'log_user_text' => $this->user_text,
1021 'log_params' => $this->params
),
1024 // FIXME: this could fail slightly for multiple matches :P
1026 wfDebug( __METHOD__
. ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
1027 $this->timestamp
. "\n" );
1030 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1032 'log_id' => $log_id,
1033 'log_type' => $this->type
,
1034 'log_action' => $this->action
,
1035 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1036 'log_user' => User
::idFromName( $this->user_text
),
1037 #'log_user_text' => $this->user_text,
1038 'log_namespace' => $this->getTitle()->getNamespace(),
1039 'log_title' => $this->getTitle()->getDBkey(),
1040 'log_comment' => $this->getComment(),
1041 'log_params' => $this->params
1043 $dbw->insert( 'logging', $data, __METHOD__
);
1046 function importUpload() {
1047 wfDebug( __METHOD__
. ": STUB\n" );
1050 // from file revert...
1051 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
1052 $comment = $wgRequest->getText( 'wpComment' );
1053 // TODO: Preserve file properties from database instead of reloading from file
1054 $status = $this->file->upload( $source, $comment, $comment );
1055 if( $status->isGood() ) {
1059 // from file upload...
1060 $this->mLocalFile = wfLocalFile( $nt );
1061 $this->mDestName = $this->mLocalFile->getName();
1063 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
1064 File::DELETE_SOURCE, $this->mFileProps );
1065 if ( !$status->isGood() ) {
1066 $resultDetails = array( 'internal' => $status->getWikiText() );
1069 // @todo Fixme: it may create a page without our desire, also wrong potentially.
1070 // and, it will record a *current* upload, but we might want an archive version here
1072 $file = wfLocalFile( $this->getTitle() );
1074 wfDebug( "IMPORT: Bad file. :(\n" );
1078 $source = $this->downloadSource();
1080 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
1084 $user = User
::newFromName( $this->user_text
);
1086 $status = $file->upload( $source,
1087 $this->getComment(),
1088 $this->getComment(), // Initial page, if none present...
1089 File
::DELETE_SOURCE
,
1091 $this->getTimestamp(),
1092 is_object( $user ) ?
( $user->isLoggedIn() ?
$user : null ) : null );
1094 if( $status->isGood() ) {
1096 wfDebug( "IMPORT: is ok?\n" );
1100 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
1105 function downloadSource() {
1106 global $wgEnableUploads;
1107 if( !$wgEnableUploads ) {
1111 $tempo = tempnam( wfTempDir(), 'download' );
1112 $f = fopen( $tempo, 'wb' );
1114 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1119 $src = $this->getSrc();
1120 $data = Http
::get( $src );
1122 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1128 fwrite( $f, $data );
1137 * @todo document (e.g. one-sentence class description).
1138 * @ingroup SpecialPage
1140 class ImportStringSource
{
1141 function __construct( $string ) {
1142 $this->mString
= $string;
1143 $this->mRead
= false;
1147 return $this->mRead
;
1150 function readChunk() {
1151 if( $this->atEnd() ) {
1154 $this->mRead
= true;
1155 return $this->mString
;
1161 * @todo document (e.g. one-sentence class description).
1162 * @ingroup SpecialPage
1164 class ImportStreamSource
{
1165 function __construct( $handle ) {
1166 $this->mHandle
= $handle;
1170 return feof( $this->mHandle
);
1173 function readChunk() {
1174 return fread( $this->mHandle
, 32768 );
1177 static function newFromFile( $filename ) {
1178 $file = @fopen
( $filename, 'rt' );
1180 return Status
::newFatal( "importcantopen" );
1182 return Status
::newGood( new ImportStreamSource( $file ) );
1185 static function newFromUpload( $fieldname = "xmlimport" ) {
1186 $upload =& $_FILES[$fieldname];
1188 if( !isset( $upload ) ||
!$upload['name'] ) {
1189 return Status
::newFatal( 'importnofile' );
1191 if( !empty( $upload['error'] ) ) {
1192 switch($upload['error']){
1193 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1194 return Status
::newFatal( 'importuploaderrorsize' );
1195 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1196 return Status
::newFatal( 'importuploaderrorsize' );
1197 case 3: # The uploaded file was only partially uploaded
1198 return Status
::newFatal( 'importuploaderrorpartial' );
1199 case 6: #Missing a temporary folder.
1200 return Status
::newFatal( 'importuploaderrortemp' );
1201 # case else: # Currently impossible
1205 $fname = $upload['tmp_name'];
1206 if( is_uploaded_file( $fname ) ) {
1207 return ImportStreamSource
::newFromFile( $fname );
1209 return Status
::newFatal( 'importnofile' );
1213 static function newFromURL( $url, $method = 'GET' ) {
1214 wfDebug( __METHOD__
. ": opening $url\n" );
1215 # Use the standard HTTP fetch function; it times out
1216 # quicker and sorts out user-agent problems which might
1217 # otherwise prevent importing from large sites, such
1218 # as the Wikimedia cluster, etc.
1219 $data = Http
::request( $method, $url );
1220 if( $data !== false ) {
1222 fwrite( $file, $data );
1225 return Status
::newGood( new ImportStreamSource( $file ) );
1227 return Status
::newFatal( 'importcantopen' );
1231 public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
1233 return Status
::newFatal( 'import-noarticle' );
1235 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1236 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1237 return Status
::newFatal( 'importbadinterwiki' );
1240 if ( $history ) $params['history'] = 1;
1241 if ( $templates ) $params['templates'] = 1;
1242 if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
1243 $url = $link->getFullUrl( $params );
1244 # For interwikis, use POST to avoid redirects.
1245 return ImportStreamSource
::newFromURL( $url, "POST" );