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