The war on redundant ampersand usage!
[lhc/web/wiklou.git] / maintenance / dumpHTML.inc
index 71049db..ca2a7df 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 /**
- * @package MediaWiki
- * @subpackage Maintenance
+ * @addtogroup Maintenance
  */
 
 define( 'REPORTING_INTERVAL', 10 );
@@ -38,6 +37,9 @@ class DumpHTML {
        # Make a copy of all images encountered
        var $makeSnapshot = false;
 
+       # Don't image description pages in doEverything()
+       var $noSharedDesc = false;
+
        # Make links assuming the script path is in the same directory as
        # the destination
        var $alternateScriptPath = false;
@@ -48,6 +50,9 @@ class DumpHTML {
        # Has setupGlobals been called?
        var $setupDone = false;
 
+       # Has to compress html pages
+       var $compress = false;
+
        # List of raw pages used in the current article
        var $rawPages;
 
@@ -64,6 +69,9 @@ class DumpHTML {
        # Max page ID, lazy initialised
        var $maxPageID = false;
 
+       # UDP profiling
+       var $udpProfile, $udpProfileCounter = 0, $udpProfileInit = false;
+
        function DumpHTML( $settings = array() ) {
                foreach ( $settings as $var => $value ) {
                        $this->$var = $value;
@@ -121,13 +129,16 @@ class DumpHTML {
                        return;
                }
                $this->doArticles();
-               $this->doLocalImageDescriptions();
-               $this->doSharedImageDescriptions();
                $this->doCategories();
                $this->doRedirects();
                if ( $this->sliceNumerator == 1 ) {
                        $this->doSpecials();
                }
+               $this->doLocalImageDescriptions();
+
+               if ( !$this->noSharedDesc ) {
+                       $this->doSharedImageDescriptions();
+               }
 
                $this->setCheckpoint( 'everything', 'done' );
        }
@@ -138,8 +149,11 @@ class DumpHTML {
         */
        function doArticles() {
                if ( $this->endID === false ) {
-                       $this->endID = $this->getMaxPageID();
+                       $end = $this->getMaxPageID();
+               } else {
+                       $end = $this->endID;
                }
+               $start = $this->startID;
                
                # Start from the checkpoint
                $cp = $this->getCheckpoint( 'article' );
@@ -173,7 +187,8 @@ class DumpHTML {
                        $title = Title::newFromID( $id );
                        if ( $title ) {
                                $ns = $title->getNamespace() ;
-                               if ( $ns != NS_CATEGORY && $title->getPrefixedDBkey() != $mainPage ) {
+                               if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI && 
+                                 $title->getPrefixedDBkey() != $mainPage ) {
                                        $this->doArticle( $title );
                                }
                        }
@@ -187,7 +202,7 @@ class DumpHTML {
 
                $this->setupGlobals();
                print "Special:Categories...";
-               $this->doArticle( Title::makeTitle( NS_SPECIAL, 'Categories' ) );
+               $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
                print "\n";
        }
 
@@ -218,7 +233,9 @@ class DumpHTML {
 
        function doImageDescriptions() {
                $this->doLocalImageDescriptions();
-               $this->doSharedImageDescriptions();
+               if ( !$this->noSharedDesc ) {
+                       $this->doSharedImageDescriptions();
+               }
        }
 
        /**
@@ -226,10 +243,9 @@ class DumpHTML {
         * have a local image
         */
        function doLocalImageDescriptions() {
-               global $wgSharedUploadDirectory;
                $chunkSize = 1000;
 
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                
                $cp = $this->getCheckpoint( 'local image' );
                if ( $cp == 'done' ) {
@@ -303,19 +319,23 @@ class DumpHTML {
                for ( $hash = $start; $hash <= $end; $hash++ ) {
                        $this->setCheckpoint( 'shared image', $hash );
 
-                       $dir = sprintf( "%01x/%02x", intval( $hash / 16 ), $hash );
-                       $paths = array_merge( glob( "{$this->sharedStaticDirectory}/$dir/*" ),
-                               glob( "{$this->sharedStaticDirectory}/thumb/$dir/*" ) );
-
-                       foreach ( $paths as $path ) {
-                               $file = wfBaseName( $path );
+                       $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
+                               intval( $hash / 16 ), $hash );
+                       $handle = @opendir( $dir );
+                       while ( $handle && $file = readdir( $handle ) ) {
+                               if ( $file[0] == '.' ) {
+                                       continue;
+                               }
                                if ( !(++$i % REPORTING_INTERVAL ) ) {
                                        print "$i\r";
                                }
 
-                               $title = Title::makeTitle( NS_IMAGE, $file );
+                               $title = Title::makeTitleSafe( NS_IMAGE, $file );
                                $this->doArticle( $title );
                        }
+                       if ( $handle ) {
+                               closedir( $handle );
+                       }
                }
                $this->setCheckpoint( 'shared image', 'done' );
                print "\n";
@@ -325,7 +345,7 @@ class DumpHTML {
                $chunkSize = 1000;
                
                $this->setupGlobals();
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                
                $cp = $this->getCheckpoint( 'category' );
                if ( $cp == 'done' ) {
@@ -333,7 +353,7 @@ class DumpHTML {
                        return;
                } elseif ( $cp !== false ) {
                        print "Resuming category page dump from $cp\n";
-                       $conds = array( 'cl_to >= ' $dbr->addQuotes( $cp ) );
+                       $conds = array( 'cl_to >= ' $dbr->addQuotes( $cp ) );
                } else {
                        print "Starting category pages\n";
                        $conds = false;
@@ -341,13 +361,13 @@ class DumpHTML {
 
                $i = 0;
                do {
-                       $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__
+                       $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__
                                array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
                        $numRows = $dbr->numRows( $res );
                        
                        while ( $row = $dbr->fetchObject( $res ) ) {
                                // Set conditions for next chunk
-                               $conds = array( 'cl_to > ' $dbr->addQuotes( $row->cl_to ) );
+                               $conds = array( 'cl_to > ' $dbr->addQuotes( $row->cl_to ) );
                                
                                // Filter pages from other slices
                                if ( !$this->sliceFilter( $row->cl_to ) ) {
@@ -383,10 +403,12 @@ class DumpHTML {
                } elseif ( $cp !== false ) {
                        print "Resuming redirect generation from page_id $cp\n";
                        $start = intval( $cp );
+               } else {
+                       $start = 1;
                }
 
                $this->setupGlobals();
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $i = 0;
 
                for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
@@ -419,9 +441,6 @@ class DumpHTML {
 
        /** Write an article specified by title */
        function doArticle( $title ) {
-               global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
-               global $wgUploadDirectory;
-
                if ( $this->noOverwrite ) {
                        $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
                        if ( file_exists( $fileName ) ) {
@@ -429,6 +448,8 @@ class DumpHTML {
                        }
                }
 
+               $this->profile();
+
                $this->rawPages = array();
                $text = $this->getArticleHTML( $title );
 
@@ -458,21 +479,41 @@ class DumpHTML {
                                print "Writing $file\n";
                                $file = fopen( $path, 'w' );
                                if ( !$file ) {
-                                       print("Can't open file $fullName for writing\n");
+                                       print("Can't open file $path for writing\n");
                                        continue;
                                }
                                fwrite( $file, $text );
                                fclose( $file );
                        }
                }
+
+               wfIncrStats( 'dumphtml_article' );
        }
 
        /** Write the given text to the file identified by the given title object */
-       function writeArticle( &$title, $text ) {
+       function writeArticle( $title, $text ) {
                $filename = $this->getHashedFilename( $title );
+
+               # Temporary hack for current dump, this should be moved to 
+               # getFriendlyName() at the earliest opportunity.
+               #
+               # Limit filename length to 255 characters, so it works on ext3.
+               # Titles are in fact limited to 255 characters, but dumpHTML 
+               # adds a suffix which may put them over the limit.
+               $length = strlen( $filename );
+               if ( $length > 255 ) {
+                       print "Warning: Filename too long ($length bytes). Skipping.\n";
+                       return;
+               }
+                       
                $fullName = "{$this->dest}/$filename";
                $fullDir = dirname( $fullName );
 
+               if ( $this->compress ) {
+                       $fullName .= ".gz";
+                       $text = gzencode( $text, 9 );                           
+               }
+
                wfMkdirParents( $fullDir, 0755 );
 
                wfSuppressWarnings();
@@ -490,7 +531,7 @@ class DumpHTML {
 
        /** Set up globals required for parsing */
        function setupGlobals( $currentDepth = NULL ) {
-               global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
+               global $wgUser, $wgStylePath, $wgArticlePath, $wgMathPath;
                global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
                global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
                global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
@@ -566,20 +607,18 @@ class DumpHTML {
                $wgUser->setOption( 'skin', $this->skin );
                $wgUser->setOption( 'editsection', 0 );
 
-               if ( $this->makeSnapshot ) {
-                       $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
-                       if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
-                               $this->makeSnapshot = false;
-                       }
+               $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
+               if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
+                       print "Disabling image snapshot because the destination is the same as the source\n";
+                       $this->makeSnapshot = false;
                }
-
                $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
 
                $this->setupDone = true;
        }
 
        /** Reads the content of a title object, executes the skin and captures the result */
-       function getArticleHTML( &$title ) {
+       function getArticleHTML( $title ) {
                global $wgOut, $wgTitle, $wgArticle, $wgUser;
 
                $linkCache =& LinkCache::singleton();
@@ -614,6 +653,7 @@ class DumpHTML {
                        }
                }
 
+       
                $sk =& $wgUser->getSkin();
                ob_start();
                $sk->outputPage( $wgOut );
@@ -669,9 +709,13 @@ ENDTEXT;
                if ( !file_exists( $destLoc ) ) {
                        wfMkdirParents( dirname( $destLoc ), 0755 );
                        if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
-                               symlink( $sourceLoc, $destLoc );
+                               if ( !symlink( $sourceLoc, $destLoc ) ) {
+                                       print "Warning: unable to create symlink at $destLoc\n";
+                               }
                        } else {
-                               copy( $sourceLoc, $destLoc );
+                               if ( !copy( $sourceLoc, $destLoc ) ) {
+                                       print "Warning: unable to copy $sourceLoc to $destLoc\n";
+                               }
                        }
                }
        }
@@ -734,6 +778,7 @@ ENDTEXT;
                                $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
                                        $wgArticlePath );
                        }
+                       $url .= $this->compress ? ".gz" : "";
                        return false;
                } else {
                        return true;
@@ -750,6 +795,7 @@ ENDTEXT;
 
                $url = false;
                if ( $query != '' ) {
+                       $params = array();
                        parse_str( $query, $params );
                        if ( isset($params['action']) && $params['action'] == 'raw' ) {
                                if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
@@ -757,6 +803,7 @@ ENDTEXT;
                                } else {
                                        $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
                                        // Clean up Monobook.css etc.
+                                       $matches = array();
                                        if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
                                                $file = $matches[1] . '.' . $matches[2];
                                        }
@@ -768,7 +815,7 @@ ENDTEXT;
                if ( $url === false ) {
                        $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
                }
-
+               $url .= $this->compress ? ".gz" : "";
                return false;
        }
 
@@ -832,6 +879,7 @@ ENDTEXT;
                }
 
                # Split into characters
+               $m = array();
                preg_match_all( '/./us', $dbk, $m );
 
                $chars = $m[0];
@@ -908,12 +956,38 @@ ENDTEXT;
 
        function getMaxPageID() {
                if ( $this->maxPageID === false ) {
-                       $dbr =& wfGetDB( DB_SLAVE );
+                       $dbr = wfGetDB( DB_SLAVE );
                        $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
                }
                return $this->maxPageID;
        }
-                       
+
+       function profile() {
+               global $wgProfiler;
+
+               if ( !$this->udpProfile ) {
+                       return;
+               }
+               if ( !$this->udpProfileInit ) {
+                       $this->udpProfileInit = true;
+               } elseif ( $this->udpProfileCounter == 1 % $this->udpProfile ) {
+                       $wgProfiler->getFunctionReport();
+                       $wgProfiler = new DumpHTML_ProfilerStub;
+               }
+               if ( $this->udpProfileCounter == 0 ) {
+                       $wgProfiler = new ProfilerSimpleUDP;
+                       $wgProfiler->setProfileID( 'dumpHTML' );
+               }
+               $this->udpProfileCounter = ( $this->udpProfileCounter + 1 ) % $this->udpProfile;
+       }
+}
+
+class DumpHTML_ProfilerStub {
+       function profileIn() {}
+       function profileOut() {}
+       function getOutput() {}
+       function close() {}
+       function getFunctionReport() {}
 }
 
 /** XML parser callback */