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 private WikiPageFactory $wikiPageFactory;
26 private TitleFormatter $titleFormatter;
27
28 public function __construct(
29 ApiMain $mainModule,
30 string $moduleName,
31 WikiPageFactory $wikiPageFactory,
32 TitleFormatter $titleFormatter
33 ) {
34 parent::__construct( $mainModule, $moduleName );
35 $this->wikiPageFactory = $wikiPageFactory;
36 $this->titleFormatter = $titleFormatter;
37 }
38
42 public function execute() {
43 $authority = $this->getAuthority();
44
45 // Fail early if the user is sitewide blocked.
46 $block = $authority->getBlock();
47 if ( $block && $block->isSitewide() ) {
48 $this->dieBlocked( $block );
49 }
50
51 $params = $this->extractRequestParams();
52
53 $continuationManager = new ApiContinuationManager( $this, [], [] );
54 $this->setContinuationManager( $continuationManager );
55
56 $forceLinkUpdate = $params['forcelinkupdate'];
57 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
58 $pageSet = $this->getPageSet();
59 $pageSet->execute();
60
61 $result = $pageSet->getInvalidTitlesAndRevisions();
62 $userName = $authority->getUser()->getName();
63 $now = wfTimestampNow();
64
65 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
66 $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
67 $r = [
68 'ns' => $pageIdentity->getNamespace(),
69 'title' => $title,
70 ];
71 $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
72
73 $purgeAuthStatus = PermissionStatus::newEmpty();
74 if ( $authority->authorizeAction( 'purge', $purgeAuthStatus ) ) {
75 // Directly purge and skip the UI part of purge()
76 $page->doPurge();
77 $r['purged'] = true;
78 } else {
79 if ( $purgeAuthStatus->isRateLimitExceeded() ) {
80 $this->addWarning( 'apierror-ratelimited' );
81 } else {
82 $this->addWarning( Status::wrap( $purgeAuthStatus )->getMessage() );
83 }
84 }
85
86 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
87 $linkpurgeAuthStatus = PermissionStatus::newEmpty();
88 if ( $authority->authorizeAction( 'linkpurge', $linkpurgeAuthStatus ) ) {
89 # Logging to better see expensive usage patterns
90 if ( $forceRecursiveLinkUpdate ) {
91 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
92 "Recursive link purge enqueued for {title}",
93 [
94 'user' => $userName,
95 'title' => $title
96 ]
97 );
98 }
99
100 $page->updateParserCache( [
101 'causeAction' => 'api-purge',
102 'causeAgent' => $userName,
103 ] );
104 $page->doSecondaryDataUpdates( [
105 'recursive' => $forceRecursiveLinkUpdate,
106 'causeAction' => 'api-purge',
107 'causeAgent' => $userName,
108 'defer' => DeferredUpdates::PRESEND,
109 'freshness' => $now,
110 ] );
111 $r['linkupdate'] = true;
112 } else {
113 if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) {
114 $this->addWarning( 'apierror-ratelimited' );
115 $forceLinkUpdate = false;
116 $forceRecursiveLinkUpdate = false;
117 } else {
118 $this->addWarning( Status::wrap( $linkpurgeAuthStatus )->getMessage() );
119 }
120 }
121 }
122
123 $result[] = $r;
124 }
125 $apiResult = $this->getResult();
126 ApiResult::setIndexedTagName( $result, 'page' );
127 $apiResult->addValue( null, $this->getModuleName(), $result );
128
129 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
130 if ( $values ) {
131 $apiResult->addValue( null, 'normalized', $values );
132 }
133 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
134 if ( $values ) {
135 $apiResult->addValue( null, 'converted', $values );
136 }
137 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
138 if ( $values ) {
139 $apiResult->addValue( null, 'redirects', $values );
140 }
141
142 $this->setContinuationManager( null );
143 $continuationManager->setContinuationIntoResult( $apiResult );
144 }
145
150 private function getPageSet() {
151 $this->mPageSet ??= new ApiPageSet( $this );
152
153 return $this->mPageSet;
154 }
155
157 public function isWriteMode() {
158 return true;
159 }
160
162 public function mustBePosted() {
163 return true;
164 }
165
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
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
196 public function getHelpUrls() {
197 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
198 }
199}
200
202class_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:61
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getResult()
Get the result object.
Definition ApiBase.php:682
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1429
setContinuationManager(?ApiContinuationManager $manager=null)
Definition ApiBase.php:729
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1539
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
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:21
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
Definition ApiPurge.php:183
__construct(ApiMain $mainModule, string $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition ApiPurge.php:28
execute()
Purges the cache of a page.
Definition ApiPurge.php:42
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
Definition ApiPurge.php:162
getAllowedParams( $flags=0)
Definition ApiPurge.php:167
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
Definition ApiPurge.php:157
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
Definition ApiPurge.php:196
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