MediaWiki  master
purgeList.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class PurgeList extends Maintenance {
35  private $namespaceId;
37  private $allNamespaces;
39  private $doDbTouch;
41  private $delay;
42 
43  public function __construct() {
44  parent::__construct();
45  $this->addDescription( "Send purge requests for listed pages to CDN.\n"
46  . "By default this expects a list of URLs or page names from STDIN. "
47  . "To query the database for input, use --namespace or --all-namespaces instead."
48  );
49  $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
50  $this->addOption( 'all-namespace', 'Purge pages in all namespaces', false, false );
51  $this->addOption( 'db-touch', 'Update the page.page_touched database field', false, false );
52  $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
53  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
54  $this->setBatchSize( 100 );
55  }
56 
57  public function execute() {
58  $this->namespaceId = $this->getOption( 'namespace' );
59  $this->allNamespaces = $this->hasOption( 'all-namespaces' );
60  $this->doDbTouch = $this->hasOption( 'db-touch' );
61  $this->delay = floatval( $this->getOption( 'delay', '0' ) );
62 
63  $conf = $this->getConfig();
64  if ( ( $this->namespaceId !== null || $this->allNamespaces )
65  && $this->doDbTouch
66  && $conf->get( 'MiserMode' )
67  ) {
68  $this->fatalError( 'Prevented mass db-invalidation (MiserMode is enabled).' );
69  }
70 
71  if ( $this->allNamespaces ) {
72  $this->purgeNamespace( false );
73  } elseif ( $this->namespaceId !== null ) {
74  $this->purgeNamespace( intval( $this->namespaceId ) );
75  } else {
76  $this->doPurge();
77  }
78  $this->output( "Done!\n" );
79  }
80 
84  private function doPurge() {
85  $stdin = $this->getStdin();
86  $urls = [];
87 
88  while ( !feof( $stdin ) ) {
89  $page = trim( fgets( $stdin ) );
90  if ( preg_match( '%^https?://%', $page ) ) {
91  $urls[] = $page;
92  } elseif ( $page !== '' ) {
93  $title = Title::newFromText( $page );
94  if ( $title ) {
95  $newUrls = $title->getCdnUrls();
96 
97  foreach ( $newUrls as $url ) {
98  $this->output( "$url\n" );
99  }
100 
101  $urls = array_merge( $urls, $newUrls );
102 
103  if ( $this->doDbTouch ) {
104  $title->invalidateCache();
105  }
106  } else {
107  $this->output( "(Invalid title '$page')\n" );
108  }
109  }
110  }
111  $this->output( "Purging " . count( $urls ) . " urls\n" );
112  $this->sendPurgeRequest( $urls );
113  }
114 
120  private function purgeNamespace( $namespace = false ) {
121  $dbr = $this->getDB( DB_REPLICA );
122  $startId = 0;
123  if ( $namespace === false ) {
124  $conds = [];
125  } else {
126  $conds = [ 'page_namespace' => $namespace ];
127  }
128  while ( true ) {
129  $res = $dbr->select( 'page',
130  [ 'page_id', 'page_namespace', 'page_title' ],
131  $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
132  __METHOD__,
133  [
134  'LIMIT' => $this->getBatchSize(),
135  'ORDER BY' => 'page_id'
136 
137  ]
138  );
139  if ( !$res->numRows() ) {
140  break;
141  }
142  $urls = [];
143  foreach ( $res as $row ) {
144  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
145  $urls = array_merge( $urls, $title->getCdnUrls() );
146  $startId = $row->page_id;
147  }
148  $this->sendPurgeRequest( $urls );
149  }
150  }
151 
156  private function sendPurgeRequest( $urls ) {
157  $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
158  if ( $this->delay > 0 ) {
159  foreach ( $urls as $url ) {
160  if ( $this->hasOption( 'verbose' ) ) {
161  $this->output( $url . "\n" );
162  }
163  $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
164  usleep( $this->delay * 1e6 );
165  }
166  } else {
167  if ( $this->hasOption( 'verbose' ) ) {
168  $this->output( implode( "\n", $urls ) . "\n" );
169  }
170  $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
171  }
172  }
173 }
174 
175 $maintClass = PurgeList::class;
176 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
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:329
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:406
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
PurgeList\$doDbTouch
bool $doDbTouch
Definition: purgeList.php:39
$maintClass
$maintClass
Definition: purgeList.php:175
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:596
PurgeList
Maintenance script that sends purge requests for listed pages to CDN.
Definition: purgeList.php:33
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
PurgeList\execute
execute()
Do the actual work.
Definition: purgeList.php:57
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
PurgeList\doPurge
doPurge()
Purge URL coming from stdin.
Definition: purgeList.php:84
PurgeList\__construct
__construct()
Default constructor.
Definition: purgeList.php:43
PurgeList\$namespaceId
string null $namespaceId
Definition: purgeList.php:35
PurgeList\$allNamespaces
bool $allNamespaces
Definition: purgeList.php:37
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
PurgeList\purgeNamespace
purgeNamespace( $namespace=false)
Purge a namespace or all pages.
Definition: purgeList.php:120
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
PurgeList\sendPurgeRequest
sendPurgeRequest( $urls)
Helper to purge an array of $urls.
Definition: purgeList.php:156
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:374
PurgeList\$delay
float $delay
Definition: purgeList.php:41