Works for limited case of history import on existing pages
[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
23 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
24 $importer = new WikiImporter();
25 if( $importer->setupFromUpload( "xmlimport" ) ) {
26 $importer->setRevisionHandler( "wfImportOldRevision" );
27 if( $importer->doImport() ) {
28 # Success!
29 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
30 } else {
31 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
32 htmlspecialchars( $importer->getError() ) ) . "</p>" );
33 }
34 } else {
35 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
36 }
37 }
38
39 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
40 $action = $wgTitle->escapeLocalUrl();
41 $wgOut->addHTML( "
42 <form enctype='multipart/form-data' method='post' action=\"$action\">
43 <input type='hidden' name='action' value='submit' />
44 <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
45 <input type='file' name='xmlimport' value='' size='40' /><br />
46 <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
47 </form>
48 " );
49 }
50
51 function wfImportOldRevision( $revision ) {
52 global $wgOut;
53 $fname = "wfImportOldRevision";
54
55 # Sneak a single revision into place
56 $ns = IntVal( $revision->title->getNamespace() );
57 $t = wfStrencode( $revision->title->getDBkey() );
58 $text = wfStrencode( $revision->getText() );
59 $ts = wfStrencode( $revision->timestamp );
60 $its = wfStrencode( wfInvertTimestamp( $revision->timestamp ) ) ;
61 $comment = wfStrencode( $revision->getComment() );
62
63 $user = User::newFromName( $revision->getUser() );
64 $user_id = IntVal( $user->getId() );
65 $user_text = wfStrencode( $user->getName() );
66
67 $minor = 0; # ??
68 $flags = "";
69
70 # Make sure it doesn't already exist
71 $sql = "SELECT 1 FROM old WHERE old_namespace=$ns AND old_title='$t' AND old_timestamp='$ts'";
72 $wgOut->addHtml( htmlspecialchars( $sql ) . "<br />\n" );
73
74 $res = wfQuery( $sql, DB_WRITE, $fname );
75 $numrows = wfNumRows( $res );
76 wfFreeResult( $res );
77 if( $numrows > 0 ) {
78 $wgOut->addHTML( "DIE<br />" );
79 return false;
80 }
81
82 $res = wfQuery( "INSERT INTO old " .
83 "(old_namespace,old_title,old_text,old_comment,old_user,old_user_text," .
84 "old_timestamp,inverse_timestamp,old_minor_edit,old_flags) " .
85 "VALUES ($ns,'$t','$text','$comment',$user_id,'$user_text','$ts','$its',$minor,'$flags')",
86 DB_WRITE, $fname );
87 $wgOut->addHTML( "OK<br />" );
88
89 return true;
90 }
91
92 class WikiRevision {
93 var $title = NULL;
94 var $timestamp = "20010115000000";
95 var $user = 0;
96 var $user_text = "";
97 var $text = "";
98 var $comment = "";
99
100 function setTitle( $text ) {
101 $text = $this->fixEncoding( $text );
102 $this->title = Title::newFromText( $text );
103 }
104
105 function setTimestamp( $ts ) {
106 # 2003-08-05T18:30:02Z
107 $this->timestamp = preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
108 }
109
110 function setUsername( $user ) {
111 $this->user_text = $this->fixEncoding( $user );
112 }
113
114 function setUserIP( $ip ) {
115 $this->user_text = $this->fixEncoding( $ip );
116 }
117
118 function setText( $text ) {
119 $this->text = $this->fixEncoding( $text );
120 }
121
122 function setComment( $text ) {
123 $this->comment = $this->fixEncoding( $text );
124 }
125
126 function fixEncoding( $data ) {
127 global $wgLang, $wgInputEncoding;
128
129 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
130 return $data;
131 } else {
132 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
133 }
134 }
135
136 function getTitle() {
137 return $this->title;
138 }
139
140 function getTimestamp() {
141 return $this->timestamp;
142 }
143
144 function getUser() {
145 return $this->user_text;
146 }
147
148 function getText() {
149 return $this->text;
150 }
151
152 function getComment() {
153 return $this->comment;
154 }
155 }
156
157 class WikiImporter {
158 var $mSource = NULL;
159 var $mError = "";
160 var $mXmlError = XML_ERROR_NONE;
161 var $mRevisionHandler = NULL;
162 var $lastfield;
163
164 function WikiImporter() {
165 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
166 }
167
168 function setError( $err ) {
169 $this->mError = $err;
170 return false;
171 }
172
173 function getError() {
174 if( $this->mXmlError == XML_ERROR_NONE ) {
175 return $this->mError;
176 } else {
177 return xml_error_string( $this->mXmlError );
178 }
179 }
180
181 function throwXmlError( $err ) {
182 $this->debug( "FAILURE: $err" );
183 }
184
185 function setupFromFile( $filename ) {
186 $this->mSource = file_get_contents( $filename );
187 return true;
188 }
189
190 function setupFromUpload( $fieldname = "xmlimport" ) {
191 global $wgOut;
192
193 $upload =& $_FILES[$fieldname];
194
195 if( !isset( $upload ) ) {
196 return $this->setError( wfMsg( "importnofile" ) );
197 }
198 if( !empty( $upload['error'] ) ) {
199 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
200 }
201 $fname = $upload['tmp_name'];
202 if( is_uploaded_file( $fname ) ) {
203 return $this->setupFromFile( $fname );
204 } else {
205 return $this->setError( wfMsg( "importnofile" ) );
206 }
207 }
208
209 function setupFromURL( $url ) {
210 # FIXME
211 wfDebugDieBacktrace( "Not yet implemented." );
212 }
213
214 function doImport() {
215 if( empty( $this->mSource ) ) {
216 return $this->setError( wfMsg( "importnotext" ) );
217 }
218
219 $parser = xml_parser_create( "UTF-8" );
220
221 # case folding violates XML standard, turn it off
222 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
223
224 xml_set_object( $parser, &$this );
225 xml_set_element_handler( $parser, "in_start", "" );
226
227 if( !xml_parse( $parser, $this->mSource, true ) ) {
228 # return error message
229 $this->mXmlError = xml_get_error_code( $parser );
230 xml_parser_free( $parser );
231 return false;
232 }
233 xml_parser_free( $parser );
234
235 return true;
236 }
237
238 function debug( $data ) {
239 global $wgOut;
240 # $wgOut->addHTML( htmlspecialchars( $data ) . "<br />\n" );
241 }
242
243 function setRevisionHandler( $functionref ) {
244 $this->mRevisionHandler = $functionref;
245 }
246
247 function defaultRevisionHandler( $revision ) {
248 $this->debug( "Got revision:" );
249 if( is_object( $revision->title ) ) {
250 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
251 } else {
252 $this->debug( "-- Title: <invalid>" );
253 }
254 $this->debug( "-- User: " . $revision->user_text );
255 $this->debug( "-- Timestamp: " . $revision->timestamp );
256 $this->debug( "-- Comment: " . $revision->comment );
257 $this->debug( "-- Text: " . $revision->text );
258 }
259
260
261
262 # XML parser callbacks from here out -- beware!
263 function donothing( $parser, $x, $y="" ) {
264 #$this->debug( "donothing" );
265 }
266
267 function in_start( $parser, $name, $attribs ) {
268 $this->debug( "in_start $name" );
269 if( $name != "mediawiki" ) {
270 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
271 }
272 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
273 }
274
275 function in_mediawiki( $parser, $name, $attribs ) {
276 $this->debug( "in_mediawiki $name" );
277 if( $name != "page" ) {
278 return $this->throwXMLerror( "Expected <page>, got <$name>" );
279 }
280 xml_set_element_handler( $parser, "in_page", "out_page" );
281 }
282 function out_mediawiki( $parser, $name ) {
283 $this->debug( "out_mediawiki $name" );
284 if( $name != "mediawiki" ) {
285 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
286 }
287 xml_set_element_handler( $parser, "donothing", "donothing" );
288 }
289
290 function in_page( $parser, $name, $attribs ) {
291 $this->debug( "in_page $name" );
292 switch( $name ) {
293 case "id":
294 case "title":
295 case "restrictions":
296 $this->appendfield = $name;
297 $this->appenddata = "";
298 $this->parenttag = "page";
299 xml_set_element_handler( $parser, "in_nothing", "out_append" );
300 xml_set_character_data_handler( $parser, "char_append" );
301 break;
302 case "revision":
303 $this->workRevision = new WikiRevision;
304 $this->workRevision->setTitle( $this->workTitle );
305 xml_set_element_handler( $parser, "in_revision", "out_revision" );
306 break;
307 default:
308 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
309 }
310 }
311
312 function out_page( $parser, $name ) {
313 $this->debug( "out_page $name" );
314 if( $name != "page" ) {
315 return $this->throwXMLerror( "Expected </page>, got </$name>" );
316 }
317 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
318
319 $this->workTitle = NULL;
320 $this->workRevision = NULL;
321 }
322
323 function in_nothing( $parser, $name, $attribs ) {
324 $this->debug( "in_nothing $name" );
325 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
326 }
327 function char_append( $parser, $data ) {
328 $this->debug( "char_append '$data'" );
329 $this->appenddata .= $data;
330 }
331 function out_append( $parser, $name ) {
332 $this->debug( "out_append $name" );
333 if( $name != $this->appendfield ) {
334 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
335 }
336 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
337 xml_set_character_data_handler( $parser, "donothing" );
338 switch( $this->appendfield ) {
339 case "title":
340 $this->workTitle = $this->appenddata;
341 break;
342 case "text":
343 $this->workRevision->setText( $this->appenddata );
344 break;
345 case "username":
346 $this->workRevision->setUsername( $this->appenddata );
347 break;
348 case "ip":
349 $this->workRevision->setUserIP( $this->appenddata );
350 break;
351 case "timestamp":
352 $this->workRevision->setTimestamp( $this->appenddata );
353 break;
354 case "comment":
355 $this->workRevision->setComment( $this->appenddata );
356 break;
357 default;
358 $this->debug( "Bad append: {$this->appendfield}" );
359 }
360 $this->appendfield = "";
361 $this->appenddata = "";
362 }
363
364 function in_revision( $parser, $name, $attribs ) {
365 $this->debug( "in_revision $name" );
366 switch( $name ) {
367 case "id":
368 case "timestamp":
369 case "comment":
370 case "text":
371 $this->parenttag = "revision";
372 $this->appendfield = $name;
373 xml_set_element_handler( $parser, "in_nothing", "out_append" );
374 xml_set_character_data_handler( $parser, "char_append" );
375 break;
376 case "contributor":
377 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
378 break;
379 default:
380 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
381 }
382 }
383 function out_revision( $parser, $name ) {
384 $this->debug( "out_revision $name" );
385 if( $name != "revision" ) {
386 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
387 }
388 xml_set_element_handler( $parser, "in_page", "out_page" );
389
390 call_user_func( $this->mRevisionHandler, $this->workRevision );
391 }
392
393 function in_contributor( $parser, $name, $attribs ) {
394 $this->debug( "in_contributor $name" );
395 switch( $name ) {
396 case "username":
397 case "ip":
398 $this->parenttag = "contributor";
399 $this->appendfield = $name;
400 xml_set_element_handler( $parser, "in_nothing", "out_append" );
401 xml_set_character_data_handler( $parser, "char_append" );
402 break;
403 default:
404 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
405 }
406 }
407
408 function out_contributor( $parser, $name ) {
409 $this->debug( "out_contributor $name" );
410 if( $name != "contributor" ) {
411 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
412 }
413 xml_set_element_handler( $parser, "in_revision", "out_revision" );
414 }
415 }
416
417
418 ?>