Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeList
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 5
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 doPurge
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
 purgeNamespace
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
 sendPurgeRequest
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Send purge requests for listed pages to CDN
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Title\Title;
25
26require_once __DIR__ . '/Maintenance.php';
27
28/**
29 * Maintenance script that sends purge requests for listed pages to CDN.
30 *
31 * @ingroup Maintenance
32 */
33class PurgeList extends Maintenance {
34    /** @var string|null */
35    private $namespaceId;
36    /** @var bool */
37    private $allNamespaces;
38    /** @var bool */
39    private $doDbTouch;
40    /** @var int */
41    private $delay;
42
43    public function __construct() {
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."
48        );
49        $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
50        $this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false );
51        $this->addOption( 'db-touch',
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.",
54            false,
55            false
56        );
57        $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
58        $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
59        $this->setBatchSize( 100 );
60    }
61
62    public function execute() {
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' ) );
67
68        if ( $this->allNamespaces ) {
69            $this->purgeNamespace( false );
70        } elseif ( $this->namespaceId !== null ) {
71            $this->purgeNamespace( intval( $this->namespaceId ) );
72        } else {
73            $this->doPurge();
74        }
75        $this->output( "Done!\n" );
76    }
77
78    /**
79     * Purge URL coming from stdin
80     */
81    private function doPurge() {
82        $stdin = $this->getStdin();
83        $urls = [];
84        $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
85
86        while ( !feof( $stdin ) ) {
87            $page = trim( fgets( $stdin ) );
88            if ( preg_match( '%^https?://%', $page ) ) {
89                $urls[] = $page;
90            } elseif ( $page !== '' ) {
91                $title = Title::newFromText( $page );
92                if ( $title ) {
93                    $newUrls = $htmlCacheUpdater->getUrls( $title );
94
95                    foreach ( $newUrls as $url ) {
96                        $this->output( "$url\n" );
97                    }
98
99                    $urls = array_merge( $urls, $newUrls );
100
101                    if ( $this->doDbTouch ) {
102                        $title->invalidateCache();
103                    }
104                } else {
105                    $this->output( "(Invalid title '$page')\n" );
106                }
107            }
108        }
109        $this->output( "Purging " . count( $urls ) . " urls\n" );
110        $this->sendPurgeRequest( $urls );
111    }
112
113    /**
114     * Purge a namespace or all pages
115     *
116     * @param int|bool $namespace
117     */
118    private function purgeNamespace( $namespace = false ) {
119        if ( $this->doDbTouch ) {
120            // NOTE: If support for this is added in the future,
121            // it MUST NOT be allowed when $wgMiserMode is enabled.
122            // Change this to a check and error about instead! (T263957)
123            $this->fatalError( 'The --db-touch option is not supported when purging by namespace.' );
124        }
125
126        $dbr = $this->getReplicaDB();
127        $htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
128        $startId = 0;
129        if ( $namespace === false ) {
130            $conds = [];
131        } else {
132            $conds = [ 'page_namespace' => $namespace ];
133        }
134        while ( true ) {
135            $res = $dbr->newSelectQueryBuilder()
136                ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
137                ->from( 'page' )
138                ->where( $conds )
139                ->andWhere( $dbr->expr( 'page_id', '>', $startId ) )
140                ->orderBy( 'page_id' )
141                ->limit( $this->getBatchSize() )
142                ->caller( __METHOD__ )->fetchResultSet();
143            if ( !$res->numRows() ) {
144                break;
145            }
146            $urls = [];
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;
151            }
152            $this->sendPurgeRequest( $urls );
153        }
154    }
155
156    /**
157     * Helper to purge an array of $urls
158     * @param array $urls List of URLS to purge from CDNs
159     */
160    private function sendPurgeRequest( $urls ) {
161        $hcu = $this->getServiceContainer()->getHtmlCacheUpdater();
162        if ( $this->delay > 0 ) {
163            foreach ( $urls as $url ) {
164                if ( $this->hasOption( 'verbose' ) ) {
165                    $this->output( $url . "\n" );
166                }
167                $hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
168                sleep( $this->delay );
169            }
170        } else {
171            if ( $this->hasOption( 'verbose' ) ) {
172                $this->output( implode( "\n", $urls ) . "\n" );
173            }
174            $hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
175        }
176    }
177}
178
179$maintClass = PurgeList::class;
180require_once RUN_MAINTENANCE_IF_MAIN;