Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 108 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RebuildFileCache | |
0.00% |
0 / 108 |
|
0.00% |
0 / 3 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
finalSetup | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 95 |
|
0.00% |
0 / 1 |
272 |
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 | use MediaWiki\Context\RequestContext; |
25 | use MediaWiki\Debug\MWDebug; |
26 | use MediaWiki\MainConfigNames; |
27 | use MediaWiki\Maintenance\Maintenance; |
28 | use MediaWiki\Settings\SettingsBuilder; |
29 | use MediaWiki\Title\Title; |
30 | use Wikimedia\AtEase\AtEase; |
31 | use Wikimedia\Rdbms\SelectQueryBuilder; |
32 | |
33 | // @codeCoverageIgnoreStart |
34 | require_once __DIR__ . '/Maintenance.php'; |
35 | // @codeCoverageIgnoreEnd |
36 | |
37 | /** |
38 | * Maintenance script that builds the file cache. |
39 | * |
40 | * @ingroup Maintenance |
41 | */ |
42 | class RebuildFileCache extends Maintenance { |
43 | /** @var bool */ |
44 | private $enabled = true; |
45 | |
46 | public function __construct() { |
47 | parent::__construct(); |
48 | $this->addDescription( 'Build the file cache' ); |
49 | $this->addOption( 'start', 'Page_id to start from', false, true ); |
50 | $this->addOption( 'end', 'Page_id to end on', false, true ); |
51 | $this->addOption( 'overwrite', 'Refresh page cache' ); |
52 | $this->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' ); |
53 | $this->setBatchSize( 100 ); |
54 | } |
55 | |
56 | public function finalSetup( SettingsBuilder $settingsBuilder ) { |
57 | $this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache ); |
58 | // Script will handle capturing output and saving it itself |
59 | $settingsBuilder->putConfigValue( MainConfigNames::UseFileCache, false ); |
60 | |
61 | // Avoid DB writes (like enotif/counters) |
62 | $this->getServiceContainer()->getReadOnlyMode() |
63 | ->setReason( 'Building cache' ); |
64 | |
65 | // Ensure no debug-specific logic ends up in the cache (must be after Setup.php) |
66 | MWDebug::deinit(); |
67 | |
68 | parent::finalSetup( $settingsBuilder ); |
69 | } |
70 | |
71 | public function execute() { |
72 | if ( !$this->enabled ) { |
73 | $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." ); |
74 | } |
75 | |
76 | $start = $this->getOption( 'start', "0" ); |
77 | if ( !ctype_digit( $start ) ) { |
78 | $this->fatalError( "Invalid value for start parameter." ); |
79 | } |
80 | $start = intval( $start ); |
81 | |
82 | $end = $this->getOption( 'end', "0" ); |
83 | if ( !ctype_digit( $end ) ) { |
84 | $this->fatalError( "Invalid value for end parameter." ); |
85 | } |
86 | $end = intval( $end ); |
87 | |
88 | $this->output( "Building page file cache from page_id {$start}!\n" ); |
89 | |
90 | $dbr = $this->getReplicaDB(); |
91 | $batchSize = $this->getBatchSize(); |
92 | $overwrite = $this->hasOption( 'overwrite' ); |
93 | $start = ( $start > 0 ) |
94 | ? $start |
95 | : $dbr->newSelectQueryBuilder() |
96 | ->select( 'MIN(page_id)' ) |
97 | ->from( 'page' ) |
98 | ->caller( __METHOD__ )->fetchField(); |
99 | $end = ( $end > 0 ) |
100 | ? $end |
101 | : $dbr->newSelectQueryBuilder() |
102 | ->select( 'MAX(page_id)' ) |
103 | ->from( 'page' ) |
104 | ->caller( __METHOD__ )->fetchField(); |
105 | if ( !$start ) { |
106 | $this->fatalError( "Nothing to do." ); |
107 | } |
108 | |
109 | $where = []; |
110 | if ( !$this->getOption( 'all' ) ) { |
111 | // If 'all' isn't passed as an option, just fall back to previous behaviour |
112 | // of using content namespaces |
113 | $where['page_namespace'] = |
114 | $this->getServiceContainer()->getNamespaceInfo()->getContentNamespaces(); |
115 | } |
116 | |
117 | // Mock request (hack, no real client) |
118 | $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; |
119 | |
120 | # Do remaining chunk |
121 | $end += $batchSize - 1; |
122 | $blockStart = $start; |
123 | $blockEnd = $start + $batchSize - 1; |
124 | |
125 | $dbw = $this->getPrimaryDB(); |
126 | // Go through each page and save the output |
127 | while ( $blockEnd <= $end ) { |
128 | // Get the pages |
129 | $res = $dbr->newSelectQueryBuilder() |
130 | ->select( [ 'page_namespace', 'page_title', 'page_id' ] ) |
131 | ->from( 'page' ) |
132 | ->useIndex( 'PRIMARY' ) |
133 | ->where( $where ) |
134 | ->andWhere( [ |
135 | $dbr->expr( 'page_id', '>=', (int)$blockStart ), |
136 | $dbr->expr( 'page_id', '<=', (int)$blockEnd ), |
137 | ] ) |
138 | ->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC ) |
139 | ->caller( __METHOD__ )->fetchResultSet(); |
140 | |
141 | $this->beginTransaction( $dbw, __METHOD__ ); // for any changes |
142 | foreach ( $res as $row ) { |
143 | $rebuilt = false; |
144 | |
145 | $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
146 | if ( $title === null ) { |
147 | $this->output( "Page {$row->page_id} has bad title\n" ); |
148 | continue; // broken title? |
149 | } |
150 | |
151 | $context = new RequestContext(); |
152 | $context->setTitle( $title ); |
153 | $article = Article::newFromTitle( $title, $context ); |
154 | $context->setWikiPage( $article->getPage() ); |
155 | |
156 | // Some extensions like FlaggedRevs while error out if this is unset |
157 | RequestContext::getMain()->setTitle( $title ); |
158 | |
159 | // If the article is cacheable, then load it |
160 | if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) { |
161 | $viewCache = new HTMLFileCache( $title, 'view' ); |
162 | $historyCache = new HTMLFileCache( $title, 'history' ); |
163 | if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) { |
164 | if ( $overwrite ) { |
165 | $rebuilt = true; |
166 | } else { |
167 | $this->output( "Page '$title' (id {$row->page_id}) already cached\n" ); |
168 | continue; // done already! |
169 | } |
170 | } |
171 | |
172 | AtEase::suppressWarnings(); // header notices |
173 | |
174 | // 1. Cache ?action=view |
175 | // Be sure to reset the mocked request time (T24852) |
176 | $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ); |
177 | ob_start(); |
178 | $article->view(); |
179 | $context->getOutput()->output(); |
180 | $context->getOutput()->clearHTML(); |
181 | $viewHtml = ob_get_clean(); |
182 | $viewCache->saveToFileCache( $viewHtml ); |
183 | |
184 | // 2. Cache ?action=history |
185 | // Be sure to reset the mocked request time (T24852) |
186 | $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ); |
187 | ob_start(); |
188 | Action::factory( 'history', $article, $context )->show(); |
189 | $context->getOutput()->output(); |
190 | $context->getOutput()->clearHTML(); |
191 | $historyHtml = ob_get_clean(); |
192 | $historyCache->saveToFileCache( $historyHtml ); |
193 | |
194 | AtEase::restoreWarnings(); |
195 | |
196 | if ( $rebuilt ) { |
197 | $this->output( "Re-cached page '$title' (id {$row->page_id})..." ); |
198 | } else { |
199 | $this->output( "Cached page '$title' (id {$row->page_id})..." ); |
200 | } |
201 | $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " . |
202 | "history: " . strlen( $historyHtml ) . " bytes]\n" ); |
203 | } else { |
204 | $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" ); |
205 | } |
206 | } |
207 | $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes |
208 | |
209 | $blockStart += $batchSize; |
210 | $blockEnd += $batchSize; |
211 | } |
212 | $this->output( "Done!\n" ); |
213 | } |
214 | } |
215 | |
216 | // @codeCoverageIgnoreStart |
217 | $maintClass = RebuildFileCache::class; |
218 | require_once RUN_MAINTENANCE_IF_MAIN; |
219 | // @codeCoverageIgnoreEnd |