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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that sends purge requests for listed pages to CDN.
29 * @ingroup Maintenance
31 class PurgeList
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->addDescription( 'Send purge requests for listed pages to CDN' );
35 $this->addOption( 'purge', 'Whether to update page_touched.', false, false );
36 $this->addOption( 'namespace', 'Namespace number', false, true );
37 $this->addOption( 'all', 'Purge all pages', false, false );
38 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
39 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
40 $this->setBatchSize( 100 );
43 public function execute() {
44 if ( $this->hasOption( 'all' ) ) {
45 $this->purgeNamespace( false );
46 } elseif ( $this->hasOption( 'namespace' ) ) {
47 $this->purgeNamespace( intval( $this->getOption( 'namespace' ) ) );
51 $this->output( "Done!\n" );
55 * Purge URL coming from stdin
57 private function doPurge() {
58 $stdin = $this->getStdin();
61 while ( !feof( $stdin ) ) {
62 $page = trim( fgets( $stdin ) );
63 if ( preg_match( '%^https?://%', $page ) ) {
65 } elseif ( $page !== '' ) {
66 $title = Title
::newFromText( $page );
68 $newUrls = $title->getCdnUrls();
70 foreach ( $newUrls as $url ) {
71 $this->output( "$url\n" );
74 $urls = array_merge( $urls, $newUrls );
76 if ( $this->getOption( 'purge' ) ) {
77 $title->invalidateCache();
80 $this->output( "(Invalid title '$page')\n" );
84 $this->output( "Purging " . count( $urls ) . " urls\n" );
85 $this->sendPurgeRequest( $urls );
89 * Purge a namespace or all pages
91 * @param int|bool $namespace
93 private function purgeNamespace( $namespace = false ) {
94 $dbr = $this->getDB( DB_REPLICA
);
96 if ( $namespace === false ) {
99 $conds = [ 'page_namespace' => $namespace ];
102 $res = $dbr->select( 'page',
103 [ 'page_id', 'page_namespace', 'page_title' ],
104 $conds +
[ 'page_id > ' . $dbr->addQuotes( $startId ) ],
107 'LIMIT' => $this->getBatchSize(),
108 'ORDER BY' => 'page_id'
112 if ( !$res->numRows() ) {
116 foreach ( $res as $row ) {
117 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
118 $urls = array_merge( $urls, $title->getCdnUrls() );
119 $startId = $row->page_id
;
121 $this->sendPurgeRequest( $urls );
126 * Helper to purge an array of $urls
127 * @param array $urls List of URLS to purge from CDNs
129 private function sendPurgeRequest( $urls ) {
130 if ( $this->hasOption( 'delay' ) ) {
131 $delay = floatval( $this->getOption( 'delay' ) );
132 foreach ( $urls as $url ) {
133 if ( $this->hasOption( 'verbose' ) ) {
134 $this->output( $url . "\n" );
136 $u = new CdnCacheUpdate( [ $url ] );
138 usleep( $delay * 1e6
);
141 if ( $this->hasOption( 'verbose' ) ) {
142 $this->output( implode( "\n", $urls ) . "\n" );
144 $u = new CdnCacheUpdate( $urls );
150 $maintClass = PurgeList
::class;
151 require_once RUN_MAINTENANCE_IF_MAIN
;