Scripts and data used for generating ZhConversion.php
[lhc/web/wiklou.git] / includes / SpecialImport.php
index be4208a..49b7204 100644 (file)
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
-function wfSpecialImport( $page = "" ) {
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+
+/**
+ * Constructor
+ */
+function wfSpecialImport( $page = '' ) {
        global $wgOut, $wgLang, $wgRequest, $wgTitle;
+       global $wgImportSources;
+       
+       ###
+       $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
+       return;
+       ###
        
        if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
-               $importer = new WikiImporter;
-               if( $importer->setupFromUpload( "xmlimport" ) ) {
-                       $importer->setRevisionHandler( "wfImportRevision" );
+               $importer = new WikiImporter();
+               
+               switch( $wgRequest->getVal( "source" ) ) {
+               case "upload":
+                       $ok = $importer->setupFromUpload( "xmlimport" );
+                       break;
+               case "interwiki":
+                       $ok = $importer->setupFromInterwiki(
+                               $wgRequest->getVal( "interwiki" ),
+                               $wgRequest->getText( "frompage" ) );
+                       break;
+               default:
+                       $ok = false;
+               }
+               
+               if( $ok ) {
+                       $importer->setRevisionHandler( "wfImportOldRevision" );
                        if( $importer->doImport() ) {
                                # Success!
                                $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
@@ -39,50 +68,58 @@ function wfSpecialImport( $page = "" ) {
        $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
        $action = $wgTitle->escapeLocalUrl();
        $wgOut->addHTML( "
-<form enctype='multipart/form-data' method='post' action=\"$action\">
-       <input type='hidden' name='action' value='submit' />
-       <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
-       <input type='file' name='xmlimport' value='' size='40' /><br />
-       <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
-</form>
+<fieldset>
+       <legend>Upload XML</legend>
+       <form enctype='multipart/form-data' method='post' action=\"$action\">
+               <input type='hidden' name='action' value='submit' />
+               <input type='hidden' name='source' value='upload' />
+               <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
+               <input type='file' name='xmlimport' value='' size='30' />
+               <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
+       </form>
+</fieldset>
+" );
+
+       if( !empty( $wgImportSources ) ) {
+               $wgOut->addHTML( "
+<fieldset>
+       <legend>Interwiki import</legend>
+       <form method='post' action=\"$action\">
+               <input type='hidden' name='action' value='submit' />
+               <input type='hidden' name='source' value='interwiki' />
+               <select name='interwiki'>
 " );
+               foreach( $wgImportSources as $interwiki ) {
+                       $iw = htmlspecialchars( $interwiki );
+                       $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
+               }
+               $wgOut->addHTML( "
+               </select>
+               <input name='frompage' />
+               <input type='submit' />
+       </form>
+</fieldset>
+" );
+       }
 }
 
-function wfImportRevision( $revision ) {
-       $fname = "wfImportRevision";
-       
-       # Sneak a single revision into place
-       $ns = $revision->title->getNamespace();
-       $t = wfStrencode( $revision->title->getDBkey() );
-       $text = wfStrencode( $revision->text );
-       $ts = wfStrencode( $revision->timestamp );
-       $its = wfStrencode( wfInvertTimestamp( $revision->timestamp ) ) ;
-       $minor = 0; # ??
-       $flags = "";
-       
-       # Make sure it doesn't already exist
-       $res = wfQuery( "SELECT COUNT(*) FROM old WHERE old_namespace=$ns AND old_title='$t' AND old_timestamp='$ts'", DB_WRITE, $fname );
-       $numrows = wfNumRows( $res );
-       wfFreeResult( $res );
-       if( $numrows > 0 ) {
-               return "gaaah";
-       }
-       
-       $res = wfQuery( "INSERT INTO old " .
-         "(old_namespace,old_title,old_text,old_comment,old_user,old_user_text," .
-         "old_timestamp,inverse_timestamp,old_minor_edit,old_flags) " .
-         "VALUES ($ns,'$t','$text','$comment',$user,'$user_text','$ts','$its',$minor,'$flags')",
-         DB_WRITE, $fname );
-       
-       return true;
+function wfImportOldRevision( &$revision ) {
+       $dbw =& wfGetDB( DB_MASTER );
+       $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
 }
 
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
 class WikiRevision {
        var $title = NULL;
        var $timestamp = "20010115000000";
        var $user = 0;
        var $user_text = "";
        var $text = "";
+       var $comment = "";
        
        function setTitle( $text ) {
                $text = $this->fixEncoding( $text );
@@ -106,13 +143,17 @@ class WikiRevision {
                $this->text = $this->fixEncoding( $text );
        }
        
+       function setComment( $text ) {
+               $this->comment = $this->fixEncoding( $text );
+       }
+       
        function fixEncoding( $data ) {
-               global $wgLang, $wgInputEncoding;
+               global $wgContLang, $wgInputEncoding;
                
                if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
                        return $data;
                } else {
-                       return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
+                       return $wgContLang->iconv( "utf-8", $wgInputEncoding, $data );
                }
        }
        
@@ -131,8 +172,17 @@ class WikiRevision {
        function getText() {
                return $this->text;
        }
+       
+       function getComment() {
+               return $this->comment;
+       }
 }
 
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
 class WikiImporter {
        var $mSource = NULL;
        var $mError = "";
@@ -186,10 +236,27 @@ class WikiImporter {
        }
        
        function setupFromURL( $url ) {
-               # FIXME
-               wfDebugDieBacktrace( "Not yet implemented." );
+               # fopen-wrappers are normally turned off for security.
+               ini_set( "allow_url_fopen", true );
+               $ret = $this->setupFromFile( $url );
+               ini_set( "allow_url_fopen", false );
+               return $ret;
+       }
+       
+       function setupFromInterwiki( $interwiki, $page ) {
+               $base = Title::getInterwikiLink( $interwiki );
+               if( empty( $base ) ) {
+                       return false;
+               } else {
+                       $import = wfUrlencode( "Special:Export/$page" );
+                       $url = str_replace( "$1", $import, $base );
+                       $this->notice( "Importing from $url" );
+                       return $this->setupFromURL( $url );
+               }
        }
        
+       # --------------
+       
        function doImport() {
                if( empty( $this->mSource ) ) {
                        return $this->setError( wfMsg( "importnotext" ) );
@@ -215,15 +282,25 @@ class WikiImporter {
        }
        
        function debug( $data ) {
-               # global $wgOut;
-               # $wgOut->addHTML( htmlspecialchars( $data ) . "<br>\n" );
+               global $wgOut;
+               # $this->notice( "DEBUG: $data\n" );
+       }
+       
+       function notice( $data ) {
+               global $wgCommandLineMode;
+               if( $wgCommandLineMode ) {
+                       print "$data\n";
+               } else {
+                       global $wgOut;
+                       $wgOut->addHTML( "<li>$data</li>\n" );
+               }
        }
        
        function setRevisionHandler( $functionref ) {
                $this->mRevisionHandler = $functionref;
        }
        
-       function defaultRevisionHandler( $revision ) {
+       function defaultRevisionHandler( &$revision ) {
                $this->debug( "Got revision:" );
                if( is_object( $revision->title ) ) {
                        $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
@@ -232,6 +309,7 @@ class WikiImporter {
                }
                $this->debug( "-- User: " . $revision->user_text );
                $this->debug( "-- Timestamp: " . $revision->timestamp );
+               $this->debug( "-- Comment: " . $revision->comment );
                $this->debug( "-- Text: " . $revision->text );
        }
        
@@ -272,6 +350,7 @@ class WikiImporter {
                case "title":
                case "restrictions":
                        $this->appendfield = $name;
+                       $this->appenddata = "";
                        $this->parenttag = "page";
                        xml_set_element_handler( $parser, "in_nothing", "out_append" );
                        xml_set_character_data_handler( $parser, "char_append" );
@@ -328,6 +407,9 @@ class WikiImporter {
                case "timestamp":
                        $this->workRevision->setTimestamp( $this->appenddata );
                        break;
+               case "comment":
+                       $this->workRevision->setComment( $this->appenddata );
+                       break;
                default;
                        $this->debug( "Bad append: {$this->appendfield}" );
                }
@@ -354,6 +436,7 @@ class WikiImporter {
                        return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
                }
        }
+       
        function out_revision( $parser, $name ) {
                $this->debug( "out_revision $name" );
                if( $name != "revision" ) {
@@ -361,7 +444,11 @@ class WikiImporter {
                }
                xml_set_element_handler( $parser, "in_page", "out_page" );
                
-               call_user_func( $this->mRevisionHandler, $this->workRevision );
+               $out = call_user_func( $this->mRevisionHandler, &$this->workRevision, &$this );
+               if( !empty( $out ) ) {
+                       global $wgOut;
+                       $wgOut->addHTML( "<li>" . $out . "</li>\n" );
+               }
        }
        
        function in_contributor( $parser, $name, $attribs ) {
@@ -386,6 +473,44 @@ class WikiImporter {
                }
                xml_set_element_handler( $parser, "in_revision", "out_revision" );
        }
+
+       function importOldRevision() {
+               $fname = "WikiImporter::importOldRevision";
+               $dbw =& wfGetDB( DB_MASTER );
+               
+               # Sneak a single revision into place
+               $user = User::newFromName( $this->getUser() );
+
+               $res = $dbw->select( 'old', 1, 
+                       $this->title->oldCond() + array( 'old_timestamp' => $this->timestamp ),
+                       $fname, 'FOR UPDATE'
+               );
+               
+               $numrows = $dbw->numRows( $res );
+               $dbw->freeResult( $res );
+               if( $numrows > 0 ) {
+                       return wfMsg( "importhistoryconflict" );
+               }
+               
+               # Insert the row
+               $oldIgnore = $dbw->ignoreErrors( true );
+               $success = $dbw->insert( 'old', 
+                       array( 
+                               'old_namespace' => intval( $this->title->getNamespace() ),
+                               'old_title' => $this->title->getDBkey(),
+                               'old_text' => $this->getText(),
+                               'old_comment' => $this->getComment(),
+                               'old_user' => intval( $user->getId() ),
+                               'old_user_text' => $user->getName(),
+                               'old_timestamp' => $this->timestamp,
+                               'inverse_timestamp' => wfInvertTimestamp( $this->timestamp ),
+                               'old_minor_edit' => 0,
+                               'old_flags' => ''
+                       ), $fname
+               );
+               
+               return wfMsg( "ok" );
+       }
 }