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
{
37 * Add all items from $values into the result
38 * @param array $result output
39 * @param array $values values to add
40 * @param string $flag the name of the boolean flag to mark this element
41 * @param string $name if given, name of the value
43 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
44 foreach ( $values as $val ) {
45 if ( $val instanceof Title
) {
47 ApiQueryBase
::addTitleInfo( $v, $val );
48 } elseif ( $name !== null ) {
49 $v = array( $name => $val );
53 if ( $flag !== null ) {
61 * Purges the cache of a page
63 public function execute() {
64 $params = $this->extractRequestParams();
66 $forceLinkUpdate = $params['forcelinkupdate'];
67 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
68 $pageSet = $this->getPageSet();
72 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
73 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
74 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
75 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
76 self
::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
77 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
79 foreach ( $pageSet->getGoodTitles() as $title ) {
81 ApiQueryBase
::addTitleInfo( $r, $title );
82 $page = WikiPage
::factory( $title );
83 $page->doPurge(); // Directly purge and skip the UI part of purge().
86 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
87 if ( !$this->getUser()->pingLimiter() ) {
88 global $wgEnableParserCache;
90 $popts = $page->makeParserOptions( 'canonical' );
92 # Parse content; note that HTML generation is only needed if we want to cache the result.
93 $content = $page->getContent( Revision
::RAW
);
94 $p_result = $content->getParserOutput( $title, $page->getLatest(), $popts, $wgEnableParserCache );
96 # Update the links tables
97 $updates = $content->getSecondaryDataUpdates(
98 $title, null, $forceRecursiveLinkUpdate, $p_result );
99 DataUpdate
::runUpdates( $updates );
101 $r['linkupdate'] = '';
103 if ( $wgEnableParserCache ) {
104 $pcache = ParserCache
::singleton();
105 $pcache->save( $p_result, $page, $popts );
108 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
109 $this->setWarning( $error['info'] );
110 $forceLinkUpdate = false;
116 $apiResult = $this->getResult();
117 $apiResult->setIndexedTagName( $result, 'page' );
118 $apiResult->addValue( null, $this->getModuleName(), $result );
120 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
122 $apiResult->addValue( null, 'normalized', $values );
124 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
126 $apiResult->addValue( null, 'converted', $values );
128 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
130 $apiResult->addValue( null, 'redirects', $values );
135 * Get a cached instance of an ApiPageSet object
138 private function getPageSet() {
139 if ( !isset( $this->mPageSet
) ) {
140 $this->mPageSet
= new ApiPageSet( $this );
142 return $this->mPageSet
;
145 public function isWriteMode() {
149 public function mustBePosted() {
150 // Anonymous users are not allowed a non-POST request
151 return !$this->getUser()->isAllowed( 'purge' );
154 public function getAllowedParams( $flags = 0 ) {
156 'forcelinkupdate' => false,
157 'forcerecursivelinkupdate' => false
160 $result +
= $this->getPageSet()->getFinalParams( $flags );
165 public function getParamDescription() {
166 return $this->getPageSet()->getFinalParamDescription()
168 'forcelinkupdate' => 'Update the links tables',
169 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
170 'the links tables for any page that uses this page as a template',
174 public function getResultProperties() {
176 ApiBase
::PROP_LIST
=> true,
179 ApiBase
::PROP_TYPE
=> 'namespace',
180 ApiBase
::PROP_NULLABLE
=> true
183 ApiBase
::PROP_TYPE
=> 'string',
184 ApiBase
::PROP_NULLABLE
=> true
187 ApiBase
::PROP_TYPE
=> 'integer',
188 ApiBase
::PROP_NULLABLE
=> true
191 ApiBase
::PROP_TYPE
=> 'integer',
192 ApiBase
::PROP_NULLABLE
=> true
194 'invalid' => 'boolean',
195 'special' => 'boolean',
196 'missing' => 'boolean',
197 'purged' => 'boolean',
198 'linkupdate' => 'boolean',
200 ApiBase
::PROP_TYPE
=> 'string',
201 ApiBase
::PROP_NULLABLE
=> true
207 public function getDescription() {
208 return array( 'Purge the cache for the given titles.',
209 'Requires a POST request if the user is not logged in.'
213 public function getPossibleErrors() {
215 parent
::getPossibleErrors(),
216 $this->getPageSet()->getFinalPossibleErrors()
220 public function getExamples() {
222 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/API:Purge';