Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 78 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PurgeChangedPages | |
0.00% |
0 / 78 |
|
0.00% |
0 / 3 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 56 |
|
0.00% |
0 / 1 |
182 | |||
pageableSortedRows | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * Send purge requests for pages edited in date range to squid/varnish. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | // @codeCoverageIgnoreStart |
25 | require_once __DIR__ . '/Maintenance.php'; |
26 | // @codeCoverageIgnoreEnd |
27 | |
28 | use MediaWiki\Title\Title; |
29 | use Wikimedia\Rdbms\IResultWrapper; |
30 | |
31 | /** |
32 | * Maintenance script that sends purge requests for pages edited in a date |
33 | * range to squid/varnish. |
34 | * |
35 | * Can be used to recover from an HTCP message partition or other major cache |
36 | * layer interruption. |
37 | * |
38 | * @ingroup Maintenance |
39 | */ |
40 | class PurgeChangedPages extends Maintenance { |
41 | |
42 | public function __construct() { |
43 | parent::__construct(); |
44 | $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' ); |
45 | $this->addOption( 'starttime', 'Starting timestamp', true, true ); |
46 | $this->addOption( 'endtime', 'Ending timestamp', true, true ); |
47 | $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true ); |
48 | $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true ); |
49 | $this->addOption( 'dry-run', 'Do not send purge requests' ); |
50 | $this->addOption( 'verbose', 'Show more output', false, false, 'v' ); |
51 | $this->setBatchSize( 100 ); |
52 | } |
53 | |
54 | public function execute() { |
55 | global $wgHTCPRouting; |
56 | |
57 | if ( $this->hasOption( 'htcp-dest' ) ) { |
58 | $parts = explode( ':', $this->getOption( 'htcp-dest' ), 2 ); |
59 | if ( count( $parts ) < 2 ) { |
60 | // Add default htcp port |
61 | $parts[] = '4827'; |
62 | } |
63 | |
64 | // Route all HTCP messages to provided host:port |
65 | $wgHTCPRouting = [ |
66 | '' => [ 'host' => $parts[0], 'port' => $parts[1] ], |
67 | ]; |
68 | if ( $this->hasOption( 'verbose' ) ) { |
69 | $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" ); |
70 | } |
71 | } |
72 | |
73 | $dbr = $this->getReplicaDB(); |
74 | $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) ); |
75 | $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) ); |
76 | |
77 | if ( $maxTime < $minTime ) { |
78 | $this->error( "\nERROR: starttime after endtime\n" ); |
79 | $this->maybeHelp( true ); |
80 | } |
81 | |
82 | $stuckCount = 0; |
83 | while ( true ) { |
84 | // Adjust bach size if we are stuck in a second that had many changes |
85 | $bSize = ( $stuckCount + 1 ) * $this->getBatchSize(); |
86 | |
87 | $res = $dbr->newSelectQueryBuilder() |
88 | ->select( [ 'rev_timestamp', 'page_namespace', 'page_title', ] ) |
89 | ->from( 'revision' ) |
90 | ->join( 'page', null, 'rev_page=page_id' ) |
91 | ->where( [ |
92 | $dbr->expr( 'rev_timestamp', '>', $minTime ), |
93 | $dbr->expr( 'rev_timestamp', '<=', $maxTime ), |
94 | ] ) |
95 | // Only get rows where the revision is the latest for the page. |
96 | // Other revisions would be duplicate and we don't need to purge if |
97 | // there has been an edit after the interesting time window. |
98 | ->andWhere( "page_latest = rev_id" ) |
99 | ->orderBy( 'rev_timestamp' ) |
100 | ->limit( $bSize ) |
101 | ->caller( __METHOD__ )->fetchResultSet(); |
102 | |
103 | if ( !$res->numRows() ) { |
104 | // nothing more found so we are done |
105 | break; |
106 | } |
107 | |
108 | // Kludge to not get stuck in loops for batches with the same timestamp |
109 | [ $rows, $lastTime ] = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize ); |
110 | if ( !count( $rows ) ) { |
111 | ++$stuckCount; |
112 | continue; |
113 | } |
114 | // Reset suck counter |
115 | $stuckCount = 0; |
116 | |
117 | $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" ); |
118 | |
119 | // Advance past the last row next time |
120 | $minTime = $lastTime; |
121 | |
122 | // Create list of URLs from page_namespace + page_title |
123 | $urls = []; |
124 | foreach ( $rows as $row ) { |
125 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
126 | $urls[] = $title->getInternalURL(); |
127 | } |
128 | |
129 | if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) { |
130 | $this->output( implode( "\n", $urls ) . "\n" ); |
131 | if ( $this->hasOption( 'dry-run' ) ) { |
132 | continue; |
133 | } |
134 | } |
135 | |
136 | // Send batch of purge requests out to CDN servers |
137 | $hcu = $this->getServiceContainer()->getHtmlCacheUpdater(); |
138 | $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE ); |
139 | |
140 | if ( $this->hasOption( 'sleep-per-batch' ) ) { |
141 | // sleep-per-batch is milliseconds, usleep wants micro seconds. |
142 | usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) ); |
143 | } |
144 | } |
145 | |
146 | $this->output( "Done!\n" ); |
147 | } |
148 | |
149 | /** |
150 | * Remove all the rows in a result set with the highest value for column |
151 | * $column unless the number of rows is less $limit. This returns the new |
152 | * array of rows and the highest value of column $column for the rows left. |
153 | * The ordering of rows is maintained. |
154 | * |
155 | * This is useful for paging on mostly-unique values that may sometimes |
156 | * have large clumps of identical values. It should be safe to do the next |
157 | * query on items with a value higher than the highest of the rows returned here. |
158 | * If this returns an empty array for a non-empty query result, then all the rows |
159 | * had the same column value and the query should be repeated with a higher LIMIT. |
160 | * |
161 | * @todo move this elsewhere |
162 | * |
163 | * @param IResultWrapper $res Query result sorted by $column (ascending) |
164 | * @param string $column |
165 | * @param int $limit |
166 | * @return array (array of rows, string column value) |
167 | */ |
168 | protected function pageableSortedRows( IResultWrapper $res, $column, $limit ) { |
169 | $rows = iterator_to_array( $res, false ); |
170 | |
171 | // Nothing to do |
172 | if ( !$rows ) { |
173 | return [ [], null ]; |
174 | } |
175 | |
176 | $lastValue = end( $rows )->$column; |
177 | if ( count( $rows ) < $limit ) { |
178 | return [ $rows, $lastValue ]; |
179 | } |
180 | |
181 | for ( $i = count( $rows ) - 1; $i >= 0; --$i ) { |
182 | if ( $rows[$i]->$column !== $lastValue ) { |
183 | break; |
184 | } |
185 | |
186 | unset( $rows[$i] ); |
187 | } |
188 | |
189 | // No more rows left |
190 | if ( !$rows ) { |
191 | return [ [], null ]; |
192 | } |
193 | |
194 | return [ $rows, end( $rows )->$column ]; |
195 | } |
196 | } |
197 | |
198 | // @codeCoverageIgnoreStart |
199 | $maintClass = PurgeChangedPages::class; |
200 | require_once RUN_MAINTENANCE_IF_MAIN; |
201 | // @codeCoverageIgnoreEnd |