MediaWiki  master
ApiPurge.php
Go to the documentation of this file.
1 <?php
23 
28 class ApiPurge extends ApiBase {
30  private $mPageSet = null;
31 
33  private $wikiPageFactory;
34 
36  private $titleFormatter;
37 
44  public function __construct(
45  ApiMain $mainModule,
46  $moduleName,
47  WikiPageFactory $wikiPageFactory,
48  TitleFormatter $titleFormatter
49  ) {
50  parent::__construct( $mainModule, $moduleName );
51  $this->wikiPageFactory = $wikiPageFactory;
52  $this->titleFormatter = $titleFormatter;
53  }
54 
58  public function execute() {
59  $user = $this->getUser();
60 
61  // Fail early if the user is sitewide blocked.
62  $block = $user->getBlock();
63  if ( $block && $block->isSitewide() ) {
64  $this->dieBlocked( $block );
65  }
66 
67  $params = $this->extractRequestParams();
68 
69  $continuationManager = new ApiContinuationManager( $this, [], [] );
70  $this->setContinuationManager( $continuationManager );
71 
72  $forceLinkUpdate = $params['forcelinkupdate'];
73  $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
74  $pageSet = $this->getPageSet();
75  $pageSet->execute();
76 
77  $result = $pageSet->getInvalidTitlesAndRevisions();
78  $userName = $user->getName();
79 
80  foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
81  $title = $this->titleFormatter->getPrefixedText( $pageIdentity );
82  $r = [
83  'ns' => $pageIdentity->getNamespace(),
84  'title' => $title,
85  ];
86  $page = $this->wikiPageFactory->newFromTitle( $pageIdentity );
87  if ( !$user->pingLimiter( 'purge' ) ) {
88  // Directly purge and skip the UI part of purge()
89  $page->doPurge();
90  $r['purged'] = true;
91  } else {
92  $this->addWarning( 'apierror-ratelimited' );
93  }
94 
95  if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
96  if ( !$user->pingLimiter( 'linkpurge' ) ) {
97  # Logging to better see expensive usage patterns
98  if ( $forceRecursiveLinkUpdate ) {
99  LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
100  "Recursive link purge enqueued for {title}",
101  [
102  'user' => $userName,
103  'title' => $title
104  ]
105  );
106  }
107 
108  $page->updateParserCache( [
109  'causeAction' => 'api-purge',
110  'causeAgent' => $userName,
111  ] );
112  $page->doSecondaryDataUpdates( [
113  'recursive' => $forceRecursiveLinkUpdate,
114  'causeAction' => 'api-purge',
115  'causeAgent' => $userName,
116  'defer' => DeferredUpdates::PRESEND,
117  ] );
118  $r['linkupdate'] = true;
119  } else {
120  $this->addWarning( 'apierror-ratelimited' );
121  $forceLinkUpdate = false;
122  }
123  }
124 
125  $result[] = $r;
126  }
127  $apiResult = $this->getResult();
128  ApiResult::setIndexedTagName( $result, 'page' );
129  $apiResult->addValue( null, $this->getModuleName(), $result );
130 
131  $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
132  if ( $values ) {
133  $apiResult->addValue( null, 'normalized', $values );
134  }
135  $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
136  if ( $values ) {
137  $apiResult->addValue( null, 'converted', $values );
138  }
139  $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
140  if ( $values ) {
141  $apiResult->addValue( null, 'redirects', $values );
142  }
143 
144  $this->setContinuationManager( null );
145  $continuationManager->setContinuationIntoResult( $apiResult );
146  }
147 
152  private function getPageSet() {
153  $this->mPageSet ??= new ApiPageSet( $this );
154 
155  return $this->mPageSet;
156  }
157 
158  public function isWriteMode() {
159  return true;
160  }
161 
162  public function mustBePosted() {
163  return true;
164  }
165 
166  public function getAllowedParams( $flags = 0 ) {
167  $result = [
168  'forcelinkupdate' => false,
169  'forcerecursivelinkupdate' => false,
170  'continue' => [
171  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
172  ],
173  ];
174  if ( $flags ) {
175  $result += $this->getPageSet()->getFinalParams( $flags );
176  }
177 
178  return $result;
179  }
180 
181  protected function getExamplesMessages() {
182  return [
183  'action=purge&titles=Main_Page|API'
184  => 'apihelp-purge-example-simple',
185  'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
186  => 'apihelp-purge-example-generator',
187  ];
188  }
189 
190  public function getHelpUrls() {
191  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
192  }
193 }
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:59
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:53
API interface for page purging.
Definition: ApiPurge.php:28
__construct(ApiMain $mainModule, $moduleName, WikiPageFactory $wikiPageFactory, TitleFormatter $titleFormatter)
Definition: ApiPurge.php:44
getAllowedParams( $flags=0)
Definition: ApiPurge.php:166
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiPurge.php:181
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiPurge.php:162
execute()
Purges the cache of a page.
Definition: ApiPurge.php:58
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiPurge.php:190
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiPurge.php:158
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.
A title formatter service for MediaWiki.