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