MediaWiki master
purgeList.php
Go to the documentation of this file.
1<?php
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
22class PurgeList extends Maintenance {
24 private $namespaceId;
26 private $allNamespaces;
28 private $doDbTouch;
30 private $delay;
31
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( "Send purge requests for listed pages to CDN.\n"
35 . "By default this expects a list of URLs or page names from STDIN. "
36 . "To query the database for input, use --namespace or --all-namespaces instead."
37 );
38 $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
39 $this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false );
40 $this->addOption( 'db-touch',
41 "Update the page.page_touched database field.\n"
42 . "This is only considered when purging by title, not when purging by namespace or URL.",
43 false,
44 false
45 );
46 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
47 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
48 $this->setBatchSize( 100 );
49 }
50
51 public function execute() {
52 $this->namespaceId = $this->getOption( 'namespace' );
53 $this->allNamespaces = $this->hasOption( 'all-namespaces' );
54 $this->doDbTouch = $this->hasOption( 'db-touch' );
55 $this->delay = intval( $this->getOption( 'delay', '0' ) );
56
57 if ( $this->allNamespaces ) {
58 $this->purgeNamespace( false );
59 } elseif ( $this->namespaceId !== null ) {
60 $this->purgeNamespace( intval( $this->namespaceId ) );
61 } else {
62 $this->doPurge();
63 }
64 $this->output( "Done!\n" );
65 }
66
70 private function doPurge() {
71 $stdin = $this->getStdin();
72 $urls = [];
73 $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
74
75 while ( !feof( $stdin ) ) {
76 $page = trim( fgets( $stdin ) );
77 if ( preg_match( '%^https?://%', $page ) ) {
78 $urls[] = $page;
79 } elseif ( $page !== '' ) {
80 $title = Title::newFromText( $page );
81 if ( $title ) {
82 $newUrls = $htmlCacheUpdater->getUrls( $title );
83
84 foreach ( $newUrls as $url ) {
85 $this->output( "$url\n" );
86 }
87
88 $urls = array_merge( $urls, $newUrls );
89
90 if ( $this->doDbTouch ) {
91 $title->invalidateCache();
92 }
93 } else {
94 $this->output( "(Invalid title '$page')\n" );
95 }
96 }
97 }
98 $this->output( "Purging " . count( $urls ) . " urls\n" );
99 $this->sendPurgeRequest( $urls );
100 }
101
107 private function purgeNamespace( $namespace = false ) {
108 if ( $this->doDbTouch ) {
109 // NOTE: If support for this is added in the future,
110 // it MUST NOT be allowed when $wgMiserMode is enabled.
111 // Change this to a check and error about instead! (T263957)
112 $this->fatalError( 'The --db-touch option is not supported when purging by namespace.' );
113 }
114
115 $dbr = $this->getReplicaDB();
116 $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
117 $startId = 0;
118 if ( $namespace === false ) {
119 $conds = [];
120 } else {
121 $conds = [ 'page_namespace' => $namespace ];
122 }
123 while ( true ) {
124 $res = $dbr->newSelectQueryBuilder()
125 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
126 ->from( 'page' )
127 ->where( $conds )
128 ->andWhere( $dbr->expr( 'page_id', '>', $startId ) )
129 ->orderBy( 'page_id' )
130 ->limit( $this->getBatchSize() )
131 ->caller( __METHOD__ )->fetchResultSet();
132 if ( !$res->numRows() ) {
133 break;
134 }
135 $urls = [];
136 foreach ( $res as $row ) {
137 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
138 $urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
139 $startId = $row->page_id;
140 }
141 $this->sendPurgeRequest( $urls );
142 }
143 }
144
149 private function sendPurgeRequest( $urls ) {
150 $hcu = $this->getServiceContainer()->getHtmlCacheUpdater();
151 if ( $this->delay > 0 ) {
152 foreach ( $urls as $url ) {
153 if ( $this->hasOption( 'verbose' ) ) {
154 $this->output( $url . "\n" );
155 }
156 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
157 sleep( $this->delay );
158 }
159 } else {
160 if ( $this->hasOption( 'verbose' ) ) {
161 $this->output( implode( "\n", $urls ) . "\n" );
162 }
163 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
164 }
165 }
166}
167
168// @codeCoverageIgnoreStart
169$maintClass = PurgeList::class;
170require_once RUN_MAINTENANCE_IF_MAIN;
171// @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.
getReplicaDB(string|false $virtualDomain=false)
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:69
Maintenance script that sends purge requests for listed pages to CDN.
Definition purgeList.php:22
execute()
Do the actual work.
Definition purgeList.php:51
__construct()
Default constructor.
Definition purgeList.php:32
$maintClass