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