3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Api
;
23 use MediaWiki\Deferred\DeferredUpdates
;
24 use MediaWiki\Logger\LoggerFactory
;
25 use MediaWiki\Page\WikiPageFactory
;
26 use MediaWiki\Permissions\PermissionStatus
;
27 use MediaWiki\Status\Status
;
28 use MediaWiki\Title\Title
;
29 use MediaWiki\Title\TitleFormatter
;
32 * API interface for page purging
35 class ApiPurge
extends ApiBase
{
36 /** @var ApiPageSet|null */
37 private $mPageSet = null;
39 private WikiPageFactory
$wikiPageFactory;
40 private TitleFormatter
$titleFormatter;
42 public function __construct(
45 WikiPageFactory
$wikiPageFactory,
46 TitleFormatter
$titleFormatter
48 parent
::__construct( $mainModule, $moduleName );
49 $this->wikiPageFactory
= $wikiPageFactory;
50 $this->titleFormatter
= $titleFormatter;
54 * Purges the cache of a page
56 public function execute() {
57 $authority = $this->getAuthority();
59 // Fail early if the user is sitewide blocked.
60 $block = $authority->getBlock();
61 if ( $block && $block->isSitewide() ) {
62 $this->dieBlocked( $block );
65 $params = $this->extractRequestParams();
67 $continuationManager = new ApiContinuationManager( $this, [], [] );
68 $this->setContinuationManager( $continuationManager );
70 $forceLinkUpdate = $params['forcelinkupdate'];
71 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
72 $pageSet = $this->getPageSet();
75 $result = $pageSet->getInvalidTitlesAndRevisions();
76 $userName = $authority->getUser()->getName();
77 $now = wfTimestampNow();
79 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
80 $title = $this->titleFormatter
->getPrefixedText( $pageIdentity );
82 'ns' => $pageIdentity->getNamespace(),
85 $page = $this->wikiPageFactory
->newFromTitle( $pageIdentity );
87 $purgeAuthStatus = PermissionStatus
::newEmpty();
88 if ( $authority->authorizeAction( 'purge', $purgeAuthStatus ) ) {
89 // Directly purge and skip the UI part of purge()
93 if ( $purgeAuthStatus->isRateLimitExceeded() ) {
94 $this->addWarning( 'apierror-ratelimited' );
96 $this->addWarning( Status
::wrap( $purgeAuthStatus )->getMessage() );
100 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
101 $linkpurgeAuthStatus = PermissionStatus
::newEmpty();
102 if ( $authority->authorizeAction( 'linkpurge', $linkpurgeAuthStatus ) ) {
103 # Logging to better see expensive usage patterns
104 if ( $forceRecursiveLinkUpdate ) {
105 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
106 "Recursive link purge enqueued for {title}",
114 $page->updateParserCache( [
115 'causeAction' => 'api-purge',
116 'causeAgent' => $userName,
118 $page->doSecondaryDataUpdates( [
119 'recursive' => $forceRecursiveLinkUpdate,
120 'causeAction' => 'api-purge',
121 'causeAgent' => $userName,
122 'defer' => DeferredUpdates
::PRESEND
,
125 $r['linkupdate'] = true;
127 if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) {
128 $this->addWarning( 'apierror-ratelimited' );
129 $forceLinkUpdate = false;
130 $forceRecursiveLinkUpdate = false;
132 $this->addWarning( Status
::wrap( $linkpurgeAuthStatus )->getMessage() );
139 $apiResult = $this->getResult();
140 ApiResult
::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
143 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
145 $apiResult->addValue( null, 'normalized', $values );
147 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
149 $apiResult->addValue( null, 'converted', $values );
151 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
153 $apiResult->addValue( null, 'redirects', $values );
156 $this->setContinuationManager( null );
157 $continuationManager->setContinuationIntoResult( $apiResult );
161 * Get a cached instance of an ApiPageSet object
164 private function getPageSet() {
165 $this->mPageSet ??
= new ApiPageSet( $this );
167 return $this->mPageSet
;
170 public function isWriteMode() {
174 public function mustBePosted() {
178 public function getAllowedParams( $flags = 0 ) {
180 'forcelinkupdate' => false,
181 'forcerecursivelinkupdate' => false,
183 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
187 $result +
= $this->getPageSet()->getFinalParams( $flags );
193 protected function getExamplesMessages() {
194 $title = Title
::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
198 "action=purge&titles={$mp}|API"
199 => 'apihelp-purge-example-simple',
200 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
201 => 'apihelp-purge-example-generator',
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
210 /** @deprecated class alias since 1.43 */
211 class_alias( ApiPurge
::class, 'ApiPurge' );