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->addDeprecation( 'apiwarn-deprecation-purge-get', 'purge-via-GET' );
45 $params = $this->extractRequestParams();
47 $continuationManager = new ApiContinuationManager( $this, [], [] );
48 $this->setContinuationManager( $continuationManager );
50 $forceLinkUpdate = $params['forcelinkupdate'];
51 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
52 $pageSet = $this->getPageSet();
55 $result = $pageSet->getInvalidTitlesAndRevisions();
56 $user = $this->getUser();
58 foreach ( $pageSet->getGoodTitles() as $title ) {
60 ApiQueryBase
::addTitleInfo( $r, $title );
61 $page = WikiPage
::factory( $title );
62 if ( !$user->pingLimiter( 'purge' ) ) {
63 $flags = WikiPage
::PURGE_ALL
;
64 if ( !$this->getRequest()->wasPosted() ) {
65 $flags ^
= WikiPage
::PURGE_GLOBAL_PCACHE
; // skip DB_MASTER write
67 // Directly purge and skip the UI part of purge()
68 $page->doPurge( $flags );
71 $this->addWarning( 'apierror-ratelimited' );
74 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
75 if ( !$user->pingLimiter( 'linkpurge' ) ) {
76 $popts = $page->makeParserOptions( 'canonical' );
78 # Parse content; note that HTML generation is only needed if we want to cache the result.
79 $content = $page->getContent( Revision
::RAW
);
81 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
82 $p_result = $content->getParserOutput(
89 # Logging to better see expensive usage patterns
90 if ( $forceRecursiveLinkUpdate ) {
91 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
92 "Recursive link purge enqueued for {title}",
94 'user' => $this->getUser()->getName(),
95 'title' => $title->getPrefixedText()
100 # Update the links tables
101 $updates = $content->getSecondaryDataUpdates(
102 $title, null, $forceRecursiveLinkUpdate, $p_result );
103 foreach ( $updates as $update ) {
104 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
107 $r['linkupdate'] = true;
109 if ( $enableParserCache ) {
110 $pcache = ParserCache
::singleton();
111 $pcache->save( $p_result, $page, $popts );
115 $this->addWarning( 'apierror-ratelimited' );
116 $forceLinkUpdate = false;
122 $apiResult = $this->getResult();
123 ApiResult
::setIndexedTagName( $result, 'page' );
124 $apiResult->addValue( null, $this->getModuleName(), $result );
126 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
128 $apiResult->addValue( null, 'normalized', $values );
130 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
132 $apiResult->addValue( null, 'converted', $values );
134 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
136 $apiResult->addValue( null, 'redirects', $values );
139 $this->setContinuationManager( null );
140 $continuationManager->setContinuationIntoResult( $apiResult );
144 * Get a cached instance of an ApiPageSet object
147 private function getPageSet() {
148 if ( !isset( $this->mPageSet
) ) {
149 $this->mPageSet
= new ApiPageSet( $this );
152 return $this->mPageSet
;
155 public function isWriteMode() {
159 public function mustBePosted() {
160 // Anonymous users are not allowed a non-POST request
161 return !$this->getUser()->isAllowed( 'purge' );
164 protected function getHelpFlags() {
165 $flags = parent
::getHelpFlags();
167 // Claim that we must be posted for the purposes of help and paraminfo.
168 // @todo Remove this when self::mustBePosted() is updated for T145649
169 if ( !in_array( 'mustbeposted', $flags, true ) ) {
170 $flags[] = 'mustbeposted';
176 public function getAllowedParams( $flags = 0 ) {
178 'forcelinkupdate' => false,
179 'forcerecursivelinkupdate' => false,
181 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
185 $result +
= $this->getPageSet()->getFinalParams( $flags );
191 protected function getExamplesMessages() {
193 'action=purge&titles=Main_Page|API'
194 => 'apihelp-purge-example-simple',
195 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
196 => 'apihelp-purge-example-generator',
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/API:Purge';