Merge "mediawiki.router: Move hashchange handler to a real method"
[mediawiki.git] / maintenance / purgeList.php
blobd49b32d5aee8d5346ca05dd961018a749d30408c
1 <?php
2 /**
3 * Send purge requests for listed pages to CDN
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Title\Title;
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
30 /**
31 * Maintenance script that sends purge requests for listed pages to CDN.
33 * @ingroup Maintenance
35 class PurgeList extends Maintenance {
36 /** @var string|null */
37 private $namespaceId;
38 /** @var bool */
39 private $allNamespaces;
40 /** @var bool */
41 private $doDbTouch;
42 /** @var int */
43 private $delay;
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."
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
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 );
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' ) );
70 if ( $this->allNamespaces ) {
71 $this->purgeNamespace( false );
72 } elseif ( $this->namespaceId !== null ) {
73 $this->purgeNamespace( intval( $this->namespaceId ) );
74 } else {
75 $this->doPurge();
77 $this->output( "Done!\n" );
80 /**
81 * Purge URL coming from stdin
83 private function doPurge() {
84 $stdin = $this->getStdin();
85 $urls = [];
86 $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
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 );
97 foreach ( $newUrls as $url ) {
98 $this->output( "$url\n" );
101 $urls = array_merge( $urls, $newUrls );
103 if ( $this->doDbTouch ) {
104 $title->invalidateCache();
106 } else {
107 $this->output( "(Invalid title '$page')\n" );
111 $this->output( "Purging " . count( $urls ) . " urls\n" );
112 $this->sendPurgeRequest( $urls );
116 * Purge a namespace or all pages
118 * @param int|bool $namespace
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.' );
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 ];
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;
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;
154 $this->sendPurgeRequest( $urls );
159 * Helper to purge an array of $urls
160 * @param array $urls List of URLS to purge from CDNs
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" );
169 $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
170 sleep( $this->delay );
172 } else {
173 if ( $this->hasOption( 'verbose' ) ) {
174 $this->output( implode( "\n", $urls ) . "\n" );
176 $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
181 // @codeCoverageIgnoreStart
182 $maintClass = PurgeList::class;
183 require_once RUN_MAINTENANCE_IF_MAIN;
184 // @codeCoverageIgnoreEnd