MediaWiki master
ApiPurge.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
30
35class ApiPurge extends ApiBase {
37 private $mPageSet = null;
38
39 private WikiPageFactory $wikiPageFactory;
40 private TitleFormatter $titleFormatter;
41
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;
51 }
52
56 public function execute() {
57 $authority = $this->getAuthority();
58
59 // Fail early if the user is sitewide blocked.
60 $block = $authority->getBlock();
61 if ( $block && $block->isSitewide() ) {
62 $this->dieBlocked( $block );
63 }
64
66
67 $continuationManager = new ApiContinuationManager( $this, [], [] );
68 $this->setContinuationManager( $continuationManager );
69
70 $forceLinkUpdate = $params['forcelinkupdate'];
71 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
72 $pageSet = $this->getPageSet();
73 $pageSet->execute();
74
75 $result = $pageSet->getInvalidTitlesAndRevisions();
76 $userName = $authority->getUser()->getName();
77 $now = wfTimestampNow();
78
79 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
80 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
81 $r = [
82 'ns' => $pageIdentity->getNamespace(),
83 'title' => $title,
84 ];
85 $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
86
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() );
97 }
98 }
99
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}",
107 [
108 'user' => $userName,
109 'title' => $title
110 ]
111 );
112 }
113
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() );
133 }
134 }
135 }
136
137 $result[] = $r;
138 }
139 $apiResult = $this->getResult();
140 ApiResult::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
142
143 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
144 if ( $values ) {
145 $apiResult->addValue( null, 'normalized', $values );
146 }
147 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
148 if ( $values ) {
149 $apiResult->addValue( null, 'converted', $values );
150 }
151 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
152 if ( $values ) {
153 $apiResult->addValue( null, 'redirects', $values );
154 }
155
156 $this->setContinuationManager( null );
157 $continuationManager->setContinuationIntoResult( $apiResult );
158 }
159
164 private function getPageSet() {
165 $this->mPageSet ??= new ApiPageSet( $this );
166
167 return $this->mPageSet;
168 }
169
170 public function isWriteMode() {
171 return true;
172 }
173
174 public function mustBePosted() {
175 return true;
176 }
177
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',
184 ],
185 ];
186 if ( $flags ) {
187 $result += $this->getPageSet()->getFinalParams( $flags );
188 }
189
190 return $result;
191 }
192
193 protected function getExamplesMessages() {
194 $title = Title::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
196
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',
202 ];
203 }
204
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
207 }
208}
209
211class_alias( ApiPurge::class, 'ApiPurge' );
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getResult()
Get the result object.
Definition ApiBase.php:710
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1483
setContinuationManager(?ApiContinuationManager $manager=null)
Definition ApiBase.php:757
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1593
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:35
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:193
__construct(ApiMain $mainModule, string $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition ApiPurge.php:42
execute()
Purges the cache of a page.
Definition ApiPurge.php:56
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:174
getAllowedParams( $flags=0)
Definition ApiPurge.php:178
isWriteMode()
Indicates whether this module requires write access to the wiki.
Definition ApiPurge.php:170
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiPurge.php:205
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Defer callable updates to run later in the PHP process.
Create PSR-3 logger objects.
Service for creating WikiPage objects.
A StatusValue for permission errors.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
A title formatter service for MediaWiki.