Some code cleanup on Special:Import, add initial version of command-line
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 require_once( 'WikiError.php' );
27
28 /**
29 * Constructor
30 */
31 function wfSpecialImport( $page = '' ) {
32 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgTitle;
33 global $wgImportSources;
34
35 ###
36 # $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
37 # return;
38 ###
39
40 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
41 switch( $wgRequest->getVal( "source" ) ) {
42 case "upload":
43 if( $wgUser->isAllowed( 'importupload' ) ) {
44 $source = ImportStreamSource::newFromUpload( "xmlimport" );
45 } else {
46 return $wgOut->permissionRequired( 'importupload' );
47 }
48 break;
49 case "interwiki":
50 $source = ImportStreamSource::newFromInterwiki(
51 $wgRequest->getVal( "interwiki" ),
52 $wgRequest->getText( "frompage" ) );
53 break;
54 default:
55 $source = new WikiError( "Unknown import source type" );
56 }
57
58 if( WikiError::isError( $source ) ) {
59 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
60 } else {
61 $importer = new WikiImporter( $source );
62 $result = $importer->doImport();
63 if( WikiError::isError( $result ) ) {
64 $wgOut->addWikiText( wfMsg( "importfailed",
65 wfEscapeWikiText( $result->getMessage() ) ) );
66 } else {
67 # Success!
68 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
69 }
70 }
71 }
72
73 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
74
75 if( $wgUser->isAllowed( 'importupload' ) ) {
76 $wgOut->addWikiText( wfMsg( "importtext" ) );
77 $wgOut->addHTML( "
78 <fieldset>
79 <legend>" . wfMsgHtml('upload') . "</legend>
80 <form enctype='multipart/form-data' method='post' action=\"$action\">
81 <input type='hidden' name='action' value='submit' />
82 <input type='hidden' name='source' value='upload' />
83 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
84 <input type='file' name='xmlimport' value='' size='30' />
85 <input type='submit' value='" . wfMsgHtml( "uploadbtn" ) . "'/>
86 </form>
87 </fieldset>
88 " );
89 } else {
90 if( empty( $wgImportSources ) ) {
91 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
92 }
93 }
94
95 if( !empty( $wgImportSources ) ) {
96 $wgOut->addHTML( "
97 <fieldset>
98 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
99 <form method='post' action=\"$action\">
100 <input type='hidden' name='action' value='submit' />
101 <input type='hidden' name='source' value='interwiki' />
102 <select name='interwiki'>
103 " );
104 foreach( $wgImportSources as $interwiki ) {
105 $iw = htmlspecialchars( $interwiki );
106 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
107 }
108 $wgOut->addHTML( "
109 </select>
110 <input name='frompage' />
111 <input type='submit' />
112 </form>
113 </fieldset>
114 " );
115 }
116 }
117
118 /**
119 *
120 * @package MediaWiki
121 * @subpackage SpecialPage
122 */
123 class WikiRevision {
124 var $title = NULL;
125 var $timestamp = "20010115000000";
126 var $user = 0;
127 var $user_text = "";
128 var $text = "";
129 var $comment = "";
130
131 function setTitle( $text ) {
132 $this->title = Title::newFromText( $text );
133 }
134
135 function setTimestamp( $ts ) {
136 # 2003-08-05T18:30:02Z
137 $this->timestamp = preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
138 }
139
140 function setUsername( $user ) {
141 $this->user_text = $user;
142 }
143
144 function setUserIP( $ip ) {
145 $this->user_text = $ip;
146 }
147
148 function setText( $text ) {
149 $this->text = $text;
150 }
151
152 function setComment( $text ) {
153 $this->comment = $text;
154 }
155
156 function getTitle() {
157 return $this->title;
158 }
159
160 function getTimestamp() {
161 return $this->timestamp;
162 }
163
164 function getUser() {
165 return $this->user_text;
166 }
167
168 function getText() {
169 return $this->text;
170 }
171
172 function getComment() {
173 return $this->comment;
174 }
175
176 function importOldRevision() {
177 $fname = "WikiImporter::importOldRevision";
178 $dbw =& wfGetDB( DB_MASTER );
179
180 # Sneak a single revision into place
181 $user = User::newFromName( $this->getUser() );
182 if( $user ) {
183 $userId = IntVal( $user->getId() );
184 $userText = $user->getName();
185 } else {
186 $userId = 0;
187 $userText = $this->getUser();
188 }
189
190 $article = new Article( $this->title );
191 $pageId = $article->getId();
192 if( $pageId == 0 ) {
193 # must create the page...
194 $pageId = $article->insertOn( $dbw );
195 }
196
197 # FIXME: Check for exact conflicts
198 # FIXME: Use original rev_id optionally
199 # FIXME: blah blah blah
200
201 #if( $numrows > 0 ) {
202 # return wfMsg( "importhistoryconflict" );
203 #}
204
205 # Insert the row
206 $revision = new Revision( array(
207 'page' => $pageId,
208 'text' => $this->getText(),
209 'comment' => $this->getComment(),
210 'user' => $userId,
211 'user_text' => $userText,
212 'timestamp' => $this->timestamp,
213 'minor_edit' => 0
214 ) );
215 $revId = $revision->insertOn( $dbw );
216 $article->updateIfNewerOn( $dbw, $revision );
217
218 return true;
219 }
220
221 }
222
223 /**
224 *
225 * @package MediaWiki
226 * @subpackage SpecialPage
227 */
228 class WikiImporter {
229 var $mSource = null;
230 var $mPageCallback = null;
231 var $mRevisionCallback = null;
232 var $lastfield;
233
234 function WikiImporter( $source ) {
235 $this->setRevisionCallback( array( &$this, "importRevision" ) );
236 $this->mSource = $source;
237 }
238
239 function throwXmlError( $err ) {
240 $this->debug( "FAILURE: $err" );
241 }
242
243 # --------------
244
245 function doImport() {
246 if( empty( $this->mSource ) ) {
247 return new WikiErrorMsg( "importnotext" );
248 }
249
250 $parser = xml_parser_create( "UTF-8" );
251
252 # case folding violates XML standard, turn it off
253 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
254
255 xml_set_object( $parser, &$this );
256 xml_set_element_handler( $parser, "in_start", "" );
257
258 do {
259 $chunk = $this->mSource->readChunk();
260 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
261 return new WikiXmlError( $parser );
262 }
263 } while( $chunk !== false && !$this->mSource->atEnd() );
264 xml_parser_free( $parser );
265
266 return true;
267 }
268
269 function debug( $data ) {
270 #wfDebug( "IMPORT: $data\n" );
271 }
272
273 function notice( $data ) {
274 global $wgCommandLineMode;
275 if( $wgCommandLineMode ) {
276 print "$data\n";
277 } else {
278 global $wgOut;
279 $wgOut->addHTML( "<li>$data</li>\n" );
280 }
281 }
282
283 /**
284 * Sets the action to perform as each new page in the stream is reached.
285 * @param callable $callback
286 * @return callable
287 */
288 function setPageCallback( $callback ) {
289 $previous = $this->mPageCallback;
290 $this->mPageCallback = $callback;
291 return $previous;
292 }
293
294 /**
295 * Sets the action to perform as each page revision is reached.
296 * @param callable $callback
297 * @return callable
298 */
299 function setRevisionCallback( $callback ) {
300 $previous = $this->mRevisionCallback;
301 $this->mRevisionCallback = $callback;
302 return $previous;
303 }
304
305 /**
306 * Default per-revision callback, performs the import.
307 * @param WikiRevision $revision
308 * @access private
309 */
310 function importRevision( &$revision ) {
311 $dbw =& wfGetDB( DB_MASTER );
312 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
313 }
314
315 /**
316 * Alternate per-revision callback, for debugging.
317 * @param WikiRevision $revision
318 * @access private
319 */
320 function debugRevisionHandler( &$revision ) {
321 $this->debug( "Got revision:" );
322 if( is_object( $revision->title ) ) {
323 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
324 } else {
325 $this->debug( "-- Title: <invalid>" );
326 }
327 $this->debug( "-- User: " . $revision->user_text );
328 $this->debug( "-- Timestamp: " . $revision->timestamp );
329 $this->debug( "-- Comment: " . $revision->comment );
330 $this->debug( "-- Text: " . $revision->text );
331 }
332
333 /**
334 * Notify the callback function when a new <page> is reached.
335 * @param Title $title
336 * @access private
337 */
338 function pageCallback( $title ) {
339 if( is_callable( $this->mPageCallback ) ) {
340 call_user_func( $this->mPageCallback, $title );
341 }
342 }
343
344
345 # XML parser callbacks from here out -- beware!
346 function donothing( $parser, $x, $y="" ) {
347 #$this->debug( "donothing" );
348 }
349
350 function in_start( $parser, $name, $attribs ) {
351 $this->debug( "in_start $name" );
352 if( $name != "mediawiki" ) {
353 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
354 }
355 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
356 }
357
358 function in_mediawiki( $parser, $name, $attribs ) {
359 $this->debug( "in_mediawiki $name" );
360 if( $name == 'siteinfo' ) {
361 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
362 } elseif( $name == 'page' ) {
363 xml_set_element_handler( $parser, "in_page", "out_page" );
364 } else {
365 return $this->throwXMLerror( "Expected <page>, got <$name>" );
366 }
367 }
368 function out_mediawiki( $parser, $name ) {
369 $this->debug( "out_mediawiki $name" );
370 if( $name != "mediawiki" ) {
371 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
372 }
373 xml_set_element_handler( $parser, "donothing", "donothing" );
374 }
375
376
377 function in_siteinfo( $parser, $name, $attribs ) {
378 // no-ops for now
379 $this->debug( "in_siteinfo $name" );
380 switch( $name ) {
381 case "sitename":
382 case "generator":
383 case "case":
384 case "namespaces":
385 case "namespace":
386 break;
387 default:
388 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
389 }
390 }
391
392 function out_siteinfo( $parser, $name ) {
393 if( $name == "siteinfo" ) {
394 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
395 }
396 }
397
398
399 function in_page( $parser, $name, $attribs ) {
400 $this->debug( "in_page $name" );
401 switch( $name ) {
402 case "id":
403 case "title":
404 case "restrictions":
405 $this->appendfield = $name;
406 $this->appenddata = "";
407 $this->parenttag = "page";
408 xml_set_element_handler( $parser, "in_nothing", "out_append" );
409 xml_set_character_data_handler( $parser, "char_append" );
410 break;
411 case "revision":
412 $this->workRevision = new WikiRevision;
413 $this->workRevision->setTitle( $this->workTitle );
414 xml_set_element_handler( $parser, "in_revision", "out_revision" );
415 break;
416 default:
417 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
418 }
419 }
420
421 function out_page( $parser, $name ) {
422 $this->debug( "out_page $name" );
423 if( $name != "page" ) {
424 return $this->throwXMLerror( "Expected </page>, got </$name>" );
425 }
426 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
427
428 $this->workTitle = NULL;
429 $this->workRevision = NULL;
430 }
431
432 function in_nothing( $parser, $name, $attribs ) {
433 $this->debug( "in_nothing $name" );
434 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
435 }
436 function char_append( $parser, $data ) {
437 $this->debug( "char_append '$data'" );
438 $this->appenddata .= $data;
439 }
440 function out_append( $parser, $name ) {
441 $this->debug( "out_append $name" );
442 if( $name != $this->appendfield ) {
443 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
444 }
445 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
446 xml_set_character_data_handler( $parser, "donothing" );
447
448 switch( $this->appendfield ) {
449 case "title":
450 $this->workTitle = $this->appenddata;
451 $this->pageCallback( $this->workTitle );
452 break;
453 case "text":
454 $this->workRevision->setText( $this->appenddata );
455 break;
456 case "username":
457 $this->workRevision->setUsername( $this->appenddata );
458 break;
459 case "ip":
460 $this->workRevision->setUserIP( $this->appenddata );
461 break;
462 case "timestamp":
463 $this->workRevision->setTimestamp( $this->appenddata );
464 break;
465 case "comment":
466 $this->workRevision->setComment( $this->appenddata );
467 break;
468 default:
469 $this->debug( "Bad append: {$this->appendfield}" );
470 }
471 $this->appendfield = "";
472 $this->appenddata = "";
473 }
474
475 function in_revision( $parser, $name, $attribs ) {
476 $this->debug( "in_revision $name" );
477 switch( $name ) {
478 case "id":
479 case "timestamp":
480 case "comment":
481 case "text":
482 $this->parenttag = "revision";
483 $this->appendfield = $name;
484 xml_set_element_handler( $parser, "in_nothing", "out_append" );
485 xml_set_character_data_handler( $parser, "char_append" );
486 break;
487 case "contributor":
488 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
489 break;
490 default:
491 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
492 }
493 }
494
495 function out_revision( $parser, $name ) {
496 $this->debug( "out_revision $name" );
497 if( $name != "revision" ) {
498 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
499 }
500 xml_set_element_handler( $parser, "in_page", "out_page" );
501
502 $out = call_user_func( $this->mRevisionCallback,
503 &$this->workRevision,
504 &$this );
505 if( !empty( $out ) ) {
506 global $wgOut;
507 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
508 }
509 }
510
511 function in_contributor( $parser, $name, $attribs ) {
512 $this->debug( "in_contributor $name" );
513 switch( $name ) {
514 case "username":
515 case "ip":
516 $this->parenttag = "contributor";
517 $this->appendfield = $name;
518 xml_set_element_handler( $parser, "in_nothing", "out_append" );
519 xml_set_character_data_handler( $parser, "char_append" );
520 break;
521 default:
522 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
523 }
524 }
525
526 function out_contributor( $parser, $name ) {
527 $this->debug( "out_contributor $name" );
528 if( $name != "contributor" ) {
529 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
530 }
531 xml_set_element_handler( $parser, "in_revision", "out_revision" );
532 }
533
534 }
535
536 class ImportStringSource {
537 function ImportStringSource( $string ) {
538 $this->mString = $string;
539 $this->mRead = false;
540 }
541
542 function atEnd() {
543 return $this->mRead;
544 }
545
546 function readChunk() {
547 if( $this->atEnd() ) {
548 return false;
549 } else {
550 $this->mRead = true;
551 return $this->mString;
552 }
553 }
554 }
555
556 class ImportStreamSource {
557 function ImportStreamSource( $handle ) {
558 $this->mHandle = $handle;
559 }
560
561 function atEnd() {
562 return feof( $this->mHandle );
563 }
564
565 function readChunk() {
566 return fread( $this->mHandle, 32768 );
567 }
568
569 function newFromFile( $filename ) {
570 $file = @fopen( $filename, 'rt' );
571 if( !$file ) {
572 return new WikiError( "Couldn't open import file" );
573 }
574 return new ImportStreamSource( $file );
575 }
576
577 function newFromUpload( $fieldname = "xmlimport" ) {
578 global $wgOut;
579
580 $upload =& $_FILES[$fieldname];
581
582 if( !isset( $upload ) ) {
583 return new WikiErrorMsg( 'importnofile' );
584 }
585 if( !empty( $upload['error'] ) ) {
586 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
587 }
588 $fname = $upload['tmp_name'];
589 if( is_uploaded_file( $fname ) ) {
590 return ImportStreamSource::newFromFile( $fname );
591 } else {
592 return new WikiErrorMsg( 'importnofile' );
593 }
594 }
595
596 function newFromURL( $url ) {
597 # fopen-wrappers are normally turned off for security.
598 ini_set( "allow_url_fopen", true );
599 $ret = ImportStreamSource::newFromFile( $url );
600 ini_set( "allow_url_fopen", false );
601 return $ret;
602 }
603
604 function newFromInterwiki( $interwiki, $page ) {
605 $base = Title::getInterwikiLink( $interwiki );
606 if( empty( $base ) ) {
607 return new WikiError( 'Bad interwiki link' );
608 } else {
609 $import = wfUrlencode( "Special:Export/$page" );
610 $url = str_replace( "$1", $import, $base );
611 return ImportStreamSource::newFromURL( $url );
612 }
613 }
614 }
615
616
617 ?>