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;
22 global $wgImportSources;
25 $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
29 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
30 $importer = new WikiImporter();
32 switch( $wgRequest->getVal( "source" ) ) {
34 $ok = $importer->setupFromUpload( "xmlimport" );
37 $ok = $importer->setupFromInterwiki(
38 $wgRequest->getVal( "interwiki" ),
39 $wgRequest->getText( "frompage" ) );
46 $importer->setRevisionHandler( "wfImportOldRevision" );
47 if( $importer->doImport() ) {
49 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
51 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
52 htmlspecialchars( $importer->getError() ) ) . "</p>" );
55 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
59 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
60 $action = $wgTitle->escapeLocalUrl();
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" ) ) . "'/>
74 if( !empty( $wgImportSources ) ) {
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'>
83 foreach( $wgImportSources as $interwiki ) {
84 $iw = htmlspecialchars( $interwiki );
85 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
89 <input name='frompage' />
90 <input type='submit' />
97 function wfImportOldRevision( &$revision ) {
99 $fname = "wfImportOldRevision";
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() );
109 $user = User
::newFromName( $revision->getUser() );
110 $user_id = IntVal( $user->getId() );
111 $user_text = wfStrencode( $user->getName() );
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 );
122 return wfMsg( "importhistoryconflict" );
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')",
131 return wfMsg( "ok" );
136 var $timestamp = "20010115000000";
142 function setTitle( $text ) {
143 $text = $this->fixEncoding( $text );
144 $this->title
= Title
::newFromText( $text );
147 function setTimestamp( $ts ) {
148 # 2003-08-05T18:30:02Z
149 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
152 function setUsername( $user ) {
153 $this->user_text
= $this->fixEncoding( $user );
156 function setUserIP( $ip ) {
157 $this->user_text
= $this->fixEncoding( $ip );
160 function setText( $text ) {
161 $this->text
= $this->fixEncoding( $text );
164 function setComment( $text ) {
165 $this->comment
= $this->fixEncoding( $text );
168 function fixEncoding( $data ) {
169 global $wgLang, $wgInputEncoding;
171 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
174 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
178 function getTitle() {
182 function getTimestamp() {
183 return $this->timestamp
;
187 return $this->user_text
;
194 function getComment() {
195 return $this->comment
;
202 var $mXmlError = XML_ERROR_NONE
;
203 var $mRevisionHandler = NULL;
206 function WikiImporter() {
207 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
210 function setError( $err ) {
211 $this->mError
= $err;
215 function getError() {
216 if( $this->mXmlError
== XML_ERROR_NONE
) {
217 return $this->mError
;
219 return xml_error_string( $this->mXmlError
);
223 function throwXmlError( $err ) {
224 $this->debug( "FAILURE: $err" );
227 function setupFromFile( $filename ) {
228 $this->mSource
= file_get_contents( $filename );
232 function setupFromUpload( $fieldname = "xmlimport" ) {
235 $upload =& $_FILES[$fieldname];
237 if( !isset( $upload ) ) {
238 return $this->setError( wfMsg( "importnofile" ) );
240 if( !empty( $upload['error'] ) ) {
241 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
243 $fname = $upload['tmp_name'];
244 if( is_uploaded_file( $fname ) ) {
245 return $this->setupFromFile( $fname );
247 return $this->setError( wfMsg( "importnofile" ) );
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 );
259 function setupFromInterwiki( $interwiki, $page ) {
260 $base = Title
::getInterwikiLink( $interwiki );
261 if( empty( $base ) ) {
264 $import = wfUrlencode( "Special:Export/$page" );
265 $url = str_replace( "$1", $import, $base );
266 $this->notice( "Importing from $url" );
267 return $this->setupFromURL( $url );
273 function doImport() {
274 if( empty( $this->mSource
) ) {
275 return $this->setError( wfMsg( "importnotext" ) );
278 $parser = xml_parser_create( "UTF-8" );
280 # case folding violates XML standard, turn it off
281 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
283 xml_set_object( $parser, &$this );
284 xml_set_element_handler( $parser, "in_start", "" );
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 );
292 xml_parser_free( $parser );
297 function debug( $data ) {
299 # $this->notice( "DEBUG: $data\n" );
302 function notice( $data ) {
303 global $wgCommandLineMode;
304 if( $wgCommandLineMode ) {
308 $wgOut->addHTML( "<li>$data</li>\n" );
312 function setRevisionHandler( $functionref ) {
313 $this->mRevisionHandler
= $functionref;
316 function defaultRevisionHandler( &$revision ) {
317 $this->debug( "Got revision:" );
318 if( is_object( $revision->title
) ) {
319 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
321 $this->debug( "-- Title: <invalid>" );
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
);
331 # XML parser callbacks from here out -- beware!
332 function donothing( $parser, $x, $y="" ) {
333 #$this->debug( "donothing" );
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>" );
341 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
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>" );
349 xml_set_element_handler( $parser, "in_page", "out_page" );
351 function out_mediawiki( $parser, $name ) {
352 $this->debug( "out_mediawiki $name" );
353 if( $name != "mediawiki" ) {
354 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
356 xml_set_element_handler( $parser, "donothing", "donothing" );
359 function in_page( $parser, $name, $attribs ) {
360 $this->debug( "in_page $name" );
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" );
372 $this->workRevision
= new WikiRevision
;
373 $this->workRevision
->setTitle( $this->workTitle
);
374 xml_set_element_handler( $parser, "in_revision", "out_revision" );
377 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
381 function out_page( $parser, $name ) {
382 $this->debug( "out_page $name" );
383 if( $name != "page" ) {
384 return $this->throwXMLerror( "Expected </page>, got </$name>" );
386 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
388 $this->workTitle
= NULL;
389 $this->workRevision
= NULL;
392 function in_nothing( $parser, $name, $attribs ) {
393 $this->debug( "in_nothing $name" );
394 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
396 function char_append( $parser, $data ) {
397 $this->debug( "char_append '$data'" );
398 $this->appenddata
.= $data;
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>" );
405 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
406 xml_set_character_data_handler( $parser, "donothing" );
407 switch( $this->appendfield
) {
409 $this->workTitle
= $this->appenddata
;
412 $this->workRevision
->setText( $this->appenddata
);
415 $this->workRevision
->setUsername( $this->appenddata
);
418 $this->workRevision
->setUserIP( $this->appenddata
);
421 $this->workRevision
->setTimestamp( $this->appenddata
);
424 $this->workRevision
->setComment( $this->appenddata
);
427 $this->debug( "Bad append: {$this->appendfield}" );
429 $this->appendfield
= "";
430 $this->appenddata
= "";
433 function in_revision( $parser, $name, $attribs ) {
434 $this->debug( "in_revision $name" );
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" );
446 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
449 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
453 function out_revision( $parser, $name ) {
454 $this->debug( "out_revision $name" );
455 if( $name != "revision" ) {
456 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
458 xml_set_element_handler( $parser, "in_page", "out_page" );
460 $out = call_user_func( $this->mRevisionHandler
, &$this->workRevision
, &$this );
461 if( !empty( $out ) ) {
463 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
467 function in_contributor( $parser, $name, $attribs ) {
468 $this->debug( "in_contributor $name" );
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" );
478 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
482 function out_contributor( $parser, $name ) {
483 $this->debug( "out_contributor $name" );
484 if( $name != "contributor" ) {
485 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
487 xml_set_element_handler( $parser, "in_revision", "out_revision" );