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