Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / api / ApiPurge.php
blobe2eae613a53475470710735c621b3d8cd5491fd9
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 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
68 $pageSet = $this->getPageSet();
69 $pageSet->execute();
71 $result = array();
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 ) {
80 $r = array();
81 ApiQueryBase::addTitleInfo( $r, $title );
82 $page = WikiPage::factory( $title );
83 $page->doPurge(); // Directly purge and skip the UI part of purge().
84 $r['purged'] = '';
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 );
107 } else {
108 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
109 $this->setWarning( $error['info'] );
110 $forceLinkUpdate = false;
114 $result[] = $r;
116 $apiResult = $this->getResult();
117 $apiResult->setIndexedTagName( $result, 'page' );
118 $apiResult->addValue( null, $this->getModuleName(), $result );
120 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
121 if ( $values ) {
122 $apiResult->addValue( null, 'normalized', $values );
124 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
125 if ( $values ) {
126 $apiResult->addValue( null, 'converted', $values );
128 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
129 if ( $values ) {
130 $apiResult->addValue( null, 'redirects', $values );
135 * Get a cached instance of an ApiPageSet object
136 * @return ApiPageSet
138 private function getPageSet() {
139 if ( !isset( $this->mPageSet ) ) {
140 $this->mPageSet = new ApiPageSet( $this );
142 return $this->mPageSet;
145 public function isWriteMode() {
146 return true;
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 ) {
155 $result = array(
156 'forcelinkupdate' => false,
157 'forcerecursivelinkupdate' => false
159 if ( $flags ) {
160 $result += $this->getPageSet()->getFinalParams( $flags );
162 return $result;
165 public function getParamDescription() {
166 return $this->getPageSet()->getFinalParamDescription()
167 + array(
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() {
175 return array(
176 ApiBase::PROP_LIST => true,
177 '' => array(
178 'ns' => array(
179 ApiBase::PROP_TYPE => 'namespace',
180 ApiBase::PROP_NULLABLE => true
182 'title' => array(
183 ApiBase::PROP_TYPE => 'string',
184 ApiBase::PROP_NULLABLE => true
186 'pageid' => array(
187 ApiBase::PROP_TYPE => 'integer',
188 ApiBase::PROP_NULLABLE => true
190 'revid' => array(
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',
199 'iw' => array(
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() {
214 return array_merge(
215 parent::getPossibleErrors(),
216 $this->getPageSet()->getFinalPossibleErrors()
220 public function getExamples() {
221 return array(
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';