(bug 8440) Add API support for new blocking flag, also fix defaults so anywhere else...
[lhc/web/wiklou.git] / includes / Import.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 *
28 * @ingroup SpecialPage
29 */
30 class WikiRevision {
31 var $title = null;
32 var $id = 0;
33 var $timestamp = "20010115000000";
34 var $user = 0;
35 var $user_text = "";
36 var $text = "";
37 var $comment = "";
38 var $minor = false;
39 var $type = "";
40 var $action = "";
41 var $params = "";
42
43 function setTitle( $title ) {
44 if( is_object( $title ) ) {
45 $this->title = $title;
46 } elseif( is_null( $title ) ) {
47 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
48 } else {
49 throw new MWException( "WikiRevision given non-object title in import." );
50 }
51 }
52
53 function setID( $id ) {
54 $this->id = $id;
55 }
56
57 function setTimestamp( $ts ) {
58 # 2003-08-05T18:30:02Z
59 $this->timestamp = wfTimestamp( TS_MW, $ts );
60 }
61
62 function setUsername( $user ) {
63 $this->user_text = $user;
64 }
65
66 function setUserIP( $ip ) {
67 $this->user_text = $ip;
68 }
69
70 function setText( $text ) {
71 $this->text = $text;
72 }
73
74 function setComment( $text ) {
75 $this->comment = $text;
76 }
77
78 function setMinor( $minor ) {
79 $this->minor = (bool)$minor;
80 }
81
82 function setSrc( $src ) {
83 $this->src = $src;
84 }
85
86 function setFilename( $filename ) {
87 $this->filename = $filename;
88 }
89
90 function setSize( $size ) {
91 $this->size = intval( $size );
92 }
93
94 function setType( $type ) {
95 $this->type = $type;
96 }
97
98 function setAction( $action ) {
99 $this->action = $action;
100 }
101
102 function setParams( $params ) {
103 $this->params = $params;
104 }
105
106 function getTitle() {
107 return $this->title;
108 }
109
110 function getID() {
111 return $this->id;
112 }
113
114 function getTimestamp() {
115 return $this->timestamp;
116 }
117
118 function getUser() {
119 return $this->user_text;
120 }
121
122 function getText() {
123 return $this->text;
124 }
125
126 function getComment() {
127 return $this->comment;
128 }
129
130 function getMinor() {
131 return $this->minor;
132 }
133
134 function getSrc() {
135 return $this->src;
136 }
137
138 function getFilename() {
139 return $this->filename;
140 }
141
142 function getSize() {
143 return $this->size;
144 }
145
146 function getType() {
147 return $this->type;
148 }
149
150 function getAction() {
151 return $this->action;
152 }
153
154 function getParams() {
155 return $this->params;
156 }
157
158 function importOldRevision() {
159 $dbw = wfGetDB( DB_MASTER );
160
161 # Sneak a single revision into place
162 $user = User::newFromName( $this->getUser() );
163 if( $user ) {
164 $userId = intval( $user->getId() );
165 $userText = $user->getName();
166 } else {
167 $userId = 0;
168 $userText = $this->getUser();
169 }
170
171 // avoid memory leak...?
172 $linkCache = LinkCache::singleton();
173 $linkCache->clear();
174
175 $article = new Article( $this->title );
176 $pageId = $article->getId();
177 if( $pageId == 0 ) {
178 # must create the page...
179 $pageId = $article->insertOn( $dbw );
180 $created = true;
181 } else {
182 $created = false;
183
184 $prior = $dbw->selectField( 'revision', '1',
185 array( 'rev_page' => $pageId,
186 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
187 'rev_user_text' => $userText,
188 'rev_comment' => $this->getComment() ),
189 __METHOD__
190 );
191 if( $prior ) {
192 // FIXME: this could fail slightly for multiple matches :P
193 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
194 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
195 return false;
196 }
197 }
198
199 # FIXME: Use original rev_id optionally (better for backups)
200 # Insert the row
201 $revision = new Revision( array(
202 'page' => $pageId,
203 'text' => $this->getText(),
204 'comment' => $this->getComment(),
205 'user' => $userId,
206 'user_text' => $userText,
207 'timestamp' => $this->timestamp,
208 'minor_edit' => $this->minor,
209 ) );
210 $revId = $revision->insertOn( $dbw );
211 $changed = $article->updateIfNewerOn( $dbw, $revision );
212
213 if( $created ) {
214 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
215 Article::onArticleCreate( $this->title );
216
217 wfDebug( __METHOD__ . ": running create updates\n" );
218 $article->createUpdates( $revision );
219
220 } elseif( $changed ) {
221 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
222 Article::onArticleEdit( $this->title, false ); // leave templatelinks for editUpdates()
223
224 wfDebug( __METHOD__ . ": running edit updates\n" );
225 $article->editUpdates(
226 $this->getText(),
227 $this->getComment(),
228 $this->minor,
229 $this->timestamp,
230 $revId );
231 }
232
233 return true;
234 }
235
236 function importLogItem() {
237 $dbw = wfGetDB( DB_MASTER );
238 # FIXME: this will not record autoblocks
239 if( !$this->getTitle() ) {
240 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
241 $this->timestamp . "\n" );
242 return;
243 }
244 # Check if it exists already
245 // FIXME: use original log ID (better for backups)
246 $prior = $dbw->selectField( 'logging', '1',
247 array( 'log_type' => $this->getType(),
248 'log_action' => $this->getAction(),
249 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
250 'log_namespace' => $this->getTitle()->getNamespace(),
251 'log_title' => $this->getTitle()->getDBkey(),
252 'log_comment' => $this->getComment(),
253 #'log_user_text' => $this->user_text,
254 'log_params' => $this->params ),
255 __METHOD__
256 );
257 // FIXME: this could fail slightly for multiple matches :P
258 if( $prior ) {
259 wfDebug( __METHOD__ . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
260 $this->timestamp . "\n" );
261 return false;
262 }
263 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
264 $data = array(
265 'log_id' => $log_id,
266 'log_type' => $this->type,
267 'log_action' => $this->action,
268 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
269 'log_user' => User::idFromName( $this->user_text ),
270 #'log_user_text' => $this->user_text,
271 'log_namespace' => $this->getTitle()->getNamespace(),
272 'log_title' => $this->getTitle()->getDBkey(),
273 'log_comment' => $this->getComment(),
274 'log_params' => $this->params
275 );
276 $dbw->insert( 'logging', $data, __METHOD__ );
277 }
278
279 function importUpload() {
280 wfDebug( __METHOD__ . ": STUB\n" );
281
282 /**
283 // from file revert...
284 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
285 $comment = $wgRequest->getText( 'wpComment' );
286 // TODO: Preserve file properties from database instead of reloading from file
287 $status = $this->file->upload( $source, $comment, $comment );
288 if( $status->isGood() ) {
289 */
290
291 /**
292 // from file upload...
293 $this->mLocalFile = wfLocalFile( $nt );
294 $this->mDestName = $this->mLocalFile->getName();
295 //....
296 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
297 File::DELETE_SOURCE, $this->mFileProps );
298 if ( !$status->isGood() ) {
299 $resultDetails = array( 'internal' => $status->getWikiText() );
300 */
301
302 // @fixme upload() uses $wgUser, which is wrong here
303 // it may also create a page without our desire, also wrong potentially.
304 // and, it will record a *current* upload, but we might want an archive version here
305
306 $file = wfLocalFile( $this->getTitle() );
307 if( !$file ) {
308 var_dump( $file );
309 wfDebug( "IMPORT: Bad file. :(\n" );
310 return false;
311 }
312
313 $source = $this->downloadSource();
314 if( !$source ) {
315 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
316 return false;
317 }
318
319 $status = $file->upload( $source,
320 $this->getComment(),
321 $this->getComment(), // Initial page, if none present...
322 File::DELETE_SOURCE,
323 false, // props...
324 $this->getTimestamp() );
325
326 if( $status->isGood() ) {
327 // yay?
328 wfDebug( "IMPORT: is ok?\n" );
329 return true;
330 }
331
332 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
333 return false;
334
335 }
336
337 function downloadSource() {
338 global $wgEnableUploads;
339 if( !$wgEnableUploads ) {
340 return false;
341 }
342
343 $tempo = tempnam( wfTempDir(), 'download' );
344 $f = fopen( $tempo, 'wb' );
345 if( !$f ) {
346 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
347 return false;
348 }
349
350 // @fixme!
351 $src = $this->getSrc();
352 $data = Http::get( $src );
353 if( !$data ) {
354 wfDebug( "IMPORT: couldn't fetch source $src\n" );
355 fclose( $f );
356 unlink( $tempo );
357 return false;
358 }
359
360 fwrite( $f, $data );
361 fclose( $f );
362
363 return $tempo;
364 }
365
366 }
367
368 /**
369 * implements Special:Import
370 * @ingroup SpecialPage
371 */
372 class WikiImporter {
373 var $mDebug = false;
374 var $mSource = null;
375 var $mPageCallback = null;
376 var $mPageOutCallback = null;
377 var $mRevisionCallback = null;
378 var $mLogItemCallback = null;
379 var $mUploadCallback = null;
380 var $mTargetNamespace = null;
381 var $lastfield;
382 var $tagStack = array();
383
384 function __construct( $source ) {
385 $this->setRevisionCallback( array( $this, "importRevision" ) );
386 $this->setUploadCallback( array( $this, "importUpload" ) );
387 $this->setLogItemCallback( array( $this, "importLogItem" ) );
388 $this->mSource = $source;
389 }
390
391 function throwXmlError( $err ) {
392 $this->debug( "FAILURE: $err" );
393 wfDebug( "WikiImporter XML error: $err\n" );
394 }
395
396 # --------------
397
398 function doImport() {
399 if( empty( $this->mSource ) ) {
400 return new WikiErrorMsg( "importnotext" );
401 }
402
403 $parser = xml_parser_create( "UTF-8" );
404
405 # case folding violates XML standard, turn it off
406 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
407
408 xml_set_object( $parser, $this );
409 xml_set_element_handler( $parser, "in_start", "" );
410
411 $offset = 0; // for context extraction on error reporting
412 do {
413 $chunk = $this->mSource->readChunk();
414 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
415 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
416 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
417 }
418 $offset += strlen( $chunk );
419 } while( $chunk !== false && !$this->mSource->atEnd() );
420 xml_parser_free( $parser );
421
422 return true;
423 }
424
425 function debug( $data ) {
426 if( $this->mDebug ) {
427 wfDebug( "IMPORT: $data\n" );
428 }
429 }
430
431 function notice( $data ) {
432 global $wgCommandLineMode;
433 if( $wgCommandLineMode ) {
434 print "$data\n";
435 } else {
436 global $wgOut;
437 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
438 }
439 }
440
441 /**
442 * Set debug mode...
443 */
444 function setDebug( $debug ) {
445 $this->mDebug = $debug;
446 }
447
448 /**
449 * Sets the action to perform as each new page in the stream is reached.
450 * @param $callback callback
451 * @return callback
452 */
453 function setPageCallback( $callback ) {
454 $previous = $this->mPageCallback;
455 $this->mPageCallback = $callback;
456 return $previous;
457 }
458
459 /**
460 * Sets the action to perform as each page in the stream is completed.
461 * Callback accepts the page title (as a Title object), a second object
462 * with the original title form (in case it's been overridden into a
463 * local namespace), and a count of revisions.
464 *
465 * @param $callback callback
466 * @return callback
467 */
468 function setPageOutCallback( $callback ) {
469 $previous = $this->mPageOutCallback;
470 $this->mPageOutCallback = $callback;
471 return $previous;
472 }
473
474 /**
475 * Sets the action to perform as each page revision is reached.
476 * @param $callback callback
477 * @return callback
478 */
479 function setRevisionCallback( $callback ) {
480 $previous = $this->mRevisionCallback;
481 $this->mRevisionCallback = $callback;
482 return $previous;
483 }
484
485 /**
486 * Sets the action to perform as each file upload version is reached.
487 * @param $callback callback
488 * @return callback
489 */
490 function setUploadCallback( $callback ) {
491 $previous = $this->mUploadCallback;
492 $this->mUploadCallback = $callback;
493 return $previous;
494 }
495
496 /**
497 * Sets the action to perform as each log item reached.
498 * @param $callback callback
499 * @return callback
500 */
501 function setLogItemCallback( $callback ) {
502 $previous = $this->mLogItemCallback;
503 $this->mLogItemCallback = $callback;
504 return $previous;
505 }
506
507 /**
508 * Set a target namespace to override the defaults
509 */
510 function setTargetNamespace( $namespace ) {
511 if( is_null( $namespace ) ) {
512 // Don't override namespaces
513 $this->mTargetNamespace = null;
514 } elseif( $namespace >= 0 ) {
515 // FIXME: Check for validity
516 $this->mTargetNamespace = intval( $namespace );
517 } else {
518 return false;
519 }
520 }
521
522 /**
523 * Default per-revision callback, performs the import.
524 * @param $revision WikiRevision
525 * @private
526 */
527 function importRevision( $revision ) {
528 $dbw = wfGetDB( DB_MASTER );
529 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
530 }
531
532 /**
533 * Default per-revision callback, performs the import.
534 * @param $revision WikiRevision
535 * @private
536 */
537 function importLogItem( $rev ) {
538 $dbw = wfGetDB( DB_MASTER );
539 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
540 }
541
542 /**
543 * Dummy for now...
544 */
545 function importUpload( $revision ) {
546 //$dbw = wfGetDB( DB_MASTER );
547 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
548 return false;
549 }
550
551 /**
552 * Alternate per-revision callback, for debugging.
553 * @param $revision WikiRevision
554 * @private
555 */
556 function debugRevisionHandler( &$revision ) {
557 $this->debug( "Got revision:" );
558 if( is_object( $revision->title ) ) {
559 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
560 } else {
561 $this->debug( "-- Title: <invalid>" );
562 }
563 $this->debug( "-- User: " . $revision->user_text );
564 $this->debug( "-- Timestamp: " . $revision->timestamp );
565 $this->debug( "-- Comment: " . $revision->comment );
566 $this->debug( "-- Text: " . $revision->text );
567 }
568
569 /**
570 * Notify the callback function when a new <page> is reached.
571 * @param $title Title
572 * @private
573 */
574 function pageCallback( $title ) {
575 if( is_callable( $this->mPageCallback ) ) {
576 call_user_func( $this->mPageCallback, $title );
577 }
578 }
579
580 /**
581 * Notify the callback function when a </page> is closed.
582 * @param $title Title
583 * @param $origTitle Title
584 * @param $revisionCount int
585 * @param $successCount Int: number of revisions for which callback returned true
586 * @private
587 */
588 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
589 if( is_callable( $this->mPageOutCallback ) ) {
590 call_user_func( $this->mPageOutCallback, $title, $origTitle,
591 $revisionCount, $successCount );
592 }
593 }
594
595 # XML parser callbacks from here out -- beware!
596 function donothing( $parser, $x, $y="" ) {
597 #$this->debug( "donothing" );
598 }
599
600 function in_start( $parser, $name, $attribs ) {
601 $this->debug( "in_start $name" );
602 if( $name != "mediawiki" ) {
603 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
604 }
605 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
606 }
607
608 function in_mediawiki( $parser, $name, $attribs ) {
609 $this->debug( "in_mediawiki $name" );
610 if( $name == 'siteinfo' ) {
611 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
612 } elseif( $name == 'page' ) {
613 $this->push( $name );
614 $this->workRevisionCount = 0;
615 $this->workSuccessCount = 0;
616 $this->uploadCount = 0;
617 $this->uploadSuccessCount = 0;
618 xml_set_element_handler( $parser, "in_page", "out_page" );
619 } elseif( $name == 'logitem' ) {
620 $this->push( $name );
621 $this->workRevision = new WikiRevision;
622 xml_set_element_handler( $parser, "in_logitem", "out_logitem" );
623 } else {
624 return $this->throwXMLerror( "Expected <page>, got <$name>" );
625 }
626 }
627 function out_mediawiki( $parser, $name ) {
628 $this->debug( "out_mediawiki $name" );
629 if( $name != "mediawiki" ) {
630 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
631 }
632 xml_set_element_handler( $parser, "donothing", "donothing" );
633 }
634
635
636 function in_siteinfo( $parser, $name, $attribs ) {
637 // no-ops for now
638 $this->debug( "in_siteinfo $name" );
639 switch( $name ) {
640 case "sitename":
641 case "base":
642 case "generator":
643 case "case":
644 case "namespaces":
645 case "namespace":
646 break;
647 default:
648 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
649 }
650 }
651
652 function out_siteinfo( $parser, $name ) {
653 if( $name == "siteinfo" ) {
654 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
655 }
656 }
657
658
659 function in_page( $parser, $name, $attribs ) {
660 $this->debug( "in_page $name" );
661 switch( $name ) {
662 case "id":
663 case "title":
664 case "restrictions":
665 $this->appendfield = $name;
666 $this->appenddata = "";
667 xml_set_element_handler( $parser, "in_nothing", "out_append" );
668 xml_set_character_data_handler( $parser, "char_append" );
669 break;
670 case "revision":
671 $this->push( "revision" );
672 if( is_object( $this->pageTitle ) ) {
673 $this->workRevision = new WikiRevision;
674 $this->workRevision->setTitle( $this->pageTitle );
675 $this->workRevisionCount++;
676 } else {
677 // Skipping items due to invalid page title
678 $this->workRevision = null;
679 }
680 xml_set_element_handler( $parser, "in_revision", "out_revision" );
681 break;
682 case "upload":
683 $this->push( "upload" );
684 if( is_object( $this->pageTitle ) ) {
685 $this->workRevision = new WikiRevision;
686 $this->workRevision->setTitle( $this->pageTitle );
687 $this->uploadCount++;
688 } else {
689 // Skipping items due to invalid page title
690 $this->workRevision = null;
691 }
692 xml_set_element_handler( $parser, "in_upload", "out_upload" );
693 break;
694 default:
695 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
696 }
697 }
698
699 function out_page( $parser, $name ) {
700 $this->debug( "out_page $name" );
701 $this->pop();
702 if( $name != "page" ) {
703 return $this->throwXMLerror( "Expected </page>, got </$name>" );
704 }
705 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
706
707 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
708 $this->workRevisionCount, $this->workSuccessCount );
709
710 $this->workTitle = null;
711 $this->workRevision = null;
712 $this->workRevisionCount = 0;
713 $this->workSuccessCount = 0;
714 $this->pageTitle = null;
715 $this->origTitle = null;
716 }
717
718 function in_nothing( $parser, $name, $attribs ) {
719 $this->debug( "in_nothing $name" );
720 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
721 }
722
723 function char_append( $parser, $data ) {
724 $this->debug( "char_append '$data'" );
725 $this->appenddata .= $data;
726 }
727
728 function out_append( $parser, $name ) {
729 $this->debug( "out_append $name" );
730 if( $name != $this->appendfield ) {
731 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
732 }
733
734 switch( $this->appendfield ) {
735 case "title":
736 $this->workTitle = $this->appenddata;
737 $this->origTitle = Title::newFromText( $this->workTitle );
738 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
739 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
740 $this->origTitle->getDBkey() );
741 } else {
742 $this->pageTitle = Title::newFromText( $this->workTitle );
743 }
744 if( is_null( $this->pageTitle ) ) {
745 // Invalid page title? Ignore the page
746 $this->notice( "Skipping invalid page title '$this->workTitle'" );
747 } else {
748 $this->pageCallback( $this->workTitle );
749 }
750 break;
751 case "id":
752 if ( $this->parentTag() == 'revision' || $this->parentTag() == 'logitem' ) {
753 if( $this->workRevision )
754 $this->workRevision->setID( $this->appenddata );
755 }
756 break;
757 case "text":
758 if( $this->workRevision )
759 $this->workRevision->setText( $this->appenddata );
760 break;
761 case "username":
762 if( $this->workRevision )
763 $this->workRevision->setUsername( $this->appenddata );
764 break;
765 case "ip":
766 if( $this->workRevision )
767 $this->workRevision->setUserIP( $this->appenddata );
768 break;
769 case "timestamp":
770 if( $this->workRevision )
771 $this->workRevision->setTimestamp( $this->appenddata );
772 break;
773 case "comment":
774 if( $this->workRevision )
775 $this->workRevision->setComment( $this->appenddata );
776 break;
777 case "type":
778 if( $this->workRevision )
779 $this->workRevision->setType( $this->appenddata );
780 break;
781 case "action":
782 if( $this->workRevision )
783 $this->workRevision->setAction( $this->appenddata );
784 break;
785 case "logtitle":
786 if( $this->workRevision )
787 $this->workRevision->setTitle( Title::newFromText( $this->appenddata ) );
788 break;
789 case "params":
790 if( $this->workRevision )
791 $this->workRevision->setParams( $this->appenddata );
792 break;
793 case "minor":
794 if( $this->workRevision )
795 $this->workRevision->setMinor( true );
796 break;
797 case "filename":
798 if( $this->workRevision )
799 $this->workRevision->setFilename( $this->appenddata );
800 break;
801 case "src":
802 if( $this->workRevision )
803 $this->workRevision->setSrc( $this->appenddata );
804 break;
805 case "size":
806 if( $this->workRevision )
807 $this->workRevision->setSize( intval( $this->appenddata ) );
808 break;
809 default:
810 $this->debug( "Bad append: {$this->appendfield}" );
811 }
812 $this->appendfield = "";
813 $this->appenddata = "";
814
815 $parent = $this->parentTag();
816 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
817 xml_set_character_data_handler( $parser, "donothing" );
818 }
819
820 function in_revision( $parser, $name, $attribs ) {
821 $this->debug( "in_revision $name" );
822 switch( $name ) {
823 case "id":
824 case "timestamp":
825 case "comment":
826 case "minor":
827 case "text":
828 $this->appendfield = $name;
829 xml_set_element_handler( $parser, "in_nothing", "out_append" );
830 xml_set_character_data_handler( $parser, "char_append" );
831 break;
832 case "contributor":
833 $this->push( "contributor" );
834 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
835 break;
836 default:
837 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
838 }
839 }
840
841 function out_revision( $parser, $name ) {
842 $this->debug( "out_revision $name" );
843 $this->pop();
844 if( $name != "revision" ) {
845 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
846 }
847 xml_set_element_handler( $parser, "in_page", "out_page" );
848
849 if( $this->workRevision ) {
850 $ok = call_user_func_array( $this->mRevisionCallback,
851 array( $this->workRevision, $this ) );
852 if( $ok ) {
853 $this->workSuccessCount++;
854 }
855 }
856 }
857
858 function in_logitem( $parser, $name, $attribs ) {
859 $this->debug( "in_logitem $name" );
860 switch( $name ) {
861 case "id":
862 case "timestamp":
863 case "comment":
864 case "type":
865 case "action":
866 case "logtitle":
867 case "params":
868 $this->appendfield = $name;
869 xml_set_element_handler( $parser, "in_nothing", "out_append" );
870 xml_set_character_data_handler( $parser, "char_append" );
871 break;
872 case "contributor":
873 $this->push( "contributor" );
874 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
875 break;
876 default:
877 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
878 }
879 }
880
881 function out_logitem( $parser, $name ) {
882 $this->debug( "out_logitem $name" );
883 $this->pop();
884 if( $name != "logitem" ) {
885 return $this->throwXMLerror( "Expected </logitem>, got </$name>" );
886 }
887 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
888
889 if( $this->workRevision ) {
890 $ok = call_user_func_array( $this->mLogItemCallback,
891 array( $this->workRevision, $this ) );
892 if( $ok ) {
893 $this->workSuccessCount++;
894 }
895 }
896 }
897
898 function in_upload( $parser, $name, $attribs ) {
899 $this->debug( "in_upload $name" );
900 switch( $name ) {
901 case "timestamp":
902 case "comment":
903 case "text":
904 case "filename":
905 case "src":
906 case "size":
907 $this->appendfield = $name;
908 xml_set_element_handler( $parser, "in_nothing", "out_append" );
909 xml_set_character_data_handler( $parser, "char_append" );
910 break;
911 case "contributor":
912 $this->push( "contributor" );
913 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
914 break;
915 default:
916 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
917 }
918 }
919
920 function out_upload( $parser, $name ) {
921 $this->debug( "out_revision $name" );
922 $this->pop();
923 if( $name != "upload" ) {
924 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
925 }
926 xml_set_element_handler( $parser, "in_page", "out_page" );
927
928 if( $this->workRevision ) {
929 $ok = call_user_func_array( $this->mUploadCallback,
930 array( $this->workRevision, $this ) );
931 if( $ok ) {
932 $this->workUploadSuccessCount++;
933 }
934 }
935 }
936
937 function in_contributor( $parser, $name, $attribs ) {
938 $this->debug( "in_contributor $name" );
939 switch( $name ) {
940 case "username":
941 case "ip":
942 case "id":
943 $this->appendfield = $name;
944 xml_set_element_handler( $parser, "in_nothing", "out_append" );
945 xml_set_character_data_handler( $parser, "char_append" );
946 break;
947 default:
948 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
949 }
950 }
951
952 function out_contributor( $parser, $name ) {
953 $this->debug( "out_contributor $name" );
954 $this->pop();
955 if( $name != "contributor" ) {
956 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
957 }
958 $parent = $this->parentTag();
959 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
960 }
961
962 private function push( $name ) {
963 array_push( $this->tagStack, $name );
964 $this->debug( "PUSH $name" );
965 }
966
967 private function pop() {
968 $name = array_pop( $this->tagStack );
969 $this->debug( "POP $name" );
970 return $name;
971 }
972
973 private function parentTag() {
974 $name = $this->tagStack[count( $this->tagStack ) - 1];
975 $this->debug( "PARENT $name" );
976 return $name;
977 }
978
979 }
980
981 /**
982 * @todo document (e.g. one-sentence class description).
983 * @ingroup SpecialPage
984 */
985 class ImportStringSource {
986 function __construct( $string ) {
987 $this->mString = $string;
988 $this->mRead = false;
989 }
990
991 function atEnd() {
992 return $this->mRead;
993 }
994
995 function readChunk() {
996 if( $this->atEnd() ) {
997 return false;
998 } else {
999 $this->mRead = true;
1000 return $this->mString;
1001 }
1002 }
1003 }
1004
1005 /**
1006 * @todo document (e.g. one-sentence class description).
1007 * @ingroup SpecialPage
1008 */
1009 class ImportStreamSource {
1010 function __construct( $handle ) {
1011 $this->mHandle = $handle;
1012 }
1013
1014 function atEnd() {
1015 return feof( $this->mHandle );
1016 }
1017
1018 function readChunk() {
1019 return fread( $this->mHandle, 32768 );
1020 }
1021
1022 static function newFromFile( $filename ) {
1023 $file = @fopen( $filename, 'rt' );
1024 if( !$file ) {
1025 return new WikiErrorMsg( "importcantopen" );
1026 }
1027 return new ImportStreamSource( $file );
1028 }
1029
1030 static function newFromUpload( $fieldname = "xmlimport" ) {
1031 $upload =& $_FILES[$fieldname];
1032
1033 if( !isset( $upload ) || !$upload['name'] ) {
1034 return new WikiErrorMsg( 'importnofile' );
1035 }
1036 if( !empty( $upload['error'] ) ) {
1037 switch($upload['error']){
1038 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1039 return new WikiErrorMsg( 'importuploaderrorsize' );
1040 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1041 return new WikiErrorMsg( 'importuploaderrorsize' );
1042 case 3: # The uploaded file was only partially uploaded
1043 return new WikiErrorMsg( 'importuploaderrorpartial' );
1044 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1045 return new WikiErrorMsg( 'importuploaderrortemp' );
1046 # case else: # Currently impossible
1047 }
1048
1049 }
1050 $fname = $upload['tmp_name'];
1051 if( is_uploaded_file( $fname ) ) {
1052 return ImportStreamSource::newFromFile( $fname );
1053 } else {
1054 return new WikiErrorMsg( 'importnofile' );
1055 }
1056 }
1057
1058 static function newFromURL( $url, $method = 'GET' ) {
1059 wfDebug( __METHOD__ . ": opening $url\n" );
1060 # Use the standard HTTP fetch function; it times out
1061 # quicker and sorts out user-agent problems which might
1062 # otherwise prevent importing from large sites, such
1063 # as the Wikimedia cluster, etc.
1064 $data = Http::request( $method, $url );
1065 if( $data !== false ) {
1066 $file = tmpfile();
1067 fwrite( $file, $data );
1068 fflush( $file );
1069 fseek( $file, 0 );
1070 return new ImportStreamSource( $file );
1071 } else {
1072 return new WikiErrorMsg( 'importcantopen' );
1073 }
1074 }
1075
1076 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
1077 if( $page == '' ) {
1078 return new WikiErrorMsg( 'import-noarticle' );
1079 }
1080 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
1081 if( is_null( $link ) || $link->getInterwiki() == '' ) {
1082 return new WikiErrorMsg( 'importbadinterwiki' );
1083 } else {
1084 $params = $history ? 'history=1' : '';
1085 $url = $link->getFullUrl( $params );
1086 # For interwikis, use POST to avoid redirects.
1087 return ImportStreamSource::newFromURL( $url, "POST" );
1088 }
1089 }
1090 }