MediaWiki REL1_41
ApiPurge.php
Go to the documentation of this file.
1<?php
26
31class ApiPurge extends ApiBase {
33 private $mPageSet = null;
34
35 private WikiPageFactory $wikiPageFactory;
36 private TitleFormatter $titleFormatter;
37
44 public function __construct(
45 ApiMain $mainModule,
46 $moduleName,
47 WikiPageFactory $wikiPageFactory,
48 TitleFormatter $titleFormatter
49 ) {
50 parent::__construct( $mainModule, $moduleName );
51 $this->wikiPageFactory = $wikiPageFactory;
52 $this->titleFormatter = $titleFormatter;
53 }
54
58 public function execute() {
59 $authority = $this->getAuthority();
60
61 // Fail early if the user is sitewide blocked.
62 $block = $authority->getBlock();
63 if ( $block && $block->isSitewide() ) {
64 $this->dieBlocked( $block );
65 }
66
67 $params = $this->extractRequestParams();
68
69 $continuationManager = new ApiContinuationManager( $this, [], [] );
70 $this->setContinuationManager( $continuationManager );
71
72 $forceLinkUpdate = $params['forcelinkupdate'];
73 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
74 $pageSet = $this->getPageSet();
75 $pageSet->execute();
76
77 $result = $pageSet->getInvalidTitlesAndRevisions();
78 $userName = $authority->getUser()->getName();
79
80 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
81 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
82 $r = [
83 'ns' => $pageIdentity->getNamespace(),
84 'title' => $title,
85 ];
86 $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
87
88 $authStatus = PermissionStatus::newEmpty();
89 if ( $authority->authorizeWrite( 'purge', $pageIdentity, $authStatus ) ) {
90 // Directly purge and skip the UI part of purge()
91 $page->doPurge();
92 $r['purged'] = true;
93 } else {
94 if ( $authStatus->isRateLimitExceeded() ) {
95 $this->addWarning( 'apierror-ratelimited' );
96 } else {
97 $this->addWarning( Status::wrap( $authStatus )->getMessage() );
98 }
99 }
100
101 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
102 if ( $authority->authorizeWrite( 'linkpurge', $pageIdentity, $authStatus ) ) {
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 ] );
124 $r['linkupdate'] = true;
125 } else {
126 if ( $authStatus->isRateLimitExceeded() ) {
127 $this->addWarning( 'apierror-ratelimited' );
128 } else {
129 $this->addWarning( Status::wrap( $authStatus )->getMessage() );
130 }
131 $forceLinkUpdate = false;
132 }
133 }
134
135 $result[] = $r;
136 }
137 $apiResult = $this->getResult();
138 ApiResult::setIndexedTagName( $result, 'page' );
139 $apiResult->addValue( null, $this->getModuleName(), $result );
140
141 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
142 if ( $values ) {
143 $apiResult->addValue( null, 'normalized', $values );
144 }
145 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
146 if ( $values ) {
147 $apiResult->addValue( null, 'converted', $values );
148 }
149 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
150 if ( $values ) {
151 $apiResult->addValue( null, 'redirects', $values );
152 }
153
154 $this->setContinuationManager( null );
155 $continuationManager->setContinuationIntoResult( $apiResult );
156 }
157
162 private function getPageSet() {
163 $this->mPageSet ??= new ApiPageSet( $this );
164
165 return $this->mPageSet;
166 }
167
168 public function isWriteMode() {
169 return true;
170 }
171
172 public function mustBePosted() {
173 return true;
174 }
175
176 public function getAllowedParams( $flags = 0 ) {
177 $result = [
178 'forcelinkupdate' => false,
179 'forcerecursivelinkupdate' => false,
180 'continue' => [
181 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
182 ],
183 ];
184 if ( $flags ) {
185 $result += $this->getPageSet()->getFinalParams( $flags );
186 }
187
188 return $result;
189 }
190
191 protected function getExamplesMessages() {
192 $title = Title::newMainPage()->getPrefixedText();
193 $mp = rawurlencode( $title );
194
195 return [
196 "action=purge&titles={$mp}|API"
197 => 'apihelp-purge-example-simple',
198 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
199 => 'apihelp-purge-example-generator',
200 ];
201 }
202
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
205 }
206}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:62
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:714
getResult()
Get the result object.
Definition ApiBase.php:667
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:807
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:169
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1433
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:528
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1544
This manages continuation state.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:64
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:31
__construct(ApiMain $mainModule, $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition ApiPurge.php:44
getAllowedParams( $flags=0)
Definition ApiPurge.php:176
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:191
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:172
execute()
Purges the cache of a page.
Definition ApiPurge.php:58
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiPurge.php:203
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:168
Create PSR-3 logger objects.
Service for creating WikiPage objects.
A StatusValue for permission errors.
Represents a title within MediaWiki.
Definition Title.php:76
A title formatter service for MediaWiki.