Merge "New hook for filters on Special:Contributions form"
[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 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that builds file cache for content pages.
28 *
29 * @ingroup Maintenance
30 */
31 class RebuildFileCache extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( '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 );
39 }
40
41 public function finalSetup() {
42 global $wgDebugToolbar;
43
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();
48 }
49
50 public function execute() {
51 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
52 global $wgOut;
53 if ( !$wgUseFileCache ) {
54 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
55 }
56
57 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
58
59 $start = $this->getOption( 'start', "0" );
60 if ( !ctype_digit( $start ) ) {
61 $this->error( "Invalid value for start parameter.", true );
62 }
63 $start = intval( $start );
64
65 $end = $this->getOption( 'end', "0" );
66 if ( !ctype_digit( $end ) ) {
67 $this->error( "Invalid value for end parameter.", true );
68 }
69 $end = intval( $end );
70
71 $this->output( "Building content page file cache from page {$start}!\n" );
72
73 $dbr = $this->getDB( 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 );
83 }
84
85 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
86
87 # Do remaining chunk
88 $end += $this->mBatchSize - 1;
89 $blockStart = $start;
90 $blockEnd = $start + $this->mBatchSize - 1;
91
92 $dbw = $this->getDB( 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' )
100 );
101
102 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
103 foreach ( $res as $row ) {
104 $rebuilt = false;
105 $wgRequestTime = microtime( true ); # bug 22852
106
107 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
108 if ( null == $title ) {
109 $this->output( "Page {$row->page_id} has bad title\n" );
110 continue; // broken title?
111 }
112
113 $context = new RequestContext;
114 $context->setTitle( $title );
115 $article = Article::newFromTitle( $title, $context );
116 $context->setWikiPage( $article->getPage() );
117
118 $wgOut = $context->getOutput(); // set display title
119
120 // If the article is cacheable, then load it
121 if ( $article->isFileCacheable() ) {
122 $cache = HTMLFileCache::newFromTitle( $title, '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!
129 }
130 }
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 MediaWiki\suppressWarnings(); // header notices
135 $wgOut->output();
136 MediaWiki\restoreWarnings();
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" );
143 }
144 } else {
145 $this->output( "Page {$row->page_id} not cacheable\n" );
146 }
147 }
148 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
149
150 $blockStart += $this->mBatchSize;
151 $blockEnd += $this->mBatchSize;
152 }
153 $this->output( "Done!\n" );
154 }
155 }
156
157 $maintClass = "RebuildFileCache";
158 require_once RUN_MAINTENANCE_IF_MAIN;