MediaWiki master
purgeChangedPages.php
Go to the documentation of this file.
1<?php
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/Maintenance.php';
12// @codeCoverageIgnoreEnd
13
17
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
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
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;
186require_once RUN_MAINTENANCE_IF_MAIN;
187// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
maybeHelp( $force=false)
Maybe show the help.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
Maintenance script that sends purge requests for pages edited in a date range to squid/varnish.
execute()
Do the actual work.
__construct()
Default constructor.
pageableSortedRows(IResultWrapper $res, $column, $limit)
Remove all the rows in a result set with the highest value for column $column unless the number of ro...
$wgHTCPRouting
Config variable stub for the HTCPRouting setting, for use by phpdoc and IDEs.
Result wrapper for grabbing data queried from an IDatabase object.