]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/rebuildFileCache.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.git] / maintenance / rebuildFileCache.php
1 <?php
2 /**
3  * Build file cache for content pages
4  *
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.
9  *
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.
14  *
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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @ingroup Maintenance
21  */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
25 class RebuildFileCache extends Maintenance {
26         public function __construct() {
27                 parent::__construct();
28                 $this->mDescription = "Build file cache for content pages";
29                 $this->addArg( 'start', 'Page_id to start from', true );
30                 $this->addArg( 'overwrite', 'Refresh page cache', false );
31                 $this->setBatchSize( 100 );
32         }
33
34         public function execute() {
35                 global $wgUseFileCache, $wgDisableCounters, $wgContentNamespaces;
36                 global $wgTitle, $wgArticle, $wgOut, $wgUser;
37                 if( !$wgUseFileCache ) {
38                         $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
39                 }
40                 $wgDisableCounters = false;
41                 $start = $this->getArg( 0, "0" );
42                 if( !ctype_digit($start) ) {
43                         $this->error( "Invalid value for start parameter.", true );
44                 }
45                 $start = intval($start);
46                 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
47                 $this->output( "Building content page file cache from page {$start}!\n" );
48
49                 $dbr = wfGetDB( DB_SLAVE );
50                 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
51                 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
52                 if( !$start ) {
53                         $this->error( "Nothing to do.", true );
54                 }
55
56                 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
57                 OutputPage::setEncodings(); # Not really used yet
58
59                 # Do remaining chunk
60                 $end += $this->mBatchSize - 1;
61                 $blockStart = $start;
62                 $blockEnd = $start + $this->mBatchSize - 1;
63         
64                 $dbw = wfGetDB( DB_MASTER );
65                 // Go through each page and save the output
66                 while( $blockEnd <= $end ) {
67                         // Get the pages
68                         $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
69                                 array('page_namespace' => $wgContentNamespaces,
70                                         "page_id BETWEEN $blockStart AND $blockEnd" ),
71                                 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
72                         );
73                         foreach( $res as $row ) {
74                                 $rebuilt = false;
75                                 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
76                                 if( null == $wgTitle ) {
77                                         $this->output( "Page {$row->page_id} has bad title\n" );
78                                         continue; // broken title?
79                                 }
80                                 $wgOut->setTitle( $wgTitle ); // set display title
81                                 $wgUser->getSkin( $wgTitle ); // set skin title
82                                 $wgArticle = new Article( $wgTitle );
83                                 // If the article is cacheable, then load it
84                                 if( $wgArticle->isFileCacheable() ) {
85                                         $cache = new HTMLFileCache( $wgTitle );
86                                         if( $cache->isFileCacheGood() ) {
87                                                 if( $overwrite ) {
88                                                         $rebuilt = true;
89                                                 } else {
90                                                         $this->output( "Page {$row->page_id} already cached\n" );
91                                                         continue; // done already!
92                                                 }
93                                         }
94                                         ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
95                                         $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
96                                         $wgArticle->view();
97                                         @$wgOut->output(); // header notices
98                                         $wgUseFileCache = true;
99                                         ob_end_clean(); // clear buffer
100                                         $wgOut = new OutputPage(); // empty out any output page garbage
101                                         if( $rebuilt )
102                                                 $this->output( "Re-cached page {$row->page_id}\n" );
103                                         else
104                                                 $this->output( "Cached page {$row->page_id}\n" );
105                                 } else {
106                                         $this->output( "Page {$row->page_id} not cacheable\n" );
107                                 }
108                                 $dbw->commit(); // commit any changes
109                         }
110                         $blockStart += $this->mBatchSize;
111                         $blockEnd += $this->mBatchSize;
112                         wfWaitForSlaves( 5 );
113                 }
114                 $this->output( "Done!\n" );
115         
116                 // Remove these to be safe
117                 if( isset($wgTitle) )
118                         unset($wgTitle);
119                 if( isset($wgArticle) )
120                         unset($wgArticle);
121         }
122 }
123
124 $maintClass = "RebuildFileCache";
125 require_once( DO_MAINTENANCE );