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 $wgDebugToolbar;
38 global $wgTitle, $wgOut;
39 if ( !$wgUseFileCache ) {
40 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
42 // Debug toolbar makes content uncacheable
43 $wgDebugToolbar = false;
45 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
47 $start = $this->getOption( 'start', "0" );
48 if ( !ctype_digit( $start ) ) {
49 $this->error( "Invalid value for start parameter.", true );
51 $start = intval( $start );
53 $end = $this->getOption( 'end', "0" );
54 if ( !ctype_digit( $end ) ) {
55 $this->error( "Invalid value for end parameter.", true );
57 $end = intval( $end );
59 $this->output( "Building content page file cache from page {$start}!\n" );
61 $dbr = wfGetDB( DB_SLAVE
);
62 $overwrite = $this->getOption( 'overwrite', false );
63 $start = ( $start > 0 )
65 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__
);
68 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__
);
70 $this->error( "Nothing to do.", true );
73 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
76 $end +
= $this->mBatchSize
- 1;
78 $blockEnd = $start +
$this->mBatchSize
- 1;
80 $dbw = wfGetDB( DB_MASTER
);
81 // Go through each page and save the output
82 while ( $blockEnd <= $end ) {
84 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
85 array( 'page_namespace' => $wgContentNamespaces,
86 "page_id BETWEEN $blockStart AND $blockEnd" ),
87 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
90 $dbw->begin(); // for any changes
91 foreach ( $res as $row ) {
93 $wgRequestTime = microtime( true ); # bug 22852
95 $wgTitle = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
96 if ( null == $wgTitle ) {
97 $this->output( "Page {$row->page_id} has bad title\n" );
98 continue; // broken title?
101 $context = new RequestContext
;
102 $context->setTitle( $wgTitle );
103 $article = Article
::newFromTitle( $wgTitle, $context );
104 $context->setWikiPage( $article->getPage() );
106 $wgOut = $context->getOutput(); // set display title
108 // If the article is cacheable, then load it
109 if ( $article->isFileCacheable() ) {
110 $cache = HTMLFileCache
::newFromTitle( $wgTitle, 'view' );
111 if ( $cache->isCacheGood() ) {
115 $this->output( "Page {$row->page_id} already cached\n" );
116 continue; // done already!
119 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
120 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
122 wfSuppressWarnings(); // header notices
125 $wgUseFileCache = true;
126 ob_end_clean(); // clear buffer
128 $this->output( "Re-cached page {$row->page_id}\n" );
130 $this->output( "Cached page {$row->page_id}\n" );
133 $this->output( "Page {$row->page_id} not cacheable\n" );
136 $dbw->commit(); // commit any changes (just for sanity)
138 $blockStart +
= $this->mBatchSize
;
139 $blockEnd +
= $this->mBatchSize
;
141 $this->output( "Done!\n" );
143 // Remove these to be safe
144 if ( isset( $wgTitle ) )
149 $maintClass = "RebuildFileCache";
150 require_once( RUN_MAINTENANCE_IF_MAIN
);