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 ) {
98 $dbw =& wfGetDB( DB_MASTER
);
99 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
104 var $timestamp = "20010115000000";
110 function setTitle( $text ) {
111 $text = $this->fixEncoding( $text );
112 $this->title
= Title
::newFromText( $text );
115 function setTimestamp( $ts ) {
116 # 2003-08-05T18:30:02Z
117 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
120 function setUsername( $user ) {
121 $this->user_text
= $this->fixEncoding( $user );
124 function setUserIP( $ip ) {
125 $this->user_text
= $this->fixEncoding( $ip );
128 function setText( $text ) {
129 $this->text
= $this->fixEncoding( $text );
132 function setComment( $text ) {
133 $this->comment
= $this->fixEncoding( $text );
136 function fixEncoding( $data ) {
137 global $wgLang, $wgInputEncoding;
139 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
142 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
146 function getTitle() {
150 function getTimestamp() {
151 return $this->timestamp
;
155 return $this->user_text
;
162 function getComment() {
163 return $this->comment
;
170 var $mXmlError = XML_ERROR_NONE
;
171 var $mRevisionHandler = NULL;
174 function WikiImporter() {
175 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
178 function setError( $err ) {
179 $this->mError
= $err;
183 function getError() {
184 if( $this->mXmlError
== XML_ERROR_NONE
) {
185 return $this->mError
;
187 return xml_error_string( $this->mXmlError
);
191 function throwXmlError( $err ) {
192 $this->debug( "FAILURE: $err" );
195 function setupFromFile( $filename ) {
196 $this->mSource
= file_get_contents( $filename );
200 function setupFromUpload( $fieldname = "xmlimport" ) {
203 $upload =& $_FILES[$fieldname];
205 if( !isset( $upload ) ) {
206 return $this->setError( wfMsg( "importnofile" ) );
208 if( !empty( $upload['error'] ) ) {
209 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
211 $fname = $upload['tmp_name'];
212 if( is_uploaded_file( $fname ) ) {
213 return $this->setupFromFile( $fname );
215 return $this->setError( wfMsg( "importnofile" ) );
219 function setupFromURL( $url ) {
220 # fopen-wrappers are normally turned off for security.
221 ini_set( "allow_url_fopen", true );
222 $ret = $this->setupFromFile( $url );
223 ini_set( "allow_url_fopen", false );
227 function setupFromInterwiki( $interwiki, $page ) {
228 $base = Title
::getInterwikiLink( $interwiki );
229 if( empty( $base ) ) {
232 $import = wfUrlencode( "Special:Export/$page" );
233 $url = str_replace( "$1", $import, $base );
234 $this->notice( "Importing from $url" );
235 return $this->setupFromURL( $url );
241 function doImport() {
242 if( empty( $this->mSource
) ) {
243 return $this->setError( wfMsg( "importnotext" ) );
246 $parser = xml_parser_create( "UTF-8" );
248 # case folding violates XML standard, turn it off
249 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
251 xml_set_object( $parser, &$this );
252 xml_set_element_handler( $parser, "in_start", "" );
254 if( !xml_parse( $parser, $this->mSource
, true ) ) {
255 # return error message
256 $this->mXmlError
= xml_get_error_code( $parser );
257 xml_parser_free( $parser );
260 xml_parser_free( $parser );
265 function debug( $data ) {
267 # $this->notice( "DEBUG: $data\n" );
270 function notice( $data ) {
271 global $wgCommandLineMode;
272 if( $wgCommandLineMode ) {
276 $wgOut->addHTML( "<li>$data</li>\n" );
280 function setRevisionHandler( $functionref ) {
281 $this->mRevisionHandler
= $functionref;
284 function defaultRevisionHandler( &$revision ) {
285 $this->debug( "Got revision:" );
286 if( is_object( $revision->title
) ) {
287 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
289 $this->debug( "-- Title: <invalid>" );
291 $this->debug( "-- User: " . $revision->user_text
);
292 $this->debug( "-- Timestamp: " . $revision->timestamp
);
293 $this->debug( "-- Comment: " . $revision->comment
);
294 $this->debug( "-- Text: " . $revision->text
);
299 # XML parser callbacks from here out -- beware!
300 function donothing( $parser, $x, $y="" ) {
301 #$this->debug( "donothing" );
304 function in_start( $parser, $name, $attribs ) {
305 $this->debug( "in_start $name" );
306 if( $name != "mediawiki" ) {
307 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
309 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
312 function in_mediawiki( $parser, $name, $attribs ) {
313 $this->debug( "in_mediawiki $name" );
314 if( $name != "page" ) {
315 return $this->throwXMLerror( "Expected <page>, got <$name>" );
317 xml_set_element_handler( $parser, "in_page", "out_page" );
319 function out_mediawiki( $parser, $name ) {
320 $this->debug( "out_mediawiki $name" );
321 if( $name != "mediawiki" ) {
322 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
324 xml_set_element_handler( $parser, "donothing", "donothing" );
327 function in_page( $parser, $name, $attribs ) {
328 $this->debug( "in_page $name" );
333 $this->appendfield
= $name;
334 $this->appenddata
= "";
335 $this->parenttag
= "page";
336 xml_set_element_handler( $parser, "in_nothing", "out_append" );
337 xml_set_character_data_handler( $parser, "char_append" );
340 $this->workRevision
= new WikiRevision
;
341 $this->workRevision
->setTitle( $this->workTitle
);
342 xml_set_element_handler( $parser, "in_revision", "out_revision" );
345 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
349 function out_page( $parser, $name ) {
350 $this->debug( "out_page $name" );
351 if( $name != "page" ) {
352 return $this->throwXMLerror( "Expected </page>, got </$name>" );
354 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
356 $this->workTitle
= NULL;
357 $this->workRevision
= NULL;
360 function in_nothing( $parser, $name, $attribs ) {
361 $this->debug( "in_nothing $name" );
362 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
364 function char_append( $parser, $data ) {
365 $this->debug( "char_append '$data'" );
366 $this->appenddata
.= $data;
368 function out_append( $parser, $name ) {
369 $this->debug( "out_append $name" );
370 if( $name != $this->appendfield
) {
371 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
373 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
374 xml_set_character_data_handler( $parser, "donothing" );
375 switch( $this->appendfield
) {
377 $this->workTitle
= $this->appenddata
;
380 $this->workRevision
->setText( $this->appenddata
);
383 $this->workRevision
->setUsername( $this->appenddata
);
386 $this->workRevision
->setUserIP( $this->appenddata
);
389 $this->workRevision
->setTimestamp( $this->appenddata
);
392 $this->workRevision
->setComment( $this->appenddata
);
395 $this->debug( "Bad append: {$this->appendfield}" );
397 $this->appendfield
= "";
398 $this->appenddata
= "";
401 function in_revision( $parser, $name, $attribs ) {
402 $this->debug( "in_revision $name" );
408 $this->parenttag
= "revision";
409 $this->appendfield
= $name;
410 xml_set_element_handler( $parser, "in_nothing", "out_append" );
411 xml_set_character_data_handler( $parser, "char_append" );
414 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
417 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
421 function out_revision( $parser, $name ) {
422 $this->debug( "out_revision $name" );
423 if( $name != "revision" ) {
424 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
426 xml_set_element_handler( $parser, "in_page", "out_page" );
428 $out = call_user_func( $this->mRevisionHandler
, &$this->workRevision
, &$this );
429 if( !empty( $out ) ) {
431 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
435 function in_contributor( $parser, $name, $attribs ) {
436 $this->debug( "in_contributor $name" );
440 $this->parenttag
= "contributor";
441 $this->appendfield
= $name;
442 xml_set_element_handler( $parser, "in_nothing", "out_append" );
443 xml_set_character_data_handler( $parser, "char_append" );
446 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
450 function out_contributor( $parser, $name ) {
451 $this->debug( "out_contributor $name" );
452 if( $name != "contributor" ) {
453 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
455 xml_set_element_handler( $parser, "in_revision", "out_revision" );
458 function importOldRevision() {
459 $fname = "WikiImporter::importOldRevision";
460 $dbw =& wfGetDB( DB_MASTER
);
462 # Sneak a single revision into place
463 $user = User
::newFromName( $this->getUser() );
465 $res = $dbw->select( 'old', 1,
466 $this->title
->oldCond() +
array( 'old_timestamp' => $this->timestamp
),
470 $numrows = $dbw->numRows( $res );
471 $dbw->freeResult( $res );
473 return wfMsg( "importhistoryconflict" );
477 $oldIgnore = $dbw->ignoreErrors( true );
478 $success = $dbw->insert( 'old',
480 'old_namespace' => intval( $this->title
->getNamespace() ),
481 'old_title' => $this->title
->getDBkey(),
482 'old_text' => $this->getText(),
483 'old_comment' => $this->getComment(),
484 'old_user' => intval( $user->getId() ),
485 'old_user_text' => $user->getName(),
486 'old_timestamp' => $this->timestamp
,
487 'inverse_timestamp' => wfInvertTimestamp( $this->timestamp
),
488 'old_minor_edit' => 0,
493 return wfMsg( "ok" );