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