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