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
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
27 use MediaWiki\MediaWikiServices
;
28 use Wikimedia\Rdbms\DBReplicationWaitError
;
31 * Maintenance script to update cached special pages.
33 * @ingroup Maintenance
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' );
45 public function execute() {
46 global $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
48 $dbw = $this->getDB( DB_MASTER
);
50 $this->doSpecialPageCacheUpdates( $dbw );
52 foreach ( QueryPage
::getPages() as $page ) {
53 list( $class, $special ) = $page;
54 $limit = $page[2] ??
null;
56 # --list : just show the name of pages
57 if ( $this->hasOption( 'list' ) ) {
58 $this->output( "$special [QueryPage]\n" );
62 if ( !$this->hasOption( 'override' )
63 && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate )
65 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
69 $specialObj = MediaWikiServices
::getInstance()->getSpecialPageFactory()->
72 $this->output( "No such special page: $special\n" );
75 if ( $specialObj instanceof QueryPage
) {
76 $queryPage = $specialObj;
78 $class = get_class( $specialObj );
79 $this->fatalError( "$class is not an instance of QueryPage.\n" );
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 );
88 $num = $queryPage->recache( $limit ??
$wgQueryCacheLimit );
89 $t2 = microtime( true );
90 if ( $num === false ) {
91 $this->output( "FAILED: database error\n" );
93 $this->output( "got $num rows in " );
96 $hours = intval( $elapsed / 3600 );
97 $minutes = intval( $elapsed %
3600 / 60 );
98 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
100 $this->output( $hours . 'h ' );
103 $this->output( $minutes . 'm ' );
105 $this->output( sprintf( "%.2fs\n", $seconds ) );
107 # Reopen any connections that have closed
108 $this->reopenAndWaitForReplicas();
110 $this->output( "cheap, skipped\n" );
112 if ( $this->hasOption( 'only' ) ) {
120 * Re-open any closed db connection, and wait for replicas
122 * Queries that take a really long time, might cause the
123 * mysql connection to "go away"
125 private function reopenAndWaitForReplicas() {
126 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
127 $lb = $lbFactory->getMainLB();
128 if ( !$lb->pingAll() ) {
129 $this->output( "\n" );
131 $this->error( "Connection failed, reconnecting in 10 seconds..." );
133 } while ( !$lb->pingAll() );
134 $this->output( "Reconnected\n\n" );
136 // Wait for the replica DB to catch up
138 $lbFactory->waitForReplication();
139 } catch ( DBReplicationWaitError
$e ) {
144 public function doSpecialPageCacheUpdates( $dbw ) {
145 global $wgSpecialPageCacheUpdates;
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" );
154 if ( !$this->hasOption( 'only' ) ||
$this->getOption( 'only' ) == $special ) {
155 if ( !is_callable( $call ) ) {
156 $this->error( "Uncallable function $call!" );
159 $this->output( sprintf( '%-30s [callback] ', $special ) );
160 $t1 = microtime( true );
161 call_user_func( $call, $dbw );
162 $t2 = microtime( true );
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;
170 $this->output( $hours . 'h ' );
173 $this->output( $minutes . 'm ' );
175 $this->output( sprintf( "%.2fs\n", $seconds ) );
176 # Wait for the replica DB to catch up
177 $this->reopenAndWaitForReplicas();
183 $maintClass = UpdateSpecialPages
::class;
184 require_once RUN_MAINTENANCE_IF_MAIN
;