37 private $allNamespaces;
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."
49 $this->
addOption(
'namespace',
'Purge pages with this namespace number',
false,
true );
50 $this->
addOption(
'all-namespaces',
'Purge pages in all namespaces',
false,
false );
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.",
57 $this->
addOption(
'delay',
'Number of seconds to delay between each purge',
false,
true );
58 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
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' ) );
68 if ( $this->allNamespaces ) {
69 $this->purgeNamespace(
false );
70 } elseif ( $this->namespaceId !==
null ) {
71 $this->purgeNamespace( intval( $this->namespaceId ) );
75 $this->
output(
"Done!\n" );
81 private function doPurge() {
86 while ( !feof( $stdin ) ) {
87 $page = trim( fgets( $stdin ) );
88 if ( preg_match(
'%^https?://%', $page ) ) {
90 } elseif ( $page !==
'' ) {
91 $title = Title::newFromText( $page );
93 $newUrls = $htmlCacheUpdater->getUrls( $title );
95 foreach ( $newUrls as $url ) {
99 $urls = array_merge( $urls, $newUrls );
101 if ( $this->doDbTouch ) {
102 $title->invalidateCache();
105 $this->
output(
"(Invalid title '$page')\n" );
109 $this->
output(
"Purging " . count( $urls ) .
" urls\n" );
110 $this->sendPurgeRequest( $urls );
118 private function purgeNamespace( $namespace =
false ) {
119 if ( $this->doDbTouch ) {
123 $this->
fatalError(
'The --db-touch option is not supported when purging by namespace.' );
129 if ( $namespace ===
false ) {
132 $conds = [
'page_namespace' => $namespace ];
135 $res = $dbr->newSelectQueryBuilder()
136 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
139 ->andWhere( $dbr->expr(
'page_id',
'>', $startId ) )
140 ->orderBy(
'page_id' )
142 ->caller( __METHOD__ )->fetchResultSet();
143 if ( !$res->numRows() ) {
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;
152 $this->sendPurgeRequest( $urls );
160 private function sendPurgeRequest( $urls ) {
162 if ( $this->delay > 0 ) {
163 foreach ( $urls as $url ) {
165 $this->
output( $url .
"\n" );
167 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
168 sleep( $this->delay );
172 $this->
output( implode(
"\n", $urls ) .
"\n" );
174 $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.
getServiceContainer()
Returns the main service container.
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.