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