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