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
29 * API interface for page purging
32 class ApiPurge
extends ApiBase
{
36 * Purges the cache of a page
38 public function execute() {
39 $params = $this->extractRequestParams();
41 $continuationManager = new ApiContinuationManager( $this, array(), array() );
42 $this->setContinuationManager( $continuationManager );
44 $forceLinkUpdate = $params['forcelinkupdate'];
45 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
46 $pageSet = $this->getPageSet();
49 $result = $pageSet->getInvalidTitlesAndRevisions();
50 $user = $this->getUser();
52 foreach ( $pageSet->getGoodTitles() as $title ) {
54 ApiQueryBase
::addTitleInfo( $r, $title );
55 $page = WikiPage
::factory( $title );
56 if ( !$user->pingLimiter( 'purge' ) ) {
57 $page->doPurge(); // Directly purge and skip the UI part of purge().
60 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
61 $this->setWarning( $error['info'] );
64 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
65 if ( !$user->pingLimiter( 'linkpurge' ) ) {
66 $popts = $page->makeParserOptions( 'canonical' );
68 # Parse content; note that HTML generation is only needed if we want to cache the result.
69 $content = $page->getContent( Revision
::RAW
);
70 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
71 $p_result = $content->getParserOutput(
78 # Update the links tables
79 $updates = $content->getSecondaryDataUpdates(
80 $title, null, $forceRecursiveLinkUpdate, $p_result );
81 DataUpdate
::runUpdates( $updates );
83 $r['linkupdate'] = true;
85 if ( $enableParserCache ) {
86 $pcache = ParserCache
::singleton();
87 $pcache->save( $p_result, $page, $popts );
90 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
91 $this->setWarning( $error['info'] );
92 $forceLinkUpdate = false;
98 $apiResult = $this->getResult();
99 ApiResult
::setIndexedTagName( $result, 'page' );
100 $apiResult->addValue( null, $this->getModuleName(), $result );
102 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
104 $apiResult->addValue( null, 'normalized', $values );
106 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
108 $apiResult->addValue( null, 'converted', $values );
110 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
112 $apiResult->addValue( null, 'redirects', $values );
115 $this->setContinuationManager( null );
116 $continuationManager->setContinuationIntoResult( $apiResult );
120 * Get a cached instance of an ApiPageSet object
123 private function getPageSet() {
124 if ( !isset( $this->mPageSet
) ) {
125 $this->mPageSet
= new ApiPageSet( $this );
128 return $this->mPageSet
;
131 public function isWriteMode() {
135 public function mustBePosted() {
136 // Anonymous users are not allowed a non-POST request
137 return !$this->getUser()->isAllowed( 'purge' );
140 public function getAllowedParams( $flags = 0 ) {
142 'forcelinkupdate' => false,
143 'forcerecursivelinkupdate' => false,
145 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
149 $result +
= $this->getPageSet()->getFinalParams( $flags );
155 protected function getExamplesMessages() {
157 'action=purge&titles=Main_Page|API'
158 => 'apihelp-purge-example-simple',
159 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
160 => 'apihelp-purge-example-generator',
164 public function getHelpUrls() {
165 return 'https://www.mediawiki.org/wiki/API:Purge';