* (bug 12938) Fix template expansion and 404 returns for action=raw with section
[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->addWikiText( '<p class="error">' . wfMsg( 'importfailed', wfEscapeWikiText( $source->getMessage() ) ) . '</p>' );
70 } else {
71 $wgOut->addWikiText( wfMsg( "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->addWikiText( '<p class="error">' . wfMsg( 'importfailed', wfEscapeWikiText( $result->getMessage() ) ) . '</p>' );
86 } elseif( WikiError::isError( $resultCount ) ) {
87 # Zero revisions
88 $wgOut->addWikiText( '<p class="error">' . wfMsg( 'importfailed', wfEscapeWikiText( $resultCount->getMessage() ) ) . '</p>' );
89 } else {
90 # Success!
91 $wgOut->addWikiText( wfMsg( 'importsuccess' ) );
92 }
93 $wgOut->addWikiText( '<hr />' );
94 }
95 }
96
97 $action = $wgTitle->getLocalUrl( 'action=submit' );
98
99 if( $wgUser->isAllowed( 'importupload' ) ) {
100 $wgOut->addWikiText( wfMsg( "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 "<input type='file' name='xmlimport' value='' size='30' />" . // No Xml function for type=file? Todo?
108 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
109 Xml::closeElement( 'form' ) .
110 Xml::closeElement( 'fieldset' )
111 );
112 } else {
113 if( empty( $wgImportSources ) ) {
114 $wgOut->addWikiText( wfMsg( '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' ) .
127 "<tr>
128 <td>" .
129 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
130 );
131 foreach( $wgImportSources as $prefix ) {
132 $iw = htmlspecialchars( $prefix );
133 $selected = ($interwiki === $prefix) ? ' selected="selected"' : '';
134 $wgOut->addHTML( Xml::option( $iw, $iw, $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 * @addtogroup 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(
221 $dbw, $title->getArticleId(), $comment, true );
222 $nullRevision->insertOn( $dbw );
223 # Update page record
224 $article = new Article( $title );
225 $article->updateRevisionOn( $dbw, $nullRevision );
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 * @addtogroup 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 getTitle() {
297 return $this->title;
298 }
299
300 function getID() {
301 return $this->id;
302 }
303
304 function getTimestamp() {
305 return $this->timestamp;
306 }
307
308 function getUser() {
309 return $this->user_text;
310 }
311
312 function getText() {
313 return $this->text;
314 }
315
316 function getComment() {
317 return $this->comment;
318 }
319
320 function getMinor() {
321 return $this->minor;
322 }
323
324 function importOldRevision() {
325 $dbw = wfGetDB( DB_MASTER );
326
327 # Sneak a single revision into place
328 $user = User::newFromName( $this->getUser() );
329 if( $user ) {
330 $userId = intval( $user->getId() );
331 $userText = $user->getName();
332 } else {
333 $userId = 0;
334 $userText = $this->getUser();
335 }
336
337 // avoid memory leak...?
338 $linkCache =& LinkCache::singleton();
339 $linkCache->clear();
340
341 $article = new Article( $this->title );
342 $pageId = $article->getId();
343 if( $pageId == 0 ) {
344 # must create the page...
345 $pageId = $article->insertOn( $dbw );
346 $created = true;
347 } else {
348 $created = false;
349
350 $prior = Revision::loadFromTimestamp( $dbw, $this->title, $this->timestamp );
351 if( !is_null( $prior ) ) {
352 // FIXME: this could fail slightly for multiple matches :P
353 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
354 $this->title->getPrefixedText() . "]], timestamp " .
355 $this->timestamp . "\n" );
356 return false;
357 }
358 }
359
360 # FIXME: Use original rev_id optionally
361 # FIXME: blah blah blah
362
363 #if( $numrows > 0 ) {
364 # return wfMsg( "importhistoryconflict" );
365 #}
366
367 # Insert the row
368 $revision = new Revision( array(
369 'page' => $pageId,
370 'text' => $this->getText(),
371 'comment' => $this->getComment(),
372 'user' => $userId,
373 'user_text' => $userText,
374 'timestamp' => $this->timestamp,
375 'minor_edit' => $this->minor,
376 ) );
377 $revId = $revision->insertOn( $dbw );
378 $changed = $article->updateIfNewerOn( $dbw, $revision );
379
380 if( $created ) {
381 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
382 Article::onArticleCreate( $this->title );
383
384 wfDebug( __METHOD__ . ": running create updates\n" );
385 $article->createUpdates( $revision );
386
387 } elseif( $changed ) {
388 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
389 Article::onArticleEdit( $this->title );
390
391 wfDebug( __METHOD__ . ": running edit updates\n" );
392 $article->editUpdates(
393 $this->getText(),
394 $this->getComment(),
395 $this->minor,
396 $this->timestamp,
397 $revId );
398 }
399
400 return true;
401 }
402
403 }
404
405 /**
406 * implements Special:Import
407 * @addtogroup SpecialPage
408 */
409 class WikiImporter {
410 var $mSource = null;
411 var $mPageCallback = null;
412 var $mPageOutCallback = null;
413 var $mRevisionCallback = null;
414 var $mTargetNamespace = null;
415 var $lastfield;
416
417 function WikiImporter( $source ) {
418 $this->setRevisionCallback( array( &$this, "importRevision" ) );
419 $this->mSource = $source;
420 }
421
422 function throwXmlError( $err ) {
423 $this->debug( "FAILURE: $err" );
424 wfDebug( "WikiImporter XML error: $err\n" );
425 }
426
427 # --------------
428
429 function doImport() {
430 if( empty( $this->mSource ) ) {
431 return new WikiErrorMsg( "importnotext" );
432 }
433
434 $parser = xml_parser_create( "UTF-8" );
435
436 # case folding violates XML standard, turn it off
437 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
438
439 xml_set_object( $parser, $this );
440 xml_set_element_handler( $parser, "in_start", "" );
441
442 $offset = 0; // for context extraction on error reporting
443 do {
444 $chunk = $this->mSource->readChunk();
445 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
446 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
447 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
448 }
449 $offset += strlen( $chunk );
450 } while( $chunk !== false && !$this->mSource->atEnd() );
451 xml_parser_free( $parser );
452
453 return true;
454 }
455
456 function debug( $data ) {
457 #wfDebug( "IMPORT: $data\n" );
458 }
459
460 function notice( $data ) {
461 global $wgCommandLineMode;
462 if( $wgCommandLineMode ) {
463 print "$data\n";
464 } else {
465 global $wgOut;
466 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
467 }
468 }
469
470 /**
471 * Sets the action to perform as each new page in the stream is reached.
472 * @param callable $callback
473 * @return callable
474 */
475 function setPageCallback( $callback ) {
476 $previous = $this->mPageCallback;
477 $this->mPageCallback = $callback;
478 return $previous;
479 }
480
481 /**
482 * Sets the action to perform as each page in the stream is completed.
483 * Callback accepts the page title (as a Title object), a second object
484 * with the original title form (in case it's been overridden into a
485 * local namespace), and a count of revisions.
486 *
487 * @param callable $callback
488 * @return callable
489 */
490 function setPageOutCallback( $callback ) {
491 $previous = $this->mPageOutCallback;
492 $this->mPageOutCallback = $callback;
493 return $previous;
494 }
495
496 /**
497 * Sets the action to perform as each page revision is reached.
498 * @param callable $callback
499 * @return callable
500 */
501 function setRevisionCallback( $callback ) {
502 $previous = $this->mRevisionCallback;
503 $this->mRevisionCallback = $callback;
504 return $previous;
505 }
506
507 /**
508 * Set a target namespace to override the defaults
509 */
510 function setTargetNamespace( $namespace ) {
511 if( is_null( $namespace ) ) {
512 // Don't override namespaces
513 $this->mTargetNamespace = null;
514 } elseif( $namespace >= 0 ) {
515 // FIXME: Check for validity
516 $this->mTargetNamespace = intval( $namespace );
517 } else {
518 return false;
519 }
520 }
521
522 /**
523 * Default per-revision callback, performs the import.
524 * @param WikiRevision $revision
525 * @private
526 */
527 function importRevision( &$revision ) {
528 $dbw = wfGetDB( DB_MASTER );
529 return $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
530 }
531
532 /**
533 * Alternate per-revision callback, for debugging.
534 * @param WikiRevision $revision
535 * @private
536 */
537 function debugRevisionHandler( &$revision ) {
538 $this->debug( "Got revision:" );
539 if( is_object( $revision->title ) ) {
540 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
541 } else {
542 $this->debug( "-- Title: <invalid>" );
543 }
544 $this->debug( "-- User: " . $revision->user_text );
545 $this->debug( "-- Timestamp: " . $revision->timestamp );
546 $this->debug( "-- Comment: " . $revision->comment );
547 $this->debug( "-- Text: " . $revision->text );
548 }
549
550 /**
551 * Notify the callback function when a new <page> is reached.
552 * @param Title $title
553 * @private
554 */
555 function pageCallback( $title ) {
556 if( is_callable( $this->mPageCallback ) ) {
557 call_user_func( $this->mPageCallback, $title );
558 }
559 }
560
561 /**
562 * Notify the callback function when a </page> is closed.
563 * @param Title $title
564 * @param Title $origTitle
565 * @param int $revisionCount
566 * @param int $successCount number of revisions for which callback returned true
567 * @private
568 */
569 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
570 if( is_callable( $this->mPageOutCallback ) ) {
571 call_user_func( $this->mPageOutCallback, $title, $origTitle,
572 $revisionCount, $successCount );
573 }
574 }
575
576
577 # XML parser callbacks from here out -- beware!
578 function donothing( $parser, $x, $y="" ) {
579 #$this->debug( "donothing" );
580 }
581
582 function in_start( $parser, $name, $attribs ) {
583 $this->debug( "in_start $name" );
584 if( $name != "mediawiki" ) {
585 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
586 }
587 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
588 }
589
590 function in_mediawiki( $parser, $name, $attribs ) {
591 $this->debug( "in_mediawiki $name" );
592 if( $name == 'siteinfo' ) {
593 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
594 } elseif( $name == 'page' ) {
595 $this->workRevisionCount = 0;
596 $this->workSuccessCount = 0;
597 xml_set_element_handler( $parser, "in_page", "out_page" );
598 } else {
599 return $this->throwXMLerror( "Expected <page>, got <$name>" );
600 }
601 }
602 function out_mediawiki( $parser, $name ) {
603 $this->debug( "out_mediawiki $name" );
604 if( $name != "mediawiki" ) {
605 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
606 }
607 xml_set_element_handler( $parser, "donothing", "donothing" );
608 }
609
610
611 function in_siteinfo( $parser, $name, $attribs ) {
612 // no-ops for now
613 $this->debug( "in_siteinfo $name" );
614 switch( $name ) {
615 case "sitename":
616 case "base":
617 case "generator":
618 case "case":
619 case "namespaces":
620 case "namespace":
621 break;
622 default:
623 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
624 }
625 }
626
627 function out_siteinfo( $parser, $name ) {
628 if( $name == "siteinfo" ) {
629 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
630 }
631 }
632
633
634 function in_page( $parser, $name, $attribs ) {
635 $this->debug( "in_page $name" );
636 switch( $name ) {
637 case "id":
638 case "title":
639 case "restrictions":
640 $this->appendfield = $name;
641 $this->appenddata = "";
642 $this->parenttag = "page";
643 xml_set_element_handler( $parser, "in_nothing", "out_append" );
644 xml_set_character_data_handler( $parser, "char_append" );
645 break;
646 case "revision":
647 if( is_object( $this->pageTitle ) ) {
648 $this->workRevision = new WikiRevision;
649 $this->workRevision->setTitle( $this->pageTitle );
650 $this->workRevisionCount++;
651 } else {
652 // Skipping items due to invalid page title
653 $this->workRevision = null;
654 }
655 xml_set_element_handler( $parser, "in_revision", "out_revision" );
656 break;
657 default:
658 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
659 }
660 }
661
662 function out_page( $parser, $name ) {
663 $this->debug( "out_page $name" );
664 if( $name != "page" ) {
665 return $this->throwXMLerror( "Expected </page>, got </$name>" );
666 }
667 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
668
669 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
670 $this->workRevisionCount, $this->workSuccessCount );
671
672 $this->workTitle = null;
673 $this->workRevision = null;
674 $this->workRevisionCount = 0;
675 $this->workSuccessCount = 0;
676 $this->pageTitle = null;
677 $this->origTitle = null;
678 }
679
680 function in_nothing( $parser, $name, $attribs ) {
681 $this->debug( "in_nothing $name" );
682 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
683 }
684 function char_append( $parser, $data ) {
685 $this->debug( "char_append '$data'" );
686 $this->appenddata .= $data;
687 }
688 function out_append( $parser, $name ) {
689 $this->debug( "out_append $name" );
690 if( $name != $this->appendfield ) {
691 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
692 }
693 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
694 xml_set_character_data_handler( $parser, "donothing" );
695
696 switch( $this->appendfield ) {
697 case "title":
698 $this->workTitle = $this->appenddata;
699 $this->origTitle = Title::newFromText( $this->workTitle );
700 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
701 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
702 $this->origTitle->getDBkey() );
703 } else {
704 $this->pageTitle = Title::newFromText( $this->workTitle );
705 }
706 if( is_null( $this->pageTitle ) ) {
707 // Invalid page title? Ignore the page
708 $this->notice( "Skipping invalid page title '$this->workTitle'" );
709 } else {
710 $this->pageCallback( $this->workTitle );
711 }
712 break;
713 case "id":
714 if ( $this->parenttag == 'revision' ) {
715 if( $this->workRevision )
716 $this->workRevision->setID( $this->appenddata );
717 }
718 break;
719 case "text":
720 if( $this->workRevision )
721 $this->workRevision->setText( $this->appenddata );
722 break;
723 case "username":
724 if( $this->workRevision )
725 $this->workRevision->setUsername( $this->appenddata );
726 break;
727 case "ip":
728 if( $this->workRevision )
729 $this->workRevision->setUserIP( $this->appenddata );
730 break;
731 case "timestamp":
732 if( $this->workRevision )
733 $this->workRevision->setTimestamp( $this->appenddata );
734 break;
735 case "comment":
736 if( $this->workRevision )
737 $this->workRevision->setComment( $this->appenddata );
738 break;
739 case "minor":
740 if( $this->workRevision )
741 $this->workRevision->setMinor( true );
742 break;
743 default:
744 $this->debug( "Bad append: {$this->appendfield}" );
745 }
746 $this->appendfield = "";
747 $this->appenddata = "";
748 }
749
750 function in_revision( $parser, $name, $attribs ) {
751 $this->debug( "in_revision $name" );
752 switch( $name ) {
753 case "id":
754 case "timestamp":
755 case "comment":
756 case "minor":
757 case "text":
758 $this->parenttag = "revision";
759 $this->appendfield = $name;
760 xml_set_element_handler( $parser, "in_nothing", "out_append" );
761 xml_set_character_data_handler( $parser, "char_append" );
762 break;
763 case "contributor":
764 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
765 break;
766 default:
767 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
768 }
769 }
770
771 function out_revision( $parser, $name ) {
772 $this->debug( "out_revision $name" );
773 if( $name != "revision" ) {
774 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
775 }
776 xml_set_element_handler( $parser, "in_page", "out_page" );
777
778 if( $this->workRevision ) {
779 $ok = call_user_func_array( $this->mRevisionCallback,
780 array( &$this->workRevision, &$this ) );
781 if( $ok ) {
782 $this->workSuccessCount++;
783 }
784 }
785 }
786
787 function in_contributor( $parser, $name, $attribs ) {
788 $this->debug( "in_contributor $name" );
789 switch( $name ) {
790 case "username":
791 case "ip":
792 case "id":
793 $this->parenttag = "contributor";
794 $this->appendfield = $name;
795 xml_set_element_handler( $parser, "in_nothing", "out_append" );
796 xml_set_character_data_handler( $parser, "char_append" );
797 break;
798 default:
799 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
800 }
801 }
802
803 function out_contributor( $parser, $name ) {
804 $this->debug( "out_contributor $name" );
805 if( $name != "contributor" ) {
806 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
807 }
808 xml_set_element_handler( $parser, "in_revision", "out_revision" );
809 }
810
811 }
812
813 /**
814 * @todo document (e.g. one-sentence class description).
815 * @addtogroup SpecialPage
816 */
817 class ImportStringSource {
818 function ImportStringSource( $string ) {
819 $this->mString = $string;
820 $this->mRead = false;
821 }
822
823 function atEnd() {
824 return $this->mRead;
825 }
826
827 function readChunk() {
828 if( $this->atEnd() ) {
829 return false;
830 } else {
831 $this->mRead = true;
832 return $this->mString;
833 }
834 }
835 }
836
837 /**
838 * @todo document (e.g. one-sentence class description).
839 * @addtogroup SpecialPage
840 */
841 class ImportStreamSource {
842 function ImportStreamSource( $handle ) {
843 $this->mHandle = $handle;
844 }
845
846 function atEnd() {
847 return feof( $this->mHandle );
848 }
849
850 function readChunk() {
851 return fread( $this->mHandle, 32768 );
852 }
853
854 static function newFromFile( $filename ) {
855 $file = @fopen( $filename, 'rt' );
856 if( !$file ) {
857 return new WikiErrorMsg( "importcantopen" );
858 }
859 return new ImportStreamSource( $file );
860 }
861
862 static function newFromUpload( $fieldname = "xmlimport" ) {
863 $upload =& $_FILES[$fieldname];
864
865 if( !isset( $upload ) || !$upload['name'] ) {
866 return new WikiErrorMsg( 'importnofile' );
867 }
868 if( !empty( $upload['error'] ) ) {
869 switch($upload['error']){
870 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
871 return new WikiErrorMsg( 'importuploaderrorsize' );
872 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
873 return new WikiErrorMsg( 'importuploaderrorsize' );
874 case 3: # The uploaded file was only partially uploaded
875 return new WikiErrorMsg( 'importuploaderrorpartial' );
876 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
877 return new WikiErrorMsg( 'importuploaderrortemp' );
878 # case else: # Currently impossible
879 }
880
881 }
882 $fname = $upload['tmp_name'];
883 if( is_uploaded_file( $fname ) ) {
884 return ImportStreamSource::newFromFile( $fname );
885 } else {
886 return new WikiErrorMsg( 'importnofile' );
887 }
888 }
889
890 function newFromURL( $url, $method = 'GET' ) {
891 wfDebug( __METHOD__ . ": opening $url\n" );
892 # Use the standard HTTP fetch function; it times out
893 # quicker and sorts out user-agent problems which might
894 # otherwise prevent importing from large sites, such
895 # as the Wikimedia cluster, etc.
896 $data = Http::request( $method, $url );
897 if( $data !== false ) {
898 $file = tmpfile();
899 fwrite( $file, $data );
900 fflush( $file );
901 fseek( $file, 0 );
902 return new ImportStreamSource( $file );
903 } else {
904 return new WikiErrorMsg( 'importcantopen' );
905 }
906 }
907
908 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
909 if( $page == '' ) {
910 return new WikiErrorMsg( 'import-noarticle' );
911 }
912 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
913 if( is_null( $link ) || $link->getInterwiki() == '' ) {
914 return new WikiErrorMsg( 'importbadinterwiki' );
915 } else {
916 $params = $history ? 'history=1' : '';
917 $url = $link->getFullUrl( $params );
918 # For interwikis, use POST to avoid redirects.
919 return ImportStreamSource::newFromURL( $url, "POST" );
920 }
921 }
922 }