Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 90 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
UpdateSpecialPages | |
0.00% |
0 / 90 |
|
0.00% |
0 / 4 |
702 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
240 | |||
reopenAndWaitForReplicas | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
doSpecialPageCacheUpdates | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | /** |
3 | * Update for cached special pages. |
4 | * Run this script periodically if you have miser mode enabled. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @file |
22 | * @ingroup Maintenance |
23 | */ |
24 | |
25 | // @codeCoverageIgnoreStart |
26 | require_once __DIR__ . '/Maintenance.php'; |
27 | // @codeCoverageIgnoreEnd |
28 | |
29 | use MediaWiki\MainConfigNames; |
30 | use MediaWiki\SpecialPage\QueryPage; |
31 | |
32 | /** |
33 | * Maintenance script to update cached special pages. |
34 | * |
35 | * @ingroup Maintenance |
36 | */ |
37 | class UpdateSpecialPages extends Maintenance { |
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 | |
129 | /** |
130 | * Re-open any closed db connection, and wait for replicas |
131 | * |
132 | * Queries that take a really long time, might cause the |
133 | * mysql connection to "go away" |
134 | */ |
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; |
189 | require_once RUN_MAINTENANCE_IF_MAIN; |
190 | // @codeCoverageIgnoreEnd |