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 );
52 $this->
addOption(
'db-touch',
'Update the page.page_touched database field',
false,
false );
53 $this->
addOption(
'delay',
'Number of seconds to delay between each purge',
false,
true );
54 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
59 $this->namespaceId = $this->
getOption(
'namespace' );
60 $this->allNamespaces = $this->
hasOption(
'all-namespaces' );
61 $this->doDbTouch = $this->
hasOption(
'db-touch' );
62 $this->delay = intval( $this->
getOption(
'delay',
'0' ) );
65 if ( ( $this->namespaceId !==
null || $this->allNamespaces )
67 && $conf->get( MainConfigNames::MiserMode )
69 $this->
fatalError(
'Prevented mass db-invalidation (MiserMode is enabled).' );
72 if ( $this->allNamespaces ) {
73 $this->purgeNamespace(
false );
74 } elseif ( $this->namespaceId !==
null ) {
75 $this->purgeNamespace( intval( $this->namespaceId ) );
79 $this->
output(
"Done!\n" );
85 private function doPurge() {
88 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
90 while ( !feof( $stdin ) ) {
91 $page = trim( fgets( $stdin ) );
92 if ( preg_match(
'%^https?://%', $page ) ) {
94 } elseif ( $page !==
'' ) {
97 $newUrls = $htmlCacheUpdater->getUrls(
$title );
99 foreach ( $newUrls as $url ) {
100 $this->
output(
"$url\n" );
103 $urls = array_merge( $urls, $newUrls );
105 if ( $this->doDbTouch ) {
106 $title->invalidateCache();
109 $this->
output(
"(Invalid title '$page')\n" );
113 $this->
output(
"Purging " . count( $urls ) .
" urls\n" );
114 $this->sendPurgeRequest( $urls );
122 private function purgeNamespace( $namespace =
false ) {
124 $htmlCacheUpdater = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
126 if ( $namespace ===
false ) {
129 $conds = [
'page_namespace' => $namespace ];
133 [
'page_id',
'page_namespace',
'page_title' ],
134 $conds + [
'page_id > ' .
$dbr->addQuotes( $startId ) ],
137 'LIMIT' => $this->getBatchSize(),
138 'ORDER BY' =>
'page_id'
142 if ( !
$res->numRows() ) {
146 foreach (
$res as $row ) {
148 $urls = array_merge( $urls, $htmlCacheUpdater->getUrls(
$title ) );
149 $startId = $row->page_id;
151 $this->sendPurgeRequest( $urls );
159 private function sendPurgeRequest( $urls ) {
160 $hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
161 if ( $this->delay > 0 ) {
162 foreach ( $urls as $url ) {
164 $this->
output( $url .
"\n" );
166 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
167 sleep( $this->delay );
171 $this->
output( implode(
"\n", $urls ) .
"\n" );
173 $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.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.