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