| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\Rdbms\IResultWrapper; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class PurgeChangedPages extends Maintenance { |
| 41 |
|
| 42 |
public function __construct() { |
| 43 |
parent::__construct(); |
| 44 |
$this->addDescription( 'Send purge requests for edits in date range to squid/varnish' ); |
| 45 |
$this->addOption( 'starttime', 'Starting timestamp', true, true ); |
| 46 |
$this->addOption( 'endtime', 'Ending timestamp', true, true ); |
| 47 |
$this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true ); |
| 48 |
$this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true ); |
| 49 |
$this->addOption( 'dry-run', 'Do not send purge requests' ); |
| 50 |
$this->addOption( 'verbose', 'Show more output', false, false, 'v' ); |
| 51 |
$this->setBatchSize( 100 ); |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
global $wgHTCPRouting; |
| 56 |
|
| 57 |
if ( $this->hasOption( 'htcp-dest' ) ) { |
| 58 |
$parts = explode( ':', $this->getOption( 'htcp-dest' ), 2 ); |
| 59 |
if ( count( $parts ) < 2 ) { |
| 60 |
|
| 61 |
$parts[] = '4827'; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
$wgHTCPRouting = [ |
| 66 |
'' => [ 'host' => $parts[0], 'port' => $parts[1] ], |
| 67 |
]; |
| 68 |
if ( $this->hasOption( 'verbose' ) ) { |
| 69 |
$this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$dbr = $this->getReplicaDB(); |
| 74 |
$minTime = $dbr->timestamp( $this->getOption( 'starttime' ) ); |
| 75 |
$maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) ); |
| 76 |
|
| 77 |
if ( $maxTime < $minTime ) { |
| 78 |
$this->error( "\nERROR: starttime after endtime\n" ); |
| 79 |
$this->maybeHelp( true ); |
| 80 |
} |
| 81 |
|
| 82 |
$stuckCount = 0; |
| 83 |
while ( true ) { |
| 84 |
|
| 85 |
$bSize = ( $stuckCount + 1 ) * $this->getBatchSize(); |
| 86 |
|
| 87 |
$res = $dbr->newSelectQueryBuilder() |
| 88 |
->select( [ 'rev_timestamp', 'page_namespace', 'page_title', ] ) |
| 89 |
->from( 'revision' ) |
| 90 |
->join( 'page', null, 'rev_page=page_id' ) |
| 91 |
->where( [ |
| 92 |
$dbr->expr( 'rev_timestamp', '>', $minTime ), |
| 93 |
$dbr->expr( 'rev_timestamp', '<=', $maxTime ), |
| 94 |
] ) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
->andWhere( "page_latest = rev_id" ) |
| 99 |
->orderBy( 'rev_timestamp' ) |
| 100 |
->limit( $bSize ) |
| 101 |
->caller( __METHOD__ )->fetchResultSet(); |
| 102 |
|
| 103 |
if ( !$res->numRows() ) { |
| 104 |
|
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
[ $rows, $lastTime ] = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize ); |
| 110 |
if ( !count( $rows ) ) { |
| 111 |
++$stuckCount; |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$stuckCount = 0; |
| 116 |
|
| 117 |
$this->output( "Processing changes from {$minTime} to {$lastTime}.\n" ); |
| 118 |
|
| 119 |
|
| 120 |
$minTime = $lastTime; |
| 121 |
|
| 122 |
|
| 123 |
$urls = []; |
| 124 |
foreach ( $rows as $row ) { |
| 125 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 126 |
$urls[] = $title->getInternalURL(); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) { |
| 130 |
$this->output( implode( "\n", $urls ) . "\n" ); |
| 131 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
$hcu = $this->getServiceContainer()->getHtmlCacheUpdater(); |
| 138 |
$hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE ); |
| 139 |
|
| 140 |
if ( $this->hasOption( 'sleep-per-batch' ) ) { |
| 141 |
|
| 142 |
usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
$this->output( "Done!\n" ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected function pageableSortedRows( IResultWrapper $res, $column, $limit ) { |
| 169 |
$rows = iterator_to_array( $res, false ); |
| 170 |
|
| 171 |
|
| 172 |
if ( !$rows ) { |
| 173 |
return [ [], null ]; |
| 174 |
} |
| 175 |
|
| 176 |
$lastValue = end( $rows )->$column; |
| 177 |
if ( count( $rows ) < $limit ) { |
| 178 |
return [ $rows, $lastValue ]; |
| 179 |
} |
| 180 |
|
| 181 |
for ( $i = count( $rows ) - 1; $i >= 0; --$i ) { |
| 182 |
if ( $rows[$i]->$column !== $lastValue ) { |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
unset( $rows[$i] ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if ( !$rows ) { |
| 191 |
return [ [], null ]; |
| 192 |
} |
| 193 |
|
| 194 |
return [ $rows, end( $rows )->$column ]; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
$maintClass = PurgeChangedPages::class; |
| 200 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 201 |
|