Merge "Simplify code to avoid interpreting "$" characters in string replacement"
[mediawiki.git] / maintenance / updateSpecialPages.php
blob16804c913d94b18b5337825d48313a888f5941a8
1 <?php
2 /**
3 * Update for cached special pages.
4 * Run this script periodically if you have miser mode enabled.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/Maintenance.php';
27 // @codeCoverageIgnoreEnd
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\SpecialPage\QueryPage;
32 /**
33 * Maintenance script to update cached special pages.
35 * @ingroup Maintenance
37 class UpdateSpecialPages extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( 'list', 'List special page names' );
41 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
42 'check correct case by calling this script with --list. ' .
43 'Ex: --only=BrokenRedirects', false, true );
44 $this->addOption( 'override', 'Also update pages that have updates disabled' );
47 public function execute() {
48 $dbw = $this->getPrimaryDB();
49 $config = $this->getConfig();
50 $specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory();
52 $this->doSpecialPageCacheUpdates( $dbw );
54 $queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit );
55 $disabledQueryPages = QueryPage::getDisabledQueryPages( $config );
56 foreach ( QueryPage::getPages() as $page ) {
57 [ , $special ] = $page;
58 $limit = $page[2] ?? $queryCacheLimit;
60 # --list : just show the name of pages
61 if ( $this->hasOption( 'list' ) ) {
62 $this->output( "$special [QueryPage]\n" );
63 continue;
66 if ( !$this->hasOption( 'override' )
67 && isset( $disabledQueryPages[$special] )
68 ) {
69 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
70 continue;
73 $specialObj = $specialPageFactory->getPage( $special );
74 if ( !$specialObj ) {
75 $this->output( "No such special page: $special\n" );
76 return;
78 if ( $specialObj instanceof QueryPage ) {
79 $queryPage = $specialObj;
80 } else {
81 $class = get_class( $specialObj );
82 $this->fatalError( "$class is not an instance of QueryPage.\n" );
85 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) {
86 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
87 if ( $queryPage->isExpensive() ) {
88 $t1 = microtime( true );
89 # Do the query
90 $num = $queryPage->recache( $limit );
91 $t2 = microtime( true );
92 if ( $num === false ) {
93 $this->output( "FAILED: database error\n" );
94 } else {
95 $this->output( "got $num rows in " );
97 $elapsed = $t2 - $t1;
98 $hours = intval( $elapsed / 3600 );
99 $minutes = intval( (int)$elapsed % 3600 / 60 );
100 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
101 if ( $hours ) {
102 $this->output( $hours . 'h ' );
104 if ( $minutes ) {
105 $this->output( $minutes . 'm ' );
107 $this->output( sprintf( "%.2fs\n", $seconds ) );
109 # Reopen any connections that have closed
110 $this->reopenAndWaitForReplicas();
111 } else {
112 // Check if this page was expensive before and now cheap
113 $cached = $queryPage->getCachedTimestamp();
114 if ( $cached ) {
115 $queryPage->deleteAllCachedData();
116 $this->reopenAndWaitForReplicas();
117 $this->output( "cheap, but deleted cached data\n" );
118 } else {
119 $this->output( "cheap, skipped\n" );
122 if ( $this->hasOption( 'only' ) ) {
123 break;
130 * Re-open any closed db connection, and wait for replicas
132 * Queries that take a really long time, might cause the
133 * mysql connection to "go away"
135 private function reopenAndWaitForReplicas() {
136 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
137 $lb = $lbFactory->getMainLB();
138 if ( !$lb->pingAll() ) {
139 $this->output( "\n" );
140 do {
141 $this->error( "Connection failed, reconnecting in 10 seconds..." );
142 sleep( 10 );
143 $this->waitForReplication();
144 } while ( !$lb->pingAll() );
145 $this->output( "Reconnected\n\n" );
147 $this->waitForReplication();
150 public function doSpecialPageCacheUpdates( $dbw ) {
151 foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) {
152 # --list : just show the name of pages
153 if ( $this->hasOption( 'list' ) ) {
154 $this->output( "$special [callback]\n" );
155 continue;
158 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) {
159 if ( !is_callable( $call ) ) {
160 $this->error( "Uncallable function $call!" );
161 continue;
163 $this->output( sprintf( '%-30s [callback] ', $special ) );
164 $t1 = microtime( true );
165 $call( $dbw );
166 $t2 = microtime( true );
168 $this->output( "completed in " );
169 $elapsed = $t2 - $t1;
170 $hours = intval( $elapsed / 3600 );
171 $minutes = intval( (int)$elapsed % 3600 / 60 );
172 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
173 if ( $hours ) {
174 $this->output( $hours . 'h ' );
176 if ( $minutes ) {
177 $this->output( $minutes . 'm ' );
179 $this->output( sprintf( "%.2fs\n", $seconds ) );
180 # Wait for the replica DB to catch up
181 $this->reopenAndWaitForReplicas();
187 // @codeCoverageIgnoreStart
188 $maintClass = UpdateSpecialPages::class;
189 require_once RUN_MAINTENANCE_IF_MAIN;
190 // @codeCoverageIgnoreEnd