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 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
99 $r['linkupdate'] = true;
101 if ( $enableParserCache ) {
102 $pcache = MediaWikiServices
::getInstance()->getParserCache();
103 $pcache->save( $p_result, $page, $popts );
107 $this->addWarning( 'apierror-ratelimited' );
108 $forceLinkUpdate = false;
114 $apiResult = $this->getResult();
115 ApiResult
::setIndexedTagName( $result, 'page' );
116 $apiResult->addValue( null, $this->getModuleName(), $result );
118 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
120 $apiResult->addValue( null, 'normalized', $values );
122 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
124 $apiResult->addValue( null, 'converted', $values );
126 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
128 $apiResult->addValue( null, 'redirects', $values );
131 $this->setContinuationManager( null );
132 $continuationManager->setContinuationIntoResult( $apiResult );
136 * Get a cached instance of an ApiPageSet object
139 private function getPageSet() {
140 if ( !isset( $this->mPageSet
) ) {
141 $this->mPageSet
= new ApiPageSet( $this );
144 return $this->mPageSet
;
147 public function isWriteMode() {
151 public function mustBePosted() {
155 public function getAllowedParams( $flags = 0 ) {
157 'forcelinkupdate' => false,
158 'forcerecursivelinkupdate' => false,
160 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
164 $result +
= $this->getPageSet()->getFinalParams( $flags );
170 protected function getExamplesMessages() {
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',
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';