Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiPurge.php
blobb68dc5cfa41713dc21f0b55664d9350bbfde45a1
1 <?php
3 /**
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
25 * @file
28 /**
29 * API interface for page purging
30 * @ingroup API
32 class ApiPurge extends ApiBase {
34 private $mPageSet;
36 /**
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 ) {
46 $v = array();
47 ApiQueryBase::addTitleInfo( $v, $val );
48 } elseif ( $name !== null ) {
49 $v = array( $name => $val );
50 } else {
51 $v = $val;
53 if ( $flag !== null ) {
54 $v[$flag] = '';
56 $result[] = $v;
60 /**
61 * Purges the cache of a page
63 public function execute() {
64 $params = $this->extractRequestParams();
66 $forceLinkUpdate = $params['forcelinkupdate'];
67 $pageSet = $this->getPageSet();
68 $pageSet->execute();
70 $result = array();
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 ) {
79 $r = array();
80 ApiQueryBase::addTitleInfo( $r, $title );
81 $page = WikiPage::factory( $title );
82 $page->doPurge(); // Directly purge and skip the UI part of purge().
83 $r['purged'] = '';
85 if ( $forceLinkUpdate ) {
86 if ( !$this->getUser()->pingLimiter() ) {
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( $title, $page->getLatest(), $popts, $wgEnableParserCache );
95 # Update the links tables
96 $updates = $content->getSecondaryDataUpdates( $title, null, true, $p_result );
97 DataUpdate::runUpdates( $updates );
99 $r['linkupdate'] = '';
101 if ( $wgEnableParserCache ) {
102 $pcache = ParserCache::singleton();
103 $pcache->save( $p_result, $page, $popts );
105 } else {
106 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
107 $this->setWarning( $error['info'] );
108 $forceLinkUpdate = false;
112 $result[] = $r;
114 $apiResult = $this->getResult();
115 $apiResult->setIndexedTagName( $result, 'page' );
116 $apiResult->addValue( null, $this->getModuleName(), $result );
118 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
119 if ( $values ) {
120 $apiResult->addValue( null, 'normalized', $values );
122 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
123 if ( $values ) {
124 $apiResult->addValue( null, 'converted', $values );
126 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
127 if ( $values ) {
128 $apiResult->addValue( null, 'redirects', $values );
133 * Get a cached instance of an ApiPageSet object
134 * @return ApiPageSet
136 private function getPageSet() {
137 if ( !isset( $this->mPageSet ) ) {
138 $this->mPageSet = new ApiPageSet( $this );
140 return $this->mPageSet;
143 public function isWriteMode() {
144 return true;
147 public function mustBePosted() {
148 // Anonymous users are not allowed a non-POST request
149 return !$this->getUser()->isAllowed( 'purge' );
152 public function getAllowedParams( $flags = 0 ) {
153 $result = array( 'forcelinkupdate' => false );
154 if ( $flags ) {
155 $result += $this->getPageSet()->getFinalParams( $flags );
157 return $result;
160 public function getParamDescription() {
161 return $this->getPageSet()->getFinalParamDescription()
162 + array( 'forcelinkupdate' => 'Update the links tables' );
165 public function getResultProperties() {
166 return array(
167 ApiBase::PROP_LIST => true,
168 '' => array(
169 'ns' => array(
170 ApiBase::PROP_TYPE => 'namespace',
171 ApiBase::PROP_NULLABLE => true
173 'title' => array(
174 ApiBase::PROP_TYPE => 'string',
175 ApiBase::PROP_NULLABLE => true
177 'pageid' => array(
178 ApiBase::PROP_TYPE => 'integer',
179 ApiBase::PROP_NULLABLE => true
181 'revid' => array(
182 ApiBase::PROP_TYPE => 'integer',
183 ApiBase::PROP_NULLABLE => true
185 'invalid' => 'boolean',
186 'special' => 'boolean',
187 'missing' => 'boolean',
188 'purged' => 'boolean',
189 'linkupdate' => 'boolean',
190 'iw' => array(
191 ApiBase::PROP_TYPE => 'string',
192 ApiBase::PROP_NULLABLE => true
198 public function getDescription() {
199 return array( 'Purge the cache for the given titles.',
200 'Requires a POST request if the user is not logged in.'
204 public function getPossibleErrors() {
205 return array_merge(
206 parent::getPossibleErrors(),
207 $this->getPageSet()->getFinalPossibleErrors()
211 public function getExamples() {
212 return array(
213 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Purge';