MediaWiki master
updateSpecialPages.php
Go to the documentation of this file.
1<?php
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/Maintenance.php';
27// @codeCoverageIgnoreEnd
28
33
40 public function __construct() {
41 parent::__construct();
42 $this->addOption( 'list', 'List special page names' );
43 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
44 'check correct case by calling this script with --list. ' .
45 'Ex: --only=BrokenRedirects', false, true );
46 $this->addOption( 'override', 'Also update pages that have updates disabled' );
47 }
48
49 public function execute() {
50 $dbw = $this->getPrimaryDB();
51 $config = $this->getConfig();
52 $specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory();
53
54 $this->doSpecialPageCacheUpdates( $dbw );
55
56 $queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit );
57 $disabledQueryPages = QueryPage::getDisabledQueryPages( $config );
58 foreach ( QueryPage::getPages() as $page ) {
59 [ , $special ] = $page;
60 $limit = $page[2] ?? $queryCacheLimit;
61
62 # --list : just show the name of pages
63 if ( $this->hasOption( 'list' ) ) {
64 $this->output( "$special [QueryPage]\n" );
65 continue;
66 }
67
68 if ( !$this->hasOption( 'override' )
69 && isset( $disabledQueryPages[$special] )
70 ) {
71 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
72 continue;
73 }
74
75 $specialObj = $specialPageFactory->getPage( $special );
76 if ( !$specialObj ) {
77 $this->output( "No such special page: $special\n" );
78 return;
79 }
80 if ( $specialObj instanceof QueryPage ) {
81 $queryPage = $specialObj;
82 } else {
83 $class = get_class( $specialObj );
84 $this->fatalError( "$class is not an instance of QueryPage.\n" );
85 }
86
87 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) {
88 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
89 if ( $queryPage->isExpensive() ) {
90 $t1 = microtime( true );
91 # Do the query
92 $num = $queryPage->recache( $limit );
93 $t2 = microtime( true );
94 if ( $num === false ) {
95 $this->output( "FAILED: database error\n" );
96 } else {
97 $this->output( "got $num rows in " );
98 $this->outputElapsedTime( $t2 - $t1 );
99 }
100 # Reopen any connections that have closed
101 $this->reopenAndWaitForReplicas();
102 } else {
103 // Check if this page was expensive before and now cheap
104 $cached = $queryPage->getCachedTimestamp();
105 if ( $cached ) {
106 $queryPage->deleteAllCachedData();
107 $this->reopenAndWaitForReplicas();
108 $this->output( "cheap, but deleted cached data\n" );
109 } else {
110 $this->output( "cheap, skipped\n" );
111 }
112 }
113 if ( $this->hasOption( 'only' ) ) {
114 break;
115 }
116 }
117 }
118 }
119
126 private function reopenAndWaitForReplicas() {
127 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
128 $lb = $lbFactory->getMainLB();
129 if ( !$lb->pingAll() ) {
130 // We don't want the tests to sleep for 10 seconds, so mark this as ignored because there is no reason to
131 // test it.
132 // @codeCoverageIgnoreStart
133 $this->output( "\n" );
134 do {
135 $this->error( "Connection failed, reconnecting in 10 seconds..." );
136 sleep( 10 );
137 $this->waitForReplication();
138 } while ( !$lb->pingAll() );
139 $this->output( "Reconnected\n\n" );
140 // @codeCoverageIgnoreEnd
141 }
142 $this->waitForReplication();
143 }
144
145 public function doSpecialPageCacheUpdates( IDatabase $dbw ) {
146 foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) {
147 # --list : just show the name of pages
148 if ( $this->hasOption( 'list' ) ) {
149 $this->output( "$special [callback]\n" );
150 continue;
151 }
152
153 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) {
154 $this->output( sprintf( '%-30s [callback] ', $special ) );
155 if ( !is_callable( $call ) ) {
156 $this->error( "Uncallable function $call!" );
157 continue;
158 }
159 $t1 = microtime( true );
160 $call( $dbw );
161 $t2 = microtime( true );
162
163 $this->output( "completed in " );
164 $this->outputElapsedTime( $t2 - $t1 );
165
166 # Wait for the replica DB to catch up
167 $this->reopenAndWaitForReplicas();
168 }
169 }
170 }
171
179 private function outputElapsedTime( float $elapsed ) {
180 $hours = intval( $elapsed / 3600 );
181 $minutes = intval( (int)$elapsed % 3600 / 60 );
182 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
183 if ( $hours ) {
184 $this->output( $hours . 'h ' );
185 }
186 if ( $minutes ) {
187 $this->output( $minutes . 'm ' );
188 }
189 $this->output( sprintf( "%.2fs\n", $seconds ) );
190 }
191}
192
193// @codeCoverageIgnoreStart
194$maintClass = UpdateSpecialPages::class;
195require_once RUN_MAINTENANCE_IF_MAIN;
196// @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.
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:87
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:45