Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / maintenance / purgeChangedPages.php
blobcf65c693153a24d83d882137d961b21ad0b913fb
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 use Wikimedia\Rdbms\ResultWrapper;
28 /**
29 * Maintenance script that sends purge requests for pages edited in a date
30 * range to squid/varnish.
32 * Can be used to recover from an HTCP message partition or other major cache
33 * layer interruption.
35 * @ingroup Maintenance
37 class PurgeChangedPages extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' );
42 $this->addOption( 'starttime', 'Starting timestamp', true, true );
43 $this->addOption( 'endtime', 'Ending timestamp', true, true );
44 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
45 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
46 $this->addOption( 'dry-run', 'Do not send purge requests' );
47 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
48 $this->setBatchSize( 100 );
51 public function execute() {
52 global $wgHTCPRouting;
54 if ( $this->hasOption( 'htcp-dest' ) ) {
55 $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
56 if ( count( $parts ) < 2 ) {
57 // Add default htcp port
58 $parts[] = '4827';
61 // Route all HTCP messages to provided host:port
62 $wgHTCPRouting = [
63 '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
65 if ( $this->hasOption( 'verbose' ) ) {
66 $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
70 $dbr = $this->getDB( DB_REPLICA );
71 $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
72 $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
74 if ( $maxTime < $minTime ) {
75 $this->error( "\nERROR: starttime after endtime\n" );
76 $this->maybeHelp( true );
79 $stuckCount = 0; // loop breaker
80 while ( true ) {
81 // Adjust bach size if we are stuck in a second that had many changes
82 $bSize = $this->mBatchSize + ( $stuckCount * $this->mBatchSize );
84 $res = $dbr->select(
85 [ 'page', 'revision' ],
87 'rev_timestamp',
88 'page_namespace',
89 'page_title',
92 "rev_timestamp > " . $dbr->addQuotes( $minTime ),
93 "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
94 // Only get rows where the revision is the latest for the page.
95 // Other revisions would be duplicate and we don't need to purge if
96 // there has been an edit after the interesting time window.
97 "page_latest = rev_id",
99 __METHOD__,
100 [ 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ],
102 'page' => [ 'INNER JOIN', 'rev_page=page_id' ],
106 if ( !$res->numRows() ) {
107 // nothing more found so we are done
108 break;
111 // Kludge to not get stuck in loops for batches with the same timestamp
112 list( $rows, $lastTime ) = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
113 if ( !count( $rows ) ) {
114 ++$stuckCount;
115 continue;
117 // Reset suck counter
118 $stuckCount = 0;
120 $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
122 // Advance past the last row next time
123 $minTime = $lastTime;
125 // Create list of URLs from page_namespace + page_title
126 $urls = [];
127 foreach ( $rows as $row ) {
128 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
129 $urls[] = $title->getInternalURL();
132 if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
133 $this->output( implode( "\n", $urls ) . "\n" );
134 if ( $this->hasOption( 'dry-run' ) ) {
135 continue;
139 // Send batch of purge requests out to squids
140 $squid = new CdnCacheUpdate( $urls, count( $urls ) );
141 $squid->doUpdate();
143 if ( $this->hasOption( 'sleep-per-batch' ) ) {
144 // sleep-per-batch is milliseconds, usleep wants micro seconds.
145 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
149 $this->output( "Done!\n" );
153 * Remove all the rows in a result set with the highest value for column
154 * $column unless the number of rows is less $limit. This returns the new
155 * array of rows and the highest value of column $column for the rows left.
156 * The ordering of rows is maintained.
158 * This is useful for paging on mostly-unique values that may sometimes
159 * have large clumps of identical values. It should be safe to do the next
160 * query on items with a value higher than the highest of the rows returned here.
161 * If this returns an empty array for a non-empty query result, then all the rows
162 * had the same column value and the query should be repeated with a higher LIMIT.
164 * @todo move this elsewhere
166 * @param ResultWrapper $res Query result sorted by $column (ascending)
167 * @param string $column
168 * @param int $limit
169 * @return array (array of rows, string column value)
171 protected function pageableSortedRows( ResultWrapper $res, $column, $limit ) {
172 $rows = iterator_to_array( $res, false );
173 $count = count( $rows );
174 if ( !$count ) {
175 return [ [], null ]; // nothing to do
176 } elseif ( $count < $limit ) {
177 return [ $rows, $rows[$count - 1]->$column ]; // no more rows left
179 $lastValue = $rows[$count - 1]->$column; // should be the highest
180 for ( $i = $count - 1; $i >= 0; --$i ) {
181 if ( $rows[$i]->$column === $lastValue ) {
182 unset( $rows[$i] );
183 } else {
184 break;
187 $lastValueLeft = count( $rows ) ? $rows[count( $rows ) - 1]->$column : null;
189 return [ $rows, $lastValueLeft ];
193 $maintClass = "PurgeChangedPages";
194 require_once RUN_MAINTENANCE_IF_MAIN;