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