Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / rebuildFileCache.php
blobf3a42c43fbfa138eefc465ff5b70b99b2f4af774
1 <?php
2 /**
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 * @file
21 * @ingroup Maintenance
24 require_once( __DIR__ . '/Maintenance.php' );
26 /**
27 * Maintenance script that builds file cache for content pages.
29 * @ingroup Maintenance
31 class RebuildFileCache extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Build file cache for content pages";
35 $this->addOption( 'start', 'Page_id to start from', false, true );
36 $this->addOption( 'end', 'Page_id to end on', false, true );
37 $this->addOption( 'overwrite', 'Refresh page cache' );
38 $this->setBatchSize( 100 );
41 public function finalSetup() {
42 global $wgDebugToolbar;
44 // Debug toolbar makes content uncacheable so we disable it.
45 // Has to be done before Setup.php initialize MWDebug
46 $wgDebugToolbar = false;
47 parent::finalSetup();
50 public function execute() {
51 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
52 global $wgTitle, $wgOut;
53 if ( !$wgUseFileCache ) {
54 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
57 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
59 $start = $this->getOption( 'start', "0" );
60 if ( !ctype_digit( $start ) ) {
61 $this->error( "Invalid value for start parameter.", true );
63 $start = intval( $start );
65 $end = $this->getOption( 'end', "0" );
66 if ( !ctype_digit( $end ) ) {
67 $this->error( "Invalid value for end parameter.", true );
69 $end = intval( $end );
71 $this->output( "Building content page file cache from page {$start}!\n" );
73 $dbr = wfGetDB( DB_SLAVE );
74 $overwrite = $this->getOption( 'overwrite', false );
75 $start = ( $start > 0 )
76 ? $start
77 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
78 $end = ( $end > 0 )
79 ? $end
80 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
81 if ( !$start ) {
82 $this->error( "Nothing to do.", true );
85 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
87 # Do remaining chunk
88 $end += $this->mBatchSize - 1;
89 $blockStart = $start;
90 $blockEnd = $start + $this->mBatchSize - 1;
92 $dbw = wfGetDB( DB_MASTER );
93 // Go through each page and save the output
94 while ( $blockEnd <= $end ) {
95 // Get the pages
96 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
97 array( 'page_namespace' => $wgContentNamespaces,
98 "page_id BETWEEN $blockStart AND $blockEnd" ),
99 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
102 $dbw->begin( __METHOD__ ); // for any changes
103 foreach ( $res as $row ) {
104 $rebuilt = false;
105 $wgRequestTime = microtime( true ); # bug 22852
107 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
108 if ( null == $wgTitle ) {
109 $this->output( "Page {$row->page_id} has bad title\n" );
110 continue; // broken title?
113 $context = new RequestContext;
114 $context->setTitle( $wgTitle );
115 $article = Article::newFromTitle( $wgTitle, $context );
116 $context->setWikiPage( $article->getPage() );
118 $wgOut = $context->getOutput(); // set display title
120 // If the article is cacheable, then load it
121 if ( $article->isFileCacheable() ) {
122 $cache = HTMLFileCache::newFromTitle( $wgTitle, 'view' );
123 if ( $cache->isCacheGood() ) {
124 if ( $overwrite ) {
125 $rebuilt = true;
126 } else {
127 $this->output( "Page {$row->page_id} already cached\n" );
128 continue; // done already!
131 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
132 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
133 $article->view();
134 wfSuppressWarnings(); // header notices
135 $wgOut->output();
136 wfRestoreWarnings();
137 $wgUseFileCache = true;
138 ob_end_clean(); // clear buffer
139 if ( $rebuilt ) {
140 $this->output( "Re-cached page {$row->page_id}\n" );
141 } else {
142 $this->output( "Cached page {$row->page_id}\n" );
144 } else {
145 $this->output( "Page {$row->page_id} not cacheable\n" );
148 $dbw->commit( __METHOD__ ); // commit any changes (just for sanity)
150 $blockStart += $this->mBatchSize;
151 $blockEnd += $this->mBatchSize;
153 $this->output( "Done!\n" );
155 // Remove these to be safe
156 if ( isset( $wgTitle ) ) {
157 unset( $wgTitle );
162 $maintClass = "RebuildFileCache";
163 require_once( RUN_MAINTENANCE_IF_MAIN );