MediaWiki master
ApiPurge.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Api;
8
16
21class ApiPurge extends ApiBase {
23 private $mPageSet = null;
24
25 public function __construct(
26 ApiMain $mainModule,
27 string $moduleName,
28 private readonly WikiPageFactory $wikiPageFactory,
29 private readonly TitleFormatter $titleFormatter,
30 ) {
31 parent::__construct( $mainModule, $moduleName );
32 }
33
37 public function execute() {
38 $authority = $this->getAuthority();
39
40 // Fail early if the user is sitewide blocked.
41 $block = $authority->getBlock();
42 if ( $block && $block->isSitewide() ) {
43 $this->dieBlocked( $block );
44 }
45
46 $params = $this->extractRequestParams();
47
48 $continuationManager = new ApiContinuationManager( $this, [], [] );
49 $this->setContinuationManager( $continuationManager );
50
51 $forceLinkUpdate = $params['forcelinkupdate'];
52 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
53 $pageSet = $this->getPageSet();
54 $pageSet->execute();
55
56 $result = $pageSet->getInvalidTitlesAndRevisions();
57 $userName = $authority->getUser()->getName();
58 $now = wfTimestampNow();
59
60 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
61 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
62 $r = [
63 'ns' => $pageIdentity->getNamespace(),
64 'title' => $title,
65 ];
66 $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
67
68 $purgeAuthStatus = PermissionStatus::newEmpty();
69 if ( $authority->authorizeAction( 'purge', $purgeAuthStatus ) ) {
70 // Directly purge and skip the UI part of purge()
71 $page->doPurge();
72 $r['purged'] = true;
73 } else {
74 if ( $purgeAuthStatus->isRateLimitExceeded() ) {
75 $this->addWarning( 'apierror-ratelimited' );
76 } else {
77 $this->addWarning( Status::wrap( $purgeAuthStatus )->getMessage() );
78 }
79 }
80
81 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
82 $linkpurgeAuthStatus = PermissionStatus::newEmpty();
83 if ( $authority->authorizeAction( 'linkpurge', $linkpurgeAuthStatus ) ) {
84 # Logging to better see expensive usage patterns
85 if ( $forceRecursiveLinkUpdate ) {
86 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
87 "Recursive link purge enqueued for {title}",
88 [
89 'user' => $userName,
90 'title' => $title
91 ]
92 );
93 }
94
95 $page->updateParserCache( [
96 'causeAction' => 'api-purge',
97 'causeAgent' => $userName,
98 ] );
99 $page->doSecondaryDataUpdates( [
100 'recursive' => $forceRecursiveLinkUpdate,
101 'causeAction' => 'api-purge',
102 'causeAgent' => $userName,
103 'defer' => DeferredUpdates::PRESEND,
104 'freshness' => $now,
105 ] );
106 $r['linkupdate'] = true;
107 } else {
108 if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) {
109 $this->addWarning( 'apierror-ratelimited' );
110 $forceLinkUpdate = false;
111 $forceRecursiveLinkUpdate = false;
112 } else {
113 $this->addWarning( Status::wrap( $linkpurgeAuthStatus )->getMessage() );
114 }
115 }
116 }
117
118 $result[] = $r;
119 }
120 $apiResult = $this->getResult();
121 ApiResult::setIndexedTagName( $result, 'page' );
122 $apiResult->addValue( null, $this->getModuleName(), $result );
123
124 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
125 if ( $values ) {
126 $apiResult->addValue( null, 'normalized', $values );
127 }
128 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
129 if ( $values ) {
130 $apiResult->addValue( null, 'converted', $values );
131 }
132 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
133 if ( $values ) {
134 $apiResult->addValue( null, 'redirects', $values );
135 }
136
137 $this->setContinuationManager( null );
138 $continuationManager->setContinuationIntoResult( $apiResult );
139 }
140
145 private function getPageSet() {
146 $this->mPageSet ??= new ApiPageSet( $this );
147
148 return $this->mPageSet;
149 }
150
152 public function isWriteMode() {
153 return true;
154 }
155
157 public function mustBePosted() {
158 return true;
159 }
160
162 public function getAllowedParams( $flags = 0 ) {
163 $result = [
164 'forcelinkupdate' => false,
165 'forcerecursivelinkupdate' => false,
166 'continue' => [
167 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
168 ],
169 ];
170 if ( $flags ) {
171 $result += $this->getPageSet()->getFinalParams( $flags );
172 }
173
174 return $result;
175 }
176
178 protected function getExamplesMessages() {
179 $title = Title::newMainPage()->getPrefixedText();
180 $mp = rawurlencode( $title );
181
182 return [
183 "action=purge&titles={$mp}|API"
184 => 'apihelp-purge-example-simple',
185 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
186 => 'apihelp-purge-example-generator',
187 ];
188 }
189
191 public function getHelpUrls() {
192 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
193 }
194}
195
197class_alias( ApiPurge::class, 'ApiPurge' );
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1439
setContinuationManager(?ApiContinuationManager $manager=null)
Definition ApiBase.php:743
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1550
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:21
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
Definition ApiPurge.php:178
execute()
Purges the cache of a page.
Definition ApiPurge.php:37
__construct(ApiMain $mainModule, string $moduleName, private readonly WikiPageFactory $wikiPageFactory, private readonly TitleFormatter $titleFormatter,)
Definition ApiPurge.php:25
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
Definition ApiPurge.php:157
getAllowedParams( $flags=0)
Definition ApiPurge.php:162
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
Definition ApiPurge.php:152
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
Definition ApiPurge.php:191
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
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:44
A title formatter service for MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:69