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