MediaWiki REL1_39
ApiPurge.php
Go to the documentation of this file.
1<?php
23
28class ApiPurge extends ApiBase {
30 private $mPageSet = null;
31
33 private $wikiPageFactory;
34
36 private $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 $user = $this->getUser();
60
61 // Fail early if the user is sitewide blocked.
62 $block = $user->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 = $user->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 if ( !$user->pingLimiter( 'purge' ) ) {
88 // Directly purge and skip the UI part of purge()
89 $page->doPurge();
90 $r['purged'] = true;
91 } else {
92 $this->addWarning( 'apierror-ratelimited' );
93 }
94
95 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
96 if ( !$user->pingLimiter( 'linkpurge' ) ) {
97 # Logging to better see expensive usage patterns
98 if ( $forceRecursiveLinkUpdate ) {
99 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
100 "Recursive link purge enqueued for {title}",
101 [
102 'user' => $userName,
103 'title' => $title
104 ]
105 );
106 }
107
108 $page->updateParserCache( [
109 'causeAction' => 'api-purge',
110 'causeAgent' => $userName,
111 ] );
112 $page->doSecondaryDataUpdates( [
113 'recursive' => $forceRecursiveLinkUpdate,
114 'causeAction' => 'api-purge',
115 'causeAgent' => $userName,
116 'defer' => DeferredUpdates::PRESEND,
117 ] );
118 $r['linkupdate'] = true;
119 } else {
120 $this->addWarning( 'apierror-ratelimited' );
121 $forceLinkUpdate = false;
122 }
123 }
124
125 $result[] = $r;
126 }
127 $apiResult = $this->getResult();
128 ApiResult::setIndexedTagName( $result, 'page' );
129 $apiResult->addValue( null, $this->getModuleName(), $result );
130
131 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
132 if ( $values ) {
133 $apiResult->addValue( null, 'normalized', $values );
134 }
135 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
136 if ( $values ) {
137 $apiResult->addValue( null, 'converted', $values );
138 }
139 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
140 if ( $values ) {
141 $apiResult->addValue( null, 'redirects', $values );
142 }
143
144 $this->setContinuationManager( null );
145 $continuationManager->setContinuationIntoResult( $apiResult );
146 }
147
152 private function getPageSet() {
153 if ( $this->mPageSet === null ) {
154 $this->mPageSet = new ApiPageSet( $this );
155 }
156
157 return $this->mPageSet;
158 }
159
160 public function isWriteMode() {
161 return true;
162 }
163
164 public function mustBePosted() {
165 return true;
166 }
167
168 public function getAllowedParams( $flags = 0 ) {
169 $result = [
170 'forcelinkupdate' => false,
171 'forcerecursivelinkupdate' => false,
172 'continue' => [
173 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
174 ],
175 ];
176 if ( $flags ) {
177 $result += $this->getPageSet()->getFinalParams( $flags );
178 }
179
180 return $result;
181 }
182
183 protected function getExamplesMessages() {
184 $title = Title::newMainPage()->getPrefixedText();
185 $mp = rawurlencode( $title );
186
187 return [
188 "action=purge&titles={$mp}|API"
189 => 'apihelp-purge-example-simple',
190 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
191 => 'apihelp-purge-example-generator',
192 ];
193 }
194
195 public function getHelpUrls() {
196 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
197 }
198}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:56
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:673
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1372
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1483
This manages continuation state.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:28
__construct(ApiMain $mainModule, $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition ApiPurge.php:44
getAllowedParams( $flags=0)
Definition ApiPurge.php:168
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:183
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:164
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:195
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:160
PSR-3 logger instance factory.
Service for creating WikiPage objects.
A title formatter service for MediaWiki.