Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / api / ApiPurge.php
blob312463835c21646dc8c8b7d8c1662180fc420b31
1 <?php
3 /**
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
25 * @file
27 use MediaWiki\Logger\LoggerFactory;
29 /**
30 * API interface for page purging
31 * @ingroup API
33 class ApiPurge extends ApiBase {
34 private $mPageSet;
36 /**
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();
48 $pageSet->execute();
50 $result = $pageSet->getInvalidTitlesAndRevisions();
51 $user = $this->getUser();
53 foreach ( $pageSet->getGoodTitles() as $title ) {
54 $r = [];
55 ApiQueryBase::addTitleInfo( $r, $title );
56 $page = WikiPage::factory( $title );
57 if ( !$user->pingLimiter( 'purge' ) ) {
58 // Directly purge and skip the UI part of purge()
59 $page->doPurge( WikiPage::PURGE_ALL );
60 $r['purged'] = true;
61 } else {
62 $this->addWarning( 'apierror-ratelimited' );
65 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
66 if ( !$user->pingLimiter( 'linkpurge' ) ) {
67 $popts = $page->makeParserOptions( 'canonical' );
69 # Parse content; note that HTML generation is only needed if we want to cache the result.
70 $content = $page->getContent( Revision::RAW );
71 if ( $content ) {
72 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
73 $p_result = $content->getParserOutput(
74 $title,
75 $page->getLatest(),
76 $popts,
77 $enableParserCache
80 # Logging to better see expensive usage patterns
81 if ( $forceRecursiveLinkUpdate ) {
82 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
83 "Recursive link purge enqueued for {title}",
85 'user' => $this->getUser()->getName(),
86 'title' => $title->getPrefixedText()
91 # Update the links tables
92 $updates = $content->getSecondaryDataUpdates(
93 $title, null, $forceRecursiveLinkUpdate, $p_result );
94 foreach ( $updates as $update ) {
95 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
98 $r['linkupdate'] = true;
100 if ( $enableParserCache ) {
101 $pcache = ParserCache::singleton();
102 $pcache->save( $p_result, $page, $popts );
105 } else {
106 $this->addWarning( 'apierror-ratelimited' );
107 $forceLinkUpdate = false;
111 $result[] = $r;
113 $apiResult = $this->getResult();
114 ApiResult::setIndexedTagName( $result, 'page' );
115 $apiResult->addValue( null, $this->getModuleName(), $result );
117 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
118 if ( $values ) {
119 $apiResult->addValue( null, 'normalized', $values );
121 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
122 if ( $values ) {
123 $apiResult->addValue( null, 'converted', $values );
125 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
126 if ( $values ) {
127 $apiResult->addValue( null, 'redirects', $values );
130 $this->setContinuationManager( null );
131 $continuationManager->setContinuationIntoResult( $apiResult );
135 * Get a cached instance of an ApiPageSet object
136 * @return ApiPageSet
138 private function getPageSet() {
139 if ( !isset( $this->mPageSet ) ) {
140 $this->mPageSet = new ApiPageSet( $this );
143 return $this->mPageSet;
146 public function isWriteMode() {
147 return true;
150 public function mustBePosted() {
151 return true;
154 public function getAllowedParams( $flags = 0 ) {
155 $result = [
156 'forcelinkupdate' => false,
157 'forcerecursivelinkupdate' => false,
158 'continue' => [
159 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
162 if ( $flags ) {
163 $result += $this->getPageSet()->getFinalParams( $flags );
166 return $result;
169 protected function getExamplesMessages() {
170 return [
171 'action=purge&titles=Main_Page|API'
172 => 'apihelp-purge-example-simple',
173 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
174 => 'apihelp-purge-example-generator',
178 public function getHelpUrls() {
179 return 'https://www.mediawiki.org/wiki/API:Purge';