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