MediaWiki  1.34.0
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_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 
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 ) {
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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
UpdateSpecialPages\reopenAndWaitForReplicas
reopenAndWaitForReplicas()
Re-open any closed db connection, and wait for replicas.
Definition: updateSpecialPages.php:125
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$maintClass
$maintClass
Definition: updateSpecialPages.php:179
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
$wgSpecialPageCacheUpdates
$wgSpecialPageCacheUpdates
Additional functions to be performed with updateSpecialPages.
Definition: DefaultSettings.php:7613
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:82
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:36
UpdateSpecialPages\__construct
__construct()
Default constructor.
Definition: updateSpecialPages.php:35
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:613
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DB_MASTER
const DB_MASTER
Definition: defines.php:26
UpdateSpecialPages\doSpecialPageCacheUpdates
doSpecialPageCacheUpdates( $dbw)
Definition: updateSpecialPages.php:140
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
QueryPage\getDisabledQueryPages
static getDisabledQueryPages(Config $config)
Get a list of query pages disabled and with it's run mode.
Definition: QueryPage.php:126
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$wgQueryCacheLimit
$wgQueryCacheLimit
Number of rows to cache in 'querycache' table when miser mode is on.
Definition: DefaultSettings.php:2275
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
QueryPage\getPages
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition: QueryPage.php:74
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
UpdateSpecialPages\execute
execute()
Do the actual work.
Definition: updateSpecialPages.php:44