3 * Build file cache for content pages
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.
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.
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
20 * @ingroup Maintenance
23 require_once( "Maintenance.php" );
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 );
33 public function execute() {
34 global $wgUseFileCache, $wgDisableCounters, $wgTitle, $wgArticle, $wgOut;
35 if( !$wgUseFileCache ) {
36 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.\n", true );
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" );
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__
);
47 $this->error( "Nothing to do.\n", true );
50 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
51 OutputPage
::setEncodings(); # Not really used yet
54 $end +
= $this->mBatchSize
- 1;
56 $blockEnd = $start +
$this->mBatchSize
- 1;
58 $dbw = wfGetDB( DB_MASTER
);
59 // Go through each page and save the output
60 while( $blockEnd <= $end ) {
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')
67 while( $row = $dbr->fetchObject( $res ) ) {
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?
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() ) {
82 $this->output( "Page {$row->page_id} already cached\n" );
83 continue; // done already!
86 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
87 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
89 @$wgOut->output(); // header notices
90 $wgUseFileCache = true;
91 ob_end_clean(); // clear buffer
92 $wgOut = new OutputPage(); // empty out any output page garbage
94 $this->output( "Re-cached page {$row->page_id}\n" );
96 $this->output( "Cached page {$row->page_id}\n" );
98 $this->output( "Page {$row->page_id} not cacheable\n" );
100 $dbw->commit(); // commit any changes
102 $blockStart +
= $this->mBatchSize
;
103 $blockEnd +
= $this->mBatchSize
;
104 wfWaitForSlaves( 5 );
106 $this->output( "Done!\n" );
108 // Remove these to be safe
109 if( isset($wgTitle) )
111 if( isset($wgArticle) )
116 $maintClass = "RebuildFileCache";
117 require_once( DO_MAINTENANCE
);