MediaWiki REL1_30
ApiPurge.php
Go to the documentation of this file.
1<?php
2
29
34class ApiPurge extends ApiBase {
35 private $mPageSet;
36
40 public function execute() {
42
43 $continuationManager = new ApiContinuationManager( $this, [], [] );
44 $this->setContinuationManager( $continuationManager );
45
46 $forceLinkUpdate = $params['forcelinkupdate'];
47 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
48 $pageSet = $this->getPageSet();
49 $pageSet->execute();
50
51 $result = $pageSet->getInvalidTitlesAndRevisions();
52 $user = $this->getUser();
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 $popts = $page->makeParserOptions( 'canonical' );
69
70 # Parse content; note that HTML generation is only needed if we want to cache the result.
71 $content = $page->getContent( Revision::RAW );
72 if ( $content ) {
73 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
74 $p_result = $content->getParserOutput(
75 $title,
76 $page->getLatest(),
77 $popts,
78 $enableParserCache
79 );
80
81 # Logging to better see expensive usage patterns
82 if ( $forceRecursiveLinkUpdate ) {
83 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
84 "Recursive link purge enqueued for {title}",
85 [
86 'user' => $this->getUser()->getName(),
87 'title' => $title->getPrefixedText()
88 ]
89 );
90 }
91
92 # Update the links tables
93 $updates = $content->getSecondaryDataUpdates(
94 $title, null, $forceRecursiveLinkUpdate, $p_result );
95 foreach ( $updates as $update ) {
96 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
97 }
98
99 $r['linkupdate'] = true;
100
101 if ( $enableParserCache ) {
102 $pcache = MediaWikiServices::getInstance()->getParserCache();
103 $pcache->save( $p_result, $page, $popts );
104 }
105 }
106 } else {
107 $this->addWarning( 'apierror-ratelimited' );
108 $forceLinkUpdate = false;
109 }
110 }
111
112 $result[] = $r;
113 }
114 $apiResult = $this->getResult();
115 ApiResult::setIndexedTagName( $result, 'page' );
116 $apiResult->addValue( null, $this->getModuleName(), $result );
117
118 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
119 if ( $values ) {
120 $apiResult->addValue( null, 'normalized', $values );
121 }
122 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
123 if ( $values ) {
124 $apiResult->addValue( null, 'converted', $values );
125 }
126 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
127 if ( $values ) {
128 $apiResult->addValue( null, 'redirects', $values );
129 }
130
131 $this->setContinuationManager( null );
132 $continuationManager->setContinuationIntoResult( $apiResult );
133 }
134
139 private function getPageSet() {
140 if ( !isset( $this->mPageSet ) ) {
141 $this->mPageSet = new ApiPageSet( $this );
142 }
143
144 return $this->mPageSet;
145 }
146
147 public function isWriteMode() {
148 return true;
149 }
150
151 public function mustBePosted() {
152 return true;
153 }
154
155 public function getAllowedParams( $flags = 0 ) {
156 $result = [
157 'forcelinkupdate' => false,
158 'forcerecursivelinkupdate' => false,
159 'continue' => [
160 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
161 ],
162 ];
163 if ( $flags ) {
164 $result += $this->getPageSet()->getFinalParams( $flags );
165 }
166
167 return $result;
168 }
169
170 protected function getExamplesMessages() {
171 return [
172 'action=purge&titles=Main_Page|API'
173 => 'apihelp-purge-example-simple',
174 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
175 => 'apihelp-purge-example-generator',
176 ];
177 }
178
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
181 }
182}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:41
setContinuationManager( $manager)
Set the continuation manager.
Definition ApiBase.php:686
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:740
getResult()
Get the result object.
Definition ApiBase.php:632
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:128
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1779
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:512
This manages continuation state.
This class contains a list of pages that the client has requested.
API interface for page purging.
Definition ApiPurge.php:34
getAllowedParams( $flags=0)
Definition ApiPurge.php:155
getExamplesMessages()
Returns usage examples for this module.
Definition ApiPurge.php:170
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition ApiPurge.php:139
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPurge.php:151
execute()
Purges the cache of a page.
Definition ApiPurge.php:40
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiPurge.php:179
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiPurge.php:147
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
getUser()
Get the User object.
getConfig()
Get the Config object.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const RAW
Definition Revision.php:100
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:121
namespace being checked & $result
Definition hooks.txt:2293
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2805
$params