4 * API for MediaWiki 1.14+
6 * Created on Sep 2, 2008
8 * Copyright © 2008 Chad Horohoe
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
27 use MediaWiki\Logger\LoggerFactory
;
28 use MediaWiki\MediaWikiServices
;
31 * API interface for page purging
34 class ApiPurge
extends ApiBase
{
38 * Purges the cache of a page
40 public function execute() {
41 $params = $this->extractRequestParams();
43 $continuationManager = new ApiContinuationManager( $this, [], [] );
44 $this->setContinuationManager( $continuationManager );
46 $forceLinkUpdate = $params['forcelinkupdate'];
47 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
48 $pageSet = $this->getPageSet();
51 $result = $pageSet->getInvalidTitlesAndRevisions();
52 $user = $this->getUser();
54 foreach ( $pageSet->getGoodTitles() as $title ) {
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()
63 $this->addWarning( 'apierror-ratelimited' );
66 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
67 if ( !$user->pingLimiter( 'linkpurge' ) ) {
68 $popts = $page->makeParserOptions( 'canonical' );
70 # Parse content; note that HTML generation is only needed if we want to cache the result.
71 $content = $page->getContent( Revision
::RAW
);
73 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
74 $p_result = $content->getParserOutput(
81 # Logging to better see expensive usage patterns
82 if ( $forceRecursiveLinkUpdate ) {
83 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
84 "Recursive link purge enqueued for {title}",
86 'user' => $this->getUser()->getName(),
87 'title' => $title->getPrefixedText()
92 # Update the links tables
93 $updates = $content->getSecondaryDataUpdates(
94 $title, null, $forceRecursiveLinkUpdate, $p_result );
95 foreach ( $updates as $update ) {
96 $update->setCause( 'api-purge', $this->getUser()->getName() );
97 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
100 $r['linkupdate'] = true;
102 if ( $enableParserCache ) {
103 $pcache = MediaWikiServices
::getInstance()->getParserCache();
104 $pcache->save( $p_result, $page, $popts );
108 $this->addWarning( 'apierror-ratelimited' );
109 $forceLinkUpdate = false;
115 $apiResult = $this->getResult();
116 ApiResult
::setIndexedTagName( $result, 'page' );
117 $apiResult->addValue( null, $this->getModuleName(), $result );
119 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
121 $apiResult->addValue( null, 'normalized', $values );
123 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
125 $apiResult->addValue( null, 'converted', $values );
127 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
129 $apiResult->addValue( null, 'redirects', $values );
132 $this->setContinuationManager( null );
133 $continuationManager->setContinuationIntoResult( $apiResult );
137 * Get a cached instance of an ApiPageSet object
140 private function getPageSet() {
141 if ( !isset( $this->mPageSet
) ) {
142 $this->mPageSet
= new ApiPageSet( $this );
145 return $this->mPageSet
;
148 public function isWriteMode() {
152 public function mustBePosted() {
156 public function getAllowedParams( $flags = 0 ) {
158 'forcelinkupdate' => false,
159 'forcerecursivelinkupdate' => false,
161 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
165 $result +
= $this->getPageSet()->getFinalParams( $flags );
171 protected function getExamplesMessages() {
173 'action=purge&titles=Main_Page|API'
174 => 'apihelp-purge-example-simple',
175 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
176 => 'apihelp-purge-example-generator',
180 public function getHelpUrls() {
181 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';