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
;
30 * API interface for page purging
33 class ApiPurge
extends ApiBase
{
37 * Purges the cache of a page
39 public function execute() {
40 $params = $this->extractRequestParams();
42 $continuationManager = new ApiContinuationManager( $this, [], [] );
43 $this->setContinuationManager( $continuationManager );
45 $forceLinkUpdate = $params['forcelinkupdate'];
46 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
47 $pageSet = $this->getPageSet();
50 $result = $pageSet->getInvalidTitlesAndRevisions();
51 $user = $this->getUser();
53 foreach ( $pageSet->getGoodTitles() as $title ) {
55 ApiQueryBase
::addTitleInfo( $r, $title );
56 $page = WikiPage
::factory( $title );
57 if ( !$user->pingLimiter( 'purge' ) ) {
58 $flags = WikiPage
::PURGE_ALL
;
59 if ( !$this->getRequest()->wasPosted() ) {
60 $flags ^
= WikiPage
::PURGE_GLOBAL_PCACHE
; // skip DB_MASTER write
62 // Directly purge and skip the UI part of purge()
63 $page->doPurge( $flags );
66 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
67 $this->setWarning( $error['info'] );
70 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
71 if ( !$user->pingLimiter( 'linkpurge' ) ) {
72 $popts = $page->makeParserOptions( 'canonical' );
74 # Parse content; note that HTML generation is only needed if we want to cache the result.
75 $content = $page->getContent( Revision
::RAW
);
77 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
78 $p_result = $content->getParserOutput(
85 # Logging to better see expensive usage patterns
86 if ( $forceRecursiveLinkUpdate ) {
87 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
88 "Recursive link purge enqueued for {title}",
90 'user' => $this->getUser()->getName(),
91 'title' => $title->getPrefixedText()
96 # Update the links tables
97 $updates = $content->getSecondaryDataUpdates(
98 $title, null, $forceRecursiveLinkUpdate, $p_result );
99 foreach ( $updates as $update ) {
100 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
103 $r['linkupdate'] = true;
105 if ( $enableParserCache ) {
106 $pcache = ParserCache
::singleton();
107 $pcache->save( $p_result, $page, $popts );
111 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
112 $this->setWarning( $error['info'] );
113 $forceLinkUpdate = false;
119 $apiResult = $this->getResult();
120 ApiResult
::setIndexedTagName( $result, 'page' );
121 $apiResult->addValue( null, $this->getModuleName(), $result );
123 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
125 $apiResult->addValue( null, 'normalized', $values );
127 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
129 $apiResult->addValue( null, 'converted', $values );
131 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
133 $apiResult->addValue( null, 'redirects', $values );
136 $this->setContinuationManager( null );
137 $continuationManager->setContinuationIntoResult( $apiResult );
141 * Get a cached instance of an ApiPageSet object
144 private function getPageSet() {
145 if ( !isset( $this->mPageSet
) ) {
146 $this->mPageSet
= new ApiPageSet( $this );
149 return $this->mPageSet
;
152 public function isWriteMode() {
156 public function mustBePosted() {
157 // Anonymous users are not allowed a non-POST request
158 return !$this->getUser()->isAllowed( 'purge' );
161 public function getAllowedParams( $flags = 0 ) {
163 'forcelinkupdate' => false,
164 'forcerecursivelinkupdate' => false,
166 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
170 $result +
= $this->getPageSet()->getFinalParams( $flags );
176 protected function getExamplesMessages() {
178 'action=purge&titles=Main_Page|API'
179 => 'apihelp-purge-example-simple',
180 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
181 => 'apihelp-purge-example-generator',
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/API:Purge';