MediaWiki REL1_37
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 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
88
89 while ( !feof( $stdin ) ) {
90 $page = trim( fgets( $stdin ) );
91 if ( preg_match( '%^https?://%', $page ) ) {
92 $urls[] = $page;
93 } elseif ( $page !== '' ) {
94 $title = Title::newFromText( $page );
95 if ( $title ) {
96 $newUrls = $htmlCacheUpdater->getUrls( $title );
97
98 foreach ( $newUrls as $url ) {
99 $this->output( "$url\n" );
100 }
101
102 $urls = array_merge( $urls, $newUrls );
103
104 if ( $this->doDbTouch ) {
105 $title->invalidateCache();
106 }
107 } else {
108 $this->output( "(Invalid title '$page')\n" );
109 }
110 }
111 }
112 $this->output( "Purging " . count( $urls ) . " urls\n" );
113 $this->sendPurgeRequest( $urls );
114 }
115
121 private function purgeNamespace( $namespace = false ) {
122 $dbr = $this->getDB( DB_REPLICA );
123 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
124 $startId = 0;
125 if ( $namespace === false ) {
126 $conds = [];
127 } else {
128 $conds = [ 'page_namespace' => $namespace ];
129 }
130 while ( true ) {
131 $res = $dbr->select( 'page',
132 [ 'page_id', 'page_namespace', 'page_title' ],
133 $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
134 __METHOD__,
135 [
136 'LIMIT' => $this->getBatchSize(),
137 'ORDER BY' => 'page_id'
138
139 ]
140 );
141 if ( !$res->numRows() ) {
142 break;
143 }
144 $urls = [];
145 foreach ( $res as $row ) {
146 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
147 $urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
148 $startId = $row->page_id;
149 }
150 $this->sendPurgeRequest( $urls );
151 }
152 }
153
158 private function sendPurgeRequest( $urls ) {
159 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
160 if ( $this->delay > 0 ) {
161 foreach ( $urls as $url ) {
162 if ( $this->hasOption( 'verbose' ) ) {
163 $this->output( $url . "\n" );
164 }
165 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
166 usleep( $this->delay * 1e6 );
167 }
168 } else {
169 if ( $this->hasOption( 'verbose' ) ) {
170 $this->output( implode( "\n", $urls ) . "\n" );
171 }
172 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
173 }
174 }
175}
176
177$maintClass = PurgeList::class;
178require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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)
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