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