disable wgDebugToolbar when rebuilding file cache
[lhc/web/wiklou.git] / maintenance / rebuildFileCache.php
1 <?php
2 /**
3 * Build file cache for content pages
4 *
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.
9 *
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.
14 *
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
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
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 );
33 }
34
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 );
41 }
42 // Debug toolbar makes content uncacheable
43 $wgDebugToolbar = false;
44
45 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
46
47 $start = $this->getOption( 'start', "0" );
48 if ( !ctype_digit( $start ) ) {
49 $this->error( "Invalid value for start parameter.", true );
50 }
51 $start = intval( $start );
52
53 $end = $this->getOption( 'end', "0" );
54 if ( !ctype_digit( $end ) ) {
55 $this->error( "Invalid value for end parameter.", true );
56 }
57 $end = intval( $end );
58
59 $this->output( "Building content page file cache from page {$start}!\n" );
60
61 $dbr = wfGetDB( DB_SLAVE );
62 $overwrite = $this->getOption( 'overwrite', false );
63 $start = ( $start > 0 )
64 ? $start
65 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
66 $end = ( $end > 0 )
67 ? $end
68 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
69 if ( !$start ) {
70 $this->error( "Nothing to do.", true );
71 }
72
73 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
74
75 # Do remaining chunk
76 $end += $this->mBatchSize - 1;
77 $blockStart = $start;
78 $blockEnd = $start + $this->mBatchSize - 1;
79
80 $dbw = wfGetDB( DB_MASTER );
81 // Go through each page and save the output
82 while ( $blockEnd <= $end ) {
83 // Get the pages
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' )
88 );
89
90 $dbw->begin(); // for any changes
91 foreach ( $res as $row ) {
92 $rebuilt = false;
93 $wgRequestTime = microtime( true ); # bug 22852
94
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?
99 }
100
101 $context = new RequestContext;
102 $context->setTitle( $wgTitle );
103 $article = Article::newFromTitle( $wgTitle, $context );
104 $context->setWikiPage( $article->getPage() );
105
106 $wgOut = $context->getOutput(); // set display title
107
108 // If the article is cacheable, then load it
109 if ( $article->isFileCacheable() ) {
110 $cache = HTMLFileCache::newFromTitle( $wgTitle, 'view' );
111 if ( $cache->isCacheGood() ) {
112 if ( $overwrite ) {
113 $rebuilt = true;
114 } else {
115 $this->output( "Page {$row->page_id} already cached\n" );
116 continue; // done already!
117 }
118 }
119 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
120 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
121 $article->view();
122 wfSuppressWarnings(); // header notices
123 $wgOut->output();
124 wfRestoreWarnings();
125 $wgUseFileCache = true;
126 ob_end_clean(); // clear buffer
127 if ( $rebuilt ) {
128 $this->output( "Re-cached page {$row->page_id}\n" );
129 } else {
130 $this->output( "Cached page {$row->page_id}\n" );
131 }
132 } else {
133 $this->output( "Page {$row->page_id} not cacheable\n" );
134 }
135 }
136 $dbw->commit(); // commit any changes (just for sanity)
137
138 $blockStart += $this->mBatchSize;
139 $blockEnd += $this->mBatchSize;
140 }
141 $this->output( "Done!\n" );
142
143 // Remove these to be safe
144 if ( isset( $wgTitle ) )
145 unset( $wgTitle );
146 }
147 }
148
149 $maintClass = "RebuildFileCache";
150 require_once( RUN_MAINTENANCE_IF_MAIN );