Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / purgeChangedPages.php
blobd4d2928a57aa2ad7966274087d841430fc0c241c
1 <?php
2 /**
3 * Send purge requests for pages edited in date range to squid/varnish.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
28 use MediaWiki\Maintenance\Maintenance;
29 use MediaWiki\Title\Title;
30 use Wikimedia\Rdbms\IResultWrapper;
32 /**
33 * Maintenance script that sends purge requests for pages edited in a date
34 * range to squid/varnish.
36 * Can be used to recover from an HTCP message partition or other major cache
37 * layer interruption.
39 * @ingroup Maintenance
41 class PurgeChangedPages extends Maintenance {
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' );
46 $this->addOption( 'starttime', 'Starting timestamp', true, true );
47 $this->addOption( 'endtime', 'Ending timestamp', true, true );
48 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
49 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
50 $this->addOption( 'dry-run', 'Do not send purge requests' );
51 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
52 $this->setBatchSize( 100 );
55 public function execute() {
56 global $wgHTCPRouting;
58 if ( $this->hasOption( 'htcp-dest' ) ) {
59 $parts = explode( ':', $this->getOption( 'htcp-dest' ), 2 );
60 if ( count( $parts ) < 2 ) {
61 // Add default htcp port
62 $parts[] = '4827';
65 // Route all HTCP messages to provided host:port
66 $wgHTCPRouting = [
67 '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
69 if ( $this->hasOption( 'verbose' ) ) {
70 $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
74 $dbr = $this->getReplicaDB();
75 $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
76 $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
78 if ( $maxTime < $minTime ) {
79 $this->error( "\nERROR: starttime after endtime\n" );
80 $this->maybeHelp( true );
83 $stuckCount = 0;
84 while ( true ) {
85 // Adjust bach size if we are stuck in a second that had many changes
86 $bSize = ( $stuckCount + 1 ) * $this->getBatchSize();
88 $res = $dbr->newSelectQueryBuilder()
89 ->select( [ 'rev_timestamp', 'page_namespace', 'page_title', ] )
90 ->from( 'revision' )
91 ->join( 'page', null, 'rev_page=page_id' )
92 ->where( [
93 $dbr->expr( 'rev_timestamp', '>', $minTime ),
94 $dbr->expr( 'rev_timestamp', '<=', $maxTime ),
95 ] )
96 // Only get rows where the revision is the latest for the page.
97 // Other revisions would be duplicate and we don't need to purge if
98 // there has been an edit after the interesting time window.
99 ->andWhere( "page_latest = rev_id" )
100 ->orderBy( 'rev_timestamp' )
101 ->limit( $bSize )
102 ->caller( __METHOD__ )->fetchResultSet();
104 if ( !$res->numRows() ) {
105 // nothing more found so we are done
106 break;
109 // Kludge to not get stuck in loops for batches with the same timestamp
110 [ $rows, $lastTime ] = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
111 if ( !count( $rows ) ) {
112 ++$stuckCount;
113 continue;
115 // Reset suck counter
116 $stuckCount = 0;
118 $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
120 // Advance past the last row next time
121 $minTime = $lastTime;
123 // Create list of URLs from page_namespace + page_title
124 $urls = [];
125 foreach ( $rows as $row ) {
126 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
127 $urls[] = $title->getInternalURL();
130 if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
131 $this->output( implode( "\n", $urls ) . "\n" );
132 if ( $this->hasOption( 'dry-run' ) ) {
133 continue;
137 // Send batch of purge requests out to CDN servers
138 $hcu = $this->getServiceContainer()->getHtmlCacheUpdater();
139 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
141 if ( $this->hasOption( 'sleep-per-batch' ) ) {
142 // sleep-per-batch is milliseconds, usleep wants micro seconds.
143 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
147 $this->output( "Done!\n" );
151 * Remove all the rows in a result set with the highest value for column
152 * $column unless the number of rows is less $limit. This returns the new
153 * array of rows and the highest value of column $column for the rows left.
154 * The ordering of rows is maintained.
156 * This is useful for paging on mostly-unique values that may sometimes
157 * have large clumps of identical values. It should be safe to do the next
158 * query on items with a value higher than the highest of the rows returned here.
159 * If this returns an empty array for a non-empty query result, then all the rows
160 * had the same column value and the query should be repeated with a higher LIMIT.
162 * @todo move this elsewhere
164 * @param IResultWrapper $res Query result sorted by $column (ascending)
165 * @param string $column
166 * @param int $limit
167 * @return array (array of rows, string column value)
169 protected function pageableSortedRows( IResultWrapper $res, $column, $limit ) {
170 $rows = iterator_to_array( $res, false );
172 // Nothing to do
173 if ( !$rows ) {
174 return [ [], null ];
177 $lastValue = end( $rows )->$column;
178 if ( count( $rows ) < $limit ) {
179 return [ $rows, $lastValue ];
182 for ( $i = count( $rows ) - 1; $i >= 0; --$i ) {
183 if ( $rows[$i]->$column !== $lastValue ) {
184 break;
187 unset( $rows[$i] );
190 // No more rows left
191 if ( !$rows ) {
192 return [ [], null ];
195 return [ $rows, end( $rows )->$column ];
199 // @codeCoverageIgnoreStart
200 $maintClass = PurgeChangedPages::class;
201 require_once RUN_MAINTENANCE_IF_MAIN;
202 // @codeCoverageIgnoreEnd