2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 function wfSpecialImport( $page = "" ) {
21 global $wgOut, $wgLang, $wgRequest, $wgTitle;
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() ) {
29 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
31 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
32 htmlspecialchars( $importer->getError() ) ) . "</p>" );
35 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
39 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
40 $action = $wgTitle->escapeLocalUrl();
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" ) ) . "'/>
51 function wfImportOldRevision( $revision ) {
53 $fname = "wfImportOldRevision";
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() );
63 $user = User
::newFromName( $revision->getUser() );
64 $user_id = IntVal( $user->getId() );
65 $user_text = wfStrencode( $user->getName() );
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" );
74 $res = wfQuery( $sql, DB_WRITE
, $fname );
75 $numrows = wfNumRows( $res );
78 $wgOut->addHTML( "DIE<br />" );
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')",
87 $wgOut->addHTML( "OK<br />" );
94 var $timestamp = "20010115000000";
100 function setTitle( $text ) {
101 $text = $this->fixEncoding( $text );
102 $this->title
= Title
::newFromText( $text );
105 function setTimestamp( $ts ) {
106 # 2003-08-05T18:30:02Z
107 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
110 function setUsername( $user ) {
111 $this->user_text
= $this->fixEncoding( $user );
114 function setUserIP( $ip ) {
115 $this->user_text
= $this->fixEncoding( $ip );
118 function setText( $text ) {
119 $this->text
= $this->fixEncoding( $text );
122 function setComment( $text ) {
123 $this->comment
= $this->fixEncoding( $text );
126 function fixEncoding( $data ) {
127 global $wgLang, $wgInputEncoding;
129 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
132 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
136 function getTitle() {
140 function getTimestamp() {
141 return $this->timestamp
;
145 return $this->user_text
;
152 function getComment() {
153 return $this->comment
;
160 var $mXmlError = XML_ERROR_NONE
;
161 var $mRevisionHandler = NULL;
164 function WikiImporter() {
165 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
168 function setError( $err ) {
169 $this->mError
= $err;
173 function getError() {
174 if( $this->mXmlError
== XML_ERROR_NONE
) {
175 return $this->mError
;
177 return xml_error_string( $this->mXmlError
);
181 function throwXmlError( $err ) {
182 $this->debug( "FAILURE: $err" );
185 function setupFromFile( $filename ) {
186 $this->mSource
= file_get_contents( $filename );
190 function setupFromUpload( $fieldname = "xmlimport" ) {
193 $upload =& $_FILES[$fieldname];
195 if( !isset( $upload ) ) {
196 return $this->setError( wfMsg( "importnofile" ) );
198 if( !empty( $upload['error'] ) ) {
199 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
201 $fname = $upload['tmp_name'];
202 if( is_uploaded_file( $fname ) ) {
203 return $this->setupFromFile( $fname );
205 return $this->setError( wfMsg( "importnofile" ) );
209 function setupFromURL( $url ) {
211 wfDebugDieBacktrace( "Not yet implemented." );
214 function doImport() {
215 if( empty( $this->mSource
) ) {
216 return $this->setError( wfMsg( "importnotext" ) );
219 $parser = xml_parser_create( "UTF-8" );
221 # case folding violates XML standard, turn it off
222 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
224 xml_set_object( $parser, &$this );
225 xml_set_element_handler( $parser, "in_start", "" );
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 );
233 xml_parser_free( $parser );
238 function debug( $data ) {
240 # $wgOut->addHTML( htmlspecialchars( $data ) . "<br />\n" );
243 function setRevisionHandler( $functionref ) {
244 $this->mRevisionHandler
= $functionref;
247 function defaultRevisionHandler( $revision ) {
248 $this->debug( "Got revision:" );
249 if( is_object( $revision->title
) ) {
250 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
252 $this->debug( "-- Title: <invalid>" );
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
);
262 # XML parser callbacks from here out -- beware!
263 function donothing( $parser, $x, $y="" ) {
264 #$this->debug( "donothing" );
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>" );
272 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
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>" );
280 xml_set_element_handler( $parser, "in_page", "out_page" );
282 function out_mediawiki( $parser, $name ) {
283 $this->debug( "out_mediawiki $name" );
284 if( $name != "mediawiki" ) {
285 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
287 xml_set_element_handler( $parser, "donothing", "donothing" );
290 function in_page( $parser, $name, $attribs ) {
291 $this->debug( "in_page $name" );
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" );
303 $this->workRevision
= new WikiRevision
;
304 $this->workRevision
->setTitle( $this->workTitle
);
305 xml_set_element_handler( $parser, "in_revision", "out_revision" );
308 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
312 function out_page( $parser, $name ) {
313 $this->debug( "out_page $name" );
314 if( $name != "page" ) {
315 return $this->throwXMLerror( "Expected </page>, got </$name>" );
317 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
319 $this->workTitle
= NULL;
320 $this->workRevision
= NULL;
323 function in_nothing( $parser, $name, $attribs ) {
324 $this->debug( "in_nothing $name" );
325 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
327 function char_append( $parser, $data ) {
328 $this->debug( "char_append '$data'" );
329 $this->appenddata
.= $data;
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>" );
336 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
337 xml_set_character_data_handler( $parser, "donothing" );
338 switch( $this->appendfield
) {
340 $this->workTitle
= $this->appenddata
;
343 $this->workRevision
->setText( $this->appenddata
);
346 $this->workRevision
->setUsername( $this->appenddata
);
349 $this->workRevision
->setUserIP( $this->appenddata
);
352 $this->workRevision
->setTimestamp( $this->appenddata
);
355 $this->workRevision
->setComment( $this->appenddata
);
358 $this->debug( "Bad append: {$this->appendfield}" );
360 $this->appendfield
= "";
361 $this->appenddata
= "";
364 function in_revision( $parser, $name, $attribs ) {
365 $this->debug( "in_revision $name" );
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" );
377 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
380 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
383 function out_revision( $parser, $name ) {
384 $this->debug( "out_revision $name" );
385 if( $name != "revision" ) {
386 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
388 xml_set_element_handler( $parser, "in_page", "out_page" );
390 call_user_func( $this->mRevisionHandler
, $this->workRevision
);
393 function in_contributor( $parser, $name, $attribs ) {
394 $this->debug( "in_contributor $name" );
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" );
404 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
408 function out_contributor( $parser, $name ) {
409 $this->debug( "out_contributor $name" );
410 if( $name != "contributor" ) {
411 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
413 xml_set_element_handler( $parser, "in_revision", "out_revision" );