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->addOption( 'start', 'Page_id to start from', false, true );
30 $this->addOption( 'end', 'Page_id to end on', false, true );
31 $this->addOption( 'overwrite', 'Refresh page cache' );
32 $this->setBatchSize( 100 );
35 public function execute() {
36 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
37 global $wgTitle, $wgOut;
38 if ( !$wgUseFileCache ) {
39 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
41 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
43 $start = $this->getOption( 'start', "0" );
44 if ( !ctype_digit( $start ) ) {
45 $this->error( "Invalid value for start parameter.", true );
47 $start = intval( $start );
49 $end = $this->getOption( 'end', "0" );
50 if ( !ctype_digit( $end ) ) {
51 $this->error( "Invalid value for end parameter.", true );
53 $end = intval( $end );
55 $this->output( "Building content page file cache from page {$start}!\n" );
57 $dbr = wfGetDB( DB_SLAVE
);
58 $overwrite = $this->getOption( 'overwrite', false );
59 $start = ( $start > 0 )
61 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__
);
64 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__
);
66 $this->error( "Nothing to do.", true );
69 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
72 $end +
= $this->mBatchSize
- 1;
74 $blockEnd = $start +
$this->mBatchSize
- 1;
76 $dbw = wfGetDB( DB_MASTER
);
77 // Go through each page and save the output
78 while ( $blockEnd <= $end ) {
80 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
81 array( 'page_namespace' => $wgContentNamespaces,
82 "page_id BETWEEN $blockStart AND $blockEnd" ),
83 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
86 $dbw->begin(); // for any changes
87 foreach ( $res as $row ) {
89 $wgRequestTime = wfTime(); # bug 22852
90 $context = new RequestContext
;
91 $wgTitle = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
92 $context->setTitle( $wgTitle );
93 if ( null == $wgTitle ) {
94 $this->output( "Page {$row->page_id} has bad title\n" );
95 continue; // broken title?
97 $wgOut = $context->getOutput(); // set display title
98 $article = new Article( $wgTitle );
99 // If the article is cacheable, then load it
100 if ( $article->isFileCacheable() ) {
101 $cache = HTMLFileCache
::newFromTitle( $wgTitle, 'view' );
102 if ( $cache->isCacheGood() ) {
106 $this->output( "Page {$row->page_id} already cached\n" );
107 continue; // done already!
110 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
111 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
113 wfSuppressWarnings(); // header notices
116 $wgUseFileCache = true;
117 ob_end_clean(); // clear buffer
119 $this->output( "Re-cached page {$row->page_id}\n" );
121 $this->output( "Cached page {$row->page_id}\n" );
124 $this->output( "Page {$row->page_id} not cacheable\n" );
127 $dbw->commit(); // commit any changes (just for sanity)
129 $blockStart +
= $this->mBatchSize
;
130 $blockEnd +
= $this->mBatchSize
;
132 $this->output( "Done!\n" );
134 // Remove these to be safe
135 if ( isset( $wgTitle ) )
140 $maintClass = "RebuildFileCache";
141 require_once( RUN_MAINTENANCE_IF_MAIN
);