MediaWiki master
purgeChangedPages.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
30
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 // Add default htcp port
61 $parts[] = '4827';
62 }
63
64 // Route all HTCP messages to provided host:port
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 // Adjust bach size if we are stuck in a second that had many changes
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 // Only get rows where the revision is the latest for the page.
96 // Other revisions would be duplicate and we don't need to purge if
97 // there has been an edit after the interesting time window.
98 ->andWhere( "page_latest = rev_id" )
99 ->orderBy( 'rev_timestamp' )
100 ->limit( $bSize )
101 ->caller( __METHOD__ )->fetchResultSet();
102
103 if ( !$res->numRows() ) {
104 // nothing more found so we are done
105 break;
106 }
107
108 // Kludge to not get stuck in loops for batches with the same timestamp
109 [ $rows, $lastTime ] = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
110 if ( !count( $rows ) ) {
111 ++$stuckCount;
112 continue;
113 }
114 // Reset suck counter
115 $stuckCount = 0;
116
117 $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
118
119 // Advance past the last row next time
120 $minTime = $lastTime;
121
122 // Create list of URLs from page_namespace + page_title
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 // Send batch of purge requests out to CDN servers
137 $hcu = $this->getServiceContainer()->getHtmlCacheUpdater();
138 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
139
140 if ( $this->hasOption( 'sleep-per-batch' ) ) {
141 // sleep-per-batch is milliseconds, usleep wants micro seconds.
142 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
143 }
144 }
145
146 $this->output( "Done!\n" );
147 }
148
168 protected function pageableSortedRows( IResultWrapper $res, $column, $limit ) {
169 $rows = iterator_to_array( $res, false );
170
171 // Nothing to do
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 // No more rows left
190 if ( !$rows ) {
191 return [ [], null ];
192 }
193
194 return [ $rows, end( $rows )->$column ];
195 }
196}
197
198// @codeCoverageIgnoreStart
199$maintClass = PurgeChangedPages::class;
200require_once RUN_MAINTENANCE_IF_MAIN;
201// @codeCoverageIgnoreEnd
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.
getServiceContainer()
Returns the main service container.
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)
Represents a title within MediaWiki.
Definition Title.php:78
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.