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