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