* (bug 23276) Add hook to Special:NewPages to modify query
[lhc/web/wiklou.git] / includes / Import.php
index 42bb521..45908a6 100644 (file)
@@ -209,6 +209,10 @@ class WikiRevision {
                        ) );
                $revId = $revision->insertOn( $dbw );
                $changed = $article->updateIfNewerOn( $dbw, $revision );
+               
+               # To be on the safe side...
+               $tempTitle = $GLOBALS['wgTitle'];
+               $GLOBALS['wgTitle'] = $this->title;
 
                if( $created ) {
                        wfDebug( __METHOD__ . ": running onArticleCreate\n" );
@@ -229,6 +233,7 @@ class WikiRevision {
                                $this->timestamp,
                                $revId );
                }
+               $GLOBALS['wgTitle'] = $tempTitle;
 
                return true;
        }
@@ -247,7 +252,10 @@ class WikiRevision {
                        array( 'log_type' => $this->getType(),
                                'log_action'    => $this->getAction(),
                                'log_timestamp' => $dbw->timestamp( $this->timestamp ),
-                               'log_user_text' => $this->user_text,
+                               'log_namespace' => $this->getTitle()->getNamespace(),
+                               'log_title'     => $this->getTitle()->getDBkey(),
+                               'log_comment'   => $this->getComment(),
+                               #'log_user_text' => $this->user_text,
                                'log_params'    => $this->params ),
                        __METHOD__
                );
@@ -257,14 +265,14 @@ class WikiRevision {
                                $this->timestamp . "\n" );
                        return false;
                }
-               $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
+               $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
                $data = array(
                        'log_id' => $log_id,
                        'log_type' => $this->type,
                        'log_action' => $this->action,
                        'log_timestamp' => $dbw->timestamp( $this->timestamp ),
                        'log_user' => User::idFromName( $this->user_text ),
-                       'log_user_text' => $this->user_text,
+                       #'log_user_text' => $this->user_text,
                        'log_namespace' => $this->getTitle()->getNamespace(),
                        'log_title' => $this->getTitle()->getDBkey(),
                        'log_comment' => $this->getComment(),
@@ -296,7 +304,7 @@ class WikiRevision {
                                $resultDetails = array( 'internal' => $status->getWikiText() );
                */
 
-               // @fixme upload() uses $wgUser, which is wrong here
+               // @todo Fixme: upload() uses $wgUser, which is wrong here
                // it may also create a page without our desire, also wrong potentially.
                // and, it will record a *current* upload, but we might want an archive version here
 
@@ -344,7 +352,7 @@ class WikiRevision {
                        return false;
                }
 
-               // @fixme!
+               // @todo Fixme!
                $src = $this->getSrc();
                $data = Http::get( $src );
                if( !$data ) {
@@ -375,6 +383,7 @@ class WikiImporter {
        var $mLogItemCallback = null;
        var $mUploadCallback = null;
        var $mTargetNamespace = null;
+       var $mXmlNamespace = false;
        var $lastfield;
        var $tagStack = array();
 
@@ -390,6 +399,22 @@ class WikiImporter {
                wfDebug( "WikiImporter XML error: $err\n" );
        }
 
+       function handleXmlNamespace ( $parser, $data, $prefix=false, $uri=false ) {
+               if( preg_match( '/www.mediawiki.org/',$prefix ) ) {
+                       $prefix = str_replace( '/','\/',$prefix );
+                       $this->mXmlNamespace='/^'.$prefix.':/';
+                }
+       }
+
+       function stripXmlNamespace($name) {
+               if( $this->mXmlNamespace ) {
+                       return(preg_replace($this->mXmlNamespace,'',$name,1));
+               }
+               else {
+                       return($name);
+               }
+       }
+
        # --------------
 
        function doImport() {
@@ -397,13 +422,14 @@ class WikiImporter {
                        return new WikiErrorMsg( "importnotext" );
                }
 
-               $parser = xml_parser_create( "UTF-8" );
+               $parser = xml_parser_create_ns( "UTF-8" );
 
                # case folding violates XML standard, turn it off
                xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
 
                xml_set_object( $parser, $this );
                xml_set_element_handler( $parser, "in_start", "" );
+               xml_set_start_namespace_decl_handler( $parser, "handleXmlNamespace" );
 
                $offset = 0; // for context extraction on error reporting
                do {
@@ -528,7 +554,7 @@ class WikiImporter {
        
        /**
         * Default per-revision callback, performs the import.
-        * @param $revision WikiRevision
+        * @param $rev WikiRevision
         * @private
         */
        function importLogItem( $rev ) {
@@ -595,6 +621,7 @@ class WikiImporter {
        }
 
        function in_start( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_start $name" );
                if( $name != "mediawiki" ) {
                        return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
@@ -603,6 +630,7 @@ class WikiImporter {
        }
 
        function in_mediawiki( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_mediawiki $name" );
                if( $name == 'siteinfo' ) {
                        xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
@@ -622,6 +650,7 @@ class WikiImporter {
                }
        }
        function out_mediawiki( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_mediawiki $name" );
                if( $name != "mediawiki" ) {
                        return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
@@ -632,6 +661,7 @@ class WikiImporter {
 
        function in_siteinfo( $parser, $name, $attribs ) {
                // no-ops for now
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_siteinfo $name" );
                switch( $name ) {
                case "sitename":
@@ -647,6 +677,7 @@ class WikiImporter {
        }
 
        function out_siteinfo( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                if( $name == "siteinfo" ) {
                        xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
                }
@@ -654,10 +685,12 @@ class WikiImporter {
 
 
        function in_page( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_page $name" );
                switch( $name ) {
                case "id":
                case "title":
+               case "redirect":
                case "restrictions":
                        $this->appendfield = $name;
                        $this->appenddata = "";
@@ -694,6 +727,7 @@ class WikiImporter {
        }
 
        function out_page( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_page $name" );
                $this->pop();
                if( $name != "page" ) {
@@ -713,6 +747,7 @@ class WikiImporter {
        }
 
        function in_nothing( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_nothing $name" );
                return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
        }
@@ -723,6 +758,7 @@ class WikiImporter {
        }
 
        function out_append( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_append $name" );
                if( $name != $this->appendfield ) {
                        return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
@@ -741,6 +777,9 @@ class WikiImporter {
                        if( is_null( $this->pageTitle ) ) {
                                // Invalid page title? Ignore the page
                                $this->notice( "Skipping invalid page title '$this->workTitle'" );
+                       } elseif( $this->pageTitle->getInterwiki() != '' ) {
+                               $this->notice( "Skipping interwiki page title '$this->workTitle'" );
+                               $this->pageTitle = null;
                        } else {
                                $this->pageCallback( $this->workTitle );
                        }
@@ -815,6 +854,7 @@ class WikiImporter {
        }
 
        function in_revision( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_revision $name" );
                switch( $name ) {
                case "id":
@@ -836,6 +876,7 @@ class WikiImporter {
        }
 
        function out_revision( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_revision $name" );
                $this->pop();
                if( $name != "revision" ) {
@@ -851,8 +892,9 @@ class WikiImporter {
                        }
                }
        }
-       
+
        function in_logitem( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_logitem $name" );
                switch( $name ) {
                case "id":
@@ -876,6 +918,7 @@ class WikiImporter {
        }
 
        function out_logitem( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_logitem $name" );
                $this->pop();
                if( $name != "logitem" ) {
@@ -893,6 +936,7 @@ class WikiImporter {
        }
 
        function in_upload( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_upload $name" );
                switch( $name ) {
                case "timestamp":
@@ -915,6 +959,7 @@ class WikiImporter {
        }
 
        function out_upload( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_revision $name" );
                $this->pop();
                if( $name != "upload" ) {
@@ -932,6 +977,7 @@ class WikiImporter {
        }
 
        function in_contributor( $parser, $name, $attribs ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "in_contributor $name" );
                switch( $name ) {
                case "username":
@@ -947,6 +993,7 @@ class WikiImporter {
        }
 
        function out_contributor( $parser, $name ) {
+               $name = $this->stripXmlNamespace($name);
                $this->debug( "out_contributor $name" );
                $this->pop();
                if( $name != "contributor" ) {
@@ -1038,9 +1085,9 @@ class ImportStreamSource {
                                        return new WikiErrorMsg( 'importuploaderrorsize' );
                                case 3: # The uploaded file was only partially uploaded
                                        return new WikiErrorMsg( 'importuploaderrorpartial' );
-                           case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
-                               return new WikiErrorMsg( 'importuploaderrortemp' );
-                           # case else: # Currently impossible
+                               case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
+                                       return new WikiErrorMsg( 'importuploaderrortemp' );
+                               # case else: # Currently impossible
                        }
 
                }
@@ -1070,7 +1117,7 @@ class ImportStreamSource {
                }
        }
 
-       public static function newFromInterwiki( $interwiki, $page, $history=false ) {
+       public static function newFromInterwiki( $interwiki, $page, $history = false, $templates = false, $pageLinkDepth = 0 ) {
                if( $page == '' ) {
                        return new WikiErrorMsg( 'import-noarticle' );
                }
@@ -1078,7 +1125,10 @@ class ImportStreamSource {
                if( is_null( $link ) || $link->getInterwiki() == '' ) {
                        return new WikiErrorMsg( 'importbadinterwiki' );
                } else {
-                       $params = $history ? 'history=1' : '';
+                       $params = array();
+                       if ( $history ) $params['history'] = 1;
+                       if ( $templates ) $params['templates'] = 1;
+                       if ( $pageLinkDepth ) $params['pagelink-depth'] = $pageLinkDepth;
                        $url = $link->getFullUrl( $params );
                        # For interwikis, use POST to avoid redirects.
                        return ImportStreamSource::newFromURL( $url, "POST" );