Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / updateSpecialPages.php
blob45f4b9728ca6a26ca09b4fa50d1ae75ed29ce10c
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\Maintenance\Maintenance;
31 use MediaWiki\SpecialPage\QueryPage;
33 /**
34 * Maintenance script to update cached special pages.
36 * @ingroup Maintenance
38 class UpdateSpecialPages extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( 'list', 'List special page names' );
42 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
43 'check correct case by calling this script with --list. ' .
44 'Ex: --only=BrokenRedirects', false, true );
45 $this->addOption( 'override', 'Also update pages that have updates disabled' );
48 public function execute() {
49 $dbw = $this->getPrimaryDB();
50 $config = $this->getConfig();
51 $specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory();
53 $this->doSpecialPageCacheUpdates( $dbw );
55 $queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit );
56 $disabledQueryPages = QueryPage::getDisabledQueryPages( $config );
57 foreach ( QueryPage::getPages() as $page ) {
58 [ , $special ] = $page;
59 $limit = $page[2] ?? $queryCacheLimit;
61 # --list : just show the name of pages
62 if ( $this->hasOption( 'list' ) ) {
63 $this->output( "$special [QueryPage]\n" );
64 continue;
67 if ( !$this->hasOption( 'override' )
68 && isset( $disabledQueryPages[$special] )
69 ) {
70 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
71 continue;
74 $specialObj = $specialPageFactory->getPage( $special );
75 if ( !$specialObj ) {
76 $this->output( "No such special page: $special\n" );
77 return;
79 if ( $specialObj instanceof QueryPage ) {
80 $queryPage = $specialObj;
81 } else {
82 $class = get_class( $specialObj );
83 $this->fatalError( "$class is not an instance of QueryPage.\n" );
86 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) {
87 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
88 if ( $queryPage->isExpensive() ) {
89 $t1 = microtime( true );
90 # Do the query
91 $num = $queryPage->recache( $limit );
92 $t2 = microtime( true );
93 if ( $num === false ) {
94 $this->output( "FAILED: database error\n" );
95 } else {
96 $this->output( "got $num rows in " );
98 $elapsed = $t2 - $t1;
99 $hours = intval( $elapsed / 3600 );
100 $minutes = intval( (int)$elapsed % 3600 / 60 );
101 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
102 if ( $hours ) {
103 $this->output( $hours . 'h ' );
105 if ( $minutes ) {
106 $this->output( $minutes . 'm ' );
108 $this->output( sprintf( "%.2fs\n", $seconds ) );
110 # Reopen any connections that have closed
111 $this->reopenAndWaitForReplicas();
112 } else {
113 // Check if this page was expensive before and now cheap
114 $cached = $queryPage->getCachedTimestamp();
115 if ( $cached ) {
116 $queryPage->deleteAllCachedData();
117 $this->reopenAndWaitForReplicas();
118 $this->output( "cheap, but deleted cached data\n" );
119 } else {
120 $this->output( "cheap, skipped\n" );
123 if ( $this->hasOption( 'only' ) ) {
124 break;
131 * Re-open any closed db connection, and wait for replicas
133 * Queries that take a really long time, might cause the
134 * mysql connection to "go away"
136 private function reopenAndWaitForReplicas() {
137 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
138 $lb = $lbFactory->getMainLB();
139 if ( !$lb->pingAll() ) {
140 $this->output( "\n" );
141 do {
142 $this->error( "Connection failed, reconnecting in 10 seconds..." );
143 sleep( 10 );
144 $this->waitForReplication();
145 } while ( !$lb->pingAll() );
146 $this->output( "Reconnected\n\n" );
148 $this->waitForReplication();
151 public function doSpecialPageCacheUpdates( $dbw ) {
152 foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) {
153 # --list : just show the name of pages
154 if ( $this->hasOption( 'list' ) ) {
155 $this->output( "$special [callback]\n" );
156 continue;
159 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) {
160 if ( !is_callable( $call ) ) {
161 $this->error( "Uncallable function $call!" );
162 continue;
164 $this->output( sprintf( '%-30s [callback] ', $special ) );
165 $t1 = microtime( true );
166 $call( $dbw );
167 $t2 = microtime( true );
169 $this->output( "completed in " );
170 $elapsed = $t2 - $t1;
171 $hours = intval( $elapsed / 3600 );
172 $minutes = intval( (int)$elapsed % 3600 / 60 );
173 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
174 if ( $hours ) {
175 $this->output( $hours . 'h ' );
177 if ( $minutes ) {
178 $this->output( $minutes . 'm ' );
180 $this->output( sprintf( "%.2fs\n", $seconds ) );
181 # Wait for the replica DB to catch up
182 $this->reopenAndWaitForReplicas();
188 // @codeCoverageIgnoreStart
189 $maintClass = UpdateSpecialPages::class;
190 require_once RUN_MAINTENANCE_IF_MAIN;
191 // @codeCoverageIgnoreEnd