Update IPSet use statements
[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 private $enabled = true;
33
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Build file cache for content pages' );
37 $this->addOption( 'start', 'Page_id to start from', false, true );
38 $this->addOption( 'end', 'Page_id to end on', false, true );
39 $this->addOption( 'overwrite', 'Refresh page cache' );
40 $this->setBatchSize( 100 );
41 }
42
43 public function finalSetup() {
44 global $wgDebugToolbar, $wgUseFileCache;
45
46 $this->enabled = $wgUseFileCache;
47 // Script will handle capturing output and saving it itself
48 $wgUseFileCache = false;
49 // Debug toolbar makes content uncacheable so we disable it.
50 // Has to be done before Setup.php initialize MWDebug
51 $wgDebugToolbar = false;
52 // Avoid DB writes (like enotif/counters)
53 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
54 ->setReason( 'Building cache' );
55
56 parent::finalSetup();
57 }
58
59 public function execute() {
60 global $wgRequestTime;
61
62 if ( !$this->enabled ) {
63 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
64 }
65
66 $start = $this->getOption( 'start', "0" );
67 if ( !ctype_digit( $start ) ) {
68 $this->fatalError( "Invalid value for start parameter." );
69 }
70 $start = intval( $start );
71
72 $end = $this->getOption( 'end', "0" );
73 if ( !ctype_digit( $end ) ) {
74 $this->fatalError( "Invalid value for end parameter." );
75 }
76 $end = intval( $end );
77
78 $this->output( "Building content page file cache from page {$start}!\n" );
79
80 $dbr = $this->getDB( DB_REPLICA );
81 $batchSize = $this->getBatchSize();
82 $overwrite = $this->hasOption( 'overwrite' );
83 $start = ( $start > 0 )
84 ? $start
85 : $dbr->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
86 $end = ( $end > 0 )
87 ? $end
88 : $dbr->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
89 if ( !$start ) {
90 $this->fatalError( "Nothing to do." );
91 }
92
93 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
94
95 # Do remaining chunk
96 $end += $batchSize - 1;
97 $blockStart = $start;
98 $blockEnd = $start + $batchSize - 1;
99
100 $dbw = $this->getDB( DB_MASTER );
101 // Go through each page and save the output
102 while ( $blockEnd <= $end ) {
103 // Get the pages
104 $res = $dbr->select( 'page',
105 [ 'page_namespace', 'page_title', 'page_id' ],
106 [ 'page_namespace' => MWNamespace::getContentNamespaces(),
107 "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
108 __METHOD__,
109 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
110 );
111
112 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
113 foreach ( $res as $row ) {
114 $rebuilt = false;
115
116 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
117 if ( null == $title ) {
118 $this->output( "Page {$row->page_id} has bad title\n" );
119 continue; // broken title?
120 }
121
122 $context = new RequestContext();
123 $context->setTitle( $title );
124 $article = Article::newFromTitle( $title, $context );
125 $context->setWikiPage( $article->getPage() );
126
127 // Some extensions like FlaggedRevs while error out if this is unset
128 RequestContext::getMain()->setTitle( $title );
129
130 // If the article is cacheable, then load it
131 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
132 $viewCache = new HTMLFileCache( $title, 'view' );
133 $historyCache = new HTMLFileCache( $title, 'history' );
134 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
135 if ( $overwrite ) {
136 $rebuilt = true;
137 } else {
138 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
139 continue; // done already!
140 }
141 }
142
143 MediaWiki\suppressWarnings(); // header notices
144 // Cache ?action=view
145 $wgRequestTime = microtime( true ); # T24852
146 ob_start();
147 $article->view();
148 $context->getOutput()->output();
149 $context->getOutput()->clearHTML();
150 $viewHtml = ob_get_clean();
151 $viewCache->saveToFileCache( $viewHtml );
152 // Cache ?action=history
153 $wgRequestTime = microtime( true ); # T24852
154 ob_start();
155 Action::factory( 'history', $article, $context )->show();
156 $context->getOutput()->output();
157 $context->getOutput()->clearHTML();
158 $historyHtml = ob_get_clean();
159 $historyCache->saveToFileCache( $historyHtml );
160 MediaWiki\restoreWarnings();
161
162 if ( $rebuilt ) {
163 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
164 } else {
165 $this->output( "Cached page '$title' (id {$row->page_id})..." );
166 }
167 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
168 "history: " . strlen( $historyHtml ) . " bytes]\n" );
169 } else {
170 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
171 }
172 }
173 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
174
175 $blockStart += $batchSize;
176 $blockEnd += $batchSize;
177 }
178 $this->output( "Done!\n" );
179 }
180 }
181
182 $maintClass = "RebuildFileCache";
183 require_once RUN_MAINTENANCE_IF_MAIN;