Add the other existing $skin.css/.js to the message files too to be consistent
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Constructor
28 */
29 function wfSpecialImport( $page = '' ) {
30 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
31 global $wgImportTargetNamespace;
32
33 $interwiki = false;
34 $namespace = $wgImportTargetNamespace;
35 $frompage = '';
36 $history = true;
37
38 if ( wfReadOnly() ) {
39 $wgOut->readOnlyPage();
40 return;
41 }
42
43 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
44 $isUpload = false;
45 $namespace = $wgRequest->getIntOrNull( 'namespace' );
46
47 switch( $wgRequest->getVal( "source" ) ) {
48 case "upload":
49 $isUpload = true;
50 if( $wgUser->isAllowed( 'importupload' ) ) {
51 $source = ImportStreamSource::newFromUpload( "xmlimport" );
52 } else {
53 return $wgOut->permissionRequired( 'importupload' );
54 }
55 break;
56 case "interwiki":
57 $interwiki = $wgRequest->getVal( 'interwiki' );
58 $history = $wgRequest->getCheck( 'interwikiHistory' );
59 $frompage = $wgRequest->getText( "frompage" );
60 $source = ImportStreamSource::newFromInterwiki(
61 $interwiki,
62 $frompage,
63 $history );
64 break;
65 default:
66 $source = new WikiErrorMsg( "importunknownsource" );
67 }
68
69 if( WikiError::isError( $source ) ) {
70 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
71 } else {
72 $wgOut->addWikiMsg( "importstart" );
73
74 $importer = new WikiImporter( $source );
75 if( !is_null( $namespace ) ) {
76 $importer->setTargetNamespace( $namespace );
77 }
78 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
79
80 $reporter->open();
81 $result = $importer->doImport();
82 $resultCount = $reporter->close();
83
84 if( WikiError::isError( $result ) ) {
85 # No source or XML parse error
86 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
87 } elseif( WikiError::isError( $resultCount ) ) {
88 # Zero revisions
89 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
90 } else {
91 # Success!
92 $wgOut->addWikiMsg( 'importsuccess' );
93 }
94 $wgOut->addWikiText( '<hr />' );
95 }
96 }
97
98 $action = $wgTitle->getLocalUrl( 'action=submit' );
99
100 if( $wgUser->isAllowed( 'importupload' ) ) {
101 $wgOut->addWikiMsg( "importtext" );
102 $wgOut->addHTML(
103 Xml::openElement( 'fieldset' ).
104 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
105 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
106 Xml::hidden( 'action', 'submit' ) .
107 Xml::hidden( 'source', 'upload' ) .
108 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
109 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
110 Xml::closeElement( 'form' ) .
111 Xml::closeElement( 'fieldset' )
112 );
113 } else {
114 if( empty( $wgImportSources ) ) {
115 $wgOut->addWikiMsg( 'importnosources' );
116 }
117 }
118
119 if( !empty( $wgImportSources ) ) {
120 $wgOut->addHTML(
121 Xml::openElement( 'fieldset' ) .
122 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
123 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
124 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
125 Xml::hidden( 'action', 'submit' ) .
126 Xml::hidden( 'source', 'interwiki' ) .
127 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
128 "<tr>
129 <td>" .
130 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
131 );
132 foreach( $wgImportSources as $prefix ) {
133 $selected = ( $interwiki === $prefix ) ? ' selected="selected"' : '';
134 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
135 }
136 $wgOut->addHTML(
137 Xml::closeElement( 'select' ) .
138 "</td>
139 <td>" .
140 Xml::input( 'frompage', 50, $frompage ) .
141 "</td>
142 </tr>
143 <tr>
144 <td>
145 </td>
146 <td>" .
147 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
148 "</td>
149 </tr>
150 <tr>
151 <td>
152 </td>
153 <td>" .
154 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
155 Xml::namespaceSelector( $namespace, '' ) .
156 "</td>
157 </tr>
158 <tr>
159 <td>
160 </td>
161 <td>" .
162 Xml::submitButton( wfMsg( 'import-interwiki-submit' ) ) .
163 "</td>
164 </tr>" .
165 Xml::closeElement( 'table' ).
166 Xml::closeElement( 'form' ) .
167 Xml::closeElement( 'fieldset' )
168 );
169 }
170 }
171
172 /**
173 * Reporting callback
174 * @ingroup SpecialPage
175 */
176 class ImportReporter {
177 function __construct( $importer, $upload, $interwiki ) {
178 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
179 $this->mPageCount = 0;
180 $this->mIsUpload = $upload;
181 $this->mInterwiki = $interwiki;
182 }
183
184 function open() {
185 global $wgOut;
186 $wgOut->addHtml( "<ul>\n" );
187 }
188
189 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
190 global $wgOut, $wgUser, $wgLang, $wgContLang;
191
192 $skin = $wgUser->getSkin();
193
194 $this->mPageCount++;
195
196 $localCount = $wgLang->formatNum( $successCount );
197 $contentCount = $wgContLang->formatNum( $successCount );
198
199 if( $successCount > 0 ) {
200 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
201 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
202 "</li>\n"
203 );
204
205 $log = new LogPage( 'import' );
206 if( $this->mIsUpload ) {
207 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
208 $contentCount );
209 $log->addEntry( 'upload', $title, $detail );
210 } else {
211 $interwiki = '[[:' . $this->mInterwiki . ':' .
212 $origTitle->getPrefixedText() . ']]';
213 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
214 $contentCount, $interwiki );
215 $log->addEntry( 'interwiki', $title, $detail );
216 }
217
218 $comment = $detail; // quick
219 $dbw = wfGetDB( DB_MASTER );
220 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
221 $nullRevision->insertOn( $dbw );
222 $article = new Article( $title );
223 # Update page record
224 $article->updateRevisionOn( $dbw, $nullRevision );
225 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, false) );
226 } else {
227 $wgOut->addHtml( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
228 }
229 }
230
231 function close() {
232 global $wgOut;
233 if( $this->mPageCount == 0 ) {
234 $wgOut->addHtml( "</ul>\n" );
235 return new WikiErrorMsg( "importnopages" );
236 }
237 $wgOut->addHtml( "</ul>\n" );
238
239 return $this->mPageCount;
240 }
241 }
242
243 /**
244 *
245 * @ingroup SpecialPage
246 */
247 class WikiRevision {
248 var $title = null;
249 var $id = 0;
250 var $timestamp = "20010115000000";
251 var $user = 0;
252 var $user_text = "";
253 var $text = "";
254 var $comment = "";
255 var $minor = false;
256
257 function setTitle( $title ) {
258 if( is_object( $title ) ) {
259 $this->title = $title;
260 } elseif( is_null( $title ) ) {
261 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
262 } else {
263 throw new MWException( "WikiRevision given non-object title in import." );
264 }
265 }
266
267 function setID( $id ) {
268 $this->id = $id;
269 }
270
271 function setTimestamp( $ts ) {
272 # 2003-08-05T18:30:02Z
273 $this->timestamp = wfTimestamp( TS_MW, $ts );
274 }
275
276 function setUsername( $user ) {
277 $this->user_text = $user;
278 }
279
280 function setUserIP( $ip ) {
281 $this->user_text = $ip;
282 }
283
284 function setText( $text ) {
285 $this->text = $text;
286 }
287
288 function setComment( $text ) {
289 $this->comment = $text;
290 }
291
292 function setMinor( $minor ) {
293 $this->minor = (bool)$minor;
294 }
295
296 function setSrc( $src ) {
297 $this->src = $src;
298 }
299
300 function setFilename( $filename ) {
301 $this->filename = $filename;
302 }
303
304 function setSize( $size ) {
305 $this->size = intval( $size );
306 }
307
308 function getTitle() {
309 return $this->title;
310 }
311
312 function getID() {
313 return $this->id;
314 }
315
316 function getTimestamp() {
317 return $this->timestamp;
318 }
319
320 function getUser() {
321 return $this->user_text;
322 }
323
324 function getText() {
325 return $this->text;
326 }
327
328 function getComment() {
329 return $this->comment;
330 }
331
332 function getMinor() {
333 return $this->minor;
334 }
335
336 function getSrc() {
337 return $this->src;
338 }
339
340 function getFilename() {
341 return $this->filename;
342 }
343
344 function getSize() {
345 return $this->size;
346 }
347
348 function importOldRevision() {
349 $dbw = wfGetDB( DB_MASTER );
350
351 # Sneak a single revision into place
352 $user = User::newFromName( $this->getUser() );
353 if( $user ) {
354 $userId = intval( $user->getId() );
355 $userText = $user->getName();
356 } else {
357 $userId = 0;
358 $userText = $this->getUser();
359 }
360
361 // avoid memory leak...?
362 $linkCache = LinkCache::singleton();
363 $linkCache->clear();
364
365 $article = new Article( $this->title );
366 $pageId = $article->getId();
367 if( $pageId == 0 ) {
368 # must create the page...
369 $pageId = $article->insertOn( $dbw );
370 $created = true;
371 } else {
372 $created = false;
373
374 $prior = Revision::loadFromTimestamp( $dbw, $this->title, $this->timestamp );
375 if( !is_null( $prior ) ) {
376 // FIXME: this could fail slightly for multiple matches :P
377 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
378 $this->title->getPrefixedText() . "]], timestamp " .
379 $this->timestamp . "\n" );
380 return false;
381 }
382 }
383
384 # FIXME: Use original rev_id optionally
385 # FIXME: blah blah blah
386
387 #if( $numrows > 0 ) {
388 # return wfMsg( "importhistoryconflict" );
389 #}
390
391 # Insert the row
392 $revision = new Revision( array(
393 'page' => $pageId,
394 'text' => $this->getText(),
395 'comment' => $this->getComment(),
396 'user' => $userId,
397 'user_text' => $userText,
398 'timestamp' => $this->timestamp,
399 'minor_edit' => $this->minor,
400 ) );
401 $revId = $revision->insertOn( $dbw );
402 $changed = $article->updateIfNewerOn( $dbw, $revision );
403
404 if( $created ) {
405 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
406 Article::onArticleCreate( $this->title );
407
408 wfDebug( __METHOD__ . ": running create updates\n" );
409 $article->createUpdates( $revision );
410
411 } elseif( $changed ) {
412 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
413 Article::onArticleEdit( $this->title );
414
415 wfDebug( __METHOD__ . ": running edit updates\n" );
416 $article->editUpdates(
417 $this->getText(),
418 $this->getComment(),
419 $this->minor,
420 $this->timestamp,
421 $revId );
422 }
423
424 return true;
425 }
426
427 function importUpload() {
428 wfDebug( __METHOD__ . ": STUB\n" );
429
430 /**
431 // from file revert...
432 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
433 $comment = $wgRequest->getText( 'wpComment' );
434 // TODO: Preserve file properties from database instead of reloading from file
435 $status = $this->file->upload( $source, $comment, $comment );
436 if( $status->isGood() ) {
437 */
438
439 /**
440 // from file upload...
441 $this->mLocalFile = wfLocalFile( $nt );
442 $this->mDestName = $this->mLocalFile->getName();
443 //....
444 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
445 File::DELETE_SOURCE, $this->mFileProps );
446 if ( !$status->isGood() ) {
447 $resultDetails = array( 'internal' => $status->getWikiText() );
448 */
449
450 // @fixme upload() uses $wgUser, which is wrong here
451 // it may also create a page without our desire, also wrong potentially.
452 // and, it will record a *current* upload, but we might want an archive version here
453
454 $file = wfLocalFile( $this->getTitle() );
455 if( !$file ) {
456 var_dump( $file );
457 wfDebug( "IMPORT: Bad file. :(\n" );
458 return false;
459 }
460
461 $source = $this->downloadSource();
462 if( !$source ) {
463 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
464 return false;
465 }
466
467 $status = $file->upload( $source,
468 $this->getComment(),
469 $this->getComment(), // Initial page, if none present...
470 File::DELETE_SOURCE,
471 false, // props...
472 $this->getTimestamp() );
473
474 if( $status->isGood() ) {
475 // yay?
476 wfDebug( "IMPORT: is ok?\n" );
477 return true;
478 }
479
480 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
481 return false;
482
483 }
484
485 function downloadSource() {
486 global $wgEnableUploads;
487 if( !$wgEnableUploads ) {
488 return false;
489 }
490
491 $tempo = tempnam( wfTempDir(), 'download' );
492 $f = fopen( $tempo, 'wb' );
493 if( !$f ) {
494 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
495 return false;
496 }
497
498 // @fixme!
499 $src = $this->getSrc();
500 $data = Http::get( $src );
501 if( !$data ) {
502 wfDebug( "IMPORT: couldn't fetch source $src\n" );
503 fclose( $f );
504 unlink( $tempo );
505 return false;
506 }
507
508 fwrite( $f, $data );
509 fclose( $f );
510
511 return $tempo;
512 }
513
514 }
515
516 /**
517 * implements Special:Import
518 * @ingroup SpecialPage
519 */
520 class WikiImporter {
521 var $mDebug = false;
522 var $mSource = null;
523 var $mPageCallback = null;
524 var $mPageOutCallback = null;
525 var $mRevisionCallback = null;
526 var $mUploadCallback = null;
527 var $mTargetNamespace = null;
528 var $lastfield;
529 var $tagStack = array();
530
531 function __construct( $source ) {
532 $this->setRevisionCallback( array( $this, "importRevision" ) );
533 $this->setUploadCallback( array( $this, "importUpload" ) );
534 $this->mSource = $source;
535 }
536
537 function throwXmlError( $err ) {
538 $this->debug( "FAILURE: $err" );
539 wfDebug( "WikiImporter XML error: $err\n" );
540 }
541
542 # --------------
543
544 function doImport() {
545 if( empty( $this->mSource ) ) {
546 return new WikiErrorMsg( "importnotext" );
547 }
548
549 $parser = xml_parser_create( "UTF-8" );
550
551 # case folding violates XML standard, turn it off
552 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
553
554 xml_set_object( $parser, $this );
555 xml_set_element_handler( $parser, "in_start", "" );
556
557 $offset = 0; // for context extraction on error reporting
558 do {
559 $chunk = $this->mSource->readChunk();
560 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
561 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
562 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
563 }
564 $offset += strlen( $chunk );
565 } while( $chunk !== false && !$this->mSource->atEnd() );
566 xml_parser_free( $parser );
567
568 return true;
569 }
570
571 function debug( $data ) {
572 if( $this->mDebug ) {
573 wfDebug( "IMPORT: $data\n" );
574 }
575 }
576
577 function notice( $data ) {
578 global $wgCommandLineMode;
579 if( $wgCommandLineMode ) {
580 print "$data\n";
581 } else {
582 global $wgOut;
583 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
584 }
585 }
586
587 /**
588 * Set debug mode...
589 */
590 function setDebug( $debug ) {
591 $this->mDebug = $debug;
592 }
593
594 /**
595 * Sets the action to perform as each new page in the stream is reached.
596 * @param $callback callback
597 * @return callback
598 */
599 function setPageCallback( $callback ) {
600 $previous = $this->mPageCallback;
601 $this->mPageCallback = $callback;
602 return $previous;
603 }
604
605 /**
606 * Sets the action to perform as each page in the stream is completed.
607 * Callback accepts the page title (as a Title object), a second object
608 * with the original title form (in case it's been overridden into a
609 * local namespace), and a count of revisions.
610 *
611 * @param $callback callback
612 * @return callback
613 */
614 function setPageOutCallback( $callback ) {
615 $previous = $this->mPageOutCallback;
616 $this->mPageOutCallback = $callback;
617 return $previous;
618 }
619
620 /**
621 * Sets the action to perform as each page revision is reached.
622 * @param $callback callback
623 * @return callback
624 */
625 function setRevisionCallback( $callback ) {
626 $previous = $this->mRevisionCallback;
627 $this->mRevisionCallback = $callback;
628 return $previous;
629 }
630
631 /**
632 * Sets the action to perform as each file upload version is reached.
633 * @param $callback callback
634 * @return callback
635 */
636 function setUploadCallback( $callback ) {
637 $previous = $this->mUploadCallback;
638 $this->mUploadCallback = $callback;
639 return $previous;
640 }
641
642 /**
643 * Set a target namespace to override the defaults
644 */
645 function setTargetNamespace( $namespace ) {
646 if( is_null( $namespace ) ) {
647 // Don't override namespaces
648 $this->mTargetNamespace = null;
649 } elseif( $namespace >= 0 ) {
650 // FIXME: Check for validity
651 $this->mTargetNamespace = intval( $namespace );
652 } else {
653 return false;
654 }
655 }
656
657 /**
658 * Default per-revision callback, performs the import.
659 * @param $revision WikiRevision
660 * @private
661 */
662 function importRevision( $revision ) {
663 $dbw = wfGetDB( DB_MASTER );
664 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
665 }
666
667 /**
668 * Dummy for now...
669 */
670 function importUpload( $revision ) {
671 //$dbw = wfGetDB( DB_MASTER );
672 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
673 return false;
674 }
675
676 /**
677 * Alternate per-revision callback, for debugging.
678 * @param $revision WikiRevision
679 * @private
680 */
681 function debugRevisionHandler( &$revision ) {
682 $this->debug( "Got revision:" );
683 if( is_object( $revision->title ) ) {
684 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
685 } else {
686 $this->debug( "-- Title: <invalid>" );
687 }
688 $this->debug( "-- User: " . $revision->user_text );
689 $this->debug( "-- Timestamp: " . $revision->timestamp );
690 $this->debug( "-- Comment: " . $revision->comment );
691 $this->debug( "-- Text: " . $revision->text );
692 }
693
694 /**
695 * Notify the callback function when a new <page> is reached.
696 * @param $title Title
697 * @private
698 */
699 function pageCallback( $title ) {
700 if( is_callable( $this->mPageCallback ) ) {
701 call_user_func( $this->mPageCallback, $title );
702 }
703 }
704
705 /**
706 * Notify the callback function when a </page> is closed.
707 * @param $title Title
708 * @param $origTitle Title
709 * @param $revisionCount int
710 * @param $successCount Int: number of revisions for which callback returned true
711 * @private
712 */
713 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
714 if( is_callable( $this->mPageOutCallback ) ) {
715 call_user_func( $this->mPageOutCallback, $title, $origTitle,
716 $revisionCount, $successCount );
717 }
718 }
719
720
721 # XML parser callbacks from here out -- beware!
722 function donothing( $parser, $x, $y="" ) {
723 #$this->debug( "donothing" );
724 }
725
726 function in_start( $parser, $name, $attribs ) {
727 $this->debug( "in_start $name" );
728 if( $name != "mediawiki" ) {
729 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
730 }
731 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
732 }
733
734 function in_mediawiki( $parser, $name, $attribs ) {
735 $this->debug( "in_mediawiki $name" );
736 if( $name == 'siteinfo' ) {
737 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
738 } elseif( $name == 'page' ) {
739 $this->push( $name );
740 $this->workRevisionCount = 0;
741 $this->workSuccessCount = 0;
742 $this->uploadCount = 0;
743 $this->uploadSuccessCount = 0;
744 xml_set_element_handler( $parser, "in_page", "out_page" );
745 } else {
746 return $this->throwXMLerror( "Expected <page>, got <$name>" );
747 }
748 }
749 function out_mediawiki( $parser, $name ) {
750 $this->debug( "out_mediawiki $name" );
751 if( $name != "mediawiki" ) {
752 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
753 }
754 xml_set_element_handler( $parser, "donothing", "donothing" );
755 }
756
757
758 function in_siteinfo( $parser, $name, $attribs ) {
759 // no-ops for now
760 $this->debug( "in_siteinfo $name" );
761 switch( $name ) {
762 case "sitename":
763 case "base":
764 case "generator":
765 case "case":
766 case "namespaces":
767 case "namespace":
768 break;
769 default:
770 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
771 }
772 }
773
774 function out_siteinfo( $parser, $name ) {
775 if( $name == "siteinfo" ) {
776 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
777 }
778 }
779
780
781 function in_page( $parser, $name, $attribs ) {
782 $this->debug( "in_page $name" );
783 switch( $name ) {
784 case "id":
785 case "title":
786 case "restrictions":
787 $this->appendfield = $name;
788 $this->appenddata = "";
789 xml_set_element_handler( $parser, "in_nothing", "out_append" );
790 xml_set_character_data_handler( $parser, "char_append" );
791 break;
792 case "revision":
793 $this->push( "revision" );
794 if( is_object( $this->pageTitle ) ) {
795 $this->workRevision = new WikiRevision;
796 $this->workRevision->setTitle( $this->pageTitle );
797 $this->workRevisionCount++;
798 } else {
799 // Skipping items due to invalid page title
800 $this->workRevision = null;
801 }
802 xml_set_element_handler( $parser, "in_revision", "out_revision" );
803 break;
804 case "upload":
805 $this->push( "upload" );
806 if( is_object( $this->pageTitle ) ) {
807 $this->workRevision = new WikiRevision;
808 $this->workRevision->setTitle( $this->pageTitle );
809 $this->uploadCount++;
810 } else {
811 // Skipping items due to invalid page title
812 $this->workRevision = null;
813 }
814 xml_set_element_handler( $parser, "in_upload", "out_upload" );
815 break;
816 default:
817 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
818 }
819 }
820
821 function out_page( $parser, $name ) {
822 $this->debug( "out_page $name" );
823 $this->pop();
824 if( $name != "page" ) {
825 return $this->throwXMLerror( "Expected </page>, got </$name>" );
826 }
827 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
828
829 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
830 $this->workRevisionCount, $this->workSuccessCount );
831
832 $this->workTitle = null;
833 $this->workRevision = null;
834 $this->workRevisionCount = 0;
835 $this->workSuccessCount = 0;
836 $this->pageTitle = null;
837 $this->origTitle = null;
838 }
839
840 function in_nothing( $parser, $name, $attribs ) {
841 $this->debug( "in_nothing $name" );
842 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
843 }
844 function char_append( $parser, $data ) {
845 $this->debug( "char_append '$data'" );
846 $this->appenddata .= $data;
847 }
848 function out_append( $parser, $name ) {
849 $this->debug( "out_append $name" );
850 if( $name != $this->appendfield ) {
851 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
852 }
853
854 switch( $this->appendfield ) {
855 case "title":
856 $this->workTitle = $this->appenddata;
857 $this->origTitle = Title::newFromText( $this->workTitle );
858 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
859 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
860 $this->origTitle->getDBkey() );
861 } else {
862 $this->pageTitle = Title::newFromText( $this->workTitle );
863 }
864 if( is_null( $this->pageTitle ) ) {
865 // Invalid page title? Ignore the page
866 $this->notice( "Skipping invalid page title '$this->workTitle'" );
867 } else {
868 $this->pageCallback( $this->workTitle );
869 }
870 break;
871 case "id":
872 if ( $this->parentTag() == 'revision' ) {
873 if( $this->workRevision )
874 $this->workRevision->setID( $this->appenddata );
875 }
876 break;
877 case "text":
878 if( $this->workRevision )
879 $this->workRevision->setText( $this->appenddata );
880 break;
881 case "username":
882 if( $this->workRevision )
883 $this->workRevision->setUsername( $this->appenddata );
884 break;
885 case "ip":
886 if( $this->workRevision )
887 $this->workRevision->setUserIP( $this->appenddata );
888 break;
889 case "timestamp":
890 if( $this->workRevision )
891 $this->workRevision->setTimestamp( $this->appenddata );
892 break;
893 case "comment":
894 if( $this->workRevision )
895 $this->workRevision->setComment( $this->appenddata );
896 break;
897 case "minor":
898 if( $this->workRevision )
899 $this->workRevision->setMinor( true );
900 break;
901 case "filename":
902 if( $this->workRevision )
903 $this->workRevision->setFilename( $this->appenddata );
904 break;
905 case "src":
906 if( $this->workRevision )
907 $this->workRevision->setSrc( $this->appenddata );
908 break;
909 case "size":
910 if( $this->workRevision )
911 $this->workRevision->setSize( intval( $this->appenddata ) );
912 break;
913 default:
914 $this->debug( "Bad append: {$this->appendfield}" );
915 }
916 $this->appendfield = "";
917 $this->appenddata = "";
918
919 $parent = $this->parentTag();
920 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
921 xml_set_character_data_handler( $parser, "donothing" );
922 }
923
924 function in_revision( $parser, $name, $attribs ) {
925 $this->debug( "in_revision $name" );
926 switch( $name ) {
927 case "id":
928 case "timestamp":
929 case "comment":
930 case "minor":
931 case "text":
932 $this->appendfield = $name;
933 xml_set_element_handler( $parser, "in_nothing", "out_append" );
934 xml_set_character_data_handler( $parser, "char_append" );
935 break;
936 case "contributor":
937 $this->push( "contributor" );
938 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
939 break;
940 default:
941 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
942 }
943 }
944
945 function out_revision( $parser, $name ) {
946 $this->debug( "out_revision $name" );
947 $this->pop();
948 if( $name != "revision" ) {
949 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
950 }
951 xml_set_element_handler( $parser, "in_page", "out_page" );
952
953 if( $this->workRevision ) {
954 $ok = call_user_func_array( $this->mRevisionCallback,
955 array( $this->workRevision, $this ) );
956 if( $ok ) {
957 $this->workSuccessCount++;
958 }
959 }
960 }
961
962 function in_upload( $parser, $name, $attribs ) {
963 $this->debug( "in_upload $name" );
964 switch( $name ) {
965 case "timestamp":
966 case "comment":
967 case "text":
968 case "filename":
969 case "src":
970 case "size":
971 $this->appendfield = $name;
972 xml_set_element_handler( $parser, "in_nothing", "out_append" );
973 xml_set_character_data_handler( $parser, "char_append" );
974 break;
975 case "contributor":
976 $this->push( "contributor" );
977 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
978 break;
979 default:
980 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
981 }
982 }
983
984 function out_upload( $parser, $name ) {
985 $this->debug( "out_revision $name" );
986 $this->pop();
987 if( $name != "upload" ) {
988 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
989 }
990 xml_set_element_handler( $parser, "in_page", "out_page" );
991
992 if( $this->workRevision ) {
993 $ok = call_user_func_array( $this->mUploadCallback,
994 array( $this->workRevision, $this ) );
995 if( $ok ) {
996 $this->workUploadSuccessCount++;
997 }
998 }
999 }
1000
1001 function in_contributor( $parser, $name, $attribs ) {
1002 $this->debug( "in_contributor $name" );
1003 switch( $name ) {
1004 case "username":
1005 case "ip":
1006 case "id":
1007 $this->appendfield = $name;
1008 xml_set_element_handler( $parser, "in_nothing", "out_append" );
1009 xml_set_character_data_handler( $parser, "char_append" );
1010 break;
1011 default:
1012 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
1013 }
1014 }
1015
1016 function out_contributor( $parser, $name ) {
1017 $this->debug( "out_contributor $name" );
1018 $this->pop();
1019 if( $name != "contributor" ) {
1020 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
1021 }
1022 $parent = $this->parentTag();
1023 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
1024 }
1025
1026 private function push( $name ) {
1027 array_push( $this->tagStack, $name );
1028 $this->debug( "PUSH $name" );
1029 }
1030
1031 private function pop() {
1032 $name = array_pop( $this->tagStack );
1033 $this->debug( "POP $name" );
1034 return $name;
1035 }
1036
1037 private function parentTag() {
1038 $name = $this->tagStack[count( $this->tagStack ) - 1];
1039 $this->debug( "PARENT $name" );
1040 return $name;
1041 }
1042
1043 }
1044
1045 /**
1046 * @todo document (e.g. one-sentence class description).
1047 * @ingroup SpecialPage
1048 */
1049 class ImportStringSource {
1050 function __construct( $string ) {
1051 $this->mString = $string;
1052 $this->mRead = false;
1053 }
1054
1055 function atEnd() {
1056 return $this->mRead;
1057 }
1058
1059 function readChunk() {
1060 if( $this->atEnd() ) {
1061 return false;
1062 } else {
1063 $this->mRead = true;
1064 return $this->mString;
1065 }
1066 }
1067 }
1068
1069 /**
1070 * @todo document (e.g. one-sentence class description).
1071 * @ingroup SpecialPage
1072 */
1073 class ImportStreamSource {
1074 function __construct( $handle ) {
1075 $this->mHandle = $handle;
1076 }
1077
1078 function atEnd() {
1079 return feof( $this->mHandle );
1080 }
1081
1082 function readChunk() {
1083 return fread( $this->mHandle, 32768 );
1084 }
1085
1086 static function newFromFile( $filename ) {
1087 $file = @fopen( $filename, 'rt' );
1088 if( !$file ) {
1089 return new WikiErrorMsg( "importcantopen" );
1090 }
1091 return new ImportStreamSource( $file );
1092 }
1093
1094 static function newFromUpload( $fieldname = "xmlimport" ) {
1095 $upload =& $_FILES[$fieldname];
1096
1097 if( !isset( $upload ) || !$upload['name'] ) {
1098 return new WikiErrorMsg( 'importnofile' );
1099 }
1100 if( !empty( $upload['error'] ) ) {
1101 switch($upload['error']){
1102 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1103 return new WikiErrorMsg( 'importuploaderrorsize' );
1104 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1105 return new WikiErrorMsg( 'importuploaderrorsize' );
1106 case 3: # The uploaded file was only partially uploaded
1107 return new WikiErrorMsg( 'importuploaderrorpartial' );
1108 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1109 return new WikiErrorMsg( 'importuploaderrortemp' );
1110 # case else: # Currently impossible
1111 }
1112
1113 }
1114 $fname = $upload['tmp_name'];
1115 if( is_uploaded_file( $fname ) ) {
1116 return ImportStreamSource::newFromFile( $fname );
1117 } else {
1118 return new WikiErrorMsg( 'importnofile' );
1119 }
1120 }
1121
1122 static function newFromURL( $url, $method = 'GET' ) {
1123 wfDebug( __METHOD__ . ": opening $url\n" );
1124 # Use the standard HTTP fetch function; it times out
1125 # quicker and sorts out user-agent problems which might
1126 # otherwise prevent importing from large sites, such
1127 # as the Wikimedia cluster, etc.
1128 $data = Http::request( $method, $url );
1129 if( $data !== false ) {
1130 $file = tmpfile();
1131 fwrite( $file, $data );
1132 fflush( $file );
1133 fseek( $file, 0 );
1134 return new ImportStreamSource( $file );
1135 } else {
1136 return new WikiErrorMsg( 'importcantopen' );
1137 }
1138 }
1139
1140 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
1141 if( $page == '' ) {
1142 return new WikiErrorMsg( 'import-noarticle' );
1143 }
1144 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
1145 if( is_null( $link ) || $link->getInterwiki() == '' ) {
1146 return new WikiErrorMsg( 'importbadinterwiki' );
1147 } else {
1148 $params = $history ? 'history=1' : '';
1149 $url = $link->getFullUrl( $params );
1150 # For interwikis, use POST to avoid redirects.
1151 return ImportStreamSource::newFromURL( $url, "POST" );
1152 }
1153 }
1154 }