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