MediaWiki REL1_40
purgeChangedPages.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
29
40
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' );
44 $this->addOption( 'starttime', 'Starting timestamp', true, true );
45 $this->addOption( 'endtime', 'Ending timestamp', true, true );
46 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
47 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
48 $this->addOption( 'dry-run', 'Do not send purge requests' );
49 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
50 $this->setBatchSize( 100 );
51 }
52
53 public function execute() {
54 global $wgHTCPRouting;
55
56 if ( $this->hasOption( 'htcp-dest' ) ) {
57 $parts = explode( ':', $this->getOption( 'htcp-dest' ), 2 );
58 if ( count( $parts ) < 2 ) {
59 // Add default htcp port
60 $parts[] = '4827';
61 }
62
63 // Route all HTCP messages to provided host:port
65 '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
66 ];
67 if ( $this->hasOption( 'verbose' ) ) {
68 $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
69 }
70 }
71
72 $dbr = $this->getDB( DB_REPLICA );
73 $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
74 $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
75
76 if ( $maxTime < $minTime ) {
77 $this->error( "\nERROR: starttime after endtime\n" );
78 $this->maybeHelp( true );
79 }
80
81 $stuckCount = 0;
82 while ( true ) {
83 // Adjust bach size if we are stuck in a second that had many changes
84 $bSize = ( $stuckCount + 1 ) * $this->getBatchSize();
85
86 $res = $dbr->select(
87 [ 'page', 'revision' ],
88 [
89 'rev_timestamp',
90 'page_namespace',
91 'page_title',
92 ],
93 [
94 "rev_timestamp > " . $dbr->addQuotes( $minTime ),
95 "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
96 // Only get rows where the revision is the latest for the page.
97 // Other revisions would be duplicate and we don't need to purge if
98 // there has been an edit after the interesting time window.
99 "page_latest = rev_id",
100 ],
101 __METHOD__,
102 [ 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ],
103 [
104 'page' => [ 'JOIN', 'rev_page=page_id' ],
105 ]
106 );
107
108 if ( !$res->numRows() ) {
109 // nothing more found so we are done
110 break;
111 }
112
113 // Kludge to not get stuck in loops for batches with the same timestamp
114 [ $rows, $lastTime ] = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
115 if ( !count( $rows ) ) {
116 ++$stuckCount;
117 continue;
118 }
119 // Reset suck counter
120 $stuckCount = 0;
121
122 $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
123
124 // Advance past the last row next time
125 $minTime = $lastTime;
126
127 // Create list of URLs from page_namespace + page_title
128 $urls = [];
129 foreach ( $rows as $row ) {
130 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
131 $urls[] = $title->getInternalURL();
132 }
133
134 if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
135 $this->output( implode( "\n", $urls ) . "\n" );
136 if ( $this->hasOption( 'dry-run' ) ) {
137 continue;
138 }
139 }
140
141 // Send batch of purge requests out to CDN servers
142 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
143 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
144
145 if ( $this->hasOption( 'sleep-per-batch' ) ) {
146 // sleep-per-batch is milliseconds, usleep wants micro seconds.
147 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
148 }
149 }
150
151 $this->output( "Done!\n" );
152 }
153
173 protected function pageableSortedRows( IResultWrapper $res, $column, $limit ) {
174 $rows = iterator_to_array( $res, false );
175
176 // Nothing to do
177 if ( !$rows ) {
178 return [ [], null ];
179 }
180
181 $lastValue = end( $rows )->$column;
182 if ( count( $rows ) < $limit ) {
183 return [ $rows, $lastValue ];
184 }
185
186 for ( $i = count( $rows ) - 1; $i >= 0; --$i ) {
187 if ( $rows[$i]->$column !== $lastValue ) {
188 break;
189 }
190
191 unset( $rows[$i] );
192 }
193
194 // No more rows left
195 if ( !$rows ) {
196 return [ [], null ];
197 }
198
199 return [ $rows, end( $rows )->$column ];
200 }
201}
202
203$maintClass = PurgeChangedPages::class;
204require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
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.
const DB_REPLICA
Definition defines.php:26