Merge "Make update.php file executable"
[mediawiki.git] / maintenance / purgeChangedPages.php
blobf0b6ec7ab7058322726ae0e64a09272dc75484a3
1 <?php
2 /**
3 * Send purge requests for pages edited in date range to squid/varnish.
5 * @section LICENSE
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Maintenance script that sends purge requests for pages edited in a date
29 * range to squid/varnish.
31 * Can be used to recover from an HTCP message partition or other major cache
32 * layer interruption.
34 * @ingroup Maintenance
36 class PurgeChangedPages extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->mDescription = 'Send purge requests for edits in date range to squid/varnish';
41 $this->addOption( 'starttime', 'Starting timestamp', true, true );
42 $this->addOption( 'endtime', 'Ending timestamp', true, true );
43 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
44 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
45 $this->addOption( 'dry-run', 'Do not send purge requests' );
46 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
47 $this->setBatchSize( 100 );
50 public function execute() {
51 global $wgHTCPRouting;
53 if ( $this->hasOption( 'htcp-dest' ) ) {
54 $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
55 if ( count( $parts ) < 2 ) {
56 // Add default htcp port
57 $parts[] = '4827';
60 // Route all HTCP messages to provided host:port
61 $wgHTCPRouting = array(
62 '' => array( 'host' => $parts[0], 'port' => $parts[1] ),
64 if ( $this->hasOption( 'verbose' ) ) {
65 $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
69 $dbr = $this->getDB( DB_SLAVE );
70 $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
71 $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
73 if ( $maxTime < $minTime ) {
74 $this->error( "\nERROR: starttime after endtime\n" );
75 $this->maybeHelp( true );
78 $stuckCount = 0; // loop breaker
79 while ( true ) {
80 // Adjust bach size if we are stuck in a second that had many changes
81 $bSize = $this->mBatchSize + ( $stuckCount * $this->mBatchSize );
83 $res = $dbr->select(
84 array( 'page', 'revision' ),
85 array(
86 'rev_timestamp',
87 'page_namespace',
88 'page_title',
90 array(
91 "rev_timestamp > " . $dbr->addQuotes( $minTime ),
92 "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
93 // Only get rows where the revision is the latest for the page.
94 // Other revisions would be duplicate and we don't need to purge if
95 // there has been an edit after the interesting time window.
96 "page_latest = rev_id",
98 __METHOD__,
99 array( 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ),
100 array(
101 'page' => array( 'INNER JOIN', 'rev_page=page_id' ),
105 if ( !$res->numRows() ) {
106 // nothing more found so we are done
107 break;
110 // Kludge to not get stuck in loops for batches with the same timestamp
111 list( $rows, $lastTime ) = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
112 if ( !count( $rows ) ) {
113 ++$stuckCount;
114 continue;
116 // Reset suck counter
117 $stuckCount = 0;
119 $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
121 // Advance past the last row next time
122 $minTime = $lastTime;
124 // Create list of URLs from page_namespace + page_title
125 $urls = array();
126 foreach ( $rows as $row ) {
127 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
128 $urls[] = $title->getInternalURL();
131 if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
132 $this->output( implode( "\n", $urls ) . "\n" );
133 if ( $this->hasOption( 'dry-run' ) ) {
134 continue;
138 // Send batch of purge requests out to squids
139 $squid = new SquidUpdate( $urls, count( $urls ) );
140 $squid->doUpdate();
142 if ( $this->hasOption( 'sleep-per-batch' ) ) {
143 // sleep-per-batch is milliseconds, usleep wants micro seconds.
144 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
148 $this->output( "Done!\n" );
152 * Remove all the rows in a result set with the highest value for column
153 * $column unless the number of rows is less $limit. This returns the new
154 * array of rows and the highest value of column $column for the rows left.
155 * The ordering of rows is maintained.
157 * This is useful for paging on mostly-unique values that may sometimes
158 * have large clumps of identical values. It should be safe to do the next
159 * query on items with a value higher than the highest of the rows returned here.
160 * If this returns an empty array for a non-empty query result, then all the rows
161 * had the same column value and the query should be repeated with a higher LIMIT.
163 * @TODO: move this elsewhere
165 * @param ResultWrapper $res Query result sorted by $column (ascending)
166 * @param string $column
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 array( array(), null ); // nothing to do
174 } elseif ( $count < $limit ) {
175 return array( $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 array( $rows, $lastValueLeft );
191 $maintClass = "PurgeChangedPages";
192 require_once RUN_MAINTENANCE_IF_MAIN;