Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / purgeChangedPages.php
blobb354399f8844bd3c41409afdb38c153428ce3bc0
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 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that sends purge requests for pages edited in a date
28 * range to squid/varnish.
30 * Can be used to recover from an HTCP message partition or other major cache
31 * layer interruption.
33 * @ingroup Maintenance
35 class PurgeChangedPages extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' );
40 $this->addOption( 'starttime', 'Starting timestamp', true, true );
41 $this->addOption( 'endtime', 'Ending timestamp', true, true );
42 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
43 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
44 $this->addOption( 'dry-run', 'Do not send purge requests' );
45 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
46 $this->setBatchSize( 100 );
49 public function execute() {
50 global $wgHTCPRouting;
52 if ( $this->hasOption( 'htcp-dest' ) ) {
53 $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
54 if ( count( $parts ) < 2 ) {
55 // Add default htcp port
56 $parts[] = '4827';
59 // Route all HTCP messages to provided host:port
60 $wgHTCPRouting = [
61 '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
63 if ( $this->hasOption( 'verbose' ) ) {
64 $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
68 $dbr = $this->getDB( DB_REPLICA );
69 $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
70 $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
72 if ( $maxTime < $minTime ) {
73 $this->error( "\nERROR: starttime after endtime\n" );
74 $this->maybeHelp( true );
77 $stuckCount = 0; // loop breaker
78 while ( true ) {
79 // Adjust bach size if we are stuck in a second that had many changes
80 $bSize = $this->mBatchSize + ( $stuckCount * $this->mBatchSize );
82 $res = $dbr->select(
83 [ 'page', 'revision' ],
85 'rev_timestamp',
86 'page_namespace',
87 'page_title',
90 "rev_timestamp > " . $dbr->addQuotes( $minTime ),
91 "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
92 // Only get rows where the revision is the latest for the page.
93 // Other revisions would be duplicate and we don't need to purge if
94 // there has been an edit after the interesting time window.
95 "page_latest = rev_id",
97 __METHOD__,
98 [ 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ],
100 'page' => [ 'INNER JOIN', 'rev_page=page_id' ],
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 list( $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 squids
138 $squid = new CdnCacheUpdate( $urls, count( $urls ) );
139 $squid->doUpdate();
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 ResultWrapper $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( ResultWrapper $res, $column, $limit ) {
170 $rows = iterator_to_array( $res, false );
171 $count = count( $rows );
172 if ( !$count ) {
173 return [ [], null ]; // nothing to do
174 } elseif ( $count < $limit ) {
175 return [ $rows, $rows[$count - 1]->$column ]; // no more rows left
177 $lastValue = $rows[$count - 1]->$column; // should be the highest
178 for ( $i = $count - 1; $i >= 0; --$i ) {
179 if ( $rows[$i]->$column === $lastValue ) {
180 unset( $rows[$i] );
181 } else {
182 break;
185 $lastValueLeft = count( $rows ) ? $rows[count( $rows ) - 1]->$column : null;
187 return [ $rows, $lastValueLeft ];
191 $maintClass = "PurgeChangedPages";
192 require_once RUN_MAINTENANCE_IF_MAIN;