]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/rebuildFileCache.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
64                 }
65
66                 $start = $this->getOption( 'start', "0" );
67                 if ( !ctype_digit( $start ) ) {
68                         $this->error( "Invalid value for start parameter.", true );
69                 }
70                 $start = intval( $start );
71
72                 $end = $this->getOption( 'end', "0" );
73                 if ( !ctype_digit( $end ) ) {
74                         $this->error( "Invalid value for end parameter.", true );
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                 $overwrite = $this->hasOption( 'overwrite' );
82                 $start = ( $start > 0 )
83                         ? $start
84                         : $dbr->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
85                 $end = ( $end > 0 )
86                         ? $end
87                         : $dbr->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
88                 if ( !$start ) {
89                         $this->error( "Nothing to do.", true );
90                 }
91
92                 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
93
94                 # Do remaining chunk
95                 $end += $this->mBatchSize - 1;
96                 $blockStart = $start;
97                 $blockEnd = $start + $this->mBatchSize - 1;
98
99                 $dbw = $this->getDB( DB_MASTER );
100                 // Go through each page and save the output
101                 while ( $blockEnd <= $end ) {
102                         // Get the pages
103                         $res = $dbr->select( 'page',
104                                 [ 'page_namespace', 'page_title', 'page_id' ],
105                                 [ 'page_namespace' => MWNamespace::getContentNamespaces(),
106                                         "page_id BETWEEN $blockStart AND $blockEnd" ],
107                                 __METHOD__,
108                                 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
109                         );
110
111                         $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
112                         foreach ( $res as $row ) {
113                                 $rebuilt = false;
114
115                                 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
116                                 if ( null == $title ) {
117                                         $this->output( "Page {$row->page_id} has bad title\n" );
118                                         continue; // broken title?
119                                 }
120
121                                 $context = new RequestContext();
122                                 $context->setTitle( $title );
123                                 $article = Article::newFromTitle( $title, $context );
124                                 $context->setWikiPage( $article->getPage() );
125
126                                 // Some extensions like FlaggedRevs while error out if this is unset
127                                 RequestContext::getMain()->setTitle( $title );
128
129                                 // If the article is cacheable, then load it
130                                 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
131                                         $viewCache = new HTMLFileCache( $title, 'view' );
132                                         $historyCache = new HTMLFileCache( $title, 'history' );
133                                         if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
134                                                 if ( $overwrite ) {
135                                                         $rebuilt = true;
136                                                 } else {
137                                                         $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
138                                                         continue; // done already!
139                                                 }
140                                         }
141
142                                         MediaWiki\suppressWarnings(); // header notices
143                                         // Cache ?action=view
144                                         $wgRequestTime = microtime( true ); # T24852
145                                         ob_start();
146                                         $article->view();
147                                         $context->getOutput()->output();
148                                         $context->getOutput()->clearHTML();
149                                         $viewHtml = ob_get_clean();
150                                         $viewCache->saveToFileCache( $viewHtml );
151                                         // Cache ?action=history
152                                         $wgRequestTime = microtime( true ); # T24852
153                                         ob_start();
154                                         Action::factory( 'history', $article, $context )->show();
155                                         $context->getOutput()->output();
156                                         $context->getOutput()->clearHTML();
157                                         $historyHtml = ob_get_clean();
158                                         $historyCache->saveToFileCache( $historyHtml );
159                                         MediaWiki\restoreWarnings();
160
161                                         if ( $rebuilt ) {
162                                                 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
163                                         } else {
164                                                 $this->output( "Cached page '$title' (id {$row->page_id})..." );
165                                         }
166                                         $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
167                                                 "history: " . strlen( $historyHtml ) . " bytes]\n" );
168                                 } else {
169                                         $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
170                                 }
171                         }
172                         $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
173
174                         $blockStart += $this->mBatchSize;
175                         $blockEnd += $this->mBatchSize;
176                 }
177                 $this->output( "Done!\n" );
178         }
179 }
180
181 $maintClass = "RebuildFileCache";
182 require_once RUN_MAINTENANCE_IF_MAIN;