3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Logger\LoggerFactory
;
24 * API interface for page purging
27 class ApiPurge
extends ApiBase
{
28 private $mPageSet = null;
31 * Purges the cache of a page
33 public function execute() {
34 $params = $this->extractRequestParams();
36 $continuationManager = new ApiContinuationManager( $this, [], [] );
37 $this->setContinuationManager( $continuationManager );
39 $forceLinkUpdate = $params['forcelinkupdate'];
40 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
41 $pageSet = $this->getPageSet();
44 $result = $pageSet->getInvalidTitlesAndRevisions();
45 $user = $this->getUser();
47 foreach ( $pageSet->getGoodTitles() as $title ) {
49 ApiQueryBase
::addTitleInfo( $r, $title );
50 $page = WikiPage
::factory( $title );
51 if ( !$user->pingLimiter( 'purge' ) ) {
52 // Directly purge and skip the UI part of purge()
56 $this->addWarning( 'apierror-ratelimited' );
59 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
60 if ( !$user->pingLimiter( 'linkpurge' ) ) {
61 # Logging to better see expensive usage patterns
62 if ( $forceRecursiveLinkUpdate ) {
63 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
64 "Recursive link purge enqueued for {title}",
66 'user' => $this->getUser()->getName(),
67 'title' => $title->getPrefixedText()
72 $page->updateParserCache( [
73 'causeAction' => 'api-purge',
74 'causeAgent' => $this->getUser()->getName(),
76 $page->doSecondaryDataUpdates( [
77 'recursive' => $forceRecursiveLinkUpdate,
78 'causeAction' => 'api-purge',
79 'causeAgent' => $this->getUser()->getName(),
80 'defer' => DeferredUpdates
::PRESEND
,
82 $r['linkupdate'] = true;
84 $this->addWarning( 'apierror-ratelimited' );
85 $forceLinkUpdate = false;
91 $apiResult = $this->getResult();
92 ApiResult
::setIndexedTagName( $result, 'page' );
93 $apiResult->addValue( null, $this->getModuleName(), $result );
95 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
97 $apiResult->addValue( null, 'normalized', $values );
99 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
101 $apiResult->addValue( null, 'converted', $values );
103 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
105 $apiResult->addValue( null, 'redirects', $values );
108 $this->setContinuationManager( null );
109 $continuationManager->setContinuationIntoResult( $apiResult );
113 * Get a cached instance of an ApiPageSet object
116 private function getPageSet() {
117 if ( $this->mPageSet
=== null ) {
118 $this->mPageSet
= new ApiPageSet( $this );
121 return $this->mPageSet
;
124 public function isWriteMode() {
128 public function mustBePosted() {
132 public function getAllowedParams( $flags = 0 ) {
134 'forcelinkupdate' => false,
135 'forcerecursivelinkupdate' => false,
137 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
141 $result +
= $this->getPageSet()->getFinalParams( $flags );
147 protected function getExamplesMessages() {
149 'action=purge&titles=Main_Page|API'
150 => 'apihelp-purge-example-simple',
151 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
152 => 'apihelp-purge-example-generator',
156 public function getHelpUrls() {
157 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';