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 $main = $this->getMain();
41 if ( !$main->isInternalMode() && !$main->getRequest()->wasPosted() ) {
42 $this->logFeatureUsage( 'purge-via-GET' );
43 $this->setWarning( 'Use of action=purge via GET is deprecated. Use POST instead.' );
46 $params = $this->extractRequestParams();
48 $continuationManager = new ApiContinuationManager( $this, [], [] );
49 $this->setContinuationManager( $continuationManager );
51 $forceLinkUpdate = $params['forcelinkupdate'];
52 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
53 $pageSet = $this->getPageSet();
56 $result = $pageSet->getInvalidTitlesAndRevisions();
57 $user = $this->getUser();
59 foreach ( $pageSet->getGoodTitles() as $title ) {
61 ApiQueryBase
::addTitleInfo( $r, $title );
62 $page = WikiPage
::factory( $title );
63 if ( !$user->pingLimiter( 'purge' ) ) {
64 $flags = WikiPage
::PURGE_ALL
;
65 if ( !$this->getRequest()->wasPosted() ) {
66 $flags ^
= WikiPage
::PURGE_GLOBAL_PCACHE
; // skip DB_MASTER write
68 // Directly purge and skip the UI part of purge()
69 $page->doPurge( $flags );
72 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
73 $this->setWarning( $error['info'] );
76 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
77 if ( !$user->pingLimiter( 'linkpurge' ) ) {
78 $popts = $page->makeParserOptions( 'canonical' );
80 # Parse content; note that HTML generation is only needed if we want to cache the result.
81 $content = $page->getContent( Revision
::RAW
);
83 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
84 $p_result = $content->getParserOutput(
91 # Logging to better see expensive usage patterns
92 if ( $forceRecursiveLinkUpdate ) {
93 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
94 "Recursive link purge enqueued for {title}",
96 'user' => $this->getUser()->getName(),
97 'title' => $title->getPrefixedText()
102 # Update the links tables
103 $updates = $content->getSecondaryDataUpdates(
104 $title, null, $forceRecursiveLinkUpdate, $p_result );
105 foreach ( $updates as $update ) {
106 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
109 $r['linkupdate'] = true;
111 if ( $enableParserCache ) {
112 $pcache = ParserCache
::singleton();
113 $pcache->save( $p_result, $page, $popts );
117 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
118 $this->setWarning( $error['info'] );
119 $forceLinkUpdate = false;
125 $apiResult = $this->getResult();
126 ApiResult
::setIndexedTagName( $result, 'page' );
127 $apiResult->addValue( null, $this->getModuleName(), $result );
129 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
131 $apiResult->addValue( null, 'normalized', $values );
133 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
135 $apiResult->addValue( null, 'converted', $values );
137 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
139 $apiResult->addValue( null, 'redirects', $values );
142 $this->setContinuationManager( null );
143 $continuationManager->setContinuationIntoResult( $apiResult );
147 * Get a cached instance of an ApiPageSet object
150 private function getPageSet() {
151 if ( !isset( $this->mPageSet
) ) {
152 $this->mPageSet
= new ApiPageSet( $this );
155 return $this->mPageSet
;
158 public function isWriteMode() {
162 public function mustBePosted() {
163 // Anonymous users are not allowed a non-POST request
164 return !$this->getUser()->isAllowed( 'purge' );
167 protected function getHelpFlags() {
168 $flags = parent
::getHelpFlags();
170 // Claim that we must be posted for the purposes of help and paraminfo.
171 // @todo Remove this when self::mustBePosted() is updated for T145649
172 if ( !in_array( 'mustbeposted', $flags, true ) ) {
173 $flags[] = 'mustbeposted';
179 public function getAllowedParams( $flags = 0 ) {
181 'forcelinkupdate' => false,
182 'forcerecursivelinkupdate' => false,
184 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
188 $result +
= $this->getPageSet()->getFinalParams( $flags );
194 protected function getExamplesMessages() {
196 'action=purge&titles=Main_Page|API'
197 => 'apihelp-purge-example-simple',
198 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
199 => 'apihelp-purge-example-generator',
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Purge';