4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Logger\LoggerFactory
;
22 use MediaWiki\MediaWikiServices
;
25 * API interface for page purging
28 class ApiPurge
extends ApiBase
{
32 * Purges the cache of a page
34 public function execute() {
35 $params = $this->extractRequestParams();
37 $continuationManager = new ApiContinuationManager( $this, [], [] );
38 $this->setContinuationManager( $continuationManager );
40 $forceLinkUpdate = $params['forcelinkupdate'];
41 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
42 $pageSet = $this->getPageSet();
45 $result = $pageSet->getInvalidTitlesAndRevisions();
46 $user = $this->getUser();
48 foreach ( $pageSet->getGoodTitles() as $title ) {
50 ApiQueryBase
::addTitleInfo( $r, $title );
51 $page = WikiPage
::factory( $title );
52 if ( !$user->pingLimiter( 'purge' ) ) {
53 // Directly purge and skip the UI part of purge()
57 $this->addWarning( 'apierror-ratelimited' );
60 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
61 if ( !$user->pingLimiter( 'linkpurge' ) ) {
62 $popts = $page->makeParserOptions( 'canonical' );
64 # Parse content; note that HTML generation is only needed if we want to cache the result.
65 $content = $page->getContent( Revision
::RAW
);
67 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
68 $p_result = $content->getParserOutput(
75 # Logging to better see expensive usage patterns
76 if ( $forceRecursiveLinkUpdate ) {
77 LoggerFactory
::getInstance( 'RecursiveLinkPurge' )->info(
78 "Recursive link purge enqueued for {title}",
80 'user' => $this->getUser()->getName(),
81 'title' => $title->getPrefixedText()
86 # Update the links tables
87 $updates = $content->getSecondaryDataUpdates(
88 $title, null, $forceRecursiveLinkUpdate, $p_result );
89 foreach ( $updates as $update ) {
90 $update->setCause( 'api-purge', $this->getUser()->getName() );
91 DeferredUpdates
::addUpdate( $update, DeferredUpdates
::PRESEND
);
94 $r['linkupdate'] = true;
96 if ( $enableParserCache ) {
97 $pcache = MediaWikiServices
::getInstance()->getParserCache();
98 $pcache->save( $p_result, $page, $popts );
102 $this->addWarning( 'apierror-ratelimited' );
103 $forceLinkUpdate = false;
109 $apiResult = $this->getResult();
110 ApiResult
::setIndexedTagName( $result, 'page' );
111 $apiResult->addValue( null, $this->getModuleName(), $result );
113 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
115 $apiResult->addValue( null, 'normalized', $values );
117 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
119 $apiResult->addValue( null, 'converted', $values );
121 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
123 $apiResult->addValue( null, 'redirects', $values );
126 $this->setContinuationManager( null );
127 $continuationManager->setContinuationIntoResult( $apiResult );
131 * Get a cached instance of an ApiPageSet object
134 private function getPageSet() {
135 if ( !isset( $this->mPageSet
) ) {
136 $this->mPageSet
= new ApiPageSet( $this );
139 return $this->mPageSet
;
142 public function isWriteMode() {
146 public function mustBePosted() {
150 public function getAllowedParams( $flags = 0 ) {
152 'forcelinkupdate' => false,
153 'forcerecursivelinkupdate' => false,
155 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
159 $result +
= $this->getPageSet()->getFinalParams( $flags );
165 protected function getExamplesMessages() {
167 'action=purge&titles=Main_Page|API'
168 => 'apihelp-purge-example-simple',
169 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
170 => 'apihelp-purge-example-generator',
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';