Merge "filebackend: avoid use of wfWikiId() in FileBackendGroup"
[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;
46
47 $dbw = $this->getDB( DB_MASTER );
48
49 $this->doSpecialPageCacheUpdates( $dbw );
50
51 $disabledQueryPages = QueryPage::getDisabledQueryPages( $this->getConfig() );
52 foreach ( QueryPage::getPages() as $page ) {
53 list( , $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 && isset( $disabledQueryPages[$special] )
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 $lbFactory->waitForReplication();
138 }
139
140 public function doSpecialPageCacheUpdates( $dbw ) {
141 global $wgSpecialPageCacheUpdates;
142
143 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
144 # --list : just show the name of pages
145 if ( $this->hasOption( 'list' ) ) {
146 $this->output( "$special [callback]\n" );
147 continue;
148 }
149
150 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $special ) {
151 if ( !is_callable( $call ) ) {
152 $this->error( "Uncallable function $call!" );
153 continue;
154 }
155 $this->output( sprintf( '%-30s [callback] ', $special ) );
156 $t1 = microtime( true );
157 call_user_func( $call, $dbw );
158 $t2 = microtime( true );
159
160 $this->output( "completed in " );
161 $elapsed = $t2 - $t1;
162 $hours = intval( $elapsed / 3600 );
163 $minutes = intval( $elapsed % 3600 / 60 );
164 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
165 if ( $hours ) {
166 $this->output( $hours . 'h ' );
167 }
168 if ( $minutes ) {
169 $this->output( $minutes . 'm ' );
170 }
171 $this->output( sprintf( "%.2fs\n", $seconds ) );
172 # Wait for the replica DB to catch up
173 $this->reopenAndWaitForReplicas();
174 }
175 }
176 }
177 }
178
179 $maintClass = UpdateSpecialPages::class;
180 require_once RUN_MAINTENANCE_IF_MAIN;