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 finalSetup() {
36 global $wgDebugToolbar;
38 // Debug toolbar makes content uncacheable so we disable it.
39 // Has to be done before Setup.php initialize MWDebug
40 $wgDebugToolbar = false;
44 public function execute() {
45 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
46 global $wgTitle, $wgOut;
47 if ( !$wgUseFileCache ) {
48 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
51 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
53 $start = $this->getOption( 'start', "0" );
54 if ( !ctype_digit( $start ) ) {
55 $this->error( "Invalid value for start parameter.", true );
57 $start = intval( $start );
59 $end = $this->getOption( 'end', "0" );
60 if ( !ctype_digit( $end ) ) {
61 $this->error( "Invalid value for end parameter.", true );
63 $end = intval( $end );
65 $this->output( "Building content page file cache from page {$start}!\n" );
67 $dbr = wfGetDB( DB_SLAVE
);
68 $overwrite = $this->getOption( 'overwrite', false );
69 $start = ( $start > 0 )
71 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__
);
74 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__
);
76 $this->error( "Nothing to do.", true );
79 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
82 $end +
= $this->mBatchSize
- 1;
84 $blockEnd = $start +
$this->mBatchSize
- 1;
86 $dbw = wfGetDB( DB_MASTER
);
87 // Go through each page and save the output
88 while ( $blockEnd <= $end ) {
90 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
91 array( 'page_namespace' => $wgContentNamespaces,
92 "page_id BETWEEN $blockStart AND $blockEnd" ),
93 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
96 $dbw->begin( __METHOD__
); // for any changes
97 foreach ( $res as $row ) {
99 $wgRequestTime = microtime( true ); # bug 22852
101 $wgTitle = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
102 if ( null == $wgTitle ) {
103 $this->output( "Page {$row->page_id} has bad title\n" );
104 continue; // broken title?
107 $context = new RequestContext
;
108 $context->setTitle( $wgTitle );
109 $article = Article
::newFromTitle( $wgTitle, $context );
110 $context->setWikiPage( $article->getPage() );
112 $wgOut = $context->getOutput(); // set display title
114 // If the article is cacheable, then load it
115 if ( $article->isFileCacheable() ) {
116 $cache = HTMLFileCache
::newFromTitle( $wgTitle, 'view' );
117 if ( $cache->isCacheGood() ) {
121 $this->output( "Page {$row->page_id} already cached\n" );
122 continue; // done already!
125 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
126 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
128 wfSuppressWarnings(); // header notices
131 $wgUseFileCache = true;
132 ob_end_clean(); // clear buffer
134 $this->output( "Re-cached page {$row->page_id}\n" );
136 $this->output( "Cached page {$row->page_id}\n" );
139 $this->output( "Page {$row->page_id} not cacheable\n" );
142 $dbw->commit( __METHOD__
); // commit any changes (just for sanity)
144 $blockStart +
= $this->mBatchSize
;
145 $blockEnd +
= $this->mBatchSize
;
147 $this->output( "Done!\n" );
149 // Remove these to be safe
150 if ( isset( $wgTitle ) )
155 $maintClass = "RebuildFileCache";
156 require_once( RUN_MAINTENANCE_IF_MAIN
);