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