* (bug 6808) Allow import to different page name
[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 = htmlspecialchars( $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( !is_null( $articleName ) ) {
75 $importer->setTargetArticleName( $articleName );
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->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 ) {
179 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
180 $this->mPageCount = 0;
181 $this->mIsUpload = $upload;
182 $this->mInterwiki = $interwiki;
183 }
184
185 function open() {
186 global $wgOut;
187 $wgOut->addHtml( "<ul>\n" );
188 }
189
190 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
191 global $wgOut, $wgUser, $wgLang, $wgContLang;
192
193 $skin = $wgUser->getSkin();
194
195 $this->mPageCount++;
196
197 $localCount = $wgLang->formatNum( $successCount );
198 $contentCount = $wgContLang->formatNum( $successCount );
199
200 if( $successCount > 0 ) {
201 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
202 " " .
203 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
204 "</li>\n" );
205
206 $log = new LogPage( 'import' );
207 if( $this->mIsUpload ) {
208 $detail = wfMsgForContent( 'import-logentry-upload-detail',
209 $contentCount );
210 $log->addEntry( 'upload', $title, $detail );
211 } else {
212 $interwiki = '[[:' . $this->mInterwiki . ':' .
213 $origTitle->getPrefixedText() . ']]';
214 $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
215 $contentCount, $interwiki );
216 $log->addEntry( 'interwiki', $title, $detail );
217 }
218
219 $comment = $detail; // quick
220 $dbw = wfGetDB( DB_MASTER );
221 $nullRevision = Revision::newNullRevision(
222 $dbw, $title->getArticleId(), $comment, true );
223 $nullRevision->insertOn( $dbw );
224 } else {
225 $wgOut->addHtml( '<li>' . wfMsgExt( 'import-nonewrevisions', array( 'parse', ) ) . '</li>' );
226 }
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 getTitle() {
296 return $this->title;
297 }
298
299 function getID() {
300 return $this->id;
301 }
302
303 function getTimestamp() {
304 return $this->timestamp;
305 }
306
307 function getUser() {
308 return $this->user_text;
309 }
310
311 function getText() {
312 return $this->text;
313 }
314
315 function getComment() {
316 return $this->comment;
317 }
318
319 function getMinor() {
320 return $this->minor;
321 }
322
323 function importOldRevision() {
324 $dbw = wfGetDB( DB_MASTER );
325
326 # Sneak a single revision into place
327 $user = User::newFromName( $this->getUser() );
328 if( $user ) {
329 $userId = intval( $user->getId() );
330 $userText = $user->getName();
331 } else {
332 $userId = 0;
333 $userText = $this->getUser();
334 }
335
336 // avoid memory leak...?
337 $linkCache =& LinkCache::singleton();
338 $linkCache->clear();
339
340 $article = new Article( $this->title );
341 $pageId = $article->getId();
342 if( $pageId == 0 ) {
343 # must create the page...
344 $pageId = $article->insertOn( $dbw );
345 $created = true;
346 } else {
347 $created = false;
348
349 $prior = Revision::loadFromTimestamp( $dbw, $this->title, $this->timestamp );
350 if( !is_null( $prior ) ) {
351 // FIXME: this could fail slightly for multiple matches :P
352 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
353 $this->title->getPrefixedText() . "]], timestamp " .
354 $this->timestamp . "\n" );
355 return false;
356 }
357 }
358
359 # FIXME: Use original rev_id optionally
360 # FIXME: blah blah blah
361
362 #if( $numrows > 0 ) {
363 # return wfMsg( "importhistoryconflict" );
364 #}
365
366 # Insert the row
367 $revision = new Revision( array(
368 'page' => $pageId,
369 'text' => $this->getText(),
370 'comment' => $this->getComment(),
371 'user' => $userId,
372 'user_text' => $userText,
373 'timestamp' => $this->timestamp,
374 'minor_edit' => $this->minor,
375 ) );
376 $revId = $revision->insertOn( $dbw );
377 $changed = $article->updateIfNewerOn( $dbw, $revision );
378
379 if( $created ) {
380 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
381 Article::onArticleCreate( $this->title );
382
383 wfDebug( __METHOD__ . ": running create updates\n" );
384 $article->createUpdates( $revision );
385
386 } elseif( $changed ) {
387 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
388 Article::onArticleEdit( $this->title );
389
390 wfDebug( __METHOD__ . ": running edit updates\n" );
391 $article->editUpdates(
392 $this->getText(),
393 $this->getComment(),
394 $this->minor,
395 $this->timestamp,
396 $revId );
397 }
398
399 return true;
400 }
401
402 }
403
404 /**
405 * implements Special:Import
406 * @addtogroup SpecialPage
407 */
408 class WikiImporter {
409 var $mSource = null;
410 var $mPageCallback = null;
411 var $mPageOutCallback = null;
412 var $mRevisionCallback = null;
413 var $mTargetNamespace = null;
414 var $mTargetArticleName = 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 * Set a target articlename to override the defaults
524 */
525 function setTargetArticleName( $articleName ) {
526 $this->mTargetArticleName = is_null( $articleName ) ? null : $articleName;
527 }
528
529 /**
530 * Default per-revision callback, performs the import.
531 * @param WikiRevision $revision
532 * @private
533 */
534 function importRevision( &$revision ) {
535 $dbw = wfGetDB( DB_MASTER );
536 return $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
537 }
538
539 /**
540 * Alternate per-revision callback, for debugging.
541 * @param WikiRevision $revision
542 * @private
543 */
544 function debugRevisionHandler( &$revision ) {
545 $this->debug( "Got revision:" );
546 if( is_object( $revision->title ) ) {
547 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
548 } else {
549 $this->debug( "-- Title: <invalid>" );
550 }
551 $this->debug( "-- User: " . $revision->user_text );
552 $this->debug( "-- Timestamp: " . $revision->timestamp );
553 $this->debug( "-- Comment: " . $revision->comment );
554 $this->debug( "-- Text: " . $revision->text );
555 }
556
557 /**
558 * Notify the callback function when a new <page> is reached.
559 * @param Title $title
560 * @private
561 */
562 function pageCallback( $title ) {
563 if( is_callable( $this->mPageCallback ) ) {
564 call_user_func( $this->mPageCallback, $title );
565 }
566 }
567
568 /**
569 * Notify the callback function when a </page> is closed.
570 * @param Title $title
571 * @param Title $origTitle
572 * @param int $revisionCount
573 * @param int $successCount number of revisions for which callback returned true
574 * @private
575 */
576 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
577 if( is_callable( $this->mPageOutCallback ) ) {
578 call_user_func( $this->mPageOutCallback, $title, $origTitle,
579 $revisionCount, $successCount );
580 }
581 }
582
583
584 # XML parser callbacks from here out -- beware!
585 function donothing( $parser, $x, $y="" ) {
586 #$this->debug( "donothing" );
587 }
588
589 function in_start( $parser, $name, $attribs ) {
590 $this->debug( "in_start $name" );
591 if( $name != "mediawiki" ) {
592 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
593 }
594 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
595 }
596
597 function in_mediawiki( $parser, $name, $attribs ) {
598 $this->debug( "in_mediawiki $name" );
599 if( $name == 'siteinfo' ) {
600 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
601 } elseif( $name == 'page' ) {
602 $this->workRevisionCount = 0;
603 $this->workSuccessCount = 0;
604 xml_set_element_handler( $parser, "in_page", "out_page" );
605 } else {
606 return $this->throwXMLerror( "Expected <page>, got <$name>" );
607 }
608 }
609 function out_mediawiki( $parser, $name ) {
610 $this->debug( "out_mediawiki $name" );
611 if( $name != "mediawiki" ) {
612 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
613 }
614 xml_set_element_handler( $parser, "donothing", "donothing" );
615 }
616
617
618 function in_siteinfo( $parser, $name, $attribs ) {
619 // no-ops for now
620 $this->debug( "in_siteinfo $name" );
621 switch( $name ) {
622 case "sitename":
623 case "base":
624 case "generator":
625 case "case":
626 case "namespaces":
627 case "namespace":
628 break;
629 default:
630 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
631 }
632 }
633
634 function out_siteinfo( $parser, $name ) {
635 if( $name == "siteinfo" ) {
636 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
637 }
638 }
639
640
641 function in_page( $parser, $name, $attribs ) {
642 $this->debug( "in_page $name" );
643 switch( $name ) {
644 case "id":
645 case "title":
646 case "restrictions":
647 $this->appendfield = $name;
648 $this->appenddata = "";
649 $this->parenttag = "page";
650 xml_set_element_handler( $parser, "in_nothing", "out_append" );
651 xml_set_character_data_handler( $parser, "char_append" );
652 break;
653 case "revision":
654 if( is_object( $this->pageTitle ) ) {
655 $this->workRevision = new WikiRevision;
656 $this->workRevision->setTitle( $this->pageTitle );
657 $this->workRevisionCount++;
658 } else {
659 // Skipping items due to invalid page title
660 $this->workRevision = null;
661 }
662 xml_set_element_handler( $parser, "in_revision", "out_revision" );
663 break;
664 default:
665 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
666 }
667 }
668
669 function out_page( $parser, $name ) {
670 $this->debug( "out_page $name" );
671 if( $name != "page" ) {
672 return $this->throwXMLerror( "Expected </page>, got </$name>" );
673 }
674 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
675
676 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
677 $this->workRevisionCount, $this->workSuccessCount );
678
679 $this->workTitle = null;
680 $this->workRevision = null;
681 $this->workRevisionCount = 0;
682 $this->workSuccessCount = 0;
683 $this->pageTitle = null;
684 $this->origTitle = null;
685 }
686
687 function in_nothing( $parser, $name, $attribs ) {
688 $this->debug( "in_nothing $name" );
689 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
690 }
691 function char_append( $parser, $data ) {
692 $this->debug( "char_append '$data'" );
693 $this->appenddata .= $data;
694 }
695 function out_append( $parser, $name ) {
696 $this->debug( "out_append $name" );
697 if( $name != $this->appendfield ) {
698 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
699 }
700 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
701 xml_set_character_data_handler( $parser, "donothing" );
702
703 switch( $this->appendfield ) {
704 case "title":
705 $this->workTitle = $this->appenddata;
706
707 if( $this->mTargetArticleName != '' ) {
708 // Import article with a different target article name
709 $this->workTitle = $this->mTargetArticleName;
710 }
711 $this->origTitle = Title::newFromText( $this->workTitle );
712 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
713 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
714 $this->origTitle->getDbKey() );
715 } else {
716 $this->pageTitle = Title::newFromText( $this->workTitle );
717 }
718 if( is_null( $this->pageTitle ) ) {
719 // Invalid page title? Ignore the page
720 $this->notice( "Skipping invalid page title '$this->workTitle'" );
721 } else {
722 $this->pageCallback( $this->workTitle );
723 }
724 break;
725 case "id":
726 if ( $this->parenttag == 'revision' ) {
727 if( $this->workRevision )
728 $this->workRevision->setID( $this->appenddata );
729 }
730 break;
731 case "text":
732 if( $this->workRevision )
733 $this->workRevision->setText( $this->appenddata );
734 break;
735 case "username":
736 if( $this->workRevision )
737 $this->workRevision->setUsername( $this->appenddata );
738 break;
739 case "ip":
740 if( $this->workRevision )
741 $this->workRevision->setUserIP( $this->appenddata );
742 break;
743 case "timestamp":
744 if( $this->workRevision )
745 $this->workRevision->setTimestamp( $this->appenddata );
746 break;
747 case "comment":
748 if( $this->workRevision )
749 $this->workRevision->setComment( $this->appenddata );
750 break;
751 case "minor":
752 if( $this->workRevision )
753 $this->workRevision->setMinor( true );
754 break;
755 default:
756 $this->debug( "Bad append: {$this->appendfield}" );
757 }
758 $this->appendfield = "";
759 $this->appenddata = "";
760 }
761
762 function in_revision( $parser, $name, $attribs ) {
763 $this->debug( "in_revision $name" );
764 switch( $name ) {
765 case "id":
766 case "timestamp":
767 case "comment":
768 case "minor":
769 case "text":
770 $this->parenttag = "revision";
771 $this->appendfield = $name;
772 xml_set_element_handler( $parser, "in_nothing", "out_append" );
773 xml_set_character_data_handler( $parser, "char_append" );
774 break;
775 case "contributor":
776 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
777 break;
778 default:
779 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
780 }
781 }
782
783 function out_revision( $parser, $name ) {
784 $this->debug( "out_revision $name" );
785 if( $name != "revision" ) {
786 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
787 }
788 xml_set_element_handler( $parser, "in_page", "out_page" );
789
790 if( $this->workRevision ) {
791 $ok = call_user_func_array( $this->mRevisionCallback,
792 array( &$this->workRevision, &$this ) );
793 if( $ok ) {
794 $this->workSuccessCount++;
795 }
796 }
797 }
798
799 function in_contributor( $parser, $name, $attribs ) {
800 $this->debug( "in_contributor $name" );
801 switch( $name ) {
802 case "username":
803 case "ip":
804 case "id":
805 $this->parenttag = "contributor";
806 $this->appendfield = $name;
807 xml_set_element_handler( $parser, "in_nothing", "out_append" );
808 xml_set_character_data_handler( $parser, "char_append" );
809 break;
810 default:
811 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
812 }
813 }
814
815 function out_contributor( $parser, $name ) {
816 $this->debug( "out_contributor $name" );
817 if( $name != "contributor" ) {
818 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
819 }
820 xml_set_element_handler( $parser, "in_revision", "out_revision" );
821 }
822
823 }
824
825 /**
826 * @todo document (e.g. one-sentence class description).
827 * @addtogroup SpecialPage
828 */
829 class ImportStringSource {
830 function ImportStringSource( $string ) {
831 $this->mString = $string;
832 $this->mRead = false;
833 }
834
835 function atEnd() {
836 return $this->mRead;
837 }
838
839 function readChunk() {
840 if( $this->atEnd() ) {
841 return false;
842 } else {
843 $this->mRead = true;
844 return $this->mString;
845 }
846 }
847 }
848
849 /**
850 * @todo document (e.g. one-sentence class description).
851 * @addtogroup SpecialPage
852 */
853 class ImportStreamSource {
854 function ImportStreamSource( $handle ) {
855 $this->mHandle = $handle;
856 }
857
858 function atEnd() {
859 return feof( $this->mHandle );
860 }
861
862 function readChunk() {
863 return fread( $this->mHandle, 32768 );
864 }
865
866 static function newFromFile( $filename ) {
867 $file = @fopen( $filename, 'rt' );
868 if( !$file ) {
869 return new WikiErrorMsg( "importcantopen" );
870 }
871 return new ImportStreamSource( $file );
872 }
873
874 static function newFromUpload( $fieldname = "xmlimport" ) {
875 $upload =& $_FILES[$fieldname];
876
877 if( !isset( $upload ) || !$upload['name'] ) {
878 return new WikiErrorMsg( 'importnofile' );
879 }
880 if( !empty( $upload['error'] ) ) {
881 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
882 }
883 $fname = $upload['tmp_name'];
884 if( is_uploaded_file( $fname ) ) {
885 return ImportStreamSource::newFromFile( $fname );
886 } else {
887 return new WikiErrorMsg( 'importnofile' );
888 }
889 }
890
891 function newFromURL( $url, $method = 'GET' ) {
892 wfDebug( __METHOD__ . ": opening $url\n" );
893 # Use the standard HTTP fetch function; it times out
894 # quicker and sorts out user-agent problems which might
895 # otherwise prevent importing from large sites, such
896 # as the Wikimedia cluster, etc.
897 $data = Http::request( $method, $url );
898 if( $data !== false ) {
899 $file = tmpfile();
900 fwrite( $file, $data );
901 fflush( $file );
902 fseek( $file, 0 );
903 return new ImportStreamSource( $file );
904 } else {
905 return new WikiErrorMsg( 'importcantopen' );
906 }
907 }
908
909 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
910 if( $page == '' ) {
911 return new WikiErrorMsg( 'import-noarticle' );
912 }
913 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
914 if( is_null( $link ) || $link->getInterwiki() == '' ) {
915 return new WikiErrorMsg( 'importbadinterwiki' );
916 } else {
917 $params = $history ? 'history=1' : '';
918 $url = $link->getFullUrl( $params );
919 # For interwikis, use POST to avoid redirects.
920 return ImportStreamSource::newFromURL( $url, "POST" );
921 }
922 }
923 }
924
925
926