merged master
[lhc/web/wiklou.git] / includes / Import.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup SpecialPage
25 */
26
27 /**
28 * XML file reader for the page data importer
29 *
30 * implements Special:Import
31 * @ingroup SpecialPage
32 */
33 class WikiImporter {
34 private $reader = null;
35 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
36 private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
37 private $mNoticeCallback, $mDebug;
38 private $mImportUploads, $mImageBasePath;
39 private $mNoUpdates = false;
40
41 /**
42 * Creates an ImportXMLReader drawing from the source provided
43 * @param $source
44 */
45 function __construct( $source ) {
46 $this->reader = new XMLReader();
47
48 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
49 $id = UploadSourceAdapter::registerSource( $source );
50 if (defined( 'LIBXML_PARSEHUGE' ) ) {
51 $this->reader->open( "uploadsource://$id", null, LIBXML_PARSEHUGE );
52 } else {
53 $this->reader->open( "uploadsource://$id" );
54 }
55
56 // Default callbacks
57 $this->setRevisionCallback( array( $this, "importRevision" ) );
58 $this->setUploadCallback( array( $this, 'importUpload' ) );
59 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
60 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
61 }
62
63 private function throwXmlError( $err ) {
64 $this->debug( "FAILURE: $err" );
65 wfDebug( "WikiImporter XML error: $err\n" );
66 }
67
68 private function debug( $data ) {
69 if( $this->mDebug ) {
70 wfDebug( "IMPORT: $data\n" );
71 }
72 }
73
74 private function warn( $data ) {
75 wfDebug( "IMPORT: $data\n" );
76 }
77
78 private function notice( $msg /*, $param, ...*/ ) {
79 $params = func_get_args();
80 array_shift( $params );
81
82 if ( is_callable( $this->mNoticeCallback ) ) {
83 call_user_func( $this->mNoticeCallback, $msg, $params );
84 } else { # No ImportReporter -> CLI
85 echo wfMessage( $msg, $params )->text() . "\n";
86 }
87 }
88
89 /**
90 * Set debug mode...
91 * @param $debug bool
92 */
93 function setDebug( $debug ) {
94 $this->mDebug = $debug;
95 }
96
97 /**
98 * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
99 * @param $noupdates bool
100 */
101 function setNoUpdates( $noupdates ) {
102 $this->mNoUpdates = $noupdates;
103 }
104
105 /**
106 * Set a callback that displays notice messages
107 *
108 * @param $callback callback
109 * @return callback
110 */
111 public function setNoticeCallback( $callback ) {
112 return wfSetVar( $this->mNoticeCallback, $callback );
113 }
114
115 /**
116 * Sets the action to perform as each new page in the stream is reached.
117 * @param $callback callback
118 * @return callback
119 */
120 public function setPageCallback( $callback ) {
121 $previous = $this->mPageCallback;
122 $this->mPageCallback = $callback;
123 return $previous;
124 }
125
126 /**
127 * Sets the action to perform as each page in the stream is completed.
128 * Callback accepts the page title (as a Title object), a second object
129 * with the original title form (in case it's been overridden into a
130 * local namespace), and a count of revisions.
131 *
132 * @param $callback callback
133 * @return callback
134 */
135 public function setPageOutCallback( $callback ) {
136 $previous = $this->mPageOutCallback;
137 $this->mPageOutCallback = $callback;
138 return $previous;
139 }
140
141 /**
142 * Sets the action to perform as each page revision is reached.
143 * @param $callback callback
144 * @return callback
145 */
146 public function setRevisionCallback( $callback ) {
147 $previous = $this->mRevisionCallback;
148 $this->mRevisionCallback = $callback;
149 return $previous;
150 }
151
152 /**
153 * Sets the action to perform as each file upload version is reached.
154 * @param $callback callback
155 * @return callback
156 */
157 public function setUploadCallback( $callback ) {
158 $previous = $this->mUploadCallback;
159 $this->mUploadCallback = $callback;
160 return $previous;
161 }
162
163 /**
164 * Sets the action to perform as each log item reached.
165 * @param $callback callback
166 * @return callback
167 */
168 public function setLogItemCallback( $callback ) {
169 $previous = $this->mLogItemCallback;
170 $this->mLogItemCallback = $callback;
171 return $previous;
172 }
173
174 /**
175 * Sets the action to perform when site info is encountered
176 * @param $callback callback
177 * @return callback
178 */
179 public function setSiteInfoCallback( $callback ) {
180 $previous = $this->mSiteInfoCallback;
181 $this->mSiteInfoCallback = $callback;
182 return $previous;
183 }
184
185 /**
186 * Set a target namespace to override the defaults
187 * @param $namespace
188 * @return bool
189 */
190 public function setTargetNamespace( $namespace ) {
191 if( is_null( $namespace ) ) {
192 // Don't override namespaces
193 $this->mTargetNamespace = null;
194 } elseif( $namespace >= 0 ) {
195 // @todo FIXME: Check for validity
196 $this->mTargetNamespace = intval( $namespace );
197 } else {
198 return false;
199 }
200 }
201
202 /**
203 * @param $dir
204 */
205 public function setImageBasePath( $dir ) {
206 $this->mImageBasePath = $dir;
207 }
208
209 /**
210 * @param $import
211 */
212 public function setImportUploads( $import ) {
213 $this->mImportUploads = $import;
214 }
215
216 /**
217 * Default per-revision callback, performs the import.
218 * @param $revision WikiRevision
219 * @return bool
220 */
221 public function importRevision( $revision ) {
222 $dbw = wfGetDB( DB_MASTER );
223 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
224 }
225
226 /**
227 * Default per-revision callback, performs the import.
228 * @param $rev WikiRevision
229 * @return bool
230 */
231 public function importLogItem( $rev ) {
232 $dbw = wfGetDB( DB_MASTER );
233 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
234 }
235
236 /**
237 * Dummy for now...
238 * @param $revision
239 * @return bool
240 */
241 public function importUpload( $revision ) {
242 $dbw = wfGetDB( DB_MASTER );
243 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
244 }
245
246 /**
247 * Mostly for hook use
248 * @param $title
249 * @param $origTitle
250 * @param $revCount
251 * @param $sRevCount
252 * @param $pageInfo
253 * @return
254 */
255 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
256 $args = func_get_args();
257 return wfRunHooks( 'AfterImportPage', $args );
258 }
259
260 /**
261 * Alternate per-revision callback, for debugging.
262 * @param $revision WikiRevision
263 */
264 public function debugRevisionHandler( &$revision ) {
265 $this->debug( "Got revision:" );
266 if( is_object( $revision->title ) ) {
267 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
268 } else {
269 $this->debug( "-- Title: <invalid>" );
270 }
271 $this->debug( "-- User: " . $revision->user_text );
272 $this->debug( "-- Timestamp: " . $revision->timestamp );
273 $this->debug( "-- Comment: " . $revision->comment );
274 $this->debug( "-- Text: " . $revision->text );
275 }
276
277 /**
278 * Notify the callback function when a new <page> is reached.
279 * @param $title Title
280 */
281 function pageCallback( $title ) {
282 if( isset( $this->mPageCallback ) ) {
283 call_user_func( $this->mPageCallback, $title );
284 }
285 }
286
287 /**
288 * Notify the callback function when a </page> is closed.
289 * @param $title Title
290 * @param $origTitle Title
291 * @param $revCount Integer
292 * @param $sucCount Int: number of revisions for which callback returned true
293 * @param $pageInfo Array: associative array of page information
294 */
295 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
296 if( isset( $this->mPageOutCallback ) ) {
297 $args = func_get_args();
298 call_user_func_array( $this->mPageOutCallback, $args );
299 }
300 }
301
302 /**
303 * Notify the callback function of a revision
304 * @param $revision WikiRevision object
305 * @return bool|mixed
306 */
307 private function revisionCallback( $revision ) {
308 if ( isset( $this->mRevisionCallback ) ) {
309 return call_user_func_array( $this->mRevisionCallback,
310 array( $revision, $this ) );
311 } else {
312 return false;
313 }
314 }
315
316 /**
317 * Notify the callback function of a new log item
318 * @param $revision WikiRevision object
319 * @return bool|mixed
320 */
321 private function logItemCallback( $revision ) {
322 if ( isset( $this->mLogItemCallback ) ) {
323 return call_user_func_array( $this->mLogItemCallback,
324 array( $revision, $this ) );
325 } else {
326 return false;
327 }
328 }
329
330 /**
331 * Shouldn't something like this be built-in to XMLReader?
332 * Fetches text contents of the current element, assuming
333 * no sub-elements or such scary things.
334 * @return string
335 * @access private
336 */
337 private function nodeContents() {
338 if( $this->reader->isEmptyElement ) {
339 return "";
340 }
341 $buffer = "";
342 while( $this->reader->read() ) {
343 switch( $this->reader->nodeType ) {
344 case XmlReader::TEXT:
345 case XmlReader::SIGNIFICANT_WHITESPACE:
346 $buffer .= $this->reader->value;
347 break;
348 case XmlReader::END_ELEMENT:
349 return $buffer;
350 }
351 }
352
353 $this->reader->close();
354 return '';
355 }
356
357 # --------------
358
359 /** Left in for debugging */
360 private function dumpElement() {
361 static $lookup = null;
362 if (!$lookup) {
363 $xmlReaderConstants = array(
364 "NONE",
365 "ELEMENT",
366 "ATTRIBUTE",
367 "TEXT",
368 "CDATA",
369 "ENTITY_REF",
370 "ENTITY",
371 "PI",
372 "COMMENT",
373 "DOC",
374 "DOC_TYPE",
375 "DOC_FRAGMENT",
376 "NOTATION",
377 "WHITESPACE",
378 "SIGNIFICANT_WHITESPACE",
379 "END_ELEMENT",
380 "END_ENTITY",
381 "XML_DECLARATION",
382 );
383 $lookup = array();
384
385 foreach( $xmlReaderConstants as $name ) {
386 $lookup[constant("XmlReader::$name")] = $name;
387 }
388 }
389
390 print( var_dump(
391 $lookup[$this->reader->nodeType],
392 $this->reader->name,
393 $this->reader->value
394 )."\n\n" );
395 }
396
397 /**
398 * Primary entry point
399 * @return bool
400 */
401 public function doImport() {
402 $this->reader->read();
403
404 if ( $this->reader->name != 'mediawiki' ) {
405 throw new MWException( "Expected <mediawiki> tag, got ".
406 $this->reader->name );
407 }
408 $this->debug( "<mediawiki> tag is correct." );
409
410 $this->debug( "Starting primary dump processing loop." );
411
412 $keepReading = $this->reader->read();
413 $skip = false;
414 while ( $keepReading ) {
415 $tag = $this->reader->name;
416 $type = $this->reader->nodeType;
417
418 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', $this ) ) {
419 // Do nothing
420 } elseif ( $tag == 'mediawiki' && $type == XmlReader::END_ELEMENT ) {
421 break;
422 } elseif ( $tag == 'siteinfo' ) {
423 $this->handleSiteInfo();
424 } elseif ( $tag == 'page' ) {
425 $this->handlePage();
426 } elseif ( $tag == 'logitem' ) {
427 $this->handleLogItem();
428 } elseif ( $tag != '#text' ) {
429 $this->warn( "Unhandled top-level XML tag $tag" );
430
431 $skip = true;
432 }
433
434 if ($skip) {
435 $keepReading = $this->reader->next();
436 $skip = false;
437 $this->debug( "Skip" );
438 } else {
439 $keepReading = $this->reader->read();
440 }
441 }
442
443 return true;
444 }
445
446 /**
447 * @return bool
448 * @throws MWException
449 */
450 private function handleSiteInfo() {
451 // Site info is useful, but not actually used for dump imports.
452 // Includes a quick short-circuit to save performance.
453 if ( ! $this->mSiteInfoCallback ) {
454 $this->reader->next();
455 return true;
456 }
457 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
458 }
459
460 private function handleLogItem() {
461 $this->debug( "Enter log item handler." );
462 $logInfo = array();
463
464 // Fields that can just be stuffed in the pageInfo object
465 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
466 'logtitle', 'params' );
467
468 while ( $this->reader->read() ) {
469 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
470 $this->reader->name == 'logitem') {
471 break;
472 }
473
474 $tag = $this->reader->name;
475
476 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag',
477 $this, $logInfo ) ) {
478 // Do nothing
479 } elseif ( in_array( $tag, $normalFields ) ) {
480 $logInfo[$tag] = $this->nodeContents();
481 } elseif ( $tag == 'contributor' ) {
482 $logInfo['contributor'] = $this->handleContributor();
483 } elseif ( $tag != '#text' ) {
484 $this->warn( "Unhandled log-item XML tag $tag" );
485 }
486 }
487
488 $this->processLogItem( $logInfo );
489 }
490
491 /**
492 * @param $logInfo
493 * @return bool|mixed
494 */
495 private function processLogItem( $logInfo ) {
496 $revision = new WikiRevision;
497
498 $revision->setID( $logInfo['id'] );
499 $revision->setType( $logInfo['type'] );
500 $revision->setAction( $logInfo['action'] );
501 $revision->setTimestamp( $logInfo['timestamp'] );
502 $revision->setParams( $logInfo['params'] );
503 $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
504 $revision->setNoUpdates( $this->mNoUpdates );
505
506 if ( isset( $logInfo['comment'] ) ) {
507 $revision->setComment( $logInfo['comment'] );
508 }
509
510 if ( isset( $logInfo['contributor']['ip'] ) ) {
511 $revision->setUserIP( $logInfo['contributor']['ip'] );
512 }
513 if ( isset( $logInfo['contributor']['username'] ) ) {
514 $revision->setUserName( $logInfo['contributor']['username'] );
515 }
516
517 return $this->logItemCallback( $revision );
518 }
519
520 private function handlePage() {
521 // Handle page data.
522 $this->debug( "Enter page handler." );
523 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
524
525 // Fields that can just be stuffed in the pageInfo object
526 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
527
528 $skip = false;
529 $badTitle = false;
530
531 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
532 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
533 $this->reader->name == 'page') {
534 break;
535 }
536
537 $tag = $this->reader->name;
538
539 if ( $badTitle ) {
540 // The title is invalid, bail out of this page
541 $skip = true;
542 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this,
543 &$pageInfo ) ) ) {
544 // Do nothing
545 } elseif ( in_array( $tag, $normalFields ) ) {
546 $pageInfo[$tag] = $this->nodeContents();
547 if ( $tag == 'title' ) {
548 $title = $this->processTitle( $pageInfo['title'] );
549
550 if ( !$title ) {
551 $badTitle = true;
552 $skip = true;
553 }
554
555 $this->pageCallback( $title );
556 list( $pageInfo['_title'], $origTitle ) = $title;
557 }
558 } elseif ( $tag == 'revision' ) {
559 $this->handleRevision( $pageInfo );
560 } elseif ( $tag == 'upload' ) {
561 $this->handleUpload( $pageInfo );
562 } elseif ( $tag != '#text' ) {
563 $this->warn( "Unhandled page XML tag $tag" );
564 $skip = true;
565 }
566 }
567
568 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
569 $pageInfo['revisionCount'],
570 $pageInfo['successfulRevisionCount'],
571 $pageInfo );
572 }
573
574 /**
575 * @param $pageInfo array
576 */
577 private function handleRevision( &$pageInfo ) {
578 $this->debug( "Enter revision handler" );
579 $revisionInfo = array();
580
581 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' );
582
583 $skip = false;
584
585 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
586 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
587 $this->reader->name == 'revision') {
588 break;
589 }
590
591 $tag = $this->reader->name;
592
593 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', $this,
594 $pageInfo, $revisionInfo ) ) {
595 // Do nothing
596 } elseif ( in_array( $tag, $normalFields ) ) {
597 $revisionInfo[$tag] = $this->nodeContents();
598 } elseif ( $tag == 'contributor' ) {
599 $revisionInfo['contributor'] = $this->handleContributor();
600 } elseif ( $tag != '#text' ) {
601 $this->warn( "Unhandled revision XML tag $tag" );
602 $skip = true;
603 }
604 }
605
606 $pageInfo['revisionCount']++;
607 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
608 $pageInfo['successfulRevisionCount']++;
609 }
610 }
611
612 /**
613 * @param $pageInfo
614 * @param $revisionInfo
615 * @return bool|mixed
616 */
617 private function processRevision( $pageInfo, $revisionInfo ) {
618 $revision = new WikiRevision;
619
620 if( isset( $revisionInfo['id'] ) ) {
621 $revision->setID( $revisionInfo['id'] );
622 }
623 if ( isset( $revisionInfo['text'] ) ) {
624 $revision->setText( $revisionInfo['text'] );
625 }
626 if ( isset( $revisionInfo['model'] ) ) {
627 $revision->setModel( $revisionInfo['model'] );
628 }
629 if ( isset( $revisionInfo['text'] ) ) {
630 $revision->setFormat( $revisionInfo['format'] );
631 }
632 $revision->setTitle( $pageInfo['_title'] );
633
634 if ( isset( $revisionInfo['timestamp'] ) ) {
635 $revision->setTimestamp( $revisionInfo['timestamp'] );
636 } else {
637 $revision->setTimestamp( wfTimestampNow() );
638 }
639
640 if ( isset( $revisionInfo['comment'] ) ) {
641 $revision->setComment( $revisionInfo['comment'] );
642 }
643
644 if ( isset( $revisionInfo['minor'] ) ) {
645 $revision->setMinor( true );
646 }
647 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
648 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
649 }
650 if ( isset( $revisionInfo['contributor']['username'] ) ) {
651 $revision->setUserName( $revisionInfo['contributor']['username'] );
652 }
653 $revision->setNoUpdates( $this->mNoUpdates );
654
655 return $this->revisionCallback( $revision );
656 }
657
658 /**
659 * @param $pageInfo
660 * @return mixed
661 */
662 private function handleUpload( &$pageInfo ) {
663 $this->debug( "Enter upload handler" );
664 $uploadInfo = array();
665
666 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
667 'src', 'size', 'sha1base36', 'archivename', 'rel' );
668
669 $skip = false;
670
671 while ( $skip ? $this->reader->next() : $this->reader->read() ) {
672 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
673 $this->reader->name == 'upload') {
674 break;
675 }
676
677 $tag = $this->reader->name;
678
679 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', $this,
680 $pageInfo ) ) {
681 // Do nothing
682 } elseif ( in_array( $tag, $normalFields ) ) {
683 $uploadInfo[$tag] = $this->nodeContents();
684 } elseif ( $tag == 'contributor' ) {
685 $uploadInfo['contributor'] = $this->handleContributor();
686 } elseif ( $tag == 'contents' ) {
687 $contents = $this->nodeContents();
688 $encoding = $this->reader->getAttribute( 'encoding' );
689 if ( $encoding === 'base64' ) {
690 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
691 $uploadInfo['isTempSrc'] = true;
692 }
693 } elseif ( $tag != '#text' ) {
694 $this->warn( "Unhandled upload XML tag $tag" );
695 $skip = true;
696 }
697 }
698
699 if ( $this->mImageBasePath && isset( $uploadInfo['rel'] ) ) {
700 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
701 if ( file_exists( $path ) ) {
702 $uploadInfo['fileSrc'] = $path;
703 $uploadInfo['isTempSrc'] = false;
704 }
705 }
706
707 if ( $this->mImportUploads ) {
708 return $this->processUpload( $pageInfo, $uploadInfo );
709 }
710 }
711
712 /**
713 * @param $contents
714 * @return string
715 */
716 private function dumpTemp( $contents ) {
717 $filename = tempnam( wfTempDir(), 'importupload' );
718 file_put_contents( $filename, $contents );
719 return $filename;
720 }
721
722 /**
723 * @param $pageInfo
724 * @param $uploadInfo
725 * @return mixed
726 */
727 private function processUpload( $pageInfo, $uploadInfo ) {
728 $revision = new WikiRevision;
729 $text = isset( $uploadInfo['text'] ) ? $uploadInfo['text'] : '';
730
731 $revision->setTitle( $pageInfo['_title'] );
732 $revision->setID( $pageInfo['id'] );
733 $revision->setTimestamp( $uploadInfo['timestamp'] );
734 $revision->setText( $text );
735 $revision->setFilename( $uploadInfo['filename'] );
736 if ( isset( $uploadInfo['archivename'] ) ) {
737 $revision->setArchiveName( $uploadInfo['archivename'] );
738 }
739 $revision->setSrc( $uploadInfo['src'] );
740 if ( isset( $uploadInfo['fileSrc'] ) ) {
741 $revision->setFileSrc( $uploadInfo['fileSrc'],
742 !empty( $uploadInfo['isTempSrc'] ) );
743 }
744 if ( isset( $uploadInfo['sha1base36'] ) ) {
745 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
746 }
747 $revision->setSize( intval( $uploadInfo['size'] ) );
748 $revision->setComment( $uploadInfo['comment'] );
749
750 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
751 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
752 }
753 if ( isset( $uploadInfo['contributor']['username'] ) ) {
754 $revision->setUserName( $uploadInfo['contributor']['username'] );
755 }
756 $revision->setNoUpdates( $this->mNoUpdates );
757
758 return call_user_func( $this->mUploadCallback, $revision );
759 }
760
761 /**
762 * @return array
763 */
764 private function handleContributor() {
765 $fields = array( 'id', 'ip', 'username' );
766 $info = array();
767
768 while ( $this->reader->read() ) {
769 if ( $this->reader->nodeType == XmlReader::END_ELEMENT &&
770 $this->reader->name == 'contributor') {
771 break;
772 }
773
774 $tag = $this->reader->name;
775
776 if ( in_array( $tag, $fields ) ) {
777 $info[$tag] = $this->nodeContents();
778 }
779 }
780
781 return $info;
782 }
783
784 /**
785 * @param $text string
786 * @return Array or false
787 */
788 private function processTitle( $text ) {
789 global $wgCommandLineMode;
790
791 $workTitle = $text;
792 $origTitle = Title::newFromText( $workTitle );
793
794 if( !is_null( $this->mTargetNamespace ) && !is_null( $origTitle ) ) {
795 # makeTitleSafe, because $origTitle can have a interwiki (different setting of interwiki map)
796 # and than dbKey can begin with a lowercase char
797 $title = Title::makeTitleSafe( $this->mTargetNamespace,
798 $origTitle->getDBkey() );
799 } else {
800 $title = Title::newFromText( $workTitle );
801 }
802
803 if( is_null( $title ) ) {
804 # Invalid page title? Ignore the page
805 $this->notice( 'import-error-invalid', $workTitle );
806 return false;
807 } elseif( $title->isExternal() ) {
808 $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
809 return false;
810 } elseif( !$title->canExist() ) {
811 $this->notice( 'import-error-special', $title->getPrefixedText() );
812 return false;
813 } elseif( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
814 # Do not import if the importing wiki user cannot edit this page
815 $this->notice( 'import-error-edit', $title->getPrefixedText() );
816 return false;
817 } elseif( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
818 # Do not import if the importing wiki user cannot create this page
819 $this->notice( 'import-error-create', $title->getPrefixedText() );
820 return false;
821 }
822
823 return array( $title, $origTitle );
824 }
825 }
826
827 /** This is a horrible hack used to keep source compatibility */
828 class UploadSourceAdapter {
829 static $sourceRegistrations = array();
830
831 private $mSource;
832 private $mBuffer;
833 private $mPosition;
834
835 /**
836 * @param $source
837 * @return string
838 */
839 static function registerSource( $source ) {
840 $id = wfRandomString();
841
842 self::$sourceRegistrations[$id] = $source;
843
844 return $id;
845 }
846
847 /**
848 * @param $path
849 * @param $mode
850 * @param $options
851 * @param $opened_path
852 * @return bool
853 */
854 function stream_open( $path, $mode, $options, &$opened_path ) {
855 $url = parse_url($path);
856 $id = $url['host'];
857
858 if ( !isset( self::$sourceRegistrations[$id] ) ) {
859 return false;
860 }
861
862 $this->mSource = self::$sourceRegistrations[$id];
863
864 return true;
865 }
866
867 /**
868 * @param $count
869 * @return string
870 */
871 function stream_read( $count ) {
872 $return = '';
873 $leave = false;
874
875 while ( !$leave && !$this->mSource->atEnd() &&
876 strlen($this->mBuffer) < $count ) {
877 $read = $this->mSource->readChunk();
878
879 if ( !strlen($read) ) {
880 $leave = true;
881 }
882
883 $this->mBuffer .= $read;
884 }
885
886 if ( strlen($this->mBuffer) ) {
887 $return = substr( $this->mBuffer, 0, $count );
888 $this->mBuffer = substr( $this->mBuffer, $count );
889 }
890
891 $this->mPosition += strlen($return);
892
893 return $return;
894 }
895
896 /**
897 * @param $data
898 * @return bool
899 */
900 function stream_write( $data ) {
901 return false;
902 }
903
904 /**
905 * @return mixed
906 */
907 function stream_tell() {
908 return $this->mPosition;
909 }
910
911 /**
912 * @return bool
913 */
914 function stream_eof() {
915 return $this->mSource->atEnd();
916 }
917
918 /**
919 * @return array
920 */
921 function url_stat() {
922 $result = array();
923
924 $result['dev'] = $result[0] = 0;
925 $result['ino'] = $result[1] = 0;
926 $result['mode'] = $result[2] = 0;
927 $result['nlink'] = $result[3] = 0;
928 $result['uid'] = $result[4] = 0;
929 $result['gid'] = $result[5] = 0;
930 $result['rdev'] = $result[6] = 0;
931 $result['size'] = $result[7] = 0;
932 $result['atime'] = $result[8] = 0;
933 $result['mtime'] = $result[9] = 0;
934 $result['ctime'] = $result[10] = 0;
935 $result['blksize'] = $result[11] = 0;
936 $result['blocks'] = $result[12] = 0;
937
938 return $result;
939 }
940 }
941
942 class XMLReader2 extends XMLReader {
943
944 /**
945 * @return bool|string
946 */
947 function nodeContents() {
948 if( $this->isEmptyElement ) {
949 return "";
950 }
951 $buffer = "";
952 while( $this->read() ) {
953 switch( $this->nodeType ) {
954 case XmlReader::TEXT:
955 case XmlReader::SIGNIFICANT_WHITESPACE:
956 $buffer .= $this->value;
957 break;
958 case XmlReader::END_ELEMENT:
959 return $buffer;
960 }
961 }
962 return $this->close();
963 }
964 }
965
966 /**
967 * @todo document (e.g. one-sentence class description).
968 * @ingroup SpecialPage
969 */
970 class WikiRevision {
971 var $importer = null;
972
973 /**
974 * @var Title
975 */
976 var $title = null;
977 var $id = 0;
978 var $timestamp = "20010115000000";
979 var $user = 0;
980 var $user_text = "";
981 var $model = null;
982 var $format = null;
983 var $text = "";
984 var $comment = "";
985 var $minor = false;
986 var $type = "";
987 var $action = "";
988 var $params = "";
989 var $fileSrc = '';
990 var $sha1base36 = false;
991 var $isTemp = false;
992 var $archiveName = '';
993 var $fileIsTemp;
994 private $mNoUpdates = false;
995
996 /**
997 * @param $title
998 * @throws MWException
999 */
1000 function setTitle( $title ) {
1001 if( is_object( $title ) ) {
1002 $this->title = $title;
1003 } elseif( is_null( $title ) ) {
1004 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
1005 } else {
1006 throw new MWException( "WikiRevision given non-object title in import." );
1007 }
1008 }
1009
1010 /**
1011 * @param $id
1012 */
1013 function setID( $id ) {
1014 $this->id = $id;
1015 }
1016
1017 /**
1018 * @param $ts
1019 */
1020 function setTimestamp( $ts ) {
1021 # 2003-08-05T18:30:02Z
1022 $this->timestamp = wfTimestamp( TS_MW, $ts );
1023 }
1024
1025 /**
1026 * @param $user
1027 */
1028 function setUsername( $user ) {
1029 $this->user_text = $user;
1030 }
1031
1032 /**
1033 * @param $ip
1034 */
1035 function setUserIP( $ip ) {
1036 $this->user_text = $ip;
1037 }
1038
1039 /**
1040 * @param $model
1041 */
1042 function setModel( $model ) {
1043 $this->model = $model;
1044 }
1045
1046 /**
1047 * @param $format
1048 */
1049 function setFormat( $format ) {
1050 $this->format = $format;
1051 }
1052
1053 /**
1054 * @param $text
1055 */
1056 function setText( $text ) {
1057 $this->text = $text;
1058 }
1059
1060 /**
1061 * @param $text
1062 */
1063 function setComment( $text ) {
1064 $this->comment = $text;
1065 }
1066
1067 /**
1068 * @param $minor
1069 */
1070 function setMinor( $minor ) {
1071 $this->minor = (bool)$minor;
1072 }
1073
1074 /**
1075 * @param $src
1076 */
1077 function setSrc( $src ) {
1078 $this->src = $src;
1079 }
1080
1081 /**
1082 * @param $src
1083 * @param $isTemp
1084 */
1085 function setFileSrc( $src, $isTemp ) {
1086 $this->fileSrc = $src;
1087 $this->fileIsTemp = $isTemp;
1088 }
1089
1090 /**
1091 * @param $sha1base36
1092 */
1093 function setSha1Base36( $sha1base36 ) {
1094 $this->sha1base36 = $sha1base36;
1095 }
1096
1097 /**
1098 * @param $filename
1099 */
1100 function setFilename( $filename ) {
1101 $this->filename = $filename;
1102 }
1103
1104 /**
1105 * @param $archiveName
1106 */
1107 function setArchiveName( $archiveName ) {
1108 $this->archiveName = $archiveName;
1109 }
1110
1111 /**
1112 * @param $size
1113 */
1114 function setSize( $size ) {
1115 $this->size = intval( $size );
1116 }
1117
1118 /**
1119 * @param $type
1120 */
1121 function setType( $type ) {
1122 $this->type = $type;
1123 }
1124
1125 /**
1126 * @param $action
1127 */
1128 function setAction( $action ) {
1129 $this->action = $action;
1130 }
1131
1132 /**
1133 * @param $params
1134 */
1135 function setParams( $params ) {
1136 $this->params = $params;
1137 }
1138
1139 /**
1140 * @param $noupdates
1141 */
1142 public function setNoUpdates( $noupdates ) {
1143 $this->mNoUpdates = $noupdates;
1144 }
1145
1146 /**
1147 * @return Title
1148 */
1149 function getTitle() {
1150 return $this->title;
1151 }
1152
1153 /**
1154 * @return int
1155 */
1156 function getID() {
1157 return $this->id;
1158 }
1159
1160 /**
1161 * @return string
1162 */
1163 function getTimestamp() {
1164 return $this->timestamp;
1165 }
1166
1167 /**
1168 * @return string
1169 */
1170 function getUser() {
1171 return $this->user_text;
1172 }
1173
1174 /**
1175 * @return string
1176 */
1177 function getText() {
1178 return $this->text;
1179 }
1180
1181 /**
1182 * @return int
1183 */
1184 function getModel() {
1185 if ( is_null( $this->model ) ) {
1186 $this->model = $this->getTitle()->getContentModel();
1187 }
1188
1189 return $this->model;
1190 }
1191
1192 /**
1193 * @return int
1194 */
1195 function getFormat() {
1196 if ( is_null( $this->model ) ) {
1197 $this->format = ContentHandler::getForTitle( $this->getTitle() )->getDefaultFormat();
1198 }
1199
1200 return $this->format;
1201 }
1202
1203 /**
1204 * @return string
1205 */
1206 function getComment() {
1207 return $this->comment;
1208 }
1209
1210 /**
1211 * @return bool
1212 */
1213 function getMinor() {
1214 return $this->minor;
1215 }
1216
1217 /**
1218 * @return mixed
1219 */
1220 function getSrc() {
1221 return $this->src;
1222 }
1223
1224 /**
1225 * @return bool|String
1226 */
1227 function getSha1() {
1228 if ( $this->sha1base36 ) {
1229 return wfBaseConvert( $this->sha1base36, 36, 16 );
1230 }
1231 return false;
1232 }
1233
1234 /**
1235 * @return string
1236 */
1237 function getFileSrc() {
1238 return $this->fileSrc;
1239 }
1240
1241 /**
1242 * @return bool
1243 */
1244 function isTempSrc() {
1245 return $this->isTemp;
1246 }
1247
1248 /**
1249 * @return mixed
1250 */
1251 function getFilename() {
1252 return $this->filename;
1253 }
1254
1255 /**
1256 * @return string
1257 */
1258 function getArchiveName() {
1259 return $this->archiveName;
1260 }
1261
1262 /**
1263 * @return mixed
1264 */
1265 function getSize() {
1266 return $this->size;
1267 }
1268
1269 /**
1270 * @return string
1271 */
1272 function getType() {
1273 return $this->type;
1274 }
1275
1276 /**
1277 * @return string
1278 */
1279 function getAction() {
1280 return $this->action;
1281 }
1282
1283 /**
1284 * @return string
1285 */
1286 function getParams() {
1287 return $this->params;
1288 }
1289
1290 /**
1291 * @return bool
1292 */
1293 function importOldRevision() {
1294 $dbw = wfGetDB( DB_MASTER );
1295
1296 # Sneak a single revision into place
1297 $user = User::newFromName( $this->getUser() );
1298 if( $user ) {
1299 $userId = intval( $user->getId() );
1300 $userText = $user->getName();
1301 $userObj = $user;
1302 } else {
1303 $userId = 0;
1304 $userText = $this->getUser();
1305 $userObj = new User;
1306 }
1307
1308 // avoid memory leak...?
1309 $linkCache = LinkCache::singleton();
1310 $linkCache->clear();
1311
1312 $page = WikiPage::factory( $this->title );
1313 if( !$page->exists() ) {
1314 # must create the page...
1315 $pageId = $page->insertOn( $dbw );
1316 $created = true;
1317 $oldcountable = null;
1318 } else {
1319 $pageId = $page->getId();
1320 $created = false;
1321
1322 $prior = $dbw->selectField( 'revision', '1',
1323 array( 'rev_page' => $pageId,
1324 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
1325 'rev_user_text' => $userText,
1326 'rev_comment' => $this->getComment() ),
1327 __METHOD__
1328 );
1329 if( $prior ) {
1330 // @todo FIXME: This could fail slightly for multiple matches :P
1331 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
1332 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
1333 return false;
1334 }
1335 $oldcountable = $page->isCountable();
1336 }
1337
1338 # @todo FIXME: Use original rev_id optionally (better for backups)
1339 # Insert the row
1340 $revision = new Revision( array(
1341 'page' => $pageId,
1342 'content_model' => $this->getModel(),
1343 'content_format' => $this->getFormat(),
1344 'text' => $this->getText(),
1345 'comment' => $this->getComment(),
1346 'user' => $userId,
1347 'user_text' => $userText,
1348 'timestamp' => $this->timestamp,
1349 'minor_edit' => $this->minor,
1350 ) );
1351 $revision->insertOn( $dbw );
1352 $changed = $page->updateIfNewerOn( $dbw, $revision );
1353
1354 if ( $changed !== false && !$this->mNoUpdates ) {
1355 wfDebug( __METHOD__ . ": running updates\n" );
1356 $page->doEditUpdates( $revision, $userObj, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
1357 }
1358
1359 return true;
1360 }
1361
1362 /**
1363 * @return mixed
1364 */
1365 function importLogItem() {
1366 $dbw = wfGetDB( DB_MASTER );
1367 # @todo FIXME: This will not record autoblocks
1368 if( !$this->getTitle() ) {
1369 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1370 $this->timestamp . "\n" );
1371 return;
1372 }
1373 # Check if it exists already
1374 // @todo FIXME: Use original log ID (better for backups)
1375 $prior = $dbw->selectField( 'logging', '1',
1376 array( 'log_type' => $this->getType(),
1377 'log_action' => $this->getAction(),
1378 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1379 'log_namespace' => $this->getTitle()->getNamespace(),
1380 'log_title' => $this->getTitle()->getDBkey(),
1381 'log_comment' => $this->getComment(),
1382 #'log_user_text' => $this->user_text,
1383 'log_params' => $this->params ),
1384 __METHOD__
1385 );
1386 // @todo FIXME: This could fail slightly for multiple matches :P
1387 if( $prior ) {
1388 wfDebug( __METHOD__ . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
1389 $this->timestamp . "\n" );
1390 return;
1391 }
1392 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1393 $data = array(
1394 'log_id' => $log_id,
1395 'log_type' => $this->type,
1396 'log_action' => $this->action,
1397 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
1398 'log_user' => User::idFromName( $this->user_text ),
1399 #'log_user_text' => $this->user_text,
1400 'log_namespace' => $this->getTitle()->getNamespace(),
1401 'log_title' => $this->getTitle()->getDBkey(),
1402 'log_comment' => $this->getComment(),
1403 'log_params' => $this->params
1404 );
1405 $dbw->insert( 'logging', $data, __METHOD__ );
1406 }
1407
1408 /**
1409 * @return bool
1410 */
1411 function importUpload() {
1412 # Construct a file
1413 $archiveName = $this->getArchiveName();
1414 if ( $archiveName ) {
1415 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
1416 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1417 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1418 } else {
1419 $file = wfLocalFile( $this->getTitle() );
1420 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
1421 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
1422 $archiveName = $file->getTimestamp() . '!' . $file->getName();
1423 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
1424 RepoGroup::singleton()->getLocalRepo(), $archiveName );
1425 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
1426 }
1427 }
1428 if( !$file ) {
1429 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
1430 return false;
1431 }
1432
1433 # Get the file source or download if necessary
1434 $source = $this->getFileSrc();
1435 $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0;
1436 if ( !$source ) {
1437 $source = $this->downloadSource();
1438 $flags |= File::DELETE_SOURCE;
1439 }
1440 if( !$source ) {
1441 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
1442 return false;
1443 }
1444 $sha1 = $this->getSha1();
1445 if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
1446 if ( $flags & File::DELETE_SOURCE ) {
1447 # Broken file; delete it if it is a temporary file
1448 unlink( $source );
1449 }
1450 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
1451 return false;
1452 }
1453
1454 $user = User::newFromName( $this->user_text );
1455
1456 # Do the actual upload
1457 if ( $archiveName ) {
1458 $status = $file->uploadOld( $source, $archiveName,
1459 $this->getTimestamp(), $this->getComment(), $user, $flags );
1460 } else {
1461 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
1462 $flags, false, $this->getTimestamp(), $user );
1463 }
1464
1465 if ( $status->isGood() ) {
1466 wfDebug( __METHOD__ . ": Succesful\n" );
1467 return true;
1468 } else {
1469 wfDebug( __METHOD__ . ': failed: ' . $status->getXml() . "\n" );
1470 return false;
1471 }
1472 }
1473
1474 /**
1475 * @return bool|string
1476 */
1477 function downloadSource() {
1478 global $wgEnableUploads;
1479 if( !$wgEnableUploads ) {
1480 return false;
1481 }
1482
1483 $tempo = tempnam( wfTempDir(), 'download' );
1484 $f = fopen( $tempo, 'wb' );
1485 if( !$f ) {
1486 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1487 return false;
1488 }
1489
1490 // @todo FIXME!
1491 $src = $this->getSrc();
1492 $data = Http::get( $src );
1493 if( !$data ) {
1494 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1495 fclose( $f );
1496 unlink( $tempo );
1497 return false;
1498 }
1499
1500 fwrite( $f, $data );
1501 fclose( $f );
1502
1503 return $tempo;
1504 }
1505
1506 }
1507
1508 /**
1509 * @todo document (e.g. one-sentence class description).
1510 * @ingroup SpecialPage
1511 */
1512 class ImportStringSource {
1513 function __construct( $string ) {
1514 $this->mString = $string;
1515 $this->mRead = false;
1516 }
1517
1518 /**
1519 * @return bool
1520 */
1521 function atEnd() {
1522 return $this->mRead;
1523 }
1524
1525 /**
1526 * @return bool|string
1527 */
1528 function readChunk() {
1529 if( $this->atEnd() ) {
1530 return false;
1531 }
1532 $this->mRead = true;
1533 return $this->mString;
1534 }
1535 }
1536
1537 /**
1538 * @todo document (e.g. one-sentence class description).
1539 * @ingroup SpecialPage
1540 */
1541 class ImportStreamSource {
1542 function __construct( $handle ) {
1543 $this->mHandle = $handle;
1544 }
1545
1546 /**
1547 * @return bool
1548 */
1549 function atEnd() {
1550 return feof( $this->mHandle );
1551 }
1552
1553 /**
1554 * @return string
1555 */
1556 function readChunk() {
1557 return fread( $this->mHandle, 32768 );
1558 }
1559
1560 /**
1561 * @param $filename string
1562 * @return Status
1563 */
1564 static function newFromFile( $filename ) {
1565 wfSuppressWarnings();
1566 $file = fopen( $filename, 'rt' );
1567 wfRestoreWarnings();
1568 if( !$file ) {
1569 return Status::newFatal( "importcantopen" );
1570 }
1571 return Status::newGood( new ImportStreamSource( $file ) );
1572 }
1573
1574 /**
1575 * @param $fieldname string
1576 * @return Status
1577 */
1578 static function newFromUpload( $fieldname = "xmlimport" ) {
1579 $upload =& $_FILES[$fieldname];
1580
1581 if( !isset( $upload ) || !$upload['name'] ) {
1582 return Status::newFatal( 'importnofile' );
1583 }
1584 if( !empty( $upload['error'] ) ) {
1585 switch($upload['error']){
1586 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1587 return Status::newFatal( 'importuploaderrorsize' );
1588 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1589 return Status::newFatal( 'importuploaderrorsize' );
1590 case 3: # The uploaded file was only partially uploaded
1591 return Status::newFatal( 'importuploaderrorpartial' );
1592 case 6: #Missing a temporary folder.
1593 return Status::newFatal( 'importuploaderrortemp' );
1594 # case else: # Currently impossible
1595 }
1596
1597 }
1598 $fname = $upload['tmp_name'];
1599 if( is_uploaded_file( $fname ) ) {
1600 return ImportStreamSource::newFromFile( $fname );
1601 } else {
1602 return Status::newFatal( 'importnofile' );
1603 }
1604 }
1605
1606 /**
1607 * @param $url
1608 * @param $method string
1609 * @return Status
1610 */
1611 static function newFromURL( $url, $method = 'GET' ) {
1612 wfDebug( __METHOD__ . ": opening $url\n" );
1613 # Use the standard HTTP fetch function; it times out
1614 # quicker and sorts out user-agent problems which might
1615 # otherwise prevent importing from large sites, such
1616 # as the Wikimedia cluster, etc.
1617 $data = Http::request( $method, $url, array( 'followRedirects' => true ) );
1618 if( $data !== false ) {
1619 $file = tmpfile();
1620 fwrite( $file, $data );
1621 fflush( $file );
1622 fseek( $file, 0 );
1623 return Status::newGood( new ImportStreamSource( $file ) );
1624 } else {
1625 return Status::newFatal( 'importcantopen' );
1626 }
1627 }
1628
1629 /**
1630 * @param $interwiki
1631 * @param $page
1632 * @param $history bool
1633 * @param $templates bool
1634 * @param $pageLinkDepth int
1635 * @return Status
1636 */
1637 public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
1638 if( $page == '' ) {
1639 return Status::newFatal( 'import-noarticle' );
1640 }
1641 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
1642 if( is_null( $link ) || $link->getInterwiki() == '' ) {
1643 return Status::newFatal( 'importbadinterwiki' );
1644 } else {
1645 $params = array();
1646 if ( $history ) $params['history'] = 1;
1647 if ( $templates ) $params['templates'] = 1;
1648 if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
1649 $url = $link->getFullUrl( $params );
1650 # For interwikis, use POST to avoid redirects.
1651 return ImportStreamSource::newFromURL( $url, "POST" );
1652 }
1653 }
1654 }