3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
28 * @ingroup SpecialPage
33 var $timestamp = "20010115000000";
43 function setTitle( $title ) {
44 if( is_object( $title ) ) {
45 $this->title
= $title;
46 } elseif( is_null( $title ) ) {
47 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
49 throw new MWException( "WikiRevision given non-object title in import." );
53 function setID( $id ) {
57 function setTimestamp( $ts ) {
58 # 2003-08-05T18:30:02Z
59 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
62 function setUsername( $user ) {
63 $this->user_text
= $user;
66 function setUserIP( $ip ) {
67 $this->user_text
= $ip;
70 function setText( $text ) {
74 function setComment( $text ) {
75 $this->comment
= $text;
78 function setMinor( $minor ) {
79 $this->minor
= (bool)$minor;
82 function setSrc( $src ) {
86 function setFilename( $filename ) {
87 $this->filename
= $filename;
90 function setSize( $size ) {
91 $this->size
= intval( $size );
94 function setType( $type ) {
98 function setAction( $action ) {
99 $this->action
= $action;
102 function setParams( $params ) {
103 $this->params
= $params;
106 function getTitle() {
114 function getTimestamp() {
115 return $this->timestamp
;
119 return $this->user_text
;
126 function getComment() {
127 return $this->comment
;
130 function getMinor() {
138 function getFilename() {
139 return $this->filename
;
150 function getAction() {
151 return $this->action
;
154 function getParams() {
155 return $this->params
;
158 function importOldRevision() {
159 $dbw = wfGetDB( DB_MASTER
);
161 # Sneak a single revision into place
162 $user = User
::newFromName( $this->getUser() );
164 $userId = intval( $user->getId() );
165 $userText = $user->getName();
168 $userText = $this->getUser();
171 // avoid memory leak...?
172 $linkCache = LinkCache
::singleton();
175 $article = new Article( $this->title
);
176 $pageId = $article->getId();
178 # must create the page...
179 $pageId = $article->insertOn( $dbw );
184 $prior = $dbw->selectField( 'revision', '1',
185 array( 'rev_page' => $pageId,
186 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
187 'rev_user_text' => $userText,
188 'rev_comment' => $this->getComment() ),
192 // FIXME: this could fail slightly for multiple matches :P
193 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
194 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
199 # FIXME: Use original rev_id optionally (better for backups)
201 $revision = new Revision( array(
203 'text' => $this->getText(),
204 'comment' => $this->getComment(),
206 'user_text' => $userText,
207 'timestamp' => $this->timestamp
,
208 'minor_edit' => $this->minor
,
210 $revId = $revision->insertOn( $dbw );
211 $changed = $article->updateIfNewerOn( $dbw, $revision );
213 # To be on the safe side...
214 $tempTitle = $GLOBALS['wgTitle'];
215 $GLOBALS['wgTitle'] = $this->title
;
218 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
219 Article
::onArticleCreate( $this->title
);
221 wfDebug( __METHOD__
. ": running create updates\n" );
222 $article->createUpdates( $revision );
224 } elseif( $changed ) {
225 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
226 Article
::onArticleEdit( $this->title
);
228 wfDebug( __METHOD__
. ": running edit updates\n" );
229 $article->editUpdates(
236 $GLOBALS['wgTitle'] = $tempTitle;
241 function importLogItem() {
242 $dbw = wfGetDB( DB_MASTER
);
243 # FIXME: this will not record autoblocks
244 if( !$this->getTitle() ) {
245 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
246 $this->timestamp
. "\n" );
249 # Check if it exists already
250 // FIXME: use original log ID (better for backups)
251 $prior = $dbw->selectField( 'logging', '1',
252 array( 'log_type' => $this->getType(),
253 'log_action' => $this->getAction(),
254 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
255 'log_namespace' => $this->getTitle()->getNamespace(),
256 'log_title' => $this->getTitle()->getDBkey(),
257 'log_comment' => $this->getComment(),
258 #'log_user_text' => $this->user_text,
259 'log_params' => $this->params
),
262 // FIXME: this could fail slightly for multiple matches :P
264 wfDebug( __METHOD__
. ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
265 $this->timestamp
. "\n" );
268 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
271 'log_type' => $this->type
,
272 'log_action' => $this->action
,
273 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
274 'log_user' => User
::idFromName( $this->user_text
),
275 #'log_user_text' => $this->user_text,
276 'log_namespace' => $this->getTitle()->getNamespace(),
277 'log_title' => $this->getTitle()->getDBkey(),
278 'log_comment' => $this->getComment(),
279 'log_params' => $this->params
281 $dbw->insert( 'logging', $data, __METHOD__
);
284 function importUpload() {
285 wfDebug( __METHOD__
. ": STUB\n" );
288 // from file revert...
289 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
290 $comment = $wgRequest->getText( 'wpComment' );
291 // TODO: Preserve file properties from database instead of reloading from file
292 $status = $this->file->upload( $source, $comment, $comment );
293 if( $status->isGood() ) {
297 // from file upload...
298 $this->mLocalFile = wfLocalFile( $nt );
299 $this->mDestName = $this->mLocalFile->getName();
301 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
302 File::DELETE_SOURCE, $this->mFileProps );
303 if ( !$status->isGood() ) {
304 $resultDetails = array( 'internal' => $status->getWikiText() );
307 // @todo Fixme: upload() uses $wgUser, which is wrong here
308 // it may also create a page without our desire, also wrong potentially.
309 // and, it will record a *current* upload, but we might want an archive version here
311 $file = wfLocalFile( $this->getTitle() );
314 wfDebug( "IMPORT: Bad file. :(\n" );
318 $source = $this->downloadSource();
320 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
324 $status = $file->upload( $source,
326 $this->getComment(), // Initial page, if none present...
329 $this->getTimestamp() );
331 if( $status->isGood() ) {
333 wfDebug( "IMPORT: is ok?\n" );
337 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
342 function downloadSource() {
343 global $wgEnableUploads;
344 if( !$wgEnableUploads ) {
348 $tempo = tempnam( wfTempDir(), 'download' );
349 $f = fopen( $tempo, 'wb' );
351 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
356 $src = $this->getSrc();
357 $data = Http
::get( $src );
359 wfDebug( "IMPORT: couldn't fetch source $src\n" );
374 * implements Special:Import
375 * @ingroup SpecialPage
380 var $mPageCallback = null;
381 var $mPageOutCallback = null;
382 var $mRevisionCallback = null;
383 var $mLogItemCallback = null;
384 var $mUploadCallback = null;
385 var $mTargetNamespace = null;
386 var $mXmlNamespace = false;
388 var $tagStack = array();
390 function __construct( $source ) {
391 $this->setRevisionCallback( array( $this, "importRevision" ) );
392 $this->setUploadCallback( array( $this, "importUpload" ) );
393 $this->setLogItemCallback( array( $this, "importLogItem" ) );
394 $this->mSource
= $source;
397 function throwXmlError( $err ) {
398 $this->debug( "FAILURE: $err" );
399 wfDebug( "WikiImporter XML error: $err\n" );
402 function handleXmlNamespace ( $parser, $data, $prefix=false, $uri=false ) {
403 if( preg_match( '/www.mediawiki.org/',$prefix ) ) {
404 $prefix = str_replace( '/','\/',$prefix );
405 $this->mXmlNamespace
='/^'.$prefix.':/';
409 function stripXmlNamespace($name) {
410 if( $this->mXmlNamespace
) {
411 return(preg_replace($this->mXmlNamespace
,'',$name,1));
420 function doImport() {
421 if( empty( $this->mSource
) ) {
422 return new WikiErrorMsg( "importnotext" );
425 $parser = xml_parser_create_ns( "UTF-8" );
427 # case folding violates XML standard, turn it off
428 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
430 xml_set_object( $parser, $this );
431 xml_set_element_handler( $parser, "in_start", "" );
432 xml_set_start_namespace_decl_handler( $parser, "handleXmlNamespace" );
434 $offset = 0; // for context extraction on error reporting
436 $chunk = $this->mSource
->readChunk();
437 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
438 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
439 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
441 $offset +
= strlen( $chunk );
442 } while( $chunk !== false && !$this->mSource
->atEnd() );
443 xml_parser_free( $parser );
448 function debug( $data ) {
449 if( $this->mDebug
) {
450 wfDebug( "IMPORT: $data\n" );
454 function notice( $data ) {
455 global $wgCommandLineMode;
456 if( $wgCommandLineMode ) {
460 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
467 function setDebug( $debug ) {
468 $this->mDebug
= $debug;
472 * Sets the action to perform as each new page in the stream is reached.
473 * @param $callback callback
476 function setPageCallback( $callback ) {
477 $previous = $this->mPageCallback
;
478 $this->mPageCallback
= $callback;
483 * Sets the action to perform as each page in the stream is completed.
484 * Callback accepts the page title (as a Title object), a second object
485 * with the original title form (in case it's been overridden into a
486 * local namespace), and a count of revisions.
488 * @param $callback callback
491 function setPageOutCallback( $callback ) {
492 $previous = $this->mPageOutCallback
;
493 $this->mPageOutCallback
= $callback;
498 * Sets the action to perform as each page revision is reached.
499 * @param $callback callback
502 function setRevisionCallback( $callback ) {
503 $previous = $this->mRevisionCallback
;
504 $this->mRevisionCallback
= $callback;
509 * Sets the action to perform as each file upload version is reached.
510 * @param $callback callback
513 function setUploadCallback( $callback ) {
514 $previous = $this->mUploadCallback
;
515 $this->mUploadCallback
= $callback;
520 * Sets the action to perform as each log item reached.
521 * @param $callback callback
524 function setLogItemCallback( $callback ) {
525 $previous = $this->mLogItemCallback
;
526 $this->mLogItemCallback
= $callback;
531 * Set a target namespace to override the defaults
533 function setTargetNamespace( $namespace ) {
534 if( is_null( $namespace ) ) {
535 // Don't override namespaces
536 $this->mTargetNamespace
= null;
537 } elseif( $namespace >= 0 ) {
538 // FIXME: Check for validity
539 $this->mTargetNamespace
= intval( $namespace );
546 * Default per-revision callback, performs the import.
547 * @param $revision WikiRevision
550 function importRevision( $revision ) {
551 $dbw = wfGetDB( DB_MASTER
);
552 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
556 * Default per-revision callback, performs the import.
557 * @param $rev WikiRevision
560 function importLogItem( $rev ) {
561 $dbw = wfGetDB( DB_MASTER
);
562 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
568 function importUpload( $revision ) {
569 //$dbw = wfGetDB( DB_MASTER );
570 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
575 * Alternate per-revision callback, for debugging.
576 * @param $revision WikiRevision
579 function debugRevisionHandler( &$revision ) {
580 $this->debug( "Got revision:" );
581 if( is_object( $revision->title
) ) {
582 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
584 $this->debug( "-- Title: <invalid>" );
586 $this->debug( "-- User: " . $revision->user_text
);
587 $this->debug( "-- Timestamp: " . $revision->timestamp
);
588 $this->debug( "-- Comment: " . $revision->comment
);
589 $this->debug( "-- Text: " . $revision->text
);
593 * Notify the callback function when a new <page> is reached.
594 * @param $title Title
597 function pageCallback( $title ) {
598 if( is_callable( $this->mPageCallback
) ) {
599 call_user_func( $this->mPageCallback
, $title );
604 * Notify the callback function when a </page> is closed.
605 * @param $title Title
606 * @param $origTitle Title
607 * @param $revisionCount int
608 * @param $successCount Int: number of revisions for which callback returned true
611 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
612 if( is_callable( $this->mPageOutCallback
) ) {
613 call_user_func( $this->mPageOutCallback
, $title, $origTitle,
614 $revisionCount, $successCount );
618 # XML parser callbacks from here out -- beware!
619 function donothing( $parser, $x, $y="" ) {
620 #$this->debug( "donothing" );
623 function in_start( $parser, $name, $attribs ) {
624 $name = $this->stripXmlNamespace($name);
625 $this->debug( "in_start $name" );
626 if( $name != "mediawiki" ) {
627 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
629 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
632 function in_mediawiki( $parser, $name, $attribs ) {
633 $name = $this->stripXmlNamespace($name);
634 $this->debug( "in_mediawiki $name" );
635 if( $name == 'siteinfo' ) {
636 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
637 } elseif( $name == 'page' ) {
638 $this->push( $name );
639 $this->workRevisionCount
= 0;
640 $this->workSuccessCount
= 0;
641 $this->uploadCount
= 0;
642 $this->uploadSuccessCount
= 0;
643 xml_set_element_handler( $parser, "in_page", "out_page" );
644 } elseif( $name == 'logitem' ) {
645 $this->push( $name );
646 $this->workRevision
= new WikiRevision
;
647 xml_set_element_handler( $parser, "in_logitem", "out_logitem" );
649 return $this->throwXMLerror( "Expected <page>, got <$name>" );
652 function out_mediawiki( $parser, $name ) {
653 $name = $this->stripXmlNamespace($name);
654 $this->debug( "out_mediawiki $name" );
655 if( $name != "mediawiki" ) {
656 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
658 xml_set_element_handler( $parser, "donothing", "donothing" );
662 function in_siteinfo( $parser, $name, $attribs ) {
664 $name = $this->stripXmlNamespace($name);
665 $this->debug( "in_siteinfo $name" );
675 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
679 function out_siteinfo( $parser, $name ) {
680 $name = $this->stripXmlNamespace($name);
681 if( $name == "siteinfo" ) {
682 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
687 function in_page( $parser, $name, $attribs ) {
688 $name = $this->stripXmlNamespace($name);
689 $this->debug( "in_page $name" );
695 $this->appendfield
= $name;
696 $this->appenddata
= "";
697 xml_set_element_handler( $parser, "in_nothing", "out_append" );
698 xml_set_character_data_handler( $parser, "char_append" );
701 $this->push( "revision" );
702 if( is_object( $this->pageTitle
) ) {
703 $this->workRevision
= new WikiRevision
;
704 $this->workRevision
->setTitle( $this->pageTitle
);
705 $this->workRevisionCount++
;
707 // Skipping items due to invalid page title
708 $this->workRevision
= null;
710 xml_set_element_handler( $parser, "in_revision", "out_revision" );
713 $this->push( "upload" );
714 if( is_object( $this->pageTitle
) ) {
715 $this->workRevision
= new WikiRevision
;
716 $this->workRevision
->setTitle( $this->pageTitle
);
717 $this->uploadCount++
;
719 // Skipping items due to invalid page title
720 $this->workRevision
= null;
722 xml_set_element_handler( $parser, "in_upload", "out_upload" );
725 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
729 function out_page( $parser, $name ) {
730 $name = $this->stripXmlNamespace($name);
731 $this->debug( "out_page $name" );
733 if( $name != "page" ) {
734 return $this->throwXMLerror( "Expected </page>, got </$name>" );
736 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
738 $this->pageOutCallback( $this->pageTitle
, $this->origTitle
,
739 $this->workRevisionCount
, $this->workSuccessCount
);
741 $this->workTitle
= null;
742 $this->workRevision
= null;
743 $this->workRevisionCount
= 0;
744 $this->workSuccessCount
= 0;
745 $this->pageTitle
= null;
746 $this->origTitle
= null;
749 function in_nothing( $parser, $name, $attribs ) {
750 $name = $this->stripXmlNamespace($name);
751 $this->debug( "in_nothing $name" );
752 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
755 function char_append( $parser, $data ) {
756 $this->debug( "char_append '$data'" );
757 $this->appenddata
.= $data;
760 function out_append( $parser, $name ) {
761 $name = $this->stripXmlNamespace($name);
762 $this->debug( "out_append $name" );
763 if( $name != $this->appendfield
) {
764 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
767 switch( $this->appendfield
) {
769 $this->workTitle
= $this->appenddata
;
770 $this->origTitle
= Title
::newFromText( $this->workTitle
);
771 if( !is_null( $this->mTargetNamespace
) && !is_null( $this->origTitle
) ) {
772 $this->pageTitle
= Title
::makeTitle( $this->mTargetNamespace
,
773 $this->origTitle
->getDBkey() );
775 $this->pageTitle
= Title
::newFromText( $this->workTitle
);
777 if( is_null( $this->pageTitle
) ) {
778 // Invalid page title? Ignore the page
779 $this->notice( "Skipping invalid page title '$this->workTitle'" );
780 } elseif( $this->pageTitle
->getInterwiki() != '' ) {
781 $this->notice( "Skipping interwiki page title '$this->workTitle'" );
782 $this->pageTitle
= null;
784 $this->pageCallback( $this->workTitle
);
788 if ( $this->parentTag() == 'revision' ||
$this->parentTag() == 'logitem' ) {
789 if( $this->workRevision
)
790 $this->workRevision
->setID( $this->appenddata
);
794 if( $this->workRevision
)
795 $this->workRevision
->setText( $this->appenddata
);
798 if( $this->workRevision
)
799 $this->workRevision
->setUsername( $this->appenddata
);
802 if( $this->workRevision
)
803 $this->workRevision
->setUserIP( $this->appenddata
);
806 if( $this->workRevision
)
807 $this->workRevision
->setTimestamp( $this->appenddata
);
810 if( $this->workRevision
)
811 $this->workRevision
->setComment( $this->appenddata
);
814 if( $this->workRevision
)
815 $this->workRevision
->setType( $this->appenddata
);
818 if( $this->workRevision
)
819 $this->workRevision
->setAction( $this->appenddata
);
822 if( $this->workRevision
)
823 $this->workRevision
->setTitle( Title
::newFromText( $this->appenddata
) );
826 if( $this->workRevision
)
827 $this->workRevision
->setParams( $this->appenddata
);
830 if( $this->workRevision
)
831 $this->workRevision
->setMinor( true );
834 if( $this->workRevision
)
835 $this->workRevision
->setFilename( $this->appenddata
);
838 if( $this->workRevision
)
839 $this->workRevision
->setSrc( $this->appenddata
);
842 if( $this->workRevision
)
843 $this->workRevision
->setSize( intval( $this->appenddata
) );
846 $this->debug( "Bad append: {$this->appendfield}" );
848 $this->appendfield
= "";
849 $this->appenddata
= "";
851 $parent = $this->parentTag();
852 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
853 xml_set_character_data_handler( $parser, "donothing" );
856 function in_revision( $parser, $name, $attribs ) {
857 $name = $this->stripXmlNamespace($name);
858 $this->debug( "in_revision $name" );
865 $this->appendfield
= $name;
866 xml_set_element_handler( $parser, "in_nothing", "out_append" );
867 xml_set_character_data_handler( $parser, "char_append" );
870 $this->push( "contributor" );
871 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
874 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
878 function out_revision( $parser, $name ) {
879 $name = $this->stripXmlNamespace($name);
880 $this->debug( "out_revision $name" );
882 if( $name != "revision" ) {
883 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
885 xml_set_element_handler( $parser, "in_page", "out_page" );
887 if( $this->workRevision
) {
888 $ok = call_user_func_array( $this->mRevisionCallback
,
889 array( $this->workRevision
, $this ) );
891 $this->workSuccessCount++
;
896 function in_logitem( $parser, $name, $attribs ) {
897 $name = $this->stripXmlNamespace($name);
898 $this->debug( "in_logitem $name" );
907 $this->appendfield
= $name;
908 xml_set_element_handler( $parser, "in_nothing", "out_append" );
909 xml_set_character_data_handler( $parser, "char_append" );
912 $this->push( "contributor" );
913 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
916 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
920 function out_logitem( $parser, $name ) {
921 $name = $this->stripXmlNamespace($name);
922 $this->debug( "out_logitem $name" );
924 if( $name != "logitem" ) {
925 return $this->throwXMLerror( "Expected </logitem>, got </$name>" );
927 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
929 if( $this->workRevision
) {
930 $ok = call_user_func_array( $this->mLogItemCallback
,
931 array( $this->workRevision
, $this ) );
933 $this->workSuccessCount++
;
938 function in_upload( $parser, $name, $attribs ) {
939 $name = $this->stripXmlNamespace($name);
940 $this->debug( "in_upload $name" );
948 $this->appendfield
= $name;
949 xml_set_element_handler( $parser, "in_nothing", "out_append" );
950 xml_set_character_data_handler( $parser, "char_append" );
953 $this->push( "contributor" );
954 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
957 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
961 function out_upload( $parser, $name ) {
962 $name = $this->stripXmlNamespace($name);
963 $this->debug( "out_revision $name" );
965 if( $name != "upload" ) {
966 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
968 xml_set_element_handler( $parser, "in_page", "out_page" );
970 if( $this->workRevision
) {
971 $ok = call_user_func_array( $this->mUploadCallback
,
972 array( $this->workRevision
, $this ) );
974 $this->workUploadSuccessCount++
;
979 function in_contributor( $parser, $name, $attribs ) {
980 $name = $this->stripXmlNamespace($name);
981 $this->debug( "in_contributor $name" );
986 $this->appendfield
= $name;
987 xml_set_element_handler( $parser, "in_nothing", "out_append" );
988 xml_set_character_data_handler( $parser, "char_append" );
991 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
995 function out_contributor( $parser, $name ) {
996 $name = $this->stripXmlNamespace($name);
997 $this->debug( "out_contributor $name" );
999 if( $name != "contributor" ) {
1000 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
1002 $parent = $this->parentTag();
1003 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
1006 private function push( $name ) {
1007 array_push( $this->tagStack
, $name );
1008 $this->debug( "PUSH $name" );
1011 private function pop() {
1012 $name = array_pop( $this->tagStack
);
1013 $this->debug( "POP $name" );
1017 private function parentTag() {
1018 $name = $this->tagStack
[count( $this->tagStack
) - 1];
1019 $this->debug( "PARENT $name" );
1026 * @todo document (e.g. one-sentence class description).
1027 * @ingroup SpecialPage
1029 class ImportStringSource
{
1030 function __construct( $string ) {
1031 $this->mString
= $string;
1032 $this->mRead
= false;
1036 return $this->mRead
;
1039 function readChunk() {
1040 if( $this->atEnd() ) {
1043 $this->mRead
= true;
1044 return $this->mString
;
1050 * @todo document (e.g. one-sentence class description).
1051 * @ingroup SpecialPage
1053 class ImportStreamSource
{
1054 function __construct( $handle ) {
1055 $this->mHandle
= $handle;
1059 return feof( $this->mHandle
);
1062 function readChunk() {
1063 return fread( $this->mHandle
, 32768 );
1066 static function newFromFile( $filename ) {
1067 $file = @fopen
( $filename, 'rt' );
1069 return new WikiErrorMsg( "importcantopen" );
1071 return new ImportStreamSource( $file );
1074 static function newFromUpload( $fieldname = "xmlimport" ) {
1075 $upload =& $_FILES[$fieldname];
1077 if( !isset( $upload ) ||
!$upload['name'] ) {
1078 return new WikiErrorMsg( 'importnofile' );
1080 if( !empty( $upload['error'] ) ) {
1081 switch($upload['error']){
1082 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1083 return new WikiErrorMsg( 'importuploaderrorsize' );
1084 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1085 return new WikiErrorMsg( 'importuploaderrorsize' );
1086 case 3: # The uploaded file was only partially uploaded
1087 return new WikiErrorMsg( 'importuploaderrorpartial' );
1088 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1089 return new WikiErrorMsg( 'importuploaderrortemp' );
1090 # case else: # Currently impossible
1094 $fname = $upload['tmp_name'];
1095 if( is_uploaded_file( $fname ) ) {
1096 return ImportStreamSource
::newFromFile( $fname );
1098 return new WikiErrorMsg( 'importnofile' );
1102 static function newFromURL( $url, $method = 'GET' ) {
1103 wfDebug( __METHOD__
. ": opening $url\n" );
1104 # Use the standard HTTP fetch function; it times out
1105 # quicker and sorts out user-agent problems which might
1106 # otherwise prevent importing from large sites, such
1107 # as the Wikimedia cluster, etc.
1108 $data = Http
::request( $method, $url );
1109 if( $data !== false ) {
1111 fwrite( $file, $data );
1114 return new ImportStreamSource( $file );
1116 return new WikiErrorMsg( 'importcantopen' );
1120 public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
1122 return new WikiErrorMsg( 'import-noarticle' );
1124 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1125 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1126 return new WikiErrorMsg( 'importbadinterwiki' );
1129 if ( $history ) $params['history'] = 1;
1130 if ( $templates ) $params['templates'] = 1;
1131 if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
1132 $url = $link->getFullUrl( $params );
1133 # For interwikis, use POST to avoid redirects.
1134 return ImportStreamSource
::newFromURL( $url, "POST" );