MediaWiki REL1_35
ApiPurge.php
Go to the documentation of this file.
1<?php
22
27class ApiPurge extends ApiBase {
28 private $mPageSet = null;
29
33 public function execute() {
34 $user = $this->getUser();
35
36 // Fail early if the user is sitewide blocked.
37 $block = $user->getBlock();
38 if ( $block && $block->isSitewide() ) {
39 $this->dieBlocked( $block );
40 }
41
42 $params = $this->extractRequestParams();
43
44 $continuationManager = new ApiContinuationManager( $this, [], [] );
45 $this->setContinuationManager( $continuationManager );
46
47 $forceLinkUpdate = $params['forcelinkupdate'];
48 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
49 $pageSet = $this->getPageSet();
50 $pageSet->execute();
51
52 $result = $pageSet->getInvalidTitlesAndRevisions();
53
54 foreach ( $pageSet->getGoodTitles() as $title ) {
55 $r = [];
56 ApiQueryBase::addTitleInfo( $r, $title );
57 $page = WikiPage::factory( $title );
58 if ( !$user->pingLimiter( 'purge' ) ) {
59 // Directly purge and skip the UI part of purge()
60 $page->doPurge();
61 $r['purged'] = true;
62 } else {
63 $this->addWarning( 'apierror-ratelimited' );
64 }
65
66 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
67 if ( !$user->pingLimiter( 'linkpurge' ) ) {
68 # Logging to better see expensive usage patterns
69 if ( $forceRecursiveLinkUpdate ) {
70 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
71 "Recursive link purge enqueued for {title}",
72 [
73 'user' => $this->getUser()->getName(),
74 'title' => $title->getPrefixedText()
75 ]
76 );
77 }
78
79 $page->updateParserCache( [
80 'causeAction' => 'api-purge',
81 'causeAgent' => $this->getUser()->getName(),
82 ] );
83 $page->doSecondaryDataUpdates( [
84 'recursive' => $forceRecursiveLinkUpdate,
85 'causeAction' => 'api-purge',
86 'causeAgent' => $this->getUser()->getName(),
87 'defer' => DeferredUpdates::PRESEND,
88 ] );
89 $r['linkupdate'] = true;
90 } else {
91 $this->addWarning( 'apierror-ratelimited' );
92 $forceLinkUpdate = false;
93 }
94 }
95
96 $result[] = $r;
97 }
98 $apiResult = $this->getResult();
99 ApiResult::setIndexedTagName( $result, 'page' );
100 $apiResult->addValue( null, $this->getModuleName(), $result );
101
102 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
103 if ( $values ) {
104 $apiResult->addValue( null, 'normalized', $values );
105 }
106 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
107 if ( $values ) {
108 $apiResult->addValue( null, 'converted', $values );
109 }
110 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
111 if ( $values ) {
112 $apiResult->addValue( null, 'redirects', $values );
113 }
114
115 $this->setContinuationManager( null );
116 $continuationManager->setContinuationIntoResult( $apiResult );
117 }
118
123 private function getPageSet() {
124 if ( $this->mPageSet === null ) {
125 $this->mPageSet = new ApiPageSet( $this );
126 }
127
128 return $this->mPageSet;
129 }
130
131 public function isWriteMode() {
132 return true;
133 }
134
135 public function mustBePosted() {
136 return true;
137 }
138
139 public function getAllowedParams( $flags = 0 ) {
140 $result = [
141 'forcelinkupdate' => false,
142 'forcerecursivelinkupdate' => false,
143 'continue' => [
144 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
145 ],
146 ];
147 if ( $flags ) {
148 $result += $this->getPageSet()->getFinalParams( $flags );
149 }
150
151 return $result;
152 }
153
154 protected function getExamplesMessages() {
155 return [
156 'action=purge&titles=Main_Page|API'
157 => 'apihelp-purge-example-simple',
158 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
159 => 'apihelp-purge-example-generator',
160 ];
161 }
162
163 public function getHelpUrls() {
164 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
165 }
166}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1464
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition ApiBase.php:676
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1356
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
This manages continuation state.
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:27
getAllowedParams( $flags=0)
Definition ApiPurge.php:139
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:154
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition ApiPurge.php:123
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition ApiPurge.php:135
execute()
Purges the cache of a page.
Definition ApiPurge.php:33
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiPurge.php:163
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:131
getUser()
Stable to override.
PSR-3 logger instance factory.