add Last-Modified header
[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 global $wgOut;
99 $fname = "wfImportOldRevision";
100
101 # Sneak a single revision into place
102 $ns = IntVal( $revision->title->getNamespace() );
103 $t = wfStrencode( $revision->title->getDBkey() );
104 $text = wfStrencode( $revision->getText() );
105 $ts = wfStrencode( $revision->timestamp );
106 $its = wfStrencode( wfInvertTimestamp( $revision->timestamp ) ) ;
107 $comment = wfStrencode( $revision->getComment() );
108
109 $user = User::newFromName( $revision->getUser() );
110 $user_id = IntVal( $user->getId() );
111 $user_text = wfStrencode( $user->getName() );
112
113 $minor = 0; # ??
114 $flags = "";
115
116 # Make sure it doesn't already exist
117 $sql = "SELECT 1 FROM old WHERE old_namespace=$ns AND old_title='$t' AND old_timestamp='$ts'";
118 $res = wfQuery( $sql, DB_WRITE, $fname );
119 $numrows = wfNumRows( $res );
120 wfFreeResult( $res );
121 if( $numrows > 0 ) {
122 return wfMsg( "importhistoryconflict" );
123 }
124
125 $res = wfQuery( "INSERT INTO old " .
126 "(old_namespace,old_title,old_text,old_comment,old_user,old_user_text," .
127 "old_timestamp,inverse_timestamp,old_minor_edit,old_flags) " .
128 "VALUES ($ns,'$t','$text','$comment',$user_id,'$user_text','$ts','$its',$minor,'$flags')",
129 DB_WRITE, $fname );
130
131 return wfMsg( "ok" );
132 }
133
134 class WikiRevision {
135 var $title = NULL;
136 var $timestamp = "20010115000000";
137 var $user = 0;
138 var $user_text = "";
139 var $text = "";
140 var $comment = "";
141
142 function setTitle( $text ) {
143 $text = $this->fixEncoding( $text );
144 $this->title = Title::newFromText( $text );
145 }
146
147 function setTimestamp( $ts ) {
148 # 2003-08-05T18:30:02Z
149 $this->timestamp = preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
150 }
151
152 function setUsername( $user ) {
153 $this->user_text = $this->fixEncoding( $user );
154 }
155
156 function setUserIP( $ip ) {
157 $this->user_text = $this->fixEncoding( $ip );
158 }
159
160 function setText( $text ) {
161 $this->text = $this->fixEncoding( $text );
162 }
163
164 function setComment( $text ) {
165 $this->comment = $this->fixEncoding( $text );
166 }
167
168 function fixEncoding( $data ) {
169 global $wgLang, $wgInputEncoding;
170
171 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
172 return $data;
173 } else {
174 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
175 }
176 }
177
178 function getTitle() {
179 return $this->title;
180 }
181
182 function getTimestamp() {
183 return $this->timestamp;
184 }
185
186 function getUser() {
187 return $this->user_text;
188 }
189
190 function getText() {
191 return $this->text;
192 }
193
194 function getComment() {
195 return $this->comment;
196 }
197 }
198
199 class WikiImporter {
200 var $mSource = NULL;
201 var $mError = "";
202 var $mXmlError = XML_ERROR_NONE;
203 var $mRevisionHandler = NULL;
204 var $lastfield;
205
206 function WikiImporter() {
207 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
208 }
209
210 function setError( $err ) {
211 $this->mError = $err;
212 return false;
213 }
214
215 function getError() {
216 if( $this->mXmlError == XML_ERROR_NONE ) {
217 return $this->mError;
218 } else {
219 return xml_error_string( $this->mXmlError );
220 }
221 }
222
223 function throwXmlError( $err ) {
224 $this->debug( "FAILURE: $err" );
225 }
226
227 function setupFromFile( $filename ) {
228 $this->mSource = file_get_contents( $filename );
229 return true;
230 }
231
232 function setupFromUpload( $fieldname = "xmlimport" ) {
233 global $wgOut;
234
235 $upload =& $_FILES[$fieldname];
236
237 if( !isset( $upload ) ) {
238 return $this->setError( wfMsg( "importnofile" ) );
239 }
240 if( !empty( $upload['error'] ) ) {
241 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
242 }
243 $fname = $upload['tmp_name'];
244 if( is_uploaded_file( $fname ) ) {
245 return $this->setupFromFile( $fname );
246 } else {
247 return $this->setError( wfMsg( "importnofile" ) );
248 }
249 }
250
251 function setupFromURL( $url ) {
252 # fopen-wrappers are normally turned off for security.
253 ini_set( "allow_url_fopen", true );
254 $ret = $this->setupFromFile( $url );
255 ini_set( "allow_url_fopen", false );
256 return $ret;
257 }
258
259 function setupFromInterwiki( $interwiki, $page ) {
260 $base = Title::getInterwikiLink( $interwiki );
261 if( empty( $base ) ) {
262 return false;
263 } else {
264 $import = wfUrlencode( "Special:Export/$page" );
265 $url = str_replace( "$1", $import, $base );
266 $this->notice( "Importing from $url" );
267 return $this->setupFromURL( $url );
268 }
269 }
270
271 # --------------
272
273 function doImport() {
274 if( empty( $this->mSource ) ) {
275 return $this->setError( wfMsg( "importnotext" ) );
276 }
277
278 $parser = xml_parser_create( "UTF-8" );
279
280 # case folding violates XML standard, turn it off
281 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
282
283 xml_set_object( $parser, &$this );
284 xml_set_element_handler( $parser, "in_start", "" );
285
286 if( !xml_parse( $parser, $this->mSource, true ) ) {
287 # return error message
288 $this->mXmlError = xml_get_error_code( $parser );
289 xml_parser_free( $parser );
290 return false;
291 }
292 xml_parser_free( $parser );
293
294 return true;
295 }
296
297 function debug( $data ) {
298 global $wgOut;
299 # $this->notice( "DEBUG: $data\n" );
300 }
301
302 function notice( $data ) {
303 global $wgCommandLineMode;
304 if( $wgCommandLineMode ) {
305 print "$data\n";
306 } else {
307 global $wgOut;
308 $wgOut->addHTML( "<li>$data</li>\n" );
309 }
310 }
311
312 function setRevisionHandler( $functionref ) {
313 $this->mRevisionHandler = $functionref;
314 }
315
316 function defaultRevisionHandler( &$revision ) {
317 $this->debug( "Got revision:" );
318 if( is_object( $revision->title ) ) {
319 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
320 } else {
321 $this->debug( "-- Title: <invalid>" );
322 }
323 $this->debug( "-- User: " . $revision->user_text );
324 $this->debug( "-- Timestamp: " . $revision->timestamp );
325 $this->debug( "-- Comment: " . $revision->comment );
326 $this->debug( "-- Text: " . $revision->text );
327 }
328
329
330
331 # XML parser callbacks from here out -- beware!
332 function donothing( $parser, $x, $y="" ) {
333 #$this->debug( "donothing" );
334 }
335
336 function in_start( $parser, $name, $attribs ) {
337 $this->debug( "in_start $name" );
338 if( $name != "mediawiki" ) {
339 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
340 }
341 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
342 }
343
344 function in_mediawiki( $parser, $name, $attribs ) {
345 $this->debug( "in_mediawiki $name" );
346 if( $name != "page" ) {
347 return $this->throwXMLerror( "Expected <page>, got <$name>" );
348 }
349 xml_set_element_handler( $parser, "in_page", "out_page" );
350 }
351 function out_mediawiki( $parser, $name ) {
352 $this->debug( "out_mediawiki $name" );
353 if( $name != "mediawiki" ) {
354 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
355 }
356 xml_set_element_handler( $parser, "donothing", "donothing" );
357 }
358
359 function in_page( $parser, $name, $attribs ) {
360 $this->debug( "in_page $name" );
361 switch( $name ) {
362 case "id":
363 case "title":
364 case "restrictions":
365 $this->appendfield = $name;
366 $this->appenddata = "";
367 $this->parenttag = "page";
368 xml_set_element_handler( $parser, "in_nothing", "out_append" );
369 xml_set_character_data_handler( $parser, "char_append" );
370 break;
371 case "revision":
372 $this->workRevision = new WikiRevision;
373 $this->workRevision->setTitle( $this->workTitle );
374 xml_set_element_handler( $parser, "in_revision", "out_revision" );
375 break;
376 default:
377 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
378 }
379 }
380
381 function out_page( $parser, $name ) {
382 $this->debug( "out_page $name" );
383 if( $name != "page" ) {
384 return $this->throwXMLerror( "Expected </page>, got </$name>" );
385 }
386 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
387
388 $this->workTitle = NULL;
389 $this->workRevision = NULL;
390 }
391
392 function in_nothing( $parser, $name, $attribs ) {
393 $this->debug( "in_nothing $name" );
394 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
395 }
396 function char_append( $parser, $data ) {
397 $this->debug( "char_append '$data'" );
398 $this->appenddata .= $data;
399 }
400 function out_append( $parser, $name ) {
401 $this->debug( "out_append $name" );
402 if( $name != $this->appendfield ) {
403 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
404 }
405 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
406 xml_set_character_data_handler( $parser, "donothing" );
407 switch( $this->appendfield ) {
408 case "title":
409 $this->workTitle = $this->appenddata;
410 break;
411 case "text":
412 $this->workRevision->setText( $this->appenddata );
413 break;
414 case "username":
415 $this->workRevision->setUsername( $this->appenddata );
416 break;
417 case "ip":
418 $this->workRevision->setUserIP( $this->appenddata );
419 break;
420 case "timestamp":
421 $this->workRevision->setTimestamp( $this->appenddata );
422 break;
423 case "comment":
424 $this->workRevision->setComment( $this->appenddata );
425 break;
426 default;
427 $this->debug( "Bad append: {$this->appendfield}" );
428 }
429 $this->appendfield = "";
430 $this->appenddata = "";
431 }
432
433 function in_revision( $parser, $name, $attribs ) {
434 $this->debug( "in_revision $name" );
435 switch( $name ) {
436 case "id":
437 case "timestamp":
438 case "comment":
439 case "text":
440 $this->parenttag = "revision";
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 case "contributor":
446 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
447 break;
448 default:
449 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
450 }
451 }
452
453 function out_revision( $parser, $name ) {
454 $this->debug( "out_revision $name" );
455 if( $name != "revision" ) {
456 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
457 }
458 xml_set_element_handler( $parser, "in_page", "out_page" );
459
460 $out = call_user_func( $this->mRevisionHandler, &$this->workRevision, &$this );
461 if( !empty( $out ) ) {
462 global $wgOut;
463 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
464 }
465 }
466
467 function in_contributor( $parser, $name, $attribs ) {
468 $this->debug( "in_contributor $name" );
469 switch( $name ) {
470 case "username":
471 case "ip":
472 $this->parenttag = "contributor";
473 $this->appendfield = $name;
474 xml_set_element_handler( $parser, "in_nothing", "out_append" );
475 xml_set_character_data_handler( $parser, "char_append" );
476 break;
477 default:
478 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
479 }
480 }
481
482 function out_contributor( $parser, $name ) {
483 $this->debug( "out_contributor $name" );
484 if( $name != "contributor" ) {
485 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
486 }
487 xml_set_element_handler( $parser, "in_revision", "out_revision" );
488 }
489 }
490
491
492 ?>