MediaWiki master
ApiPurge.php
Go to the documentation of this file.
1<?php
28
33class ApiPurge extends ApiBase {
35 private $mPageSet = null;
36
37 private WikiPageFactory $wikiPageFactory;
38 private TitleFormatter $titleFormatter;
39
46 public function __construct(
47 ApiMain $mainModule,
48 $moduleName,
49 WikiPageFactory $wikiPageFactory,
50 TitleFormatter $titleFormatter
51 ) {
52 parent::__construct( $mainModule, $moduleName );
53 $this->wikiPageFactory = $wikiPageFactory;
54 $this->titleFormatter = $titleFormatter;
55 }
56
60 public function execute() {
61 $authority = $this->getAuthority();
62
63 // Fail early if the user is sitewide blocked.
64 $block = $authority->getBlock();
65 if ( $block && $block->isSitewide() ) {
66 $this->dieBlocked( $block );
67 }
68
70
71 $continuationManager = new ApiContinuationManager( $this, [], [] );
72 $this->setContinuationManager( $continuationManager );
73
74 $forceLinkUpdate = $params['forcelinkupdate'];
75 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
76 $pageSet = $this->getPageSet();
77 $pageSet->execute();
78
79 $result = $pageSet->getInvalidTitlesAndRevisions();
80 $userName = $authority->getUser()->getName();
81 $now = wfTimestampNow();
82
83 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
84 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
85 $r = [
86 'ns' => $pageIdentity->getNamespace(),
87 'title' => $title,
88 ];
89 $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
90
91 $purgeAuthStatus = PermissionStatus::newEmpty();
92 if ( $authority->authorizeAction( 'purge', $purgeAuthStatus ) ) {
93 // Directly purge and skip the UI part of purge()
94 $page->doPurge();
95 $r['purged'] = true;
96 } else {
97 if ( $purgeAuthStatus->isRateLimitExceeded() ) {
98 $this->addWarning( 'apierror-ratelimited' );
99 } else {
100 $this->addWarning( Status::wrap( $purgeAuthStatus )->getMessage() );
101 }
102 }
103
104 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
105 $linkpurgeAuthStatus = PermissionStatus::newEmpty();
106 if ( $authority->authorizeAction( 'linkpurge', $linkpurgeAuthStatus ) ) {
107 # Logging to better see expensive usage patterns
108 if ( $forceRecursiveLinkUpdate ) {
109 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
110 "Recursive link purge enqueued for {title}",
111 [
112 'user' => $userName,
113 'title' => $title
114 ]
115 );
116 }
117
118 $page->updateParserCache( [
119 'causeAction' => 'api-purge',
120 'causeAgent' => $userName,
121 ] );
122 $page->doSecondaryDataUpdates( [
123 'recursive' => $forceRecursiveLinkUpdate,
124 'causeAction' => 'api-purge',
125 'causeAgent' => $userName,
126 'defer' => DeferredUpdates::PRESEND,
127 'freshness' => $now,
128 ] );
129 $r['linkupdate'] = true;
130 } else {
131 if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) {
132 $this->addWarning( 'apierror-ratelimited' );
133 $forceLinkUpdate = false;
134 $forceRecursiveLinkUpdate = false;
135 } else {
136 $this->addWarning( Status::wrap( $linkpurgeAuthStatus )->getMessage() );
137 }
138 }
139 }
140
141 $result[] = $r;
142 }
143 $apiResult = $this->getResult();
144 ApiResult::setIndexedTagName( $result, 'page' );
145 $apiResult->addValue( null, $this->getModuleName(), $result );
146
147 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
148 if ( $values ) {
149 $apiResult->addValue( null, 'normalized', $values );
150 }
151 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
152 if ( $values ) {
153 $apiResult->addValue( null, 'converted', $values );
154 }
155 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
156 if ( $values ) {
157 $apiResult->addValue( null, 'redirects', $values );
158 }
159
160 $this->setContinuationManager( null );
161 $continuationManager->setContinuationIntoResult( $apiResult );
162 }
163
168 private function getPageSet() {
169 $this->mPageSet ??= new ApiPageSet( $this );
170
171 return $this->mPageSet;
172 }
173
174 public function isWriteMode() {
175 return true;
176 }
177
178 public function mustBePosted() {
179 return true;
180 }
181
182 public function getAllowedParams( $flags = 0 ) {
183 $result = [
184 'forcelinkupdate' => false,
185 'forcerecursivelinkupdate' => false,
186 'continue' => [
187 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
188 ],
189 ];
190 if ( $flags ) {
191 $result += $this->getPageSet()->getFinalParams( $flags );
192 }
193
194 return $result;
195 }
196
197 protected function getExamplesMessages() {
198 $title = Title::newMainPage()->getPrefixedText();
199 $mp = rawurlencode( $title );
200
201 return [
202 "action=purge&titles={$mp}|API"
203 => 'apihelp-purge-example-simple',
204 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
205 => 'apihelp-purge-example-generator',
206 ];
207 }
208
209 public function getHelpUrls() {
210 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
211 }
212}
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:65
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:728
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:172
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1458
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1568
This manages continuation state.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:33
__construct(ApiMain $mainModule, $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition ApiPurge.php:46
getAllowedParams( $flags=0)
Definition ApiPurge.php:182
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:197
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:178
execute()
Purges the cache of a page.
Definition ApiPurge.php:60
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiPurge.php:209
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:174
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:79
A title formatter service for MediaWiki.