MediaWiki  1.34.0
purgeList.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class PurgeList extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Send purge requests for listed pages to CDN' );
35  $this->addOption( 'purge', 'Whether to update page_touched.', false, false );
36  $this->addOption( 'namespace', 'Namespace number', false, true );
37  $this->addOption( 'all', 'Purge all pages', false, false );
38  $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
39  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
40  $this->setBatchSize( 100 );
41  }
42 
43  public function execute() {
44  if ( $this->hasOption( 'all' ) ) {
45  $this->purgeNamespace( false );
46  } elseif ( $this->hasOption( 'namespace' ) ) {
47  $this->purgeNamespace( intval( $this->getOption( 'namespace' ) ) );
48  } else {
49  $this->doPurge();
50  }
51  $this->output( "Done!\n" );
52  }
53 
57  private function doPurge() {
58  $stdin = $this->getStdin();
59  $urls = [];
60 
61  while ( !feof( $stdin ) ) {
62  $page = trim( fgets( $stdin ) );
63  if ( preg_match( '%^https?://%', $page ) ) {
64  $urls[] = $page;
65  } elseif ( $page !== '' ) {
66  $title = Title::newFromText( $page );
67  if ( $title ) {
68  $newUrls = $title->getCdnUrls();
69 
70  foreach ( $newUrls as $url ) {
71  $this->output( "$url\n" );
72  }
73 
74  $urls = array_merge( $urls, $newUrls );
75 
76  if ( $this->getOption( 'purge' ) ) {
77  $title->invalidateCache();
78  }
79  } else {
80  $this->output( "(Invalid title '$page')\n" );
81  }
82  }
83  }
84  $this->output( "Purging " . count( $urls ) . " urls\n" );
85  $this->sendPurgeRequest( $urls );
86  }
87 
93  private function purgeNamespace( $namespace = false ) {
94  $dbr = $this->getDB( DB_REPLICA );
95  $startId = 0;
96  if ( $namespace === false ) {
97  $conds = [];
98  } else {
99  $conds = [ 'page_namespace' => $namespace ];
100  }
101  while ( true ) {
102  $res = $dbr->select( 'page',
103  [ 'page_id', 'page_namespace', 'page_title' ],
104  $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
105  __METHOD__,
106  [
107  'LIMIT' => $this->getBatchSize(),
108  'ORDER BY' => 'page_id'
109 
110  ]
111  );
112  if ( !$res->numRows() ) {
113  break;
114  }
115  $urls = [];
116  foreach ( $res as $row ) {
117  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
118  $urls = array_merge( $urls, $title->getCdnUrls() );
119  $startId = $row->page_id;
120  }
121  $this->sendPurgeRequest( $urls );
122  }
123  }
124 
129  private function sendPurgeRequest( $urls ) {
130  if ( $this->hasOption( 'delay' ) ) {
131  $delay = floatval( $this->getOption( 'delay' ) );
132  foreach ( $urls as $url ) {
133  if ( $this->hasOption( 'verbose' ) ) {
134  $this->output( $url . "\n" );
135  }
136  $u = new CdnCacheUpdate( [ $url ] );
137  $u->doUpdate();
138  usleep( $delay * 1e6 );
139  }
140  } else {
141  if ( $this->hasOption( 'verbose' ) ) {
142  $this->output( implode( "\n", $urls ) . "\n" );
143  }
144  $u = new CdnCacheUpdate( $urls );
145  $u->doUpdate();
146  }
147  }
148 }
149 
150 $maintClass = PurgeList::class;
151 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:426
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$maintClass
$maintClass
Definition: purgeList.php:150
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
$dbr
$dbr
Definition: testCompression.php:50
PurgeList
Maintenance script that sends purge requests for listed pages to CDN.
Definition: purgeList.php:31
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
PurgeList\execute
execute()
Do the actual work.
Definition: purgeList.php:43
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
PurgeList\doPurge
doPurge()
Purge URL coming from stdin.
Definition: purgeList.php:57
PurgeList\__construct
__construct()
Default constructor.
Definition: purgeList.php:32
CdnCacheUpdate
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
Definition: CdnCacheUpdate.php:30
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
PurgeList\purgeNamespace
purgeNamespace( $namespace=false)
Purge a namespace or all pages.
Definition: purgeList.php:93
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
PurgeList\sendPurgeRequest
sendPurgeRequest( $urls)
Helper to purge an array of $urls.
Definition: purgeList.php:129
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394