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