MediaWiki  1.23.15
purgeChangedPages.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
37 
38  public function __construct() {
39  parent::__construct();
40  $this->mDescription = 'Send purge requests for edits in date range to squid/varnish';
41  $this->addOption( 'starttime', 'Starting timestamp', true, true );
42  $this->addOption( 'endtime', 'Ending timestamp', true, true );
43  $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
44  $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
45  $this->addOption( 'dry-run', 'Do not send purge requests' );
46  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
47  $this->setBatchSize( 100 );
48  }
49 
50  public function execute() {
51  global $wgHTCPRouting;
52 
53  if ( $this->hasOption( 'htcp-dest' ) ) {
54  $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
55  if ( count( $parts ) < 2 ) {
56  // Add default htcp port
57  $parts[] = '4827';
58  }
59 
60  // Route all HTCP messages to provided host:port
61  $wgHTCPRouting = array(
62  '' => array( 'host' => $parts[0], 'port' => $parts[1] ),
63  );
64  if ( $this->hasOption( 'verbose' ) ) {
65  $this->output( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
66  }
67  }
68 
69  $dbr = $this->getDB( DB_SLAVE );
70  $minTime = $dbr->timestamp( $this->getOption( 'starttime' ) );
71  $maxTime = $dbr->timestamp( $this->getOption( 'endtime' ) );
72 
73  if ( $maxTime < $minTime ) {
74  $this->error( "\nERROR: starttime after endtime\n" );
75  $this->maybeHelp( true );
76  }
77 
78  $stuckCount = 0; // loop breaker
79  while ( true ) {
80  // Adjust bach size if we are stuck in a second that had many changes
81  $bSize = $this->mBatchSize + ( $stuckCount * $this->mBatchSize );
82 
83  $res = $dbr->select(
84  array( 'page', 'revision' ),
85  array(
86  'rev_timestamp',
87  'page_namespace',
88  'page_title',
89  ),
90  array(
91  "rev_timestamp > " . $dbr->addQuotes( $minTime ),
92  "rev_timestamp <= " . $dbr->addQuotes( $maxTime ),
93  // Only get rows where the revision is the latest for the page.
94  // Other revisions would be duplicate and we don't need to purge if
95  // there has been an edit after the interesting time window.
96  "page_latest = rev_id",
97  ),
98  __METHOD__,
99  array( 'ORDER BY' => 'rev_timestamp', 'LIMIT' => $bSize ),
100  array(
101  'page' => array( 'INNER JOIN', 'rev_page=page_id' ),
102  )
103  );
104 
105  if ( !$res->numRows() ) {
106  // nothing more found so we are done
107  break;
108  }
109 
110  // Kludge to not get stuck in loops for batches with the same timestamp
111  list( $rows, $lastTime ) = $this->pageableSortedRows( $res, 'rev_timestamp', $bSize );
112  if ( !count( $rows ) ) {
113  ++$stuckCount;
114  continue;
115  }
116  // Reset suck counter
117  $stuckCount = 0;
118 
119  $this->output( "Processing changes from {$minTime} to {$lastTime}.\n" );
120 
121  // Advance past the last row next time
122  $minTime = $lastTime;
123 
124  // Create list of URLs from page_namespace + page_title
125  $urls = array();
126  foreach ( $rows as $row ) {
127  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
128  $urls[] = $title->getInternalURL();
129  }
130 
131  if ( $this->hasOption( 'dry-run' ) || $this->hasOption( 'verbose' ) ) {
132  $this->output( implode( "\n", $urls ) . "\n" );
133  if ( $this->hasOption( 'dry-run' ) ) {
134  continue;
135  }
136  }
137 
138  // Send batch of purge requests out to squids
139  $squid = new SquidUpdate( $urls, count( $urls ) );
140  $squid->doUpdate();
141 
142  if ( $this->hasOption( 'sleep-per-batch' ) ) {
143  // sleep-per-batch is milliseconds, usleep wants micro seconds.
144  usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
145  }
146  }
147 
148  $this->output( "Done!\n" );
149  }
150 
169  protected function pageableSortedRows( ResultWrapper $res, $column, $limit ) {
170  $rows = iterator_to_array( $res, false );
171  $count = count( $rows );
172  if ( !$count ) {
173  return array( array(), null ); // nothing to do
174  } elseif ( $count < $limit ) {
175  return array( $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  return array( $rows, $lastValueLeft );
187  }
188 }
189 
190 $maintClass = "PurgeChangedPages";
191 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:97
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:710
PurgeChangedPages\pageableSortedRows
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...
Definition: purgeChangedPages.php:169
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
PurgeChangedPages\execute
execute()
Do the actual work.
Definition: purgeChangedPages.php:50
PurgeChangedPages\__construct
__construct()
Default constructor.
Definition: purgeChangedPages.php:38
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$limit
if( $sleep) $limit
Definition: importImages.php:99
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
PurgeChangedPages
Maintenance script that sends purge requests for pages edited in a date range to squid/varnish.
Definition: purgeChangedPages.php:36
$dbr
$dbr
Definition: testCompression.php:48
Maintenance\getDB
& getDB( $db, $groups=array(), $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1007
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SquidUpdate
Handles purging appropriate Squid URLs given a title (or titles)
Definition: SquidUpdate.php:28
list
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
$maintClass
$maintClass
Definition: purgeChangedPages.php:190
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$count
$count
Definition: UtfNormalTest2.php:96
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
as
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
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
$res
$res
Definition: database.txt:21
ResultWrapper
Result wrapper for grabbing data queried by someone else.
Definition: DatabaseUtility.php:99
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:254