remove spurious htmlspecialchars() on input; remove unnecessary null checks
[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 $articleName = '';
34 $namespace = $wgImportTargetNamespace;
35 $frompage = '';
36 $history = true;
37
38 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
39 $isUpload = false;
40 $articleName = $wgRequest->getText( 'articlename' );
41 $namespace = $wgRequest->getIntOrNull( 'namespace' );
42
43 switch( $wgRequest->getVal( "source" ) ) {
44 case "upload":
45 $isUpload = true;
46 if( $wgUser->isAllowed( 'importupload' ) ) {
47 $source = ImportStreamSource::newFromUpload( "xmlimport" );
48 } else {
49 return $wgOut->permissionRequired( 'importupload' );
50 }
51 break;
52 case "interwiki":
53 $interwiki = $wgRequest->getVal( 'interwiki' );
54 $history = $wgRequest->getCheck( 'interwikiHistory' );
55 $frompage = $wgRequest->getText( "frompage" );
56 $source = ImportStreamSource::newFromInterwiki(
57 $interwiki,
58 $frompage,
59 $history );
60 break;
61 default:
62 $source = new WikiErrorMsg( "importunknownsource" );
63 }
64
65 if( WikiError::isError( $source ) ) {
66 $wgOut->addWikiText( '<p class="error">' . wfMsg( "importfailed", wfEscapeWikiText( $source->getMessage() ) ) . '</p>' );
67 } else {
68 $wgOut->addWikiText( wfMsg( "importstart" ) );
69
70 $importer = new WikiImporter( $source );
71 if( !is_null( $namespace ) ) {
72 $importer->setTargetNamespace( $namespace );
73 }
74 if( $articleName !== '' ) {
75 $importer->setTargetArticleName( $articleName );
76 }
77 $reporter = new ImportReporter( $importer, $isUpload, $interwiki, $frompage );
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->escapeLocalUrl( '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 Xml::hidden( 'MAX_FILE_SIZE', '2000000' ) .
108 "<input type='file' name='xmlimport' value='' size='30' />" . // No Xml function for type=file? Todo?
109 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
110 Xml::closeElement( 'form' ) .
111 Xml::closeElement( 'fieldset' ) );
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 foreach( $wgImportSources as $prefix ) {
131 $iw = htmlspecialchars( $prefix );
132 $selected = ($interwiki === $prefix) ? ' selected="selected"' : '';
133 $wgOut->addHTML( Xml::option( $iw, $iw, $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></td>
144 <td>" .
145 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
146 "</td>
147 </tr>
148 <tr>
149 <td></td>
150 <td>" .
151 Xml::inputLabel( wfMsg( 'import-articlename' ), 'articlename', 'articlename', 50, $articleName ) .
152 "</td>
153 </tr>
154 <tr>
155 <td></td>
156 <td>" .
157 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
158 Xml::namespaceSelector( $namespace, '' ) .
159 "</td>
160 </tr>
161 <tr>
162 <td></td>
163 <td>" .
164 Xml::submitButton( wfMsg( 'import-interwiki-submit' ) ) .
165 "</td>
166 </tr>" .
167 Xml::closeElement( 'table' ).
168 Xml::closeElement( 'form' ) .
169 Xml::closeElement( 'fieldset' ) );
170 }
171 }
172
173 /**
174 * Reporting callback
175 * @addtogroup SpecialPage
176 */
177 class ImportReporter {
178 function __construct( $importer, $upload, $interwiki, $frompage ) {
179 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
180 $this->mPageCount = 0;
181 $this->mIsUpload = $upload;
182 $this->mInterwiki = $interwiki;
183 $this->mFrompage = $frompage;
184 }
185
186 function open() {
187 global $wgOut;
188 $wgOut->addHtml( "<ul>\n" );
189 }
190
191 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
192 global $wgOut, $wgUser, $wgLang, $wgContLang;
193
194 $skin = $wgUser->getSkin();
195
196 $this->mPageCount++;
197
198 $localCount = $wgLang->formatNum( $successCount );
199 $contentCount = $wgContLang->formatNum( $successCount );
200
201 if( $successCount > 0 ) {
202 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
203 " " .
204 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
205 "</li>\n" );
206
207 $log = new LogPage( 'import' );
208 if( $this->mIsUpload ) {
209 $detail = wfMsgForContent( 'import-logentry-upload-detail',
210 $contentCount );
211 $log->addEntry( 'upload', $title, $detail );
212 } else {
213 // Show the source article name in log
214 $origin = $this->mFrompage != $origTitle->getPrefixedText()
215 ? $this->mFrompage
216 : $origTitle->getPrefixedText();
217 $interwiki = '[[:' . $this->mInterwiki . ':' . $origin . ']]';
218 $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
219 $contentCount, $interwiki );
220 $log->addEntry( 'interwiki', $title, $detail );
221 }
222
223 $comment = $detail; // quick
224 $dbw = wfGetDB( DB_MASTER );
225 $nullRevision = Revision::newNullRevision(
226 $dbw, $title->getArticleId(), $comment, true );
227 $nullRevision->insertOn( $dbw );
228 } else {
229 $wgOut->addHtml( '<li>' . wfMsgExt( 'import-nonewrevisions', array( 'parse', ) ) . '</li>' );
230 }
231
232 }
233
234 function close() {
235 global $wgOut;
236 if( $this->mPageCount == 0 ) {
237 $wgOut->addHtml( "</ul>\n" );
238 return new WikiErrorMsg( "importnopages" );
239 }
240 $wgOut->addHtml( "</ul>\n" );
241
242 return $this->mPageCount;
243 }
244 }
245
246 /**
247 *
248 * @addtogroup SpecialPage
249 */
250 class WikiRevision {
251 var $title = null;
252 var $id = 0;
253 var $timestamp = "20010115000000";
254 var $user = 0;
255 var $user_text = "";
256 var $text = "";
257 var $comment = "";
258 var $minor = false;
259
260 function setTitle( $title ) {
261 if( is_object( $title ) ) {
262 $this->title = $title;
263 } elseif( is_null( $title ) ) {
264 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
265 } else {
266 throw new MWException( "WikiRevision given non-object title in import." );
267 }
268 }
269
270 function setID( $id ) {
271 $this->id = $id;
272 }
273
274 function setTimestamp( $ts ) {
275 # 2003-08-05T18:30:02Z
276 $this->timestamp = wfTimestamp( TS_MW, $ts );
277 }
278
279 function setUsername( $user ) {
280 $this->user_text = $user;
281 }
282
283 function setUserIP( $ip ) {
284 $this->user_text = $ip;
285 }
286
287 function setText( $text ) {
288 $this->text = $text;
289 }
290
291 function setComment( $text ) {
292 $this->comment = $text;
293 }
294
295 function setMinor( $minor ) {
296 $this->minor = (bool)$minor;
297 }
298
299 function getTitle() {
300 return $this->title;
301 }
302
303 function getID() {
304 return $this->id;
305 }
306
307 function getTimestamp() {
308 return $this->timestamp;
309 }
310
311 function getUser() {
312 return $this->user_text;
313 }
314
315 function getText() {
316 return $this->text;
317 }
318
319 function getComment() {
320 return $this->comment;
321 }
322
323 function getMinor() {
324 return $this->minor;
325 }
326
327 function importOldRevision() {
328 $dbw = wfGetDB( DB_MASTER );
329
330 # Sneak a single revision into place
331 $user = User::newFromName( $this->getUser() );
332 if( $user ) {
333 $userId = intval( $user->getId() );
334 $userText = $user->getName();
335 } else {
336 $userId = 0;
337 $userText = $this->getUser();
338 }
339
340 // avoid memory leak...?
341 $linkCache =& LinkCache::singleton();
342 $linkCache->clear();
343
344 $article = new Article( $this->title );
345 $pageId = $article->getId();
346 if( $pageId == 0 ) {
347 # must create the page...
348 $pageId = $article->insertOn( $dbw );
349 $created = true;
350 } else {
351 $created = false;
352
353 $prior = Revision::loadFromTimestamp( $dbw, $this->title, $this->timestamp );
354 if( !is_null( $prior ) ) {
355 // FIXME: this could fail slightly for multiple matches :P
356 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
357 $this->title->getPrefixedText() . "]], timestamp " .
358 $this->timestamp . "\n" );
359 return false;
360 }
361 }
362
363 # FIXME: Use original rev_id optionally
364 # FIXME: blah blah blah
365
366 #if( $numrows > 0 ) {
367 # return wfMsg( "importhistoryconflict" );
368 #}
369
370 # Insert the row
371 $revision = new Revision( array(
372 'page' => $pageId,
373 'text' => $this->getText(),
374 'comment' => $this->getComment(),
375 'user' => $userId,
376 'user_text' => $userText,
377 'timestamp' => $this->timestamp,
378 'minor_edit' => $this->minor,
379 ) );
380 $revId = $revision->insertOn( $dbw );
381 $changed = $article->updateIfNewerOn( $dbw, $revision );
382
383 if( $created ) {
384 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
385 Article::onArticleCreate( $this->title );
386
387 wfDebug( __METHOD__ . ": running create updates\n" );
388 $article->createUpdates( $revision );
389
390 } elseif( $changed ) {
391 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
392 Article::onArticleEdit( $this->title );
393
394 wfDebug( __METHOD__ . ": running edit updates\n" );
395 $article->editUpdates(
396 $this->getText(),
397 $this->getComment(),
398 $this->minor,
399 $this->timestamp,
400 $revId );
401 }
402
403 return true;
404 }
405
406 }
407
408 /**
409 * implements Special:Import
410 * @addtogroup SpecialPage
411 */
412 class WikiImporter {
413 var $mSource = null;
414 var $mPageCallback = null;
415 var $mPageOutCallback = null;
416 var $mRevisionCallback = null;
417 var $mTargetNamespace = null;
418 var $mTargetArticleName = null;
419 var $lastfield;
420
421 function WikiImporter( $source ) {
422 $this->setRevisionCallback( array( &$this, "importRevision" ) );
423 $this->mSource = $source;
424 }
425
426 function throwXmlError( $err ) {
427 $this->debug( "FAILURE: $err" );
428 wfDebug( "WikiImporter XML error: $err\n" );
429 }
430
431 # --------------
432
433 function doImport() {
434 if( empty( $this->mSource ) ) {
435 return new WikiErrorMsg( "importnotext" );
436 }
437
438 $parser = xml_parser_create( "UTF-8" );
439
440 # case folding violates XML standard, turn it off
441 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
442
443 xml_set_object( $parser, $this );
444 xml_set_element_handler( $parser, "in_start", "" );
445
446 $offset = 0; // for context extraction on error reporting
447 do {
448 $chunk = $this->mSource->readChunk();
449 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
450 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
451 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
452 }
453 $offset += strlen( $chunk );
454 } while( $chunk !== false && !$this->mSource->atEnd() );
455 xml_parser_free( $parser );
456
457 return true;
458 }
459
460 function debug( $data ) {
461 #wfDebug( "IMPORT: $data\n" );
462 }
463
464 function notice( $data ) {
465 global $wgCommandLineMode;
466 if( $wgCommandLineMode ) {
467 print "$data\n";
468 } else {
469 global $wgOut;
470 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
471 }
472 }
473
474 /**
475 * Sets the action to perform as each new page in the stream is reached.
476 * @param callable $callback
477 * @return callable
478 */
479 function setPageCallback( $callback ) {
480 $previous = $this->mPageCallback;
481 $this->mPageCallback = $callback;
482 return $previous;
483 }
484
485 /**
486 * Sets the action to perform as each page in the stream is completed.
487 * Callback accepts the page title (as a Title object), a second object
488 * with the original title form (in case it's been overridden into a
489 * local namespace), and a count of revisions.
490 *
491 * @param callable $callback
492 * @return callable
493 */
494 function setPageOutCallback( $callback ) {
495 $previous = $this->mPageOutCallback;
496 $this->mPageOutCallback = $callback;
497 return $previous;
498 }
499
500 /**
501 * Sets the action to perform as each page revision is reached.
502 * @param callable $callback
503 * @return callable
504 */
505 function setRevisionCallback( $callback ) {
506 $previous = $this->mRevisionCallback;
507 $this->mRevisionCallback = $callback;
508 return $previous;
509 }
510
511 /**
512 * Set a target namespace to override the defaults
513 */
514 function setTargetNamespace( $namespace ) {
515 if( is_null( $namespace ) ) {
516 // Don't override namespaces
517 $this->mTargetNamespace = null;
518 } elseif( $namespace >= 0 ) {
519 // FIXME: Check for validity
520 $this->mTargetNamespace = intval( $namespace );
521 } else {
522 return false;
523 }
524 }
525
526 /**
527 * Set a target articlename to override the defaults
528 */
529 function setTargetArticleName( $articleName ) {
530 $this->mTargetArticleName = $articleName;
531 }
532
533 /**
534 * Default per-revision callback, performs the import.
535 * @param WikiRevision $revision
536 * @private
537 */
538 function importRevision( &$revision ) {
539 $dbw = wfGetDB( DB_MASTER );
540 return $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
541 }
542
543 /**
544 * Alternate per-revision callback, for debugging.
545 * @param WikiRevision $revision
546 * @private
547 */
548 function debugRevisionHandler( &$revision ) {
549 $this->debug( "Got revision:" );
550 if( is_object( $revision->title ) ) {
551 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
552 } else {
553 $this->debug( "-- Title: <invalid>" );
554 }
555 $this->debug( "-- User: " . $revision->user_text );
556 $this->debug( "-- Timestamp: " . $revision->timestamp );
557 $this->debug( "-- Comment: " . $revision->comment );
558 $this->debug( "-- Text: " . $revision->text );
559 }
560
561 /**
562 * Notify the callback function when a new <page> is reached.
563 * @param Title $title
564 * @private
565 */
566 function pageCallback( $title ) {
567 if( is_callable( $this->mPageCallback ) ) {
568 call_user_func( $this->mPageCallback, $title );
569 }
570 }
571
572 /**
573 * Notify the callback function when a </page> is closed.
574 * @param Title $title
575 * @param Title $origTitle
576 * @param int $revisionCount
577 * @param int $successCount number of revisions for which callback returned true
578 * @private
579 */
580 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
581 if( is_callable( $this->mPageOutCallback ) ) {
582 call_user_func( $this->mPageOutCallback, $title, $origTitle,
583 $revisionCount, $successCount );
584 }
585 }
586
587
588 # XML parser callbacks from here out -- beware!
589 function donothing( $parser, $x, $y="" ) {
590 #$this->debug( "donothing" );
591 }
592
593 function in_start( $parser, $name, $attribs ) {
594 $this->debug( "in_start $name" );
595 if( $name != "mediawiki" ) {
596 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
597 }
598 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
599 }
600
601 function in_mediawiki( $parser, $name, $attribs ) {
602 $this->debug( "in_mediawiki $name" );
603 if( $name == 'siteinfo' ) {
604 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
605 } elseif( $name == 'page' ) {
606 $this->workRevisionCount = 0;
607 $this->workSuccessCount = 0;
608 xml_set_element_handler( $parser, "in_page", "out_page" );
609 } else {
610 return $this->throwXMLerror( "Expected <page>, got <$name>" );
611 }
612 }
613 function out_mediawiki( $parser, $name ) {
614 $this->debug( "out_mediawiki $name" );
615 if( $name != "mediawiki" ) {
616 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
617 }
618 xml_set_element_handler( $parser, "donothing", "donothing" );
619 }
620
621
622 function in_siteinfo( $parser, $name, $attribs ) {
623 // no-ops for now
624 $this->debug( "in_siteinfo $name" );
625 switch( $name ) {
626 case "sitename":
627 case "base":
628 case "generator":
629 case "case":
630 case "namespaces":
631 case "namespace":
632 break;
633 default:
634 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
635 }
636 }
637
638 function out_siteinfo( $parser, $name ) {
639 if( $name == "siteinfo" ) {
640 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
641 }
642 }
643
644
645 function in_page( $parser, $name, $attribs ) {
646 $this->debug( "in_page $name" );
647 switch( $name ) {
648 case "id":
649 case "title":
650 case "restrictions":
651 $this->appendfield = $name;
652 $this->appenddata = "";
653 $this->parenttag = "page";
654 xml_set_element_handler( $parser, "in_nothing", "out_append" );
655 xml_set_character_data_handler( $parser, "char_append" );
656 break;
657 case "revision":
658 if( is_object( $this->pageTitle ) ) {
659 $this->workRevision = new WikiRevision;
660 $this->workRevision->setTitle( $this->pageTitle );
661 $this->workRevisionCount++;
662 } else {
663 // Skipping items due to invalid page title
664 $this->workRevision = null;
665 }
666 xml_set_element_handler( $parser, "in_revision", "out_revision" );
667 break;
668 default:
669 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
670 }
671 }
672
673 function out_page( $parser, $name ) {
674 $this->debug( "out_page $name" );
675 if( $name != "page" ) {
676 return $this->throwXMLerror( "Expected </page>, got </$name>" );
677 }
678 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
679
680 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
681 $this->workRevisionCount, $this->workSuccessCount );
682
683 $this->workTitle = null;
684 $this->workRevision = null;
685 $this->workRevisionCount = 0;
686 $this->workSuccessCount = 0;
687 $this->pageTitle = null;
688 $this->origTitle = null;
689 }
690
691 function in_nothing( $parser, $name, $attribs ) {
692 $this->debug( "in_nothing $name" );
693 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
694 }
695 function char_append( $parser, $data ) {
696 $this->debug( "char_append '$data'" );
697 $this->appenddata .= $data;
698 }
699 function out_append( $parser, $name ) {
700 $this->debug( "out_append $name" );
701 if( $name != $this->appendfield ) {
702 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
703 }
704 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
705 xml_set_character_data_handler( $parser, "donothing" );
706
707 switch( $this->appendfield ) {
708 case "title":
709 $this->workTitle = $this->appenddata;
710
711 if( $this->mTargetArticleName != '' ) {
712 // Import article with a different target article name
713 $this->workTitle = $this->mTargetArticleName;
714 }
715 $this->origTitle = Title::newFromText( $this->workTitle );
716 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
717 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
718 $this->origTitle->getDbKey() );
719 } else {
720 $this->pageTitle = Title::newFromText( $this->workTitle );
721 }
722 if( is_null( $this->pageTitle ) ) {
723 // Invalid page title? Ignore the page
724 $this->notice( "Skipping invalid page title '$this->workTitle'" );
725 } else {
726 $this->pageCallback( $this->workTitle );
727 }
728 break;
729 case "id":
730 if ( $this->parenttag == 'revision' ) {
731 if( $this->workRevision )
732 $this->workRevision->setID( $this->appenddata );
733 }
734 break;
735 case "text":
736 if( $this->workRevision )
737 $this->workRevision->setText( $this->appenddata );
738 break;
739 case "username":
740 if( $this->workRevision )
741 $this->workRevision->setUsername( $this->appenddata );
742 break;
743 case "ip":
744 if( $this->workRevision )
745 $this->workRevision->setUserIP( $this->appenddata );
746 break;
747 case "timestamp":
748 if( $this->workRevision )
749 $this->workRevision->setTimestamp( $this->appenddata );
750 break;
751 case "comment":
752 if( $this->workRevision )
753 $this->workRevision->setComment( $this->appenddata );
754 break;
755 case "minor":
756 if( $this->workRevision )
757 $this->workRevision->setMinor( true );
758 break;
759 default:
760 $this->debug( "Bad append: {$this->appendfield}" );
761 }
762 $this->appendfield = "";
763 $this->appenddata = "";
764 }
765
766 function in_revision( $parser, $name, $attribs ) {
767 $this->debug( "in_revision $name" );
768 switch( $name ) {
769 case "id":
770 case "timestamp":
771 case "comment":
772 case "minor":
773 case "text":
774 $this->parenttag = "revision";
775 $this->appendfield = $name;
776 xml_set_element_handler( $parser, "in_nothing", "out_append" );
777 xml_set_character_data_handler( $parser, "char_append" );
778 break;
779 case "contributor":
780 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
781 break;
782 default:
783 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
784 }
785 }
786
787 function out_revision( $parser, $name ) {
788 $this->debug( "out_revision $name" );
789 if( $name != "revision" ) {
790 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
791 }
792 xml_set_element_handler( $parser, "in_page", "out_page" );
793
794 if( $this->workRevision ) {
795 $ok = call_user_func_array( $this->mRevisionCallback,
796 array( &$this->workRevision, &$this ) );
797 if( $ok ) {
798 $this->workSuccessCount++;
799 }
800 }
801 }
802
803 function in_contributor( $parser, $name, $attribs ) {
804 $this->debug( "in_contributor $name" );
805 switch( $name ) {
806 case "username":
807 case "ip":
808 case "id":
809 $this->parenttag = "contributor";
810 $this->appendfield = $name;
811 xml_set_element_handler( $parser, "in_nothing", "out_append" );
812 xml_set_character_data_handler( $parser, "char_append" );
813 break;
814 default:
815 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
816 }
817 }
818
819 function out_contributor( $parser, $name ) {
820 $this->debug( "out_contributor $name" );
821 if( $name != "contributor" ) {
822 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
823 }
824 xml_set_element_handler( $parser, "in_revision", "out_revision" );
825 }
826
827 }
828
829 /**
830 * @todo document (e.g. one-sentence class description).
831 * @addtogroup SpecialPage
832 */
833 class ImportStringSource {
834 function ImportStringSource( $string ) {
835 $this->mString = $string;
836 $this->mRead = false;
837 }
838
839 function atEnd() {
840 return $this->mRead;
841 }
842
843 function readChunk() {
844 if( $this->atEnd() ) {
845 return false;
846 } else {
847 $this->mRead = true;
848 return $this->mString;
849 }
850 }
851 }
852
853 /**
854 * @todo document (e.g. one-sentence class description).
855 * @addtogroup SpecialPage
856 */
857 class ImportStreamSource {
858 function ImportStreamSource( $handle ) {
859 $this->mHandle = $handle;
860 }
861
862 function atEnd() {
863 return feof( $this->mHandle );
864 }
865
866 function readChunk() {
867 return fread( $this->mHandle, 32768 );
868 }
869
870 static function newFromFile( $filename ) {
871 $file = @fopen( $filename, 'rt' );
872 if( !$file ) {
873 return new WikiErrorMsg( "importcantopen" );
874 }
875 return new ImportStreamSource( $file );
876 }
877
878 static function newFromUpload( $fieldname = "xmlimport" ) {
879 $upload =& $_FILES[$fieldname];
880
881 if( !isset( $upload ) || !$upload['name'] ) {
882 return new WikiErrorMsg( 'importnofile' );
883 }
884 if( !empty( $upload['error'] ) ) {
885 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
886 }
887 $fname = $upload['tmp_name'];
888 if( is_uploaded_file( $fname ) ) {
889 return ImportStreamSource::newFromFile( $fname );
890 } else {
891 return new WikiErrorMsg( 'importnofile' );
892 }
893 }
894
895 function newFromURL( $url, $method = 'GET' ) {
896 wfDebug( __METHOD__ . ": opening $url\n" );
897 # Use the standard HTTP fetch function; it times out
898 # quicker and sorts out user-agent problems which might
899 # otherwise prevent importing from large sites, such
900 # as the Wikimedia cluster, etc.
901 $data = Http::request( $method, $url );
902 if( $data !== false ) {
903 $file = tmpfile();
904 fwrite( $file, $data );
905 fflush( $file );
906 fseek( $file, 0 );
907 return new ImportStreamSource( $file );
908 } else {
909 return new WikiErrorMsg( 'importcantopen' );
910 }
911 }
912
913 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
914 if( $page == '' ) {
915 return new WikiErrorMsg( 'import-noarticle' );
916 }
917 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
918 if( is_null( $link ) || $link->getInterwiki() == '' ) {
919 return new WikiErrorMsg( 'importbadinterwiki' );
920 } else {
921 $params = $history ? 'history=1' : '';
922 $url = $link->getFullUrl( $params );
923 # For interwikis, use POST to avoid redirects.
924 return ImportStreamSource::newFromURL( $url, "POST" );
925 }
926 }
927 }
928
929
930