MediaWiki master
updateSpecialPages.php
Go to the documentation of this file.
1<?php
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
19
26 public function __construct() {
27 parent::__construct();
28 $this->addOption( 'list', 'List special page names' );
29 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
30 'check correct case by calling this script with --list. ' .
31 'Ex: --only=BrokenRedirects', false, true );
32 $this->addOption( 'override', 'Also update pages that have updates disabled' );
33 }
34
35 public function execute() {
36 $dbw = $this->getPrimaryDB();
37 $config = $this->getConfig();
38 $specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory();
39
40 $this->doSpecialPageCacheUpdates( $dbw );
41
42 $queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit );
43 $disabledQueryPages = QueryPage::getDisabledQueryPages( $config );
44 foreach ( QueryPage::getPages() as $page ) {
45 [ , $special ] = $page;
46 $limit = $page[2] ?? $queryCacheLimit;
47
48 # --list : just show the name of pages
49 if ( $this->hasOption( 'list' ) ) {
50 $this->output( "$special [QueryPage]\n" );
51 continue;
52 }
53
54 if ( !$this->hasOption( 'override' )
55 && isset( $disabledQueryPages[$special] )
56 ) {
57 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
58 continue;
59 }
60
61 $specialObj = $specialPageFactory->getPage( $special );
62 if ( !$specialObj ) {
63 $this->output( "No such special page: $special\n" );
64 return;
65 }
66 if ( $specialObj instanceof QueryPage ) {
67 $queryPage = $specialObj;
68 } else {
69 $class = get_class( $specialObj );
70 $this->fatalError( "$class is not an instance of QueryPage.\n" );
71 }
72
73 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) {
74 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
75 if ( $queryPage->isExpensive() ) {
76 $t1 = microtime( true );
77 # Do the query
78 $num = $queryPage->recache( $limit );
79 $t2 = microtime( true );
80 if ( $num === false ) {
81 $this->output( "FAILED: database error\n" );
82 } else {
83 $this->output( "got $num rows in " );
84 $this->outputElapsedTime( $t2 - $t1 );
85 }
86 # Reopen any connections that have closed
87 $this->reopenAndWaitForReplicas();
88 } else {
89 // Check if this page was expensive before and now cheap
90 $cached = $queryPage->getCachedTimestamp();
91 if ( $cached ) {
92 $queryPage->deleteAllCachedData();
93 $this->reopenAndWaitForReplicas();
94 $this->output( "cheap, but deleted cached data\n" );
95 } else {
96 $this->output( "cheap, skipped\n" );
97 }
98 }
99 if ( $this->hasOption( 'only' ) ) {
100 break;
101 }
102 }
103 }
104 }
105
112 private function reopenAndWaitForReplicas() {
113 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
114 $lb = $lbFactory->getMainLB();
115 if ( !$lb->pingAll() ) {
116 // We don't want the tests to sleep for 10 seconds, so mark this as ignored because there is no reason to
117 // test it.
118 // @codeCoverageIgnoreStart
119 $this->output( "\n" );
120 do {
121 $this->error( "Connection failed, reconnecting in 10 seconds..." );
122 sleep( 10 );
123 $this->waitForReplication();
124 } while ( !$lb->pingAll() );
125 $this->output( "Reconnected\n\n" );
126 // @codeCoverageIgnoreEnd
127 }
128 $this->waitForReplication();
129 }
130
131 public function doSpecialPageCacheUpdates( IDatabase $dbw ) {
132 foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) {
133 # --list : just show the name of pages
134 if ( $this->hasOption( 'list' ) ) {
135 $this->output( "$special [callback]\n" );
136 continue;
137 }
138
139 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) {
140 $this->output( sprintf( '%-30s [callback] ', $special ) );
141 if ( !is_callable( $call ) ) {
142 $this->error( "Uncallable function $call!" );
143 continue;
144 }
145 $t1 = microtime( true );
146 $call( $dbw );
147 $t2 = microtime( true );
148
149 $this->output( "completed in " );
150 $this->outputElapsedTime( $t2 - $t1 );
151
152 # Wait for the replica DB to catch up
153 $this->reopenAndWaitForReplicas();
154 }
155 }
156 }
157
165 private function outputElapsedTime( float $elapsed ) {
166 $hours = intval( $elapsed / 3600 );
167 $minutes = intval( (int)$elapsed % 3600 / 60 );
168 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
169 if ( $hours ) {
170 $this->output( $hours . 'h ' );
171 }
172 if ( $minutes ) {
173 $this->output( $minutes . 'm ' );
174 }
175 $this->output( sprintf( "%.2fs\n", $seconds ) );
176 }
177}
178
179// @codeCoverageIgnoreStart
180$maintClass = UpdateSpecialPages::class;
181require_once RUN_MAINTENANCE_IF_MAIN;
182// @codeCoverageIgnoreEnd
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
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:77
Maintenance script to update cached special pages.
__construct()
Default constructor.
doSpecialPageCacheUpdates(IDatabase $dbw)
execute()
Do the actual work.
Interface to a relational database.
Definition IDatabase.php:31