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 $authStatus = PermissionStatus::newEmpty();
92 if ( $authority->authorizeAction( 'purge', $authStatus ) ) {
93 // Directly purge and skip the UI part of purge()
94 $page->doPurge();
95 $r['purged'] = true;
96 } else {
97 if ( $authStatus->isRateLimitExceeded() ) {
98 $this->addWarning( 'apierror-ratelimited' );
99 } else {
100 $this->addWarning( Status::wrap( $authStatus )->getMessage() );
101 }
102 }
103
104 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
105 if ( $authority->authorizeAction( 'linkpurge', $authStatus ) ) {
106 # Logging to better see expensive usage patterns
107 if ( $forceRecursiveLinkUpdate ) {
108 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
109 "Recursive link purge enqueued for {title}",
110 [
111 'user' => $userName,
112 'title' => $title
113 ]
114 );
115 }
116
117 $page->updateParserCache( [
118 'causeAction' => 'api-purge',
119 'causeAgent' => $userName,
120 ] );
121 $page->doSecondaryDataUpdates( [
122 'recursive' => $forceRecursiveLinkUpdate,
123 'causeAction' => 'api-purge',
124 'causeAgent' => $userName,
125 'defer' => DeferredUpdates::PRESEND,
126 'freshness' => $now,
127 ] );
128 $r['linkupdate'] = true;
129 } else {
130 if ( $authStatus->isRateLimitExceeded() ) {
131 $this->addWarning( 'apierror-ratelimited' );
132 $forceLinkUpdate = false;
133 $forceRecursiveLinkUpdate = false;
134 } else {
135 $this->addWarning( Status::wrap( $authStatus )->getMessage() );
136 }
137 }
138 }
139
140 $result[] = $r;
141 }
142 $apiResult = $this->getResult();
143 ApiResult::setIndexedTagName( $result, 'page' );
144 $apiResult->addValue( null, $this->getModuleName(), $result );
145
146 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
147 if ( $values ) {
148 $apiResult->addValue( null, 'normalized', $values );
149 }
150 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
151 if ( $values ) {
152 $apiResult->addValue( null, 'converted', $values );
153 }
154 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
155 if ( $values ) {
156 $apiResult->addValue( null, 'redirects', $values );
157 }
158
159 $this->setContinuationManager( null );
160 $continuationManager->setContinuationIntoResult( $apiResult );
161 }
162
167 private function getPageSet() {
168 $this->mPageSet ??= new ApiPageSet( $this );
169
170 return $this->mPageSet;
171 }
172
173 public function isWriteMode() {
174 return true;
175 }
176
177 public function mustBePosted() {
178 return true;
179 }
180
181 public function getAllowedParams( $flags = 0 ) {
182 $result = [
183 'forcelinkupdate' => false,
184 'forcerecursivelinkupdate' => false,
185 'continue' => [
186 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
187 ],
188 ];
189 if ( $flags ) {
190 $result += $this->getPageSet()->getFinalParams( $flags );
191 }
192
193 return $result;
194 }
195
196 protected function getExamplesMessages() {
197 $title = Title::newMainPage()->getPrefixedText();
198 $mp = rawurlencode( $title );
199
200 return [
201 "action=purge&titles={$mp}|API"
202 => 'apihelp-purge-example-simple',
203 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
204 => 'apihelp-purge-example-generator',
205 ];
206 }
207
208 public function getHelpUrls() {
209 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
210 }
211}
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:64
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:718
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1451
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1562
This manages continuation state.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
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:181
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:196
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:177
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:208
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:173
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.