MediaWiki master
purgeList.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
36class PurgeList extends Maintenance {
38 private $namespaceId;
40 private $allNamespaces;
42 private $doDbTouch;
44 private $delay;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( "Send purge requests for listed pages to CDN.\n"
49 . "By default this expects a list of URLs or page names from STDIN. "
50 . "To query the database for input, use --namespace or --all-namespaces instead."
51 );
52 $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
53 $this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false );
54 $this->addOption( 'db-touch',
55 "Update the page.page_touched database field.\n"
56 . "This is only considered when purging by title, not when purging by namespace or URL.",
57 false,
58 false
59 );
60 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
61 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
62 $this->setBatchSize( 100 );
63 }
64
65 public function execute() {
66 $this->namespaceId = $this->getOption( 'namespace' );
67 $this->allNamespaces = $this->hasOption( 'all-namespaces' );
68 $this->doDbTouch = $this->hasOption( 'db-touch' );
69 $this->delay = intval( $this->getOption( 'delay', '0' ) );
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 = $this->getServiceContainer()->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 if ( $this->doDbTouch ) {
123 // NOTE: If support for this is added in the future,
124 // it MUST NOT be allowed when $wgMiserMode is enabled.
125 // Change this to a check and error about instead! (T263957)
126 $this->fatalError( 'The --db-touch option is not supported when purging by namespace.' );
127 }
128
129 $dbr = $this->getReplicaDB();
130 $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
131 $startId = 0;
132 if ( $namespace === false ) {
133 $conds = [];
134 } else {
135 $conds = [ 'page_namespace' => $namespace ];
136 }
137 while ( true ) {
138 $res = $dbr->newSelectQueryBuilder()
139 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
140 ->from( 'page' )
141 ->where( $conds )
142 ->andWhere( $dbr->expr( 'page_id', '>', $startId ) )
143 ->orderBy( 'page_id' )
144 ->limit( $this->getBatchSize() )
145 ->caller( __METHOD__ )->fetchResultSet();
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 = $this->getServiceContainer()->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// @codeCoverageIgnoreStart
183$maintClass = PurgeList::class;
184require_once RUN_MAINTENANCE_IF_MAIN;
185// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78
Maintenance script that sends purge requests for listed pages to CDN.
Definition purgeList.php:36
execute()
Do the actual work.
Definition purgeList.php:65
__construct()
Default constructor.
Definition purgeList.php:46
$maintClass