MediaWiki  1.33.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() {
46 
47  $dbw = $this->getDB( DB_MASTER );
48 
49  $this->doSpecialPageCacheUpdates( $dbw );
50 
51  foreach ( QueryPage::getPages() as $page ) {
52  list( , $special ) = $page;
53  $limit = $page[2] ?? null;
54 
55  # --list : just show the name of pages
56  if ( $this->hasOption( 'list' ) ) {
57  $this->output( "$special [QueryPage]\n" );
58  continue;
59  }
60 
61  if ( !$this->hasOption( 'override' )
62  && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate )
63  ) {
64  $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
65  continue;
66  }
67 
68  $specialObj = MediaWikiServices::getInstance()->getSpecialPageFactory()->
69  getPage( $special );
70  if ( !$specialObj ) {
71  $this->output( "No such special page: $special\n" );
72  exit;
73  }
74  if ( $specialObj instanceof QueryPage ) {
75  $queryPage = $specialObj;
76  } else {
77  $class = get_class( $specialObj );
78  $this->fatalError( "$class is not an instance of QueryPage.\n" );
79  die;
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  $this->output( "cheap, skipped\n" );
110  }
111  if ( $this->hasOption( 'only' ) ) {
112  break;
113  }
114  }
115  }
116  }
117 
124  private function reopenAndWaitForReplicas() {
125  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
126  $lb = $lbFactory->getMainLB();
127  if ( !$lb->pingAll() ) {
128  $this->output( "\n" );
129  do {
130  $this->error( "Connection failed, reconnecting in 10 seconds..." );
131  sleep( 10 );
132  } while ( !$lb->pingAll() );
133  $this->output( "Reconnected\n\n" );
134  }
135  // Wait for the replica DB to catch up
136  $lbFactory->waitForReplication();
137  }
138 
139  public function doSpecialPageCacheUpdates( $dbw ) {
141 
142  foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
143  # --list : just show the name of pages
144  if ( $this->hasOption( 'list' ) ) {
145  $this->output( "$special [callback]\n" );
146  continue;
147  }
148 
149  if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $special ) {
150  if ( !is_callable( $call ) ) {
151  $this->error( "Uncallable function $call!" );
152  continue;
153  }
154  $this->output( sprintf( '%-30s [callback] ', $special ) );
155  $t1 = microtime( true );
156  call_user_func( $call, $dbw );
157  $t2 = microtime( true );
158 
159  $this->output( "completed in " );
160  $elapsed = $t2 - $t1;
161  $hours = intval( $elapsed / 3600 );
162  $minutes = intval( $elapsed % 3600 / 60 );
163  $seconds = $elapsed - $hours * 3600 - $minutes * 60;
164  if ( $hours ) {
165  $this->output( $hours . 'h ' );
166  }
167  if ( $minutes ) {
168  $this->output( $minutes . 'm ' );
169  }
170  $this->output( sprintf( "%.2fs\n", $seconds ) );
171  # Wait for the replica DB to catch up
172  $this->reopenAndWaitForReplicas();
173  }
174  }
175  }
176 }
177 
179 require_once RUN_MAINTENANCE_IF_MAIN;
UpdateSpecialPages\reopenAndWaitForReplicas
reopenAndWaitForReplicas()
Re-open any closed db connection, and wait for replicas.
Definition: updateSpecialPages.php:124
$maintClass
$maintClass
Definition: updateSpecialPages.php:178
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
$wgSpecialPageCacheUpdates
$wgSpecialPageCacheUpdates
Additional functions to be performed with updateSpecialPages.
Definition: DefaultSettings.php:7595
UpdateSpecialPages
Maintenance script to update cached special pages.
Definition: updateSpecialPages.php:34
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
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:35
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
UpdateSpecialPages\__construct
__construct()
Default constructor.
Definition: updateSpecialPages.php:35
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
UpdateSpecialPages\doSpecialPageCacheUpdates
doSpecialPageCacheUpdates( $dbw)
Definition: updateSpecialPages.php:139
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$wgQueryCacheLimit
$wgQueryCacheLimit
Number of rows to cache in 'querycache' table when miser mode is on.
Definition: DefaultSettings.php:2266
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:462
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
QueryPage\getPages
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition: QueryPage.php:73
$wgDisableQueryPageUpdate
$wgDisableQueryPageUpdate
Set this to an array of special page names to prevent maintenance/updateSpecialPages....
Definition: DefaultSettings.php:7923
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
UpdateSpecialPages\execute
execute()
Do the actual work.
Definition: updateSpecialPages.php:44