MediaWiki REL1_35
purgeList.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
33class PurgeList extends Maintenance {
35 private $namespaceId;
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-namespaces', '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;
176require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
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)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script that sends purge requests for listed pages to CDN.
Definition purgeList.php:33
bool $allNamespaces
Definition purgeList.php:37
string null $namespaceId
Definition purgeList.php:35
execute()
Do the actual work.
Definition purgeList.php:57
float $delay
Definition purgeList.php:41
__construct()
Default constructor.
Definition purgeList.php:43
sendPurgeRequest( $urls)
Helper to purge an array of $urls.
purgeNamespace( $namespace=false)
Purge a namespace or all pages.
bool $doDbTouch
Definition purgeList.php:39
doPurge()
Purge URL coming from stdin.
Definition purgeList.php:84
$maintClass
const DB_REPLICA
Definition defines.php:25