Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiPurge.php
blobc4ffa722efd806ab6e7f73a035b88dae9c1a46d5
1 <?php
2 /**
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
18 * @file
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;
31 /**
32 * API interface for page purging
33 * @ingroup API
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(
43 ApiMain $mainModule,
44 string $moduleName,
45 WikiPageFactory $wikiPageFactory,
46 TitleFormatter $titleFormatter
47 ) {
48 parent::__construct( $mainModule, $moduleName );
49 $this->wikiPageFactory = $wikiPageFactory;
50 $this->titleFormatter = $titleFormatter;
53 /**
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();
73 $pageSet->execute();
75 $result = $pageSet->getInvalidTitlesAndRevisions();
76 $userName = $authority->getUser()->getName();
77 $now = wfTimestampNow();
79 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
80 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
81 $r = [
82 'ns' => $pageIdentity->getNamespace(),
83 'title' => $title,
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()
90 $page->doPurge();
91 $r['purged'] = true;
92 } else {
93 if ( $purgeAuthStatus->isRateLimitExceeded() ) {
94 $this->addWarning( 'apierror-ratelimited' );
95 } else {
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}",
108 'user' => $userName,
109 'title' => $title
114 $page->updateParserCache( [
115 'causeAction' => 'api-purge',
116 'causeAgent' => $userName,
117 ] );
118 $page->doSecondaryDataUpdates( [
119 'recursive' => $forceRecursiveLinkUpdate,
120 'causeAction' => 'api-purge',
121 'causeAgent' => $userName,
122 'defer' => DeferredUpdates::PRESEND,
123 'freshness' => $now,
124 ] );
125 $r['linkupdate'] = true;
126 } else {
127 if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) {
128 $this->addWarning( 'apierror-ratelimited' );
129 $forceLinkUpdate = false;
130 $forceRecursiveLinkUpdate = false;
131 } else {
132 $this->addWarning( Status::wrap( $linkpurgeAuthStatus )->getMessage() );
137 $result[] = $r;
139 $apiResult = $this->getResult();
140 ApiResult::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
143 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
144 if ( $values ) {
145 $apiResult->addValue( null, 'normalized', $values );
147 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
148 if ( $values ) {
149 $apiResult->addValue( null, 'converted', $values );
151 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
152 if ( $values ) {
153 $apiResult->addValue( null, 'redirects', $values );
156 $this->setContinuationManager( null );
157 $continuationManager->setContinuationIntoResult( $apiResult );
161 * Get a cached instance of an ApiPageSet object
162 * @return ApiPageSet
164 private function getPageSet() {
165 $this->mPageSet ??= new ApiPageSet( $this );
167 return $this->mPageSet;
170 public function isWriteMode() {
171 return true;
174 public function mustBePosted() {
175 return true;
178 public function getAllowedParams( $flags = 0 ) {
179 $result = [
180 'forcelinkupdate' => false,
181 'forcerecursivelinkupdate' => false,
182 'continue' => [
183 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
186 if ( $flags ) {
187 $result += $this->getPageSet()->getFinalParams( $flags );
190 return $result;
193 protected function getExamplesMessages() {
194 $title = Title::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
197 return [
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' );