MediaWiki  master
updateSpecialPages.php
Go to the documentation of this file.
1 <?php
25 require_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
107  $this->reopenAndWaitForReplicas();
108  } else {
109  // Check if this page was expensive before and now cheap
110  $cached = $queryPage->getCachedTimestamp();
111  if ( $cached ) {
112  $queryPage->deleteAllCachedData();
113  $this->reopenAndWaitForReplicas();
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
180  $this->reopenAndWaitForReplicas();
181  }
182  }
183  }
184 }
185 
186 $maintClass = UpdateSpecialPages::class;
187 require_once RUN_MAINTENANCE_IF_MAIN;
UpdateSpecialPages\reopenAndWaitForReplicas
reopenAndWaitForReplicas()
Re-open any closed db connection, and wait for replicas.
Definition: updateSpecialPages.php:132
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:202
$maintClass
$maintClass
Definition: updateSpecialPages.php:186
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
$wgSpecialPageCacheUpdates
$wgSpecialPageCacheUpdates
Additional functions to be performed with updateSpecialPages.
Definition: DefaultSettings.php:8778
UpdateSpecialPages
Maintenance script to update cached special pages.
Definition: updateSpecialPages.php:34
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
QueryPage
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
UpdateSpecialPages\__construct
__construct()
Default constructor.
Definition: updateSpecialPages.php:35
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:598
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
UpdateSpecialPages\doSpecialPageCacheUpdates
doSpecialPageCacheUpdates( $dbw)
Definition: updateSpecialPages.php:147
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
QueryPage\getDisabledQueryPages
static getDisabledQueryPages(Config $config)
Get a list of query pages disabled and with it's run mode.
Definition: QueryPage.php:157
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
$wgQueryCacheLimit
$wgQueryCacheLimit
Number of rows to cache in 'querycache' table when miser mode is on.
Definition: DefaultSettings.php:2639
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
QueryPage\getPages
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition: QueryPage.php:85
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
UpdateSpecialPages\execute
execute()
Do the actual work.
Definition: updateSpecialPages.php:44