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 $forceLinkUpdate = $params['forcelinkupdate'];
42 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
43 $pageSet = $this->getPageSet();
46 $result = $pageSet->getInvalidTitlesAndRevisions();
48 foreach ( $pageSet->getGoodTitles() as $title ) {
50 ApiQueryBase
::addTitleInfo( $r, $title );
51 $page = WikiPage
::factory( $title );
52 $page->doPurge(); // Directly purge and skip the UI part of purge().
55 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
56 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
57 global $wgEnableParserCache;
59 $popts = $page->makeParserOptions( 'canonical' );
61 # Parse content; note that HTML generation is only needed if we want to cache the result.
62 $content = $page->getContent( Revision
::RAW
);
63 $p_result = $content->getParserOutput(
70 # Update the links tables
71 $updates = $content->getSecondaryDataUpdates(
72 $title, null, $forceRecursiveLinkUpdate, $p_result );
73 DataUpdate
::runUpdates( $updates );
75 $r['linkupdate'] = '';
77 if ( $wgEnableParserCache ) {
78 $pcache = ParserCache
::singleton();
79 $pcache->save( $p_result, $page, $popts );
82 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
83 $this->setWarning( $error['info'] );
84 $forceLinkUpdate = false;
90 $apiResult = $this->getResult();
91 $apiResult->setIndexedTagName( $result, 'page' );
92 $apiResult->addValue( null, $this->getModuleName(), $result );
94 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
96 $apiResult->addValue( null, 'normalized', $values );
98 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
100 $apiResult->addValue( null, 'converted', $values );
102 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
104 $apiResult->addValue( null, 'redirects', $values );
109 * Get a cached instance of an ApiPageSet object
112 private function getPageSet() {
113 if ( !isset( $this->mPageSet
) ) {
114 $this->mPageSet
= new ApiPageSet( $this );
117 return $this->mPageSet
;
120 public function isWriteMode() {
124 public function mustBePosted() {
125 // Anonymous users are not allowed a non-POST request
126 return !$this->getUser()->isAllowed( 'purge' );
129 public function getAllowedParams( $flags = 0 ) {
131 'forcelinkupdate' => false,
132 'forcerecursivelinkupdate' => false
135 $result +
= $this->getPageSet()->getFinalParams( $flags );
141 public function getParamDescription() {
142 return $this->getPageSet()->getFinalParamDescription()
144 'forcelinkupdate' => 'Update the links tables',
145 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
146 'the links tables for any page that uses this page as a template',
150 public function getResultProperties() {
152 ApiBase
::PROP_LIST
=> true,
155 ApiBase
::PROP_TYPE
=> 'namespace',
156 ApiBase
::PROP_NULLABLE
=> true
159 ApiBase
::PROP_TYPE
=> 'string',
160 ApiBase
::PROP_NULLABLE
=> true
163 ApiBase
::PROP_TYPE
=> 'integer',
164 ApiBase
::PROP_NULLABLE
=> true
167 ApiBase
::PROP_TYPE
=> 'integer',
168 ApiBase
::PROP_NULLABLE
=> true
170 'invalid' => 'boolean',
171 'special' => 'boolean',
172 'missing' => 'boolean',
173 'purged' => 'boolean',
174 'linkupdate' => 'boolean',
176 ApiBase
::PROP_TYPE
=> 'string',
177 ApiBase
::PROP_NULLABLE
=> true
183 public function getDescription() {
184 return array( 'Purge the cache for the given titles.',
185 'Requires a POST request if the user is not logged in.'
189 public function getPossibleErrors() {
191 parent
::getPossibleErrors(),
192 $this->getPageSet()->getFinalPossibleErrors()
196 public function getExamples() {
198 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
202 public function getHelpUrls() {
203 return 'https://www.mediawiki.org/wiki/API:Purge';