MediaWiki
master
Toggle main menu visibility
purgeList.php
Go to the documentation of this file.
1
<?php
9
10
use
MediaWiki\Maintenance\Maintenance
;
11
use MediaWiki\Title\Title;
12
13
// @codeCoverageIgnoreStart
14
require_once __DIR__ .
'/Maintenance.php'
;
15
// @codeCoverageIgnoreEnd
16
22
class
PurgeList
extends
Maintenance
{
24
private
$namespaceId;
26
private
$allNamespaces;
28
private
$doDbTouch;
30
private
$delay;
31
32
public
function
__construct
() {
33
parent::__construct();
34
$this->addDescription(
"Send purge requests for listed pages to CDN.\n"
35
.
"By default this expects a list of URLs or page names from STDIN. "
36
.
"To query the database for input, use --namespace or --all-namespaces instead."
37
);
38
$this->addOption(
'namespace'
,
'Purge pages with this namespace number'
,
false
,
true
);
39
$this->addOption(
'all-namespaces'
,
'Purge pages in all namespaces'
,
false
,
false
);
40
$this->addOption(
'db-touch'
,
41
"Update the page.page_touched database field.\n"
42
.
"This is only considered when purging by title, not when purging by namespace or URL."
,
43
false
,
44
false
45
);
46
$this->addOption(
'delay'
,
'Number of seconds to delay between each purge'
,
false
,
true
);
47
$this->addOption(
'verbose'
,
'Show more output'
,
false
,
false
,
'v'
);
48
$this->setBatchSize( 100 );
49
}
50
51
public
function
execute
() {
52
$this->namespaceId = $this->getOption(
'namespace'
);
53
$this->allNamespaces = $this->hasOption(
'all-namespaces'
);
54
$this->doDbTouch = $this->hasOption(
'db-touch'
);
55
$this->delay = intval( $this->getOption(
'delay'
,
'0'
) );
56
57
if
( $this->allNamespaces ) {
58
$this->purgeNamespace(
false
);
59
} elseif ( $this->namespaceId !==
null
) {
60
$this->purgeNamespace( intval( $this->namespaceId ) );
61
}
else
{
62
$this->doPurge();
63
}
64
$this->output(
"Done!\n"
);
65
}
66
70
private
function
doPurge() {
71
$stdin = $this->getStdin();
72
$urls = [];
73
$htmlCacheUpdater = $this->getServiceContainer()->getHTMLCacheUpdater();
74
75
while
( !feof( $stdin ) ) {
76
$page = trim( fgets( $stdin ) );
77
if
( preg_match(
'%^https?://%'
, $page ) ) {
78
$urls[] = $page;
79
} elseif ( $page !==
''
) {
80
$title =
Title::newFromText
( $page );
81
if
( $title ) {
82
$newUrls = $htmlCacheUpdater->getUrls( $title );
83
84
foreach
( $newUrls as
$url
) {
85
$this->output(
"$url\n"
);
86
}
87
88
$urls = array_merge( $urls, $newUrls );
89
90
if
( $this->doDbTouch ) {
91
$title->invalidateCache();
92
}
93
}
else
{
94
$this->output(
"(Invalid title '$page')\n"
);
95
}
96
}
97
}
98
$this->output(
"Purging "
. count( $urls ) .
" urls\n"
);
99
$this->sendPurgeRequest( $urls );
100
}
101
107
private
function
purgeNamespace( $namespace =
false
) {
108
if
( $this->doDbTouch ) {
109
// NOTE: If support for this is added in the future,
110
// it MUST NOT be allowed when $wgMiserMode is enabled.
111
// Change this to a check and error about instead! (T263957)
112
$this->fatalError(
'The --db-touch option is not supported when purging by namespace.'
);
113
}
114
115
$dbr = $this->getReplicaDB();
116
$htmlCacheUpdater = $this->getServiceContainer()->getHTMLCacheUpdater();
117
$startId = 0;
118
if
( $namespace ===
false
) {
119
$conds = [];
120
}
else
{
121
$conds = [
'page_namespace'
=> $namespace ];
122
}
123
while
(
true
) {
124
$res = $dbr->newSelectQueryBuilder()
125
->select( [
'page_id'
,
'page_namespace'
,
'page_title'
] )
126
->from(
'page'
)
127
->where( $conds )
128
->andWhere( $dbr->expr(
'page_id'
,
'>'
, $startId ) )
129
->orderBy(
'page_id'
)
130
->limit( $this->getBatchSize() )
131
->caller( __METHOD__ )->fetchResultSet();
132
if
( !$res->numRows() ) {
133
break
;
134
}
135
$urls = [];
136
foreach
( $res as $row ) {
137
$title =
Title::makeTitle
( $row->page_namespace, $row->page_title );
138
$urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
139
$startId = $row->page_id;
140
}
141
$this->sendPurgeRequest( $urls );
142
}
143
}
144
149
private
function
sendPurgeRequest( $urls ) {
150
$hcu = $this->getServiceContainer()->getHTMLCacheUpdater();
151
if
( $this->delay > 0 ) {
152
foreach
( $urls as
$url
) {
153
if
( $this->hasOption(
'verbose'
) ) {
154
$this->output(
$url
.
"\n"
);
155
}
156
$hcu->purgeUrls(
$url
, $hcu::PURGE_NAIVE );
157
sleep( $this->delay );
158
}
159
}
else
{
160
if
( $this->hasOption(
'verbose'
) ) {
161
$this->output( implode(
"\n"
, $urls ) .
"\n"
);
162
}
163
$hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
164
}
165
}
166
}
167
168
// @codeCoverageIgnoreStart
169
$maintClass
= PurgeList::class;
170
require_once RUN_MAINTENANCE_IF_MAIN;
171
// @codeCoverageIgnoreEnd
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\Title\Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition
Title.php:388
MediaWiki\Title\Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition
Title.php:609
PurgeList
Maintenance script that sends purge requests for listed pages to CDN.
Definition
purgeList.php:22
PurgeList\execute
execute()
Definition
purgeList.php:51
PurgeList\__construct
__construct()
Definition
purgeList.php:32
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
$url
$url
Definition
opensearch_desc.php:25
maintenance
purgeList.php
Generated on
for MediaWiki by
1.18.0