MediaWiki 1.40.4
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',
53 "Update the page.page_touched database field.\n"
54 . "This is only considered when purging by title, not when purging by namespace or URL.",
55 false,
56 false
57 );
58 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
59 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
60 $this->setBatchSize( 100 );
61 }
62
63 public function execute() {
64 $this->namespaceId = $this->getOption( 'namespace' );
65 $this->allNamespaces = $this->hasOption( 'all-namespaces' );
66 $this->doDbTouch = $this->hasOption( 'db-touch' );
67 $this->delay = intval( $this->getOption( 'delay', '0' ) );
68
69 if ( $this->allNamespaces ) {
70 $this->purgeNamespace( false );
71 } elseif ( $this->namespaceId !== null ) {
72 $this->purgeNamespace( intval( $this->namespaceId ) );
73 } else {
74 $this->doPurge();
75 }
76 $this->output( "Done!\n" );
77 }
78
82 private function doPurge() {
83 $stdin = $this->getStdin();
84 $urls = [];
85 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
86
87 while ( !feof( $stdin ) ) {
88 $page = trim( fgets( $stdin ) );
89 if ( preg_match( '%^https?://%', $page ) ) {
90 $urls[] = $page;
91 } elseif ( $page !== '' ) {
92 $title = Title::newFromText( $page );
93 if ( $title ) {
94 $newUrls = $htmlCacheUpdater->getUrls( $title );
95
96 foreach ( $newUrls as $url ) {
97 $this->output( "$url\n" );
98 }
99
100 $urls = array_merge( $urls, $newUrls );
101
102 if ( $this->doDbTouch ) {
103 $title->invalidateCache();
104 }
105 } else {
106 $this->output( "(Invalid title '$page')\n" );
107 }
108 }
109 }
110 $this->output( "Purging " . count( $urls ) . " urls\n" );
111 $this->sendPurgeRequest( $urls );
112 }
113
119 private function purgeNamespace( $namespace = false ) {
120 if ( $this->doDbTouch ) {
121 // NOTE: If support for this is added in the future,
122 // it MUST NOT be allowed when $wgMiserMode is enabled.
123 // Change this to a check and error about instead! (T263957)
124 $this->fatalError( 'The --db-touch option is not supported when purging by namespace.' );
125 }
126
127 $dbr = $this->getDB( DB_REPLICA );
128 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
129 $startId = 0;
130 if ( $namespace === false ) {
131 $conds = [];
132 } else {
133 $conds = [ 'page_namespace' => $namespace ];
134 }
135 while ( true ) {
136 $res = $dbr->select( 'page',
137 [ 'page_id', 'page_namespace', 'page_title' ],
138 $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
139 __METHOD__,
140 [
141 'LIMIT' => $this->getBatchSize(),
142 'ORDER BY' => 'page_id'
143
144 ]
145 );
146 if ( !$res->numRows() ) {
147 break;
148 }
149 $urls = [];
150 foreach ( $res as $row ) {
151 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
152 $urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
153 $startId = $row->page_id;
154 }
155 $this->sendPurgeRequest( $urls );
156 }
157 }
158
163 private function sendPurgeRequest( $urls ) {
164 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
165 if ( $this->delay > 0 ) {
166 foreach ( $urls as $url ) {
167 if ( $this->hasOption( 'verbose' ) ) {
168 $this->output( $url . "\n" );
169 }
170 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
171 sleep( $this->delay );
172 }
173 } else {
174 if ( $this->hasOption( 'verbose' ) ) {
175 $this->output( implode( "\n", $urls ) . "\n" );
176 }
177 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
178 }
179 }
180}
181
182$maintClass = PurgeList::class;
183require_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.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Maintenance script that sends purge requests for listed pages to CDN.
Definition purgeList.php:34
execute()
Do the actual work.
Definition purgeList.php:63
__construct()
Default constructor.
Definition purgeList.php:44
$maintClass
const DB_REPLICA
Definition defines.php:26