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 XMLReader2();
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 //$dbw = wfGetDB( DB_MASTER );
196 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
201 * Mostly for hook use
203 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
204 $args = func_get_args();
205 return wfRunHooks( 'AfterImportPage', $args );
209 * Alternate per-revision callback, for debugging.
210 * @param $revision WikiRevision
212 public function debugRevisionHandler( &$revision ) {
213 $this->debug( "Got revision:" );
214 if( is_object( $revision->title
) ) {
215 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
217 $this->debug( "-- Title: <invalid>" );
219 $this->debug( "-- User: " . $revision->user_text
);
220 $this->debug( "-- Timestamp: " . $revision->timestamp
);
221 $this->debug( "-- Comment: " . $revision->comment
);
222 $this->debug( "-- Text: " . $revision->text
);
226 * Notify the callback function when a new <page> is reached.
227 * @param $title Title
229 function pageCallback( $title ) {
230 if( isset( $this->mPageCallback
) ) {
231 call_user_func( $this->mPageCallback
, $title );
236 * Notify the callback function when a </page> is closed.
237 * @param $title Title
238 * @param $origTitle Title
239 * @param $revCount Integer
240 * @param $sucCount Int: number of revisions for which callback returned true
241 * @param $pageInfo Array: associative array of page information
243 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
244 if( isset( $this->mPageOutCallback
) ) {
245 $args = func_get_args();
246 call_user_func_array( $this->mPageOutCallback
, $args );
251 * Notify the callback function of a revision
252 * @param $revision A WikiRevision object
254 private function revisionCallback( $revision ) {
255 if ( isset( $this->mRevisionCallback
) ) {
256 return call_user_func_array( $this->mRevisionCallback
,
257 array( $revision, $this ) );
264 * Notify the callback function of a new log item
265 * @param $revision A WikiRevision object
267 private function logItemCallback( $revision ) {
268 if ( isset( $this->mLogItemCallback
) ) {
269 return call_user_func_array( $this->mLogItemCallback
,
270 array( $revision, $this ) );
277 * Shouldn't something like this be built-in to XMLReader?
278 * Fetches text contents of the current element, assuming
279 * no sub-elements or such scary things.
283 private function nodeContents() {
284 return $this->reader
->nodeContents();
289 /** Left in for debugging */
290 private function dumpElement() {
291 static $lookup = null;
293 $xmlReaderConstants = array(
308 "SIGNIFICANT_WHITESPACE",
315 foreach( $xmlReaderConstants as $name ) {
316 $lookup[constant("XmlReader::$name")] = $name;
321 $lookup[$this->reader
->nodeType
],
328 * Primary entry point
330 public function doImport() {
331 $this->reader
->read();
333 if ( $this->reader
->name
!= 'mediawiki' ) {
334 throw new MWException( "Expected <mediawiki> tag, got ".
335 $this->reader
->name
);
337 $this->debug( "<mediawiki> tag is correct." );
339 $this->debug( "Starting primary dump processing loop." );
341 $keepReading = $this->reader
->read();
343 while ( $keepReading ) {
344 $tag = $this->reader
->name
;
345 $type = $this->reader
->nodeType
;
347 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this->reader
) ) {
349 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
351 } elseif ( $tag == 'siteinfo' ) {
352 $this->handleSiteInfo();
353 } elseif ( $tag == 'page' ) {
355 } elseif ( $tag == 'logitem' ) {
356 $this->handleLogItem();
357 } elseif ( $tag != '#text' ) {
358 $this->warn( "Unhandled top-level XML tag $tag" );
364 $keepReading = $this->reader
->next();
366 $this->debug( "Skip" );
368 $keepReading = $this->reader
->read();
375 private function handleSiteInfo() {
376 // Site info is useful, but not actually used for dump imports.
377 // Includes a quick short-circuit to save performance.
378 if ( ! $this->mSiteInfoCallback
) {
379 $this->reader
->next();
382 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
385 private function handleLogItem() {
386 $this->debug( "Enter log item handler." );
389 // Fields that can just be stuffed in the pageInfo object
390 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
391 'logtitle', 'params' );
393 while ( $this->reader
->read() ) {
394 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
395 $this->reader
->name
== 'logitem') {
399 $tag = $this->reader
->name
;
401 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
402 $this->reader
, $logInfo ) ) {
404 } elseif ( in_array( $tag, $normalFields ) ) {
405 $logInfo[$tag] = $this->nodeContents();
406 } elseif ( $tag == 'contributor' ) {
407 $logInfo['contributor'] = $this->handleContributor();
408 } elseif ( $tag != '#text' ) {
409 $this->warn( "Unhandled log-item XML tag $tag" );
413 $this->processLogItem( $logInfo );
416 private function processLogItem( $logInfo ) {
417 $revision = new WikiRevision
;
419 $revision->setID( $logInfo['id'] );
420 $revision->setType( $logInfo['type'] );
421 $revision->setAction( $logInfo['action'] );
422 $revision->setTimestamp( $logInfo['timestamp'] );
423 $revision->setParams( $logInfo['params'] );
424 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
426 if ( isset( $logInfo['comment'] ) ) {
427 $revision->setComment( $logInfo['comment'] );
430 if ( isset( $logInfo['contributor']['ip'] ) ) {
431 $revision->setUserIP( $logInfo['contributor']['ip'] );
433 if ( isset( $logInfo['contributor']['username'] ) ) {
434 $revision->setUserName( $logInfo['contributor']['username'] );
437 return $this->logItemCallback( $revision );
440 private function handlePage() {
442 $this->debug( "Enter page handler." );
443 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
445 // Fields that can just be stuffed in the pageInfo object
446 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
451 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
452 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
453 $this->reader
->name
== 'page') {
457 $tag = $this->reader
->name
;
460 // The title is invalid, bail out of this page
462 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this->reader
,
465 } elseif ( in_array( $tag, $normalFields ) ) {
466 $pageInfo[$tag] = $this->nodeContents();
467 if ( $tag == 'title' ) {
468 $title = $this->processTitle( $pageInfo['title'] );
475 $this->pageCallback( $title );
476 list( $pageInfo['_title'], $origTitle ) = $title;
478 } elseif ( $tag == 'revision' ) {
479 $this->handleRevision( $pageInfo );
480 } elseif ( $tag == 'upload' ) {
481 $this->handleUpload( $pageInfo );
482 } elseif ( $tag != '#text' ) {
483 $this->warn( "Unhandled page XML tag $tag" );
488 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
489 $pageInfo['revisionCount'],
490 $pageInfo['successfulRevisionCount'],
494 private function handleRevision( &$pageInfo ) {
495 $this->debug( "Enter revision handler" );
496 $revisionInfo = array();
498 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'text' );
502 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
503 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
504 $this->reader
->name
== 'revision') {
508 $tag = $this->reader
->name
;
510 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this->reader
,
511 $pageInfo, $revisionInfo ) ) {
513 } elseif ( in_array( $tag, $normalFields ) ) {
514 $revisionInfo[$tag] = $this->nodeContents();
515 } elseif ( $tag == 'contributor' ) {
516 $revisionInfo['contributor'] = $this->handleContributor();
517 } elseif ( $tag != '#text' ) {
518 $this->warn( "Unhandled revision XML tag $tag" );
523 $pageInfo['revisionCount']++
;
524 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
525 $pageInfo['successfulRevisionCount']++
;
529 private function processRevision( $pageInfo, $revisionInfo ) {
530 $revision = new WikiRevision
;
532 $revision->setID( $revisionInfo['id'] );
533 $revision->setText( $revisionInfo['text'] );
534 $revision->setTitle( $pageInfo['_title'] );
535 $revision->setTimestamp( $revisionInfo['timestamp'] );
537 if ( isset( $revisionInfo['comment'] ) ) {
538 $revision->setComment( $revisionInfo['comment'] );
541 if ( isset( $revisionInfo['minor'] ) )
542 $revision->setMinor( true );
544 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
545 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
547 if ( isset( $revisionInfo['contributor']['username'] ) ) {
548 $revision->setUserName( $revisionInfo['contributor']['username'] );
551 return $this->revisionCallback( $revision );
554 private function handleUpload( &$pageInfo ) {
555 $this->debug( "Enter upload handler" );
556 $uploadInfo = array();
558 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
563 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
564 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
565 $this->reader
->name
== 'upload') {
569 $tag = $this->reader
->name
;
571 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this->reader
,
574 } elseif ( in_array( $tag, $normalFields ) ) {
575 $uploadInfo[$tag] = $this->nodeContents();
576 } elseif ( $tag == 'contributor' ) {
577 $uploadInfo['contributor'] = $this->handleContributor();
578 } elseif ( $tag != '#text' ) {
579 $this->warn( "Unhandled upload XML tag $tag" );
584 return $this->processUpload( $pageInfo, $uploadInfo );
587 private function processUpload( $pageInfo, $uploadInfo ) {
588 $revision = new WikiRevision
;
590 $revision->setTitle( $pageInfo['_title'] );
591 $revision->setID( $uploadInfo['id'] );
592 $revision->setTimestamp( $uploadInfo['timestamp'] );
593 $revision->setText( $uploadInfo['text'] );
594 $revision->setFilename( $uploadInfo['filename'] );
595 $revision->setSrc( $uploadInfo['src'] );
596 $revision->setSize( intval( $uploadInfo['size'] ) );
597 $revision->setComment( $uploadInfo['comment'] );
599 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
600 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
602 if ( isset( $uploadInfo['contributor']['username'] ) ) {
603 $revision->setUserName( $uploadInfo['contributor']['username'] );
606 return $this->uploadCallback( $revision );
609 private function handleContributor() {
610 $fields = array( 'id', 'ip', 'username' );
613 while ( $this->reader
->read() ) {
614 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
615 $this->reader
->name
== 'contributor') {
619 $tag = $this->reader
->name
;
621 if ( in_array( $tag, $fields ) ) {
622 $info[$tag] = $this->nodeContents();
629 private function processTitle( $text ) {
631 $origTitle = Title
::newFromText( $workTitle );
633 if( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
634 $title = Title
::makeTitle( $this->mTargetNamespace
,
635 $origTitle->getDBkey() );
637 $title = Title
::newFromText( $workTitle );
640 if( is_null( $title ) ) {
641 // Invalid page title? Ignore the page
642 $this->notice( "Skipping invalid page title '$workTitle'" );
644 } elseif( $title->getInterwiki() != '' ) {
645 $this->notice( "Skipping interwiki page title '$workTitle'" );
649 return array( $origTitle, $title );
653 /** This is a horrible hack used to keep source compatibility */
654 class UploadSourceAdapter
{
655 static $sourceRegistrations = array();
661 static function registerSource( $source ) {
662 $id = wfGenerateToken();
664 self
::$sourceRegistrations[$id] = $source;
669 function stream_open( $path, $mode, $options, &$opened_path ) {
670 $url = parse_url($path);
673 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
677 $this->mSource
= self
::$sourceRegistrations[$id];
682 function stream_read( $count ) {
686 while ( !$leave && !$this->mSource
->atEnd() &&
687 strlen($this->mBuffer
) < $count ) {
688 $read = $this->mSource
->readChunk();
690 if ( !strlen($read) ) {
694 $this->mBuffer
.= $read;
697 if ( strlen($this->mBuffer
) ) {
698 $return = substr( $this->mBuffer
, 0, $count );
699 $this->mBuffer
= substr( $this->mBuffer
, $count );
702 $this->mPosition +
= strlen($return);
707 function stream_write( $data ) {
711 function stream_tell() {
712 return $this->mPosition
;
715 function stream_eof() {
716 return $this->mSource
->atEnd();
719 function url_stat() {
722 $result['dev'] = $result[0] = 0;
723 $result['ino'] = $result[1] = 0;
724 $result['mode'] = $result[2] = 0;
725 $result['nlink'] = $result[3] = 0;
726 $result['uid'] = $result[4] = 0;
727 $result['gid'] = $result[5] = 0;
728 $result['rdev'] = $result[6] = 0;
729 $result['size'] = $result[7] = 0;
730 $result['atime'] = $result[8] = 0;
731 $result['mtime'] = $result[9] = 0;
732 $result['ctime'] = $result[10] = 0;
733 $result['blksize'] = $result[11] = 0;
734 $result['blocks'] = $result[12] = 0;
740 class XMLReader2
extends XMLReader
{
741 function nodeContents() {
742 if( $this->isEmptyElement
) {
746 while( $this->read() ) {
747 switch( $this->nodeType
) {
748 case XmlReader
::TEXT
:
749 case XmlReader
::SIGNIFICANT_WHITESPACE
:
750 $buffer .= $this->value
;
752 case XmlReader
::END_ELEMENT
:
756 return $this->close();
761 * @todo document (e.g. one-sentence class description).
762 * @ingroup SpecialPage
767 var $timestamp = "20010115000000";
777 function setTitle( $title ) {
778 if( is_object( $title ) ) {
779 $this->title
= $title;
780 } elseif( is_null( $title ) ) {
781 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
783 throw new MWException( "WikiRevision given non-object title in import." );
787 function setID( $id ) {
791 function setTimestamp( $ts ) {
792 # 2003-08-05T18:30:02Z
793 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
796 function setUsername( $user ) {
797 $this->user_text
= $user;
800 function setUserIP( $ip ) {
801 $this->user_text
= $ip;
804 function setText( $text ) {
808 function setComment( $text ) {
809 $this->comment
= $text;
812 function setMinor( $minor ) {
813 $this->minor
= (bool)$minor;
816 function setSrc( $src ) {
820 function setFilename( $filename ) {
821 $this->filename
= $filename;
824 function setSize( $size ) {
825 $this->size
= intval( $size );
828 function setType( $type ) {
832 function setAction( $action ) {
833 $this->action
= $action;
836 function setParams( $params ) {
837 $this->params
= $params;
840 function getTitle() {
848 function getTimestamp() {
849 return $this->timestamp
;
853 return $this->user_text
;
860 function getComment() {
861 return $this->comment
;
864 function getMinor() {
872 function getFilename() {
873 return $this->filename
;
884 function getAction() {
885 return $this->action
;
888 function getParams() {
889 return $this->params
;
892 function importOldRevision() {
893 $dbw = wfGetDB( DB_MASTER
);
895 # Sneak a single revision into place
896 $user = User
::newFromName( $this->getUser() );
898 $userId = intval( $user->getId() );
899 $userText = $user->getName();
902 $userText = $this->getUser();
905 // avoid memory leak...?
906 $linkCache = LinkCache
::singleton();
909 $article = new Article( $this->title
);
910 $pageId = $article->getId();
912 # must create the page...
913 $pageId = $article->insertOn( $dbw );
918 $prior = $dbw->selectField( 'revision', '1',
919 array( 'rev_page' => $pageId,
920 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
921 'rev_user_text' => $userText,
922 'rev_comment' => $this->getComment() ),
926 // FIXME: this could fail slightly for multiple matches :P
927 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
928 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
933 # FIXME: Use original rev_id optionally (better for backups)
935 $revision = new Revision( array(
937 'text' => $this->getText(),
938 'comment' => $this->getComment(),
940 'user_text' => $userText,
941 'timestamp' => $this->timestamp
,
942 'minor_edit' => $this->minor
,
944 $revId = $revision->insertOn( $dbw );
945 $changed = $article->updateIfNewerOn( $dbw, $revision );
947 # To be on the safe side...
948 $tempTitle = $GLOBALS['wgTitle'];
949 $GLOBALS['wgTitle'] = $this->title
;
952 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
953 Article
::onArticleCreate( $this->title
);
955 wfDebug( __METHOD__
. ": running create updates\n" );
956 $article->createUpdates( $revision );
958 } elseif( $changed ) {
959 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
960 Article
::onArticleEdit( $this->title
);
962 wfDebug( __METHOD__
. ": running edit updates\n" );
963 $article->editUpdates(
970 $GLOBALS['wgTitle'] = $tempTitle;
975 function importLogItem() {
976 $dbw = wfGetDB( DB_MASTER
);
977 # FIXME: this will not record autoblocks
978 if( !$this->getTitle() ) {
979 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
980 $this->timestamp
. "\n" );
983 # Check if it exists already
984 // FIXME: use original log ID (better for backups)
985 $prior = $dbw->selectField( 'logging', '1',
986 array( 'log_type' => $this->getType(),
987 'log_action' => $this->getAction(),
988 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
989 'log_namespace' => $this->getTitle()->getNamespace(),
990 'log_title' => $this->getTitle()->getDBkey(),
991 'log_comment' => $this->getComment(),
992 #'log_user_text' => $this->user_text,
993 'log_params' => $this->params
),
996 // FIXME: this could fail slightly for multiple matches :P
998 wfDebug( __METHOD__
. ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
999 $this->timestamp
. "\n" );
1002 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1004 'log_id' => $log_id,
1005 'log_type' => $this->type
,
1006 'log_action' => $this->action
,
1007 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1008 'log_user' => User
::idFromName( $this->user_text
),
1009 #'log_user_text' => $this->user_text,
1010 'log_namespace' => $this->getTitle()->getNamespace(),
1011 'log_title' => $this->getTitle()->getDBkey(),
1012 'log_comment' => $this->getComment(),
1013 'log_params' => $this->params
1015 $dbw->insert( 'logging', $data, __METHOD__
);
1018 function importUpload() {
1019 wfDebug( __METHOD__
. ": STUB\n" );
1022 // from file revert...
1023 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
1024 $comment = $wgRequest->getText( 'wpComment' );
1025 // TODO: Preserve file properties from database instead of reloading from file
1026 $status = $this->file->upload( $source, $comment, $comment );
1027 if( $status->isGood() ) {
1031 // from file upload...
1032 $this->mLocalFile = wfLocalFile( $nt );
1033 $this->mDestName = $this->mLocalFile->getName();
1035 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
1036 File::DELETE_SOURCE, $this->mFileProps );
1037 if ( !$status->isGood() ) {
1038 $resultDetails = array( 'internal' => $status->getWikiText() );
1041 // @todo Fixme: upload() uses $wgUser, which is wrong here
1042 // it may also create a page without our desire, also wrong potentially.
1043 // and, it will record a *current* upload, but we might want an archive version here
1045 $file = wfLocalFile( $this->getTitle() );
1047 wfDebug( "IMPORT: Bad file. :(\n" );
1051 $source = $this->downloadSource();
1053 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
1057 $status = $file->upload( $source,
1058 $this->getComment(),
1059 $this->getComment(), // Initial page, if none present...
1060 File
::DELETE_SOURCE
,
1062 $this->getTimestamp() );
1064 if( $status->isGood() ) {
1066 wfDebug( "IMPORT: is ok?\n" );
1070 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
1075 function downloadSource() {
1076 global $wgEnableUploads;
1077 if( !$wgEnableUploads ) {
1081 $tempo = tempnam( wfTempDir(), 'download' );
1082 $f = fopen( $tempo, 'wb' );
1084 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1089 $src = $this->getSrc();
1090 $data = Http
::get( $src );
1092 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1098 fwrite( $f, $data );
1107 * @todo document (e.g. one-sentence class description).
1108 * @ingroup SpecialPage
1110 class ImportStringSource
{
1111 function __construct( $string ) {
1112 $this->mString
= $string;
1113 $this->mRead
= false;
1117 return $this->mRead
;
1120 function readChunk() {
1121 if( $this->atEnd() ) {
1124 $this->mRead
= true;
1125 return $this->mString
;
1131 * @todo document (e.g. one-sentence class description).
1132 * @ingroup SpecialPage
1134 class ImportStreamSource
{
1135 function __construct( $handle ) {
1136 $this->mHandle
= $handle;
1140 return feof( $this->mHandle
);
1143 function readChunk() {
1144 return fread( $this->mHandle
, 32768 );
1147 static function newFromFile( $filename ) {
1148 $file = @fopen
( $filename, 'rt' );
1150 return Status
::newFatal( "importcantopen" );
1152 return Status
::newGood( new ImportStreamSource( $file ) );
1155 static function newFromUpload( $fieldname = "xmlimport" ) {
1156 $upload =& $_FILES[$fieldname];
1158 if( !isset( $upload ) ||
!$upload['name'] ) {
1159 return Status
::newFatal( 'importnofile' );
1161 if( !empty( $upload['error'] ) ) {
1162 switch($upload['error']){
1163 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1164 return Status
::newFatal( 'importuploaderrorsize' );
1165 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1166 return Status
::newFatal( 'importuploaderrorsize' );
1167 case 3: # The uploaded file was only partially uploaded
1168 return Status
::newFatal( 'importuploaderrorpartial' );
1169 case 6: #Missing a temporary folder.
1170 return Status
::newFatal( 'importuploaderrortemp' );
1171 # case else: # Currently impossible
1175 $fname = $upload['tmp_name'];
1176 if( is_uploaded_file( $fname ) ) {
1177 return ImportStreamSource
::newFromFile( $fname );
1179 return Status
::newFatal( 'importnofile' );
1183 static function newFromURL( $url, $method = 'GET' ) {
1184 wfDebug( __METHOD__
. ": opening $url\n" );
1185 # Use the standard HTTP fetch function; it times out
1186 # quicker and sorts out user-agent problems which might
1187 # otherwise prevent importing from large sites, such
1188 # as the Wikimedia cluster, etc.
1189 $data = Http
::request( $method, $url );
1190 if( $data !== false ) {
1192 fwrite( $file, $data );
1195 return Status
::newGood( new ImportStreamSource( $file ) );
1197 return Status
::newFatal( 'importcantopen' );
1201 public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
1203 return Status
::newFatal( 'import-noarticle' );
1205 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1206 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1207 return Status
::newFatal( 'importbadinterwiki' );
1210 if ( $history ) $params['history'] = 1;
1211 if ( $templates ) $params['templates'] = 1;
1212 if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
1213 $url = $link->getFullUrl( $params );
1214 # For interwikis, use POST to avoid redirects.
1215 return ImportStreamSource
::newFromURL( $url, "POST" );