Whoops, debugging comment.
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /**
27 * Constructor
28 */
29 function wfSpecialImport( $page = '' ) {
30 global $wgOut, $wgLang, $wgRequest, $wgTitle;
31 global $wgImportSources;
32
33 ###
34 $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
35 return;
36 ###
37
38 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
39 $importer = new WikiImporter();
40
41 switch( $wgRequest->getVal( "source" ) ) {
42 case "upload":
43 $ok = $importer->setupFromUpload( "xmlimport" );
44 break;
45 case "interwiki":
46 $ok = $importer->setupFromInterwiki(
47 $wgRequest->getVal( "interwiki" ),
48 $wgRequest->getText( "frompage" ) );
49 break;
50 default:
51 $ok = false;
52 }
53
54 if( $ok ) {
55 $importer->setRevisionHandler( "wfImportOldRevision" );
56 if( $importer->doImport() ) {
57 # Success!
58 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
59 } else {
60 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
61 htmlspecialchars( $importer->getError() ) ) . "</p>" );
62 }
63 } else {
64 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
65 }
66 }
67
68 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
69 $action = $wgTitle->escapeLocalUrl();
70 $wgOut->addHTML( "
71 <fieldset>
72 <legend>Upload XML</legend>
73 <form enctype='multipart/form-data' method='post' action=\"$action\">
74 <input type='hidden' name='action' value='submit' />
75 <input type='hidden' name='source' value='upload' />
76 <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
77 <input type='file' name='xmlimport' value='' size='30' />
78 <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
79 </form>
80 </fieldset>
81 " );
82
83 if( !empty( $wgImportSources ) ) {
84 $wgOut->addHTML( "
85 <fieldset>
86 <legend>Interwiki import</legend>
87 <form method='post' action=\"$action\">
88 <input type='hidden' name='action' value='submit' />
89 <input type='hidden' name='source' value='interwiki' />
90 <select name='interwiki'>
91 " );
92 foreach( $wgImportSources as $interwiki ) {
93 $iw = htmlspecialchars( $interwiki );
94 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
95 }
96 $wgOut->addHTML( "
97 </select>
98 <input name='frompage' />
99 <input type='submit' />
100 </form>
101 </fieldset>
102 " );
103 }
104 }
105
106 function wfImportOldRevision( &$revision ) {
107 $dbw =& wfGetDB( DB_MASTER );
108 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
109 }
110
111 /**
112 *
113 * @package MediaWiki
114 * @subpackage SpecialPage
115 */
116 class WikiRevision {
117 var $title = NULL;
118 var $timestamp = "20010115000000";
119 var $user = 0;
120 var $user_text = "";
121 var $text = "";
122 var $comment = "";
123
124 function setTitle( $text ) {
125 $text = $this->fixEncoding( $text );
126 $this->title = Title::newFromText( $text );
127 }
128
129 function setTimestamp( $ts ) {
130 # 2003-08-05T18:30:02Z
131 $this->timestamp = preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
132 }
133
134 function setUsername( $user ) {
135 $this->user_text = $this->fixEncoding( $user );
136 }
137
138 function setUserIP( $ip ) {
139 $this->user_text = $this->fixEncoding( $ip );
140 }
141
142 function setText( $text ) {
143 $this->text = $this->fixEncoding( $text );
144 }
145
146 function setComment( $text ) {
147 $this->comment = $this->fixEncoding( $text );
148 }
149
150 function fixEncoding( $data ) {
151 global $wgLang, $wgInputEncoding;
152
153 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
154 return $data;
155 } else {
156 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
157 }
158 }
159
160 function getTitle() {
161 return $this->title;
162 }
163
164 function getTimestamp() {
165 return $this->timestamp;
166 }
167
168 function getUser() {
169 return $this->user_text;
170 }
171
172 function getText() {
173 return $this->text;
174 }
175
176 function getComment() {
177 return $this->comment;
178 }
179 }
180
181 /**
182 *
183 * @package MediaWiki
184 * @subpackage SpecialPage
185 */
186 class WikiImporter {
187 var $mSource = NULL;
188 var $mError = "";
189 var $mXmlError = XML_ERROR_NONE;
190 var $mRevisionHandler = NULL;
191 var $lastfield;
192
193 function WikiImporter() {
194 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
195 }
196
197 function setError( $err ) {
198 $this->mError = $err;
199 return false;
200 }
201
202 function getError() {
203 if( $this->mXmlError == XML_ERROR_NONE ) {
204 return $this->mError;
205 } else {
206 return xml_error_string( $this->mXmlError );
207 }
208 }
209
210 function throwXmlError( $err ) {
211 $this->debug( "FAILURE: $err" );
212 }
213
214 function setupFromFile( $filename ) {
215 $this->mSource = file_get_contents( $filename );
216 return true;
217 }
218
219 function setupFromUpload( $fieldname = "xmlimport" ) {
220 global $wgOut;
221
222 $upload =& $_FILES[$fieldname];
223
224 if( !isset( $upload ) ) {
225 return $this->setError( wfMsg( "importnofile" ) );
226 }
227 if( !empty( $upload['error'] ) ) {
228 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
229 }
230 $fname = $upload['tmp_name'];
231 if( is_uploaded_file( $fname ) ) {
232 return $this->setupFromFile( $fname );
233 } else {
234 return $this->setError( wfMsg( "importnofile" ) );
235 }
236 }
237
238 function setupFromURL( $url ) {
239 # fopen-wrappers are normally turned off for security.
240 ini_set( "allow_url_fopen", true );
241 $ret = $this->setupFromFile( $url );
242 ini_set( "allow_url_fopen", false );
243 return $ret;
244 }
245
246 function setupFromInterwiki( $interwiki, $page ) {
247 $base = Title::getInterwikiLink( $interwiki );
248 if( empty( $base ) ) {
249 return false;
250 } else {
251 $import = wfUrlencode( "Special:Export/$page" );
252 $url = str_replace( "$1", $import, $base );
253 $this->notice( "Importing from $url" );
254 return $this->setupFromURL( $url );
255 }
256 }
257
258 # --------------
259
260 function doImport() {
261 if( empty( $this->mSource ) ) {
262 return $this->setError( wfMsg( "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 if( !xml_parse( $parser, $this->mSource, true ) ) {
274 # return error message
275 $this->mXmlError = xml_get_error_code( $parser );
276 xml_parser_free( $parser );
277 return false;
278 }
279 xml_parser_free( $parser );
280
281 return true;
282 }
283
284 function debug( $data ) {
285 global $wgOut;
286 # $this->notice( "DEBUG: $data\n" );
287 }
288
289 function notice( $data ) {
290 global $wgCommandLineMode;
291 if( $wgCommandLineMode ) {
292 print "$data\n";
293 } else {
294 global $wgOut;
295 $wgOut->addHTML( "<li>$data</li>\n" );
296 }
297 }
298
299 function setRevisionHandler( $functionref ) {
300 $this->mRevisionHandler = $functionref;
301 }
302
303 function defaultRevisionHandler( &$revision ) {
304 $this->debug( "Got revision:" );
305 if( is_object( $revision->title ) ) {
306 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
307 } else {
308 $this->debug( "-- Title: <invalid>" );
309 }
310 $this->debug( "-- User: " . $revision->user_text );
311 $this->debug( "-- Timestamp: " . $revision->timestamp );
312 $this->debug( "-- Comment: " . $revision->comment );
313 $this->debug( "-- Text: " . $revision->text );
314 }
315
316
317
318 # XML parser callbacks from here out -- beware!
319 function donothing( $parser, $x, $y="" ) {
320 #$this->debug( "donothing" );
321 }
322
323 function in_start( $parser, $name, $attribs ) {
324 $this->debug( "in_start $name" );
325 if( $name != "mediawiki" ) {
326 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
327 }
328 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
329 }
330
331 function in_mediawiki( $parser, $name, $attribs ) {
332 $this->debug( "in_mediawiki $name" );
333 if( $name != "page" ) {
334 return $this->throwXMLerror( "Expected <page>, got <$name>" );
335 }
336 xml_set_element_handler( $parser, "in_page", "out_page" );
337 }
338 function out_mediawiki( $parser, $name ) {
339 $this->debug( "out_mediawiki $name" );
340 if( $name != "mediawiki" ) {
341 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
342 }
343 xml_set_element_handler( $parser, "donothing", "donothing" );
344 }
345
346 function in_page( $parser, $name, $attribs ) {
347 $this->debug( "in_page $name" );
348 switch( $name ) {
349 case "id":
350 case "title":
351 case "restrictions":
352 $this->appendfield = $name;
353 $this->appenddata = "";
354 $this->parenttag = "page";
355 xml_set_element_handler( $parser, "in_nothing", "out_append" );
356 xml_set_character_data_handler( $parser, "char_append" );
357 break;
358 case "revision":
359 $this->workRevision = new WikiRevision;
360 $this->workRevision->setTitle( $this->workTitle );
361 xml_set_element_handler( $parser, "in_revision", "out_revision" );
362 break;
363 default:
364 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
365 }
366 }
367
368 function out_page( $parser, $name ) {
369 $this->debug( "out_page $name" );
370 if( $name != "page" ) {
371 return $this->throwXMLerror( "Expected </page>, got </$name>" );
372 }
373 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
374
375 $this->workTitle = NULL;
376 $this->workRevision = NULL;
377 }
378
379 function in_nothing( $parser, $name, $attribs ) {
380 $this->debug( "in_nothing $name" );
381 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
382 }
383 function char_append( $parser, $data ) {
384 $this->debug( "char_append '$data'" );
385 $this->appenddata .= $data;
386 }
387 function out_append( $parser, $name ) {
388 $this->debug( "out_append $name" );
389 if( $name != $this->appendfield ) {
390 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
391 }
392 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
393 xml_set_character_data_handler( $parser, "donothing" );
394 switch( $this->appendfield ) {
395 case "title":
396 $this->workTitle = $this->appenddata;
397 break;
398 case "text":
399 $this->workRevision->setText( $this->appenddata );
400 break;
401 case "username":
402 $this->workRevision->setUsername( $this->appenddata );
403 break;
404 case "ip":
405 $this->workRevision->setUserIP( $this->appenddata );
406 break;
407 case "timestamp":
408 $this->workRevision->setTimestamp( $this->appenddata );
409 break;
410 case "comment":
411 $this->workRevision->setComment( $this->appenddata );
412 break;
413 default;
414 $this->debug( "Bad append: {$this->appendfield}" );
415 }
416 $this->appendfield = "";
417 $this->appenddata = "";
418 }
419
420 function in_revision( $parser, $name, $attribs ) {
421 $this->debug( "in_revision $name" );
422 switch( $name ) {
423 case "id":
424 case "timestamp":
425 case "comment":
426 case "text":
427 $this->parenttag = "revision";
428 $this->appendfield = $name;
429 xml_set_element_handler( $parser, "in_nothing", "out_append" );
430 xml_set_character_data_handler( $parser, "char_append" );
431 break;
432 case "contributor":
433 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
434 break;
435 default:
436 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
437 }
438 }
439
440 function out_revision( $parser, $name ) {
441 $this->debug( "out_revision $name" );
442 if( $name != "revision" ) {
443 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
444 }
445 xml_set_element_handler( $parser, "in_page", "out_page" );
446
447 $out = call_user_func( $this->mRevisionHandler, &$this->workRevision, &$this );
448 if( !empty( $out ) ) {
449 global $wgOut;
450 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
451 }
452 }
453
454 function in_contributor( $parser, $name, $attribs ) {
455 $this->debug( "in_contributor $name" );
456 switch( $name ) {
457 case "username":
458 case "ip":
459 $this->parenttag = "contributor";
460 $this->appendfield = $name;
461 xml_set_element_handler( $parser, "in_nothing", "out_append" );
462 xml_set_character_data_handler( $parser, "char_append" );
463 break;
464 default:
465 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
466 }
467 }
468
469 function out_contributor( $parser, $name ) {
470 $this->debug( "out_contributor $name" );
471 if( $name != "contributor" ) {
472 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
473 }
474 xml_set_element_handler( $parser, "in_revision", "out_revision" );
475 }
476
477 function importOldRevision() {
478 $fname = "WikiImporter::importOldRevision";
479 $dbw =& wfGetDB( DB_MASTER );
480
481 # Sneak a single revision into place
482 $user = User::newFromName( $this->getUser() );
483
484 $res = $dbw->select( 'old', 1,
485 $this->title->oldCond() + array( 'old_timestamp' => $this->timestamp ),
486 $fname, 'FOR UPDATE'
487 );
488
489 $numrows = $dbw->numRows( $res );
490 $dbw->freeResult( $res );
491 if( $numrows > 0 ) {
492 return wfMsg( "importhistoryconflict" );
493 }
494
495 # Insert the row
496 $oldIgnore = $dbw->ignoreErrors( true );
497 $success = $dbw->insert( 'old',
498 array(
499 'old_namespace' => intval( $this->title->getNamespace() ),
500 'old_title' => $this->title->getDBkey(),
501 'old_text' => $this->getText(),
502 'old_comment' => $this->getComment(),
503 'old_user' => intval( $user->getId() ),
504 'old_user_text' => $user->getName(),
505 'old_timestamp' => $this->timestamp,
506 'inverse_timestamp' => wfInvertTimestamp( $this->timestamp ),
507 'old_minor_edit' => 0,
508 'old_flags' => ''
509 ), $fname
510 );
511
512 return wfMsg( "ok" );
513 }
514 }
515
516
517 ?>