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( dirname(__FILE__
) . '/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' ) );
30 $this->addOption( 'overwrite', 'Refresh page cache', false );
31 $this->setBatchSize( 100 );
34 public function execute() {
35 global $wgUseFileCache, $wgContentNamespaces, $wgDisableCounters, $wgTitle, $wgArticle, $wgOut;
36 if( !$wgUseFileCache ) {
37 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
39 $wgDisableCounters = false;
40 $start = $this->getArg( 0, 0 );
41 if( !ctype_digit($start) ) {
42 $this->error( "Invalid value for start parameter.", true );
44 $start = intval($start);
45 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
46 $this->output( "Building content page file cache from page {$start}!\n" );
48 $dbr = wfGetDB( DB_SLAVE
);
49 $start = $start > 0 ?
$start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__
);
50 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__
);
52 $this->error( "Nothing to do.", true );
55 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
56 OutputPage
::setEncodings(); # Not really used yet
59 $end +
= $this->mBatchSize
- 1;
61 $blockEnd = $start +
$this->mBatchSize
- 1;
63 $dbw = wfGetDB( DB_MASTER
);
64 // Go through each page and save the output
65 while( $blockEnd <= $end ) {
67 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
68 array('page_namespace' => $wgContentNamespaces,
69 "page_id BETWEEN $blockStart AND $blockEnd" ),
70 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
72 while( $row = $dbr->fetchObject( $res ) ) {
74 $wgTitle = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
75 if( null == $wgTitle ) {
76 $this->output( "Page {$row->page_id} bad title\n" );
77 continue; // broken title?
79 $wgArticle = new Article( $wgTitle );
80 // If the article is cacheable, then load it
81 if( $wgArticle->isFileCacheable() ) {
82 $cache = new HTMLFileCache( $wgTitle );
83 if( $cache->isFileCacheGood() ) {
87 $this->output( "Page {$row->page_id} already cached\n" );
88 continue; // done already!
91 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
92 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
94 @$wgOut->output(); // header notices
95 $wgUseFileCache = true;
96 ob_end_clean(); // clear buffer
97 $wgOut = new OutputPage(); // empty out any output page garbage
99 $this->output( "Re-cached page {$row->page_id}\n" );
101 $this->output( "Cached page {$row->page_id}\n" );
103 $this->output( "Page {$row->page_id} not cacheable\n" );
105 $dbw->commit(); // commit any changes
107 $blockStart +
= $this->mBatchSize
;
108 $blockEnd +
= $this->mBatchSize
;
109 wfWaitForSlaves( 5 );
111 $this->output( "Done!\n" );
113 // Remove these to be safe
114 if( isset($wgTitle) )
116 if( isset($wgArticle) )
121 $maintClass = "RebuildFileCache";
122 require_once( DO_MAINTENANCE
);