fix more breakage from r54255
[lhc/web/wiklou.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->addArgs( array( 'start', 'overwrite' ) );
30 $this->setBatchSize( 100 );
31 }
32
33 public function execute() {
34 global $wgUseFileCache, $wgContentNamespaces, $wgDisableCounters, $wgTitle, $wgArticle, $wgOut;
35 if( !$wgUseFileCache ) {
36 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
37 }
38 $wgDisableCounters = false;
39 $start = intval( $this->getArg( 0, 0 ) );
40 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
41 $this->output( "Building content page file cache from page {$start}!\n" );
42
43 $dbr = wfGetDB( DB_SLAVE );
44 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
45 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
46 if( !$start ) {
47 $this->error( "Nothing to do.", true );
48 }
49
50 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
51 OutputPage::setEncodings(); # Not really used yet
52
53 # Do remaining chunk
54 $end += $this->mBatchSize - 1;
55 $blockStart = $start;
56 $blockEnd = $start + $this->mBatchSize - 1;
57
58 $dbw = wfGetDB( DB_MASTER );
59 // Go through each page and save the output
60 while( $blockEnd <= $end ) {
61 // Get the pages
62 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
63 array('page_namespace' => $wgContentNamespaces,
64 "page_id BETWEEN $blockStart AND $blockEnd" ),
65 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
66 );
67 while( $row = $dbr->fetchObject( $res ) ) {
68 $rebuilt = false;
69 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
70 if( null == $wgTitle ) {
71 $this->output( "Page {$row->page_id} bad title\n" );
72 continue; // broken title?
73 }
74 $wgArticle = new Article( $wgTitle );
75 // If the article is cacheable, then load it
76 if( $wgArticle->isFileCacheable() ) {
77 $cache = new HTMLFileCache( $wgTitle );
78 if( $cache->isFileCacheGood() ) {
79 if( $overwrite ) {
80 $rebuilt = true;
81 } else {
82 $this->output( "Page {$row->page_id} already cached\n" );
83 continue; // done already!
84 }
85 }
86 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
87 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
88 $wgArticle->view();
89 @$wgOut->output(); // header notices
90 $wgUseFileCache = true;
91 ob_end_clean(); // clear buffer
92 $wgOut = new OutputPage(); // empty out any output page garbage
93 if( $rebuilt )
94 $this->output( "Re-cached page {$row->page_id}\n" );
95 else
96 $this->output( "Cached page {$row->page_id}\n" );
97 } else {
98 $this->output( "Page {$row->page_id} not cacheable\n" );
99 }
100 $dbw->commit(); // commit any changes
101 }
102 $blockStart += $this->mBatchSize;
103 $blockEnd += $this->mBatchSize;
104 wfWaitForSlaves( 5 );
105 }
106 $this->output( "Done!\n" );
107
108 // Remove these to be safe
109 if( isset($wgTitle) )
110 unset($wgTitle);
111 if( isset($wgArticle) )
112 unset($wgArticle);
113 }
114 }
115
116 $maintClass = "RebuildFileCache";
117 require_once( DO_MAINTENANCE );