39 private $allNamespaces;
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."
51 $this->
addOption(
'namespace',
'Purge pages with this namespace number',
false,
true );
52 $this->
addOption(
'all-namespaces',
'Purge pages in all namespaces',
false,
false );
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.",
59 $this->
addOption(
'delay',
'Number of seconds to delay between each purge',
false,
true );
60 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
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' ) );
70 if ( $this->allNamespaces ) {
71 $this->purgeNamespace(
false );
72 } elseif ( $this->namespaceId !==
null ) {
73 $this->purgeNamespace( intval( $this->namespaceId ) );
77 $this->
output(
"Done!\n" );
83 private function doPurge() {
88 while ( !feof( $stdin ) ) {
89 $page = trim( fgets( $stdin ) );
90 if ( preg_match(
'%^https?://%', $page ) ) {
92 } elseif ( $page !==
'' ) {
93 $title = Title::newFromText( $page );
95 $newUrls = $htmlCacheUpdater->getUrls( $title );
97 foreach ( $newUrls as
$url ) {
101 $urls = array_merge( $urls, $newUrls );
103 if ( $this->doDbTouch ) {
104 $title->invalidateCache();
107 $this->
output(
"(Invalid title '$page')\n" );
111 $this->
output(
"Purging " . count( $urls ) .
" urls\n" );
112 $this->sendPurgeRequest( $urls );
120 private function purgeNamespace( $namespace =
false ) {
121 if ( $this->doDbTouch ) {
125 $this->
fatalError(
'The --db-touch option is not supported when purging by namespace.' );
131 if ( $namespace ===
false ) {
134 $conds = [
'page_namespace' => $namespace ];
137 $res = $dbr->newSelectQueryBuilder()
138 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
141 ->andWhere( $dbr->expr(
'page_id',
'>', $startId ) )
142 ->orderBy(
'page_id' )
144 ->caller( __METHOD__ )->fetchResultSet();
145 if ( !$res->numRows() ) {
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;
154 $this->sendPurgeRequest( $urls );
162 private function sendPurgeRequest( $urls ) {
164 if ( $this->delay > 0 ) {
165 foreach ( $urls as
$url ) {
167 $this->
output( $url .
"\n" );
169 $hcu->purgeUrls(
$url, $hcu::PURGE_NAIVE );
170 sleep( $this->delay );
174 $this->
output( implode(
"\n", $urls ) .
"\n" );
176 $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.