c3cef11e2ffb6e89c9050cc7ed62e19f7e253147
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
27 function wfSpecialImport( $page = '' ) {
28 global $wgOut, $wgLang, $wgRequest, $wgTitle;
29 global $wgImportSources;
32 $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
36 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
37 $importer = new WikiImporter();
39 switch( $wgRequest->getVal( "source" ) ) {
41 $ok = $importer->setupFromUpload( "xmlimport" );
44 $ok = $importer->setupFromInterwiki(
45 $wgRequest->getVal( "interwiki" ),
46 $wgRequest->getText( "frompage" ) );
53 $importer->setRevisionHandler( "wfImportOldRevision" );
54 if( $importer->doImport() ) {
56 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
58 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
59 htmlspecialchars( $importer->getError() ) ) . "</p>" );
62 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
66 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
67 $action = $wgTitle->escapeLocalUrl();
70 <legend>Upload XML</legend>
71 <form enctype='multipart/form-data' method='post' action=\"$action\">
72 <input type='hidden' name='action' value='submit' />
73 <input type='hidden' name='source' value='upload' />
74 <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
75 <input type='file' name='xmlimport' value='' size='30' />
76 <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
81 if( !empty( $wgImportSources ) ) {
84 <legend>Interwiki import</legend>
85 <form method='post' action=\"$action\">
86 <input type='hidden' name='action' value='submit' />
87 <input type='hidden' name='source' value='interwiki' />
88 <select name='interwiki'>
90 foreach( $wgImportSources as $interwiki ) {
91 $iw = htmlspecialchars( $interwiki );
92 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
96 <input name='frompage' />
97 <input type='submit' />
104 function wfImportOldRevision( &$revision ) {
105 $dbw =& wfGetDB( DB_MASTER
);
106 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
114 var $timestamp = "20010115000000";
120 function setTitle( $text ) {
121 $text = $this->fixEncoding( $text );
122 $this->title
= Title
::newFromText( $text );
125 function setTimestamp( $ts ) {
126 # 2003-08-05T18:30:02Z
127 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
130 function setUsername( $user ) {
131 $this->user_text
= $this->fixEncoding( $user );
134 function setUserIP( $ip ) {
135 $this->user_text
= $this->fixEncoding( $ip );
138 function setText( $text ) {
139 $this->text
= $this->fixEncoding( $text );
142 function setComment( $text ) {
143 $this->comment
= $this->fixEncoding( $text );
146 function fixEncoding( $data ) {
147 global $wgLang, $wgInputEncoding;
149 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
152 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
156 function getTitle() {
160 function getTimestamp() {
161 return $this->timestamp
;
165 return $this->user_text
;
172 function getComment() {
173 return $this->comment
;
183 var $mXmlError = XML_ERROR_NONE
;
184 var $mRevisionHandler = NULL;
187 function WikiImporter() {
188 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
191 function setError( $err ) {
192 $this->mError
= $err;
196 function getError() {
197 if( $this->mXmlError
== XML_ERROR_NONE
) {
198 return $this->mError
;
200 return xml_error_string( $this->mXmlError
);
204 function throwXmlError( $err ) {
205 $this->debug( "FAILURE: $err" );
208 function setupFromFile( $filename ) {
209 $this->mSource
= file_get_contents( $filename );
213 function setupFromUpload( $fieldname = "xmlimport" ) {
216 $upload =& $_FILES[$fieldname];
218 if( !isset( $upload ) ) {
219 return $this->setError( wfMsg( "importnofile" ) );
221 if( !empty( $upload['error'] ) ) {
222 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
224 $fname = $upload['tmp_name'];
225 if( is_uploaded_file( $fname ) ) {
226 return $this->setupFromFile( $fname );
228 return $this->setError( wfMsg( "importnofile" ) );
232 function setupFromURL( $url ) {
233 # fopen-wrappers are normally turned off for security.
234 ini_set( "allow_url_fopen", true );
235 $ret = $this->setupFromFile( $url );
236 ini_set( "allow_url_fopen", false );
240 function setupFromInterwiki( $interwiki, $page ) {
241 $base = Title
::getInterwikiLink( $interwiki );
242 if( empty( $base ) ) {
245 $import = wfUrlencode( "Special:Export/$page" );
246 $url = str_replace( "$1", $import, $base );
247 $this->notice( "Importing from $url" );
248 return $this->setupFromURL( $url );
254 function doImport() {
255 if( empty( $this->mSource
) ) {
256 return $this->setError( wfMsg( "importnotext" ) );
259 $parser = xml_parser_create( "UTF-8" );
261 # case folding violates XML standard, turn it off
262 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
264 xml_set_object( $parser, &$this );
265 xml_set_element_handler( $parser, "in_start", "" );
267 if( !xml_parse( $parser, $this->mSource
, true ) ) {
268 # return error message
269 $this->mXmlError
= xml_get_error_code( $parser );
270 xml_parser_free( $parser );
273 xml_parser_free( $parser );
278 function debug( $data ) {
280 # $this->notice( "DEBUG: $data\n" );
283 function notice( $data ) {
284 global $wgCommandLineMode;
285 if( $wgCommandLineMode ) {
289 $wgOut->addHTML( "<li>$data</li>\n" );
293 function setRevisionHandler( $functionref ) {
294 $this->mRevisionHandler
= $functionref;
297 function defaultRevisionHandler( &$revision ) {
298 $this->debug( "Got revision:" );
299 if( is_object( $revision->title
) ) {
300 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
302 $this->debug( "-- Title: <invalid>" );
304 $this->debug( "-- User: " . $revision->user_text
);
305 $this->debug( "-- Timestamp: " . $revision->timestamp
);
306 $this->debug( "-- Comment: " . $revision->comment
);
307 $this->debug( "-- Text: " . $revision->text
);
312 # XML parser callbacks from here out -- beware!
313 function donothing( $parser, $x, $y="" ) {
314 #$this->debug( "donothing" );
317 function in_start( $parser, $name, $attribs ) {
318 $this->debug( "in_start $name" );
319 if( $name != "mediawiki" ) {
320 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
322 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
325 function in_mediawiki( $parser, $name, $attribs ) {
326 $this->debug( "in_mediawiki $name" );
327 if( $name != "page" ) {
328 return $this->throwXMLerror( "Expected <page>, got <$name>" );
330 xml_set_element_handler( $parser, "in_page", "out_page" );
332 function out_mediawiki( $parser, $name ) {
333 $this->debug( "out_mediawiki $name" );
334 if( $name != "mediawiki" ) {
335 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
337 xml_set_element_handler( $parser, "donothing", "donothing" );
340 function in_page( $parser, $name, $attribs ) {
341 $this->debug( "in_page $name" );
346 $this->appendfield
= $name;
347 $this->appenddata
= "";
348 $this->parenttag
= "page";
349 xml_set_element_handler( $parser, "in_nothing", "out_append" );
350 xml_set_character_data_handler( $parser, "char_append" );
353 $this->workRevision
= new WikiRevision
;
354 $this->workRevision
->setTitle( $this->workTitle
);
355 xml_set_element_handler( $parser, "in_revision", "out_revision" );
358 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
362 function out_page( $parser, $name ) {
363 $this->debug( "out_page $name" );
364 if( $name != "page" ) {
365 return $this->throwXMLerror( "Expected </page>, got </$name>" );
367 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
369 $this->workTitle
= NULL;
370 $this->workRevision
= NULL;
373 function in_nothing( $parser, $name, $attribs ) {
374 $this->debug( "in_nothing $name" );
375 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
377 function char_append( $parser, $data ) {
378 $this->debug( "char_append '$data'" );
379 $this->appenddata
.= $data;
381 function out_append( $parser, $name ) {
382 $this->debug( "out_append $name" );
383 if( $name != $this->appendfield
) {
384 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
386 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
387 xml_set_character_data_handler( $parser, "donothing" );
388 switch( $this->appendfield
) {
390 $this->workTitle
= $this->appenddata
;
393 $this->workRevision
->setText( $this->appenddata
);
396 $this->workRevision
->setUsername( $this->appenddata
);
399 $this->workRevision
->setUserIP( $this->appenddata
);
402 $this->workRevision
->setTimestamp( $this->appenddata
);
405 $this->workRevision
->setComment( $this->appenddata
);
408 $this->debug( "Bad append: {$this->appendfield}" );
410 $this->appendfield
= "";
411 $this->appenddata
= "";
414 function in_revision( $parser, $name, $attribs ) {
415 $this->debug( "in_revision $name" );
421 $this->parenttag
= "revision";
422 $this->appendfield
= $name;
423 xml_set_element_handler( $parser, "in_nothing", "out_append" );
424 xml_set_character_data_handler( $parser, "char_append" );
427 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
430 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
434 function out_revision( $parser, $name ) {
435 $this->debug( "out_revision $name" );
436 if( $name != "revision" ) {
437 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
439 xml_set_element_handler( $parser, "in_page", "out_page" );
441 $out = call_user_func( $this->mRevisionHandler
, &$this->workRevision
, &$this );
442 if( !empty( $out ) ) {
444 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
448 function in_contributor( $parser, $name, $attribs ) {
449 $this->debug( "in_contributor $name" );
453 $this->parenttag
= "contributor";
454 $this->appendfield
= $name;
455 xml_set_element_handler( $parser, "in_nothing", "out_append" );
456 xml_set_character_data_handler( $parser, "char_append" );
459 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
463 function out_contributor( $parser, $name ) {
464 $this->debug( "out_contributor $name" );
465 if( $name != "contributor" ) {
466 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
468 xml_set_element_handler( $parser, "in_revision", "out_revision" );
471 function importOldRevision() {
472 $fname = "WikiImporter::importOldRevision";
473 $dbw =& wfGetDB( DB_MASTER
);
475 # Sneak a single revision into place
476 $user = User
::newFromName( $this->getUser() );
478 $res = $dbw->select( 'old', 1,
479 $this->title
->oldCond() +
array( 'old_timestamp' => $this->timestamp
),
483 $numrows = $dbw->numRows( $res );
484 $dbw->freeResult( $res );
486 return wfMsg( "importhistoryconflict" );
490 $oldIgnore = $dbw->ignoreErrors( true );
491 $success = $dbw->insert( 'old',
493 'old_namespace' => intval( $this->title
->getNamespace() ),
494 'old_title' => $this->title
->getDBkey(),
495 'old_text' => $this->getText(),
496 'old_comment' => $this->getComment(),
497 'old_user' => intval( $user->getId() ),
498 'old_user_text' => $user->getName(),
499 'old_timestamp' => $this->timestamp
,
500 'inverse_timestamp' => wfInvertTimestamp( $this->timestamp
),
501 'old_minor_edit' => 0,
506 return wfMsg( "ok" );