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
23 * @subpackage SpecialPage
29 function wfSpecialImport( $page = '' ) {
30 global $wgOut, $wgLang, $wgRequest, $wgTitle;
31 global $wgImportSources;
34 $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
38 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
39 $importer = new WikiImporter();
41 switch( $wgRequest->getVal( "source" ) ) {
43 $ok = $importer->setupFromUpload( "xmlimport" );
46 $ok = $importer->setupFromInterwiki(
47 $wgRequest->getVal( "interwiki" ),
48 $wgRequest->getText( "frompage" ) );
55 $importer->setRevisionHandler( "wfImportOldRevision" );
56 if( $importer->doImport() ) {
58 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
60 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
61 htmlspecialchars( $importer->getError() ) ) . "</p>" );
64 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
68 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
69 $action = $wgTitle->escapeLocalUrl();
72 <legend>Upload XML</legend>
73 <form enctype='multipart/form-data' method='post' action=\"$action\">
74 <input type='hidden' name='action' value='submit' />
75 <input type='hidden' name='source' value='upload' />
76 <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
77 <input type='file' name='xmlimport' value='' size='30' />
78 <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
83 if( !empty( $wgImportSources ) ) {
86 <legend>Interwiki import</legend>
87 <form method='post' action=\"$action\">
88 <input type='hidden' name='action' value='submit' />
89 <input type='hidden' name='source' value='interwiki' />
90 <select name='interwiki'>
92 foreach( $wgImportSources as $interwiki ) {
93 $iw = htmlspecialchars( $interwiki );
94 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
98 <input name='frompage' />
99 <input type='submit' />
106 function wfImportOldRevision( &$revision ) {
107 $dbw =& wfGetDB( DB_MASTER
);
108 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
114 * @subpackage SpecialPage
118 var $timestamp = "20010115000000";
124 function setTitle( $text ) {
125 $text = $this->fixEncoding( $text );
126 $this->title
= Title
::newFromText( $text );
129 function setTimestamp( $ts ) {
130 # 2003-08-05T18:30:02Z
131 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
134 function setUsername( $user ) {
135 $this->user_text
= $this->fixEncoding( $user );
138 function setUserIP( $ip ) {
139 $this->user_text
= $this->fixEncoding( $ip );
142 function setText( $text ) {
143 $this->text
= $this->fixEncoding( $text );
146 function setComment( $text ) {
147 $this->comment
= $this->fixEncoding( $text );
150 function fixEncoding( $data ) {
151 global $wgLang, $wgInputEncoding;
153 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
156 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
160 function getTitle() {
164 function getTimestamp() {
165 return $this->timestamp
;
169 return $this->user_text
;
176 function getComment() {
177 return $this->comment
;
184 * @subpackage SpecialPage
189 var $mXmlError = XML_ERROR_NONE
;
190 var $mRevisionHandler = NULL;
193 function WikiImporter() {
194 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
197 function setError( $err ) {
198 $this->mError
= $err;
202 function getError() {
203 if( $this->mXmlError
== XML_ERROR_NONE
) {
204 return $this->mError
;
206 return xml_error_string( $this->mXmlError
);
210 function throwXmlError( $err ) {
211 $this->debug( "FAILURE: $err" );
214 function setupFromFile( $filename ) {
215 $this->mSource
= file_get_contents( $filename );
219 function setupFromUpload( $fieldname = "xmlimport" ) {
222 $upload =& $_FILES[$fieldname];
224 if( !isset( $upload ) ) {
225 return $this->setError( wfMsg( "importnofile" ) );
227 if( !empty( $upload['error'] ) ) {
228 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
230 $fname = $upload['tmp_name'];
231 if( is_uploaded_file( $fname ) ) {
232 return $this->setupFromFile( $fname );
234 return $this->setError( wfMsg( "importnofile" ) );
238 function setupFromURL( $url ) {
239 # fopen-wrappers are normally turned off for security.
240 ini_set( "allow_url_fopen", true );
241 $ret = $this->setupFromFile( $url );
242 ini_set( "allow_url_fopen", false );
246 function setupFromInterwiki( $interwiki, $page ) {
247 $base = Title
::getInterwikiLink( $interwiki );
248 if( empty( $base ) ) {
251 $import = wfUrlencode( "Special:Export/$page" );
252 $url = str_replace( "$1", $import, $base );
253 $this->notice( "Importing from $url" );
254 return $this->setupFromURL( $url );
260 function doImport() {
261 if( empty( $this->mSource
) ) {
262 return $this->setError( wfMsg( "importnotext" ) );
265 $parser = xml_parser_create( "UTF-8" );
267 # case folding violates XML standard, turn it off
268 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
270 xml_set_object( $parser, &$this );
271 xml_set_element_handler( $parser, "in_start", "" );
273 if( !xml_parse( $parser, $this->mSource
, true ) ) {
274 # return error message
275 $this->mXmlError
= xml_get_error_code( $parser );
276 xml_parser_free( $parser );
279 xml_parser_free( $parser );
284 function debug( $data ) {
286 # $this->notice( "DEBUG: $data\n" );
289 function notice( $data ) {
290 global $wgCommandLineMode;
291 if( $wgCommandLineMode ) {
295 $wgOut->addHTML( "<li>$data</li>\n" );
299 function setRevisionHandler( $functionref ) {
300 $this->mRevisionHandler
= $functionref;
303 function defaultRevisionHandler( &$revision ) {
304 $this->debug( "Got revision:" );
305 if( is_object( $revision->title
) ) {
306 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
308 $this->debug( "-- Title: <invalid>" );
310 $this->debug( "-- User: " . $revision->user_text
);
311 $this->debug( "-- Timestamp: " . $revision->timestamp
);
312 $this->debug( "-- Comment: " . $revision->comment
);
313 $this->debug( "-- Text: " . $revision->text
);
318 # XML parser callbacks from here out -- beware!
319 function donothing( $parser, $x, $y="" ) {
320 #$this->debug( "donothing" );
323 function in_start( $parser, $name, $attribs ) {
324 $this->debug( "in_start $name" );
325 if( $name != "mediawiki" ) {
326 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
328 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
331 function in_mediawiki( $parser, $name, $attribs ) {
332 $this->debug( "in_mediawiki $name" );
333 if( $name != "page" ) {
334 return $this->throwXMLerror( "Expected <page>, got <$name>" );
336 xml_set_element_handler( $parser, "in_page", "out_page" );
338 function out_mediawiki( $parser, $name ) {
339 $this->debug( "out_mediawiki $name" );
340 if( $name != "mediawiki" ) {
341 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
343 xml_set_element_handler( $parser, "donothing", "donothing" );
346 function in_page( $parser, $name, $attribs ) {
347 $this->debug( "in_page $name" );
352 $this->appendfield
= $name;
353 $this->appenddata
= "";
354 $this->parenttag
= "page";
355 xml_set_element_handler( $parser, "in_nothing", "out_append" );
356 xml_set_character_data_handler( $parser, "char_append" );
359 $this->workRevision
= new WikiRevision
;
360 $this->workRevision
->setTitle( $this->workTitle
);
361 xml_set_element_handler( $parser, "in_revision", "out_revision" );
364 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
368 function out_page( $parser, $name ) {
369 $this->debug( "out_page $name" );
370 if( $name != "page" ) {
371 return $this->throwXMLerror( "Expected </page>, got </$name>" );
373 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
375 $this->workTitle
= NULL;
376 $this->workRevision
= NULL;
379 function in_nothing( $parser, $name, $attribs ) {
380 $this->debug( "in_nothing $name" );
381 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
383 function char_append( $parser, $data ) {
384 $this->debug( "char_append '$data'" );
385 $this->appenddata
.= $data;
387 function out_append( $parser, $name ) {
388 $this->debug( "out_append $name" );
389 if( $name != $this->appendfield
) {
390 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
392 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
393 xml_set_character_data_handler( $parser, "donothing" );
394 switch( $this->appendfield
) {
396 $this->workTitle
= $this->appenddata
;
399 $this->workRevision
->setText( $this->appenddata
);
402 $this->workRevision
->setUsername( $this->appenddata
);
405 $this->workRevision
->setUserIP( $this->appenddata
);
408 $this->workRevision
->setTimestamp( $this->appenddata
);
411 $this->workRevision
->setComment( $this->appenddata
);
414 $this->debug( "Bad append: {$this->appendfield}" );
416 $this->appendfield
= "";
417 $this->appenddata
= "";
420 function in_revision( $parser, $name, $attribs ) {
421 $this->debug( "in_revision $name" );
427 $this->parenttag
= "revision";
428 $this->appendfield
= $name;
429 xml_set_element_handler( $parser, "in_nothing", "out_append" );
430 xml_set_character_data_handler( $parser, "char_append" );
433 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
436 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
440 function out_revision( $parser, $name ) {
441 $this->debug( "out_revision $name" );
442 if( $name != "revision" ) {
443 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
445 xml_set_element_handler( $parser, "in_page", "out_page" );
447 $out = call_user_func( $this->mRevisionHandler
, &$this->workRevision
, &$this );
448 if( !empty( $out ) ) {
450 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
454 function in_contributor( $parser, $name, $attribs ) {
455 $this->debug( "in_contributor $name" );
459 $this->parenttag
= "contributor";
460 $this->appendfield
= $name;
461 xml_set_element_handler( $parser, "in_nothing", "out_append" );
462 xml_set_character_data_handler( $parser, "char_append" );
465 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
469 function out_contributor( $parser, $name ) {
470 $this->debug( "out_contributor $name" );
471 if( $name != "contributor" ) {
472 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
474 xml_set_element_handler( $parser, "in_revision", "out_revision" );
477 function importOldRevision() {
478 $fname = "WikiImporter::importOldRevision";
479 $dbw =& wfGetDB( DB_MASTER
);
481 # Sneak a single revision into place
482 $user = User
::newFromName( $this->getUser() );
484 $res = $dbw->select( 'old', 1,
485 $this->title
->oldCond() +
array( 'old_timestamp' => $this->timestamp
),
489 $numrows = $dbw->numRows( $res );
490 $dbw->freeResult( $res );
492 return wfMsg( "importhistoryconflict" );
496 $oldIgnore = $dbw->ignoreErrors( true );
497 $success = $dbw->insert( 'old',
499 'old_namespace' => intval( $this->title
->getNamespace() ),
500 'old_title' => $this->title
->getDBkey(),
501 'old_text' => $this->getText(),
502 'old_comment' => $this->getComment(),
503 'old_user' => intval( $user->getId() ),
504 'old_user_text' => $user->getName(),
505 'old_timestamp' => $this->timestamp
,
506 'inverse_timestamp' => wfInvertTimestamp( $this->timestamp
),
507 'old_minor_edit' => 0,
512 return wfMsg( "ok" );