MediaWiki  1.28.0
purgeChangedPages.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
36 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Send purge requests for edits in date range to squid/varnish' );
40  $this->addOption( 'starttime', 'Starting timestamp', true, true );
41  $this->addOption( 'endtime', 'Ending timestamp', true, true );
42  $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
43  $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
44  $this->addOption( 'dry-run', 'Do not send purge requests' );
45  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
46  $this->setBatchSize( 100 );
47  }
48 
49  public function execute() {
51 
52  if ( $this->hasOption( 'htcp-dest' ) ) {
53  $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
54  if ( count( $parts ) < 2 ) {
55  // Add default htcp port
56  $parts[] = '4827';
57  }
58 
59  // Route all HTCP messages to provided host:port
60  $wgHTCPRouting = [
61  '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
62  ];
63  if ( $this->hasOption( 'verbose' ) ) {
64  $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
65  }
66  }
67 
68  $dbr = $this->getDB( DB_REPLICA );
69  $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
70  $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
71 
72  if ( $maxTime < $minTime ) {
73  $this->error( "\nERROR: starttime after endtime\n" );
74  $this->maybeHelp( true );
75  }
76 
77  $stuckCount = 0; // loop breaker
78  while ( true ) {
79  // Adjust bach size if we are stuck in a second that had many changes
80  $bSize = $this->mBatchSize + ( $stuckCount * $this->mBatchSize );
81 
82  $res = $dbr->select(
83  [ 'page', 'revision' ],
84  [
85  'rev_timestamp',
86  'page_namespace',
87  'page_title',
88  ],
89  [
90  "rev_timestamp > " . $dbr->addQuotes( $minTime ),
91  "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
92  // Only get rows where the revision is the latest for the page.
93  // Other revisions would be duplicate and we don't need to purge if
94  // there has been an edit after the interesting time window.
95  "page_latest = rev_id",
96  ],
97  __METHOD__,
98  [ 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ],
99  [
100  'page' => [ 'INNER JOIN', 'rev_page=page_id' ],
101  ]
102  );
103 
104  if ( !$res->numRows() ) {
105  // nothing more found so we are done
106  break;
107  }
108 
109  // Kludge to not get stuck in loops for batches with the same timestamp
110  list( $rows, $lastTime ) = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
111  if ( !count( $rows ) ) {
112  ++$stuckCount;
113  continue;
114  }
115  // Reset suck counter
116  $stuckCount = 0;
117 
118  $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
119 
120  // Advance past the last row next time
121  $minTime = $lastTime;
122 
123  // Create list of URLs from page_namespace + page_title
124  $urls = [];
125  foreach ( $rows as $row ) {
126  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
127  $urls[] = $title->getInternalURL();
128  }
129 
130  if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
131  $this->output( implode( "\n", $urls ) . "\n" );
132  if ( $this->hasOption( 'dry-run' ) ) {
133  continue;
134  }
135  }
136 
137  // Send batch of purge requests out to squids
138  $squid = new CdnCacheUpdate( $urls, count( $urls ) );
139  $squid->doUpdate();
140 
141  if ( $this->hasOption( 'sleep-per-batch' ) ) {
142  // sleep-per-batch is milliseconds, usleep wants micro seconds.
143  usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
144  }
145  }
146 
147  $this->output( "Done!\n" );
148  }
149 
169  protected function pageableSortedRows( ResultWrapper $res, $column, $limit ) {
170  $rows = iterator_to_array( $res, false );
171  $count = count( $rows );
172  if ( !$count ) {
173  return [ [], null ]; // nothing to do
174  } elseif ( $count < $limit ) {
175  return [ $rows, $rows[$count - 1]->$column ]; // no more rows left
176  }
177  $lastValue = $rows[$count - 1]->$column; // should be the highest
178  for ( $i = $count - 1; $i >= 0; --$i ) {
179  if ( $rows[$i]->$column === $lastValue ) {
180  unset( $rows[$i] );
181  } else {
182  break;
183  }
184  }
185  $lastValueLeft = count( $rows ) ? $rows[count( $rows ) - 1]->$column : null;
186 
187  return [ $rows, $lastValueLeft ];
188  }
189 }
190 
191 $maintClass = "PurgeChangedPages";
192 require_once RUN_MAINTENANCE_IF_MAIN;
pageableSortedRows(ResultWrapper $res, $column, $limit)
Remove all the rows in a result set with the highest value for column $column unless the number of ro...
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Handles purging appropriate CDN URLs given a title (or titles)
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption($name)
Checks to see if a particular param exists.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$res
Definition: database.txt:21
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
addDescription($text)
Set the description text.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getOption($name, $default=null)
Get an option, or return the default.
output($out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
maybeHelp($force=false)
Maybe show the help.
int $mBatchSize
Batch size.
Definition: Maintenance.php:98
error($err, $die=0)
Throw an error to the user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
$count
const DB_REPLICA
Definition: defines.php:22
Result wrapper for grabbing data queried from an IDatabase object.
setBatchSize($s=0)
Set the batch size.
$wgHTCPRouting
Routing configuration for HTCP multicast purging.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
Maintenance script that sends purge requests for pages edited in a date range to squid/varnish.