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