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