MediaWiki REL1_39
purgeList.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
34class PurgeList extends Maintenance {
36 private $namespaceId;
38 private $allNamespaces;
40 private $doDbTouch;
42 private $delay;
43
44 public function __construct() {
45 parent::__construct();
46 $this->addDescription( "Send purge requests for listed pages to CDN.\n"
47 . "By default this expects a list of URLs or page names from STDIN. "
48 . "To query the database for input, use --namespace or --all-namespaces instead."
49 );
50 $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
51 $this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false );
52 $this->addOption( 'db-touch', 'Update the page.page_touched database field', false, false );
53 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
54 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
55 $this->setBatchSize( 100 );
56 }
57
58 public function execute() {
59 $this->namespaceId = $this->getOption( 'namespace' );
60 $this->allNamespaces = $this->hasOption( 'all-namespaces' );
61 $this->doDbTouch = $this->hasOption( 'db-touch' );
62 $this->delay = intval( $this->getOption( 'delay', '0' ) );
63
64 $conf = $this->getConfig();
65 if ( ( $this->namespaceId !== null || $this->allNamespaces )
66 && $this->doDbTouch
67 && $conf->get( MainConfigNames::MiserMode )
68 ) {
69 $this->fatalError( 'Prevented mass db-invalidation (MiserMode is enabled).' );
70 }
71
72 if ( $this->allNamespaces ) {
73 $this->purgeNamespace( false );
74 } elseif ( $this->namespaceId !== null ) {
75 $this->purgeNamespace( intval( $this->namespaceId ) );
76 } else {
77 $this->doPurge();
78 }
79 $this->output( "Done!\n" );
80 }
81
85 private function doPurge() {
86 $stdin = $this->getStdin();
87 $urls = [];
88 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
89
90 while ( !feof( $stdin ) ) {
91 $page = trim( fgets( $stdin ) );
92 if ( preg_match( '%^https?://%', $page ) ) {
93 $urls[] = $page;
94 } elseif ( $page !== '' ) {
95 $title = Title::newFromText( $page );
96 if ( $title ) {
97 $newUrls = $htmlCacheUpdater->getUrls( $title );
98
99 foreach ( $newUrls as $url ) {
100 $this->output( "$url\n" );
101 }
102
103 $urls = array_merge( $urls, $newUrls );
104
105 if ( $this->doDbTouch ) {
106 $title->invalidateCache();
107 }
108 } else {
109 $this->output( "(Invalid title '$page')\n" );
110 }
111 }
112 }
113 $this->output( "Purging " . count( $urls ) . " urls\n" );
114 $this->sendPurgeRequest( $urls );
115 }
116
122 private function purgeNamespace( $namespace = false ) {
123 $dbr = $this->getDB( DB_REPLICA );
124 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
125 $startId = 0;
126 if ( $namespace === false ) {
127 $conds = [];
128 } else {
129 $conds = [ 'page_namespace' => $namespace ];
130 }
131 while ( true ) {
132 $res = $dbr->select( 'page',
133 [ 'page_id', 'page_namespace', 'page_title' ],
134 $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
135 __METHOD__,
136 [
137 'LIMIT' => $this->getBatchSize(),
138 'ORDER BY' => 'page_id'
139
140 ]
141 );
142 if ( !$res->numRows() ) {
143 break;
144 }
145 $urls = [];
146 foreach ( $res as $row ) {
147 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
148 $urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
149 $startId = $row->page_id;
150 }
151 $this->sendPurgeRequest( $urls );
152 }
153 }
154
159 private function sendPurgeRequest( $urls ) {
160 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
161 if ( $this->delay > 0 ) {
162 foreach ( $urls as $url ) {
163 if ( $this->hasOption( 'verbose' ) ) {
164 $this->output( $url . "\n" );
165 }
166 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
167 sleep( $this->delay );
168 }
169 } else {
170 if ( $this->hasOption( 'verbose' ) ) {
171 $this->output( implode( "\n", $urls ) . "\n" );
172 }
173 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
174 }
175 }
176}
177
178$maintClass = PurgeList::class;
179require_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.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Maintenance script that sends purge requests for listed pages to CDN.
Definition purgeList.php:34
execute()
Do the actual work.
Definition purgeList.php:58
__construct()
Default constructor.
Definition purgeList.php:44
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:370
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition Title.php:638
$maintClass
const DB_REPLICA
Definition defines.php:26