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