38 private $allNamespaces;
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."
50 $this->
addOption(
'namespace',
'Purge pages with this namespace number',
false,
true );
51 $this->
addOption(
'all-namespaces',
'Purge pages in all namespaces',
false,
false );
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.",
58 $this->
addOption(
'delay',
'Number of seconds to delay between each purge',
false,
true );
59 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
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' ) );
69 if ( $this->allNamespaces ) {
70 $this->purgeNamespace(
false );
71 } elseif ( $this->namespaceId !==
null ) {
72 $this->purgeNamespace( intval( $this->namespaceId ) );
76 $this->
output(
"Done!\n" );
82 private function doPurge() {
85 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
87 while ( !feof( $stdin ) ) {
88 $page = trim( fgets( $stdin ) );
89 if ( preg_match(
'%^https?://%', $page ) ) {
91 } elseif ( $page !==
'' ) {
92 $title = Title::newFromText( $page );
94 $newUrls = $htmlCacheUpdater->getUrls(
$title );
96 foreach ( $newUrls as $url ) {
100 $urls = array_merge( $urls, $newUrls );
102 if ( $this->doDbTouch ) {
103 $title->invalidateCache();
106 $this->
output(
"(Invalid title '$page')\n" );
110 $this->
output(
"Purging " . count( $urls ) .
" urls\n" );
111 $this->sendPurgeRequest( $urls );
119 private function purgeNamespace( $namespace =
false ) {
120 if ( $this->doDbTouch ) {
124 $this->
fatalError(
'The --db-touch option is not supported when purging by namespace.' );
128 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
130 if ( $namespace ===
false ) {
133 $conds = [
'page_namespace' => $namespace ];
137 [
'page_id',
'page_namespace',
'page_title' ],
138 $conds + [
'page_id > ' .
$dbr->addQuotes( $startId ) ],
141 'LIMIT' => $this->getBatchSize(),
142 'ORDER BY' =>
'page_id'
146 if ( !
$res->numRows() ) {
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;
155 $this->sendPurgeRequest( $urls );
163 private function sendPurgeRequest( $urls ) {
164 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
165 if ( $this->delay > 0 ) {
166 foreach ( $urls as $url ) {
168 $this->
output( $url .
"\n" );
170 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
171 sleep( $this->delay );
175 $this->
output( implode(
"\n", $urls ) .
"\n" );
177 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.