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 = SpecialPageFactory
::getPage( $special );
71 $this->output( "No such special page: $special\n" );
74 if ( $specialObj instanceof QueryPage
) {
75 $queryPage = $specialObj;
77 $class = get_class( $specialObj );
78 $this->fatalError( "$class is not an instance of QueryPage.\n" );
82 if ( !$this->hasOption( 'only' ) ||
$this->getOption( 'only' ) == $queryPage->getName() ) {
83 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
84 if ( $queryPage->isExpensive() ) {
85 $t1 = microtime( true );
87 $num = $queryPage->recache( $limit === null ?
$wgQueryCacheLimit : $limit );
88 $t2 = microtime( true );
89 if ( $num === false ) {
90 $this->output( "FAILED: database error\n" );
92 $this->output( "got $num rows in " );
95 $hours = intval( $elapsed / 3600 );
96 $minutes = intval( $elapsed %
3600 / 60 );
97 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
99 $this->output( $hours . 'h ' );
102 $this->output( $minutes . 'm ' );
104 $this->output( sprintf( "%.2fs\n", $seconds ) );
106 # Reopen any connections that have closed
107 $this->reopenAndWaitForReplicas();
109 $this->output( "cheap, skipped\n" );
111 if ( $this->hasOption( 'only' ) ) {
119 * Re-open any closed db connection, and wait for replicas
121 * Queries that take a really long time, might cause the
122 * mysql connection to "go away"
124 private function reopenAndWaitForReplicas() {
125 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
126 $lb = $lbFactory->getMainLB();
127 if ( !$lb->pingAll() ) {
128 $this->output( "\n" );
130 $this->error( "Connection failed, reconnecting in 10 seconds..." );
132 } while ( !$lb->pingAll() );
133 $this->output( "Reconnected\n\n" );
135 // Wait for the replica DB to catch up
137 $lbFactory->waitForReplication();
138 } catch ( DBReplicationWaitError
$e ) {
143 public function doSpecialPageCacheUpdates( $dbw ) {
144 global $wgSpecialPageCacheUpdates;
146 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
147 # --list : just show the name of pages
148 if ( $this->hasOption( 'list' ) ) {
149 $this->output( "$special [callback]\n" );
153 if ( !$this->hasOption( 'only' ) ||
$this->getOption( 'only' ) == $special ) {
154 if ( !is_callable( $call ) ) {
155 $this->error( "Uncallable function $call!" );
158 $this->output( sprintf( '%-30s [callback] ', $special ) );
159 $t1 = microtime( true );
160 call_user_func( $call, $dbw );
161 $t2 = microtime( true );
163 $this->output( "completed in " );
164 $elapsed = $t2 - $t1;
165 $hours = intval( $elapsed / 3600 );
166 $minutes = intval( $elapsed %
3600 / 60 );
167 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
169 $this->output( $hours . 'h ' );
172 $this->output( $minutes . 'm ' );
174 $this->output( sprintf( "%.2fs\n", $seconds ) );
175 # Wait for the replica DB to catch up
176 $this->reopenAndWaitForReplicas();
182 $maintClass = UpdateSpecialPages
::class;
183 require_once RUN_MAINTENANCE_IF_MAIN
;