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 * Add all items from $values into the result
37 * @param array $result output
38 * @param array $values values to add
39 * @param string $flag the name of the boolean flag to mark this element
40 * @param string $name if given, name of the value
42 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
43 foreach ( $values as $val ) {
44 if ( $val instanceof Title
) {
46 ApiQueryBase
::addTitleInfo( $v, $val );
47 } elseif ( $name !== null ) {
48 $v = array( $name => $val );
52 if ( $flag !== null ) {
60 * Purges the cache of a page
62 public function execute() {
63 $params = $this->extractRequestParams();
65 $forceLinkUpdate = $params['forcelinkupdate'];
66 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
67 $pageSet = $this->getPageSet();
71 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
72 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
73 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
74 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
75 self
::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
76 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
78 foreach ( $pageSet->getGoodTitles() as $title ) {
80 ApiQueryBase
::addTitleInfo( $r, $title );
81 $page = WikiPage
::factory( $title );
82 $page->doPurge(); // Directly purge and skip the UI part of purge().
85 if ( $forceLinkUpdate ||
$forceRecursiveLinkUpdate ) {
86 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
87 global $wgEnableParserCache;
89 $popts = $page->makeParserOptions( 'canonical' );
91 # Parse content; note that HTML generation is only needed if we want to cache the result.
92 $content = $page->getContent( Revision
::RAW
);
93 $p_result = $content->getParserOutput(
100 # Update the links tables
101 $updates = $content->getSecondaryDataUpdates(
102 $title, null, $forceRecursiveLinkUpdate, $p_result );
103 DataUpdate
::runUpdates( $updates );
105 $r['linkupdate'] = '';
107 if ( $wgEnableParserCache ) {
108 $pcache = ParserCache
::singleton();
109 $pcache->save( $p_result, $page, $popts );
112 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
113 $this->setWarning( $error['info'] );
114 $forceLinkUpdate = false;
120 $apiResult = $this->getResult();
121 $apiResult->setIndexedTagName( $result, 'page' );
122 $apiResult->addValue( null, $this->getModuleName(), $result );
124 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
126 $apiResult->addValue( null, 'normalized', $values );
128 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
130 $apiResult->addValue( null, 'converted', $values );
132 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
134 $apiResult->addValue( null, 'redirects', $values );
139 * Get a cached instance of an ApiPageSet object
142 private function getPageSet() {
143 if ( !isset( $this->mPageSet
) ) {
144 $this->mPageSet
= new ApiPageSet( $this );
147 return $this->mPageSet
;
150 public function isWriteMode() {
154 public function mustBePosted() {
155 // Anonymous users are not allowed a non-POST request
156 return !$this->getUser()->isAllowed( 'purge' );
159 public function getAllowedParams( $flags = 0 ) {
161 'forcelinkupdate' => false,
162 'forcerecursivelinkupdate' => false
165 $result +
= $this->getPageSet()->getFinalParams( $flags );
171 public function getParamDescription() {
172 return $this->getPageSet()->getFinalParamDescription()
174 'forcelinkupdate' => 'Update the links tables',
175 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
176 'the links tables for any page that uses this page as a template',
180 public function getResultProperties() {
182 ApiBase
::PROP_LIST
=> true,
185 ApiBase
::PROP_TYPE
=> 'namespace',
186 ApiBase
::PROP_NULLABLE
=> true
189 ApiBase
::PROP_TYPE
=> 'string',
190 ApiBase
::PROP_NULLABLE
=> true
193 ApiBase
::PROP_TYPE
=> 'integer',
194 ApiBase
::PROP_NULLABLE
=> true
197 ApiBase
::PROP_TYPE
=> 'integer',
198 ApiBase
::PROP_NULLABLE
=> true
200 'invalid' => 'boolean',
201 'special' => 'boolean',
202 'missing' => 'boolean',
203 'purged' => 'boolean',
204 'linkupdate' => 'boolean',
206 ApiBase
::PROP_TYPE
=> 'string',
207 ApiBase
::PROP_NULLABLE
=> true
213 public function getDescription() {
214 return array( 'Purge the cache for the given titles.',
215 'Requires a POST request if the user is not logged in.'
219 public function getPossibleErrors() {
221 parent
::getPossibleErrors(),
222 $this->getPageSet()->getFinalPossibleErrors()
226 public function getExamples() {
228 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/API:Purge';