Don't throw an exception when waiting for replication times out
[lhc/web/wiklou.git] / maintenance / updateSpecialPages.php
1 <?php
2 /**
3 * Update for cached special pages.
4 * Run this script periodically if you have miser mode enabled.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Maintenance script to update cached special pages.
31 *
32 * @ingroup Maintenance
33 */
34 class UpdateSpecialPages extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addOption( 'list', 'List special page names' );
38 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
39 'check correct case by calling this script with --list. ' .
40 'Ex: --only=BrokenRedirects', false, true );
41 $this->addOption( 'override', 'Also update pages that have updates disabled' );
42 }
43
44 public function execute() {
45 global $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
46
47 $dbw = $this->getDB( DB_MASTER );
48
49 $this->doSpecialPageCacheUpdates( $dbw );
50
51 foreach ( QueryPage::getPages() as $page ) {
52 list( $class, $special ) = $page;
53 $limit = $page[2] ?? null;
54
55 # --list : just show the name of pages
56 if ( $this->hasOption( 'list' ) ) {
57 $this->output( "$special [QueryPage]\n" );
58 continue;
59 }
60
61 if ( !$this->hasOption( 'override' )
62 && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate )
63 ) {
64 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
65 continue;
66 }
67
68 $specialObj = SpecialPageFactory::getPage( $special );
69 if ( !$specialObj ) {
70 $this->output( "No such special page: $special\n" );
71 exit;
72 }
73 if ( $specialObj instanceof QueryPage ) {
74 $queryPage = $specialObj;
75 } else {
76 $class = get_class( $specialObj );
77 $this->fatalError( "$class is not an instance of QueryPage.\n" );
78 die;
79 }
80
81 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
82 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
83 if ( $queryPage->isExpensive() ) {
84 $t1 = microtime( true );
85 # Do the query
86 $num = $queryPage->recache( $limit ?? $wgQueryCacheLimit );
87 $t2 = microtime( true );
88 if ( $num === false ) {
89 $this->output( "FAILED: database error\n" );
90 } else {
91 $this->output( "got $num rows in " );
92
93 $elapsed = $t2 - $t1;
94 $hours = intval( $elapsed / 3600 );
95 $minutes = intval( $elapsed % 3600 / 60 );
96 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
97 if ( $hours ) {
98 $this->output( $hours . 'h ' );
99 }
100 if ( $minutes ) {
101 $this->output( $minutes . 'm ' );
102 }
103 $this->output( sprintf( "%.2fs\n", $seconds ) );
104 }
105 # Reopen any connections that have closed
106 $this->reopenAndWaitForReplicas();
107 } else {
108 $this->output( "cheap, skipped\n" );
109 }
110 if ( $this->hasOption( 'only' ) ) {
111 break;
112 }
113 }
114 }
115 }
116
117 /**
118 * Re-open any closed db connection, and wait for replicas
119 *
120 * Queries that take a really long time, might cause the
121 * mysql connection to "go away"
122 */
123 private function reopenAndWaitForReplicas() {
124 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
125 $lb = $lbFactory->getMainLB();
126 if ( !$lb->pingAll() ) {
127 $this->output( "\n" );
128 do {
129 $this->error( "Connection failed, reconnecting in 10 seconds..." );
130 sleep( 10 );
131 } while ( !$lb->pingAll() );
132 $this->output( "Reconnected\n\n" );
133 }
134 // Wait for the replica DB to catch up
135 $lbFactory->waitForReplication();
136 }
137
138 public function doSpecialPageCacheUpdates( $dbw ) {
139 global $wgSpecialPageCacheUpdates;
140
141 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
142 # --list : just show the name of pages
143 if ( $this->hasOption( 'list' ) ) {
144 $this->output( "$special [callback]\n" );
145 continue;
146 }
147
148 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $special ) {
149 if ( !is_callable( $call ) ) {
150 $this->error( "Uncallable function $call!" );
151 continue;
152 }
153 $this->output( sprintf( '%-30s [callback] ', $special ) );
154 $t1 = microtime( true );
155 call_user_func( $call, $dbw );
156 $t2 = microtime( true );
157
158 $this->output( "completed in " );
159 $elapsed = $t2 - $t1;
160 $hours = intval( $elapsed / 3600 );
161 $minutes = intval( $elapsed % 3600 / 60 );
162 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
163 if ( $hours ) {
164 $this->output( $hours . 'h ' );
165 }
166 if ( $minutes ) {
167 $this->output( $minutes . 'm ' );
168 }
169 $this->output( sprintf( "%.2fs\n", $seconds ) );
170 # Wait for the replica DB to catch up
171 $this->reopenAndWaitForReplicas();
172 }
173 }
174 }
175 }
176
177 $maintClass = UpdateSpecialPages::class;
178 require_once RUN_MAINTENANCE_IF_MAIN;