MediaWiki REL1_37
updateSpecialPages.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
28
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_PRIMARY );
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 return;
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 }
81
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 );
86 # Do the query
87 $num = $queryPage->recache( $limit ?? $wgQueryCacheLimit );
88 $t2 = microtime( true );
89 if ( $num === false ) {
90 $this->output( "FAILED: database error\n" );
91 } else {
92 $this->output( "got $num rows in " );
93
94 $elapsed = $t2 - $t1;
95 $hours = intval( $elapsed / 3600 );
96 $minutes = intval( $elapsed % 3600 / 60 );
97 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
98 if ( $hours ) {
99 $this->output( $hours . 'h ' );
100 }
101 if ( $minutes ) {
102 $this->output( $minutes . 'm ' );
103 }
104 $this->output( sprintf( "%.2fs\n", $seconds ) );
105 }
106 # Reopen any connections that have closed
108 } else {
109 // Check if this page was expensive before and now cheap
110 $cached = $queryPage->getCachedTimestamp();
111 if ( $cached ) {
112 $queryPage->deleteAllCachedData();
114 $this->output( "cheap, but deleted cached data\n" );
115 } else {
116 $this->output( "cheap, skipped\n" );
117 }
118 }
119 if ( $this->hasOption( 'only' ) ) {
120 break;
121 }
122 }
123 }
124 }
125
132 private function reopenAndWaitForReplicas() {
133 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
134 $lb = $lbFactory->getMainLB();
135 if ( !$lb->pingAll() ) {
136 $this->output( "\n" );
137 do {
138 $this->error( "Connection failed, reconnecting in 10 seconds..." );
139 sleep( 10 );
140 } while ( !$lb->pingAll() );
141 $this->output( "Reconnected\n\n" );
142 }
143 // Wait for the replica DB to catch up
144 $lbFactory->waitForReplication();
145 }
146
147 public function doSpecialPageCacheUpdates( $dbw ) {
149
150 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
151 # --list : just show the name of pages
152 if ( $this->hasOption( 'list' ) ) {
153 $this->output( "$special [callback]\n" );
154 continue;
155 }
156
157 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $special ) {
158 if ( !is_callable( $call ) ) {
159 $this->error( "Uncallable function $call!" );
160 continue;
161 }
162 $this->output( sprintf( '%-30s [callback] ', $special ) );
163 $t1 = microtime( true );
164 call_user_func( $call, $dbw );
165 $t2 = microtime( true );
166
167 $this->output( "completed in " );
168 $elapsed = $t2 - $t1;
169 $hours = intval( $elapsed / 3600 );
170 $minutes = intval( $elapsed % 3600 / 60 );
171 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
172 if ( $hours ) {
173 $this->output( $hours . 'h ' );
174 }
175 if ( $minutes ) {
176 $this->output( $minutes . 'm ' );
177 }
178 $this->output( sprintf( "%.2fs\n", $seconds ) );
179 # Wait for the replica DB to catch up
181 }
182 }
183 }
184}
185
186$maintClass = UpdateSpecialPages::class;
187require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$wgQueryCacheLimit
Number of rows to cache in 'querycache' table when miser mode is on.
$wgSpecialPageCacheUpdates
Additional functions to be performed with updateSpecialPages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:41
static getDisabledQueryPages(Config $config)
Get a list of query pages disabled and with it's run mode.
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition QueryPage.php:85
Maintenance script to update cached special pages.
__construct()
Default constructor.
reopenAndWaitForReplicas()
Re-open any closed db connection, and wait for replicas.
execute()
Do the actual work.
const DB_PRIMARY
Definition defines.php:27