Merge "Document the block duration tooltip"
[lhc/web/wiklou.git] / includes / api / ApiPurge.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.14+
5 *
6 * Created on Sep 2, 2008
7 *
8 * Copyright © 2008 Chad Horohoe
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27
28 /**
29 * API interface for page purging
30 * @ingroup API
31 */
32 class ApiPurge extends ApiBase {
33 private $mPageSet;
34
35 /**
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
41 */
42 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
43 foreach ( $values as $val ) {
44 if ( $val instanceof Title ) {
45 $v = array();
46 ApiQueryBase::addTitleInfo( $v, $val );
47 } elseif ( $name !== null ) {
48 $v = array( $name => $val );
49 } else {
50 $v = $val;
51 }
52 if ( $flag !== null ) {
53 $v[$flag] = '';
54 }
55 $result[] = $v;
56 }
57 }
58
59 /**
60 * Purges the cache of a page
61 */
62 public function execute() {
63 $params = $this->extractRequestParams();
64
65 $forceLinkUpdate = $params['forcelinkupdate'];
66 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
67 $pageSet = $this->getPageSet();
68 $pageSet->execute();
69
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() );
77
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'] = '';
84
85 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
86 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
87 global $wgEnableParserCache;
88
89 $popts = $page->makeParserOptions( 'canonical' );
90
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(
94 $title,
95 $page->getLatest(),
96 $popts,
97 $wgEnableParserCache
98 );
99
100 # Update the links tables
101 $updates = $content->getSecondaryDataUpdates(
102 $title, null, $forceRecursiveLinkUpdate, $p_result );
103 DataUpdate::runUpdates( $updates );
104
105 $r['linkupdate'] = '';
106
107 if ( $wgEnableParserCache ) {
108 $pcache = ParserCache::singleton();
109 $pcache->save( $p_result, $page, $popts );
110 }
111 } else {
112 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
113 $this->setWarning( $error['info'] );
114 $forceLinkUpdate = false;
115 }
116 }
117
118 $result[] = $r;
119 }
120 $apiResult = $this->getResult();
121 $apiResult->setIndexedTagName( $result, 'page' );
122 $apiResult->addValue( null, $this->getModuleName(), $result );
123
124 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
125 if ( $values ) {
126 $apiResult->addValue( null, 'normalized', $values );
127 }
128 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
129 if ( $values ) {
130 $apiResult->addValue( null, 'converted', $values );
131 }
132 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
133 if ( $values ) {
134 $apiResult->addValue( null, 'redirects', $values );
135 }
136 }
137
138 /**
139 * Get a cached instance of an ApiPageSet object
140 * @return ApiPageSet
141 */
142 private function getPageSet() {
143 if ( !isset( $this->mPageSet ) ) {
144 $this->mPageSet = new ApiPageSet( $this );
145 }
146
147 return $this->mPageSet;
148 }
149
150 public function isWriteMode() {
151 return true;
152 }
153
154 public function mustBePosted() {
155 // Anonymous users are not allowed a non-POST request
156 return !$this->getUser()->isAllowed( 'purge' );
157 }
158
159 public function getAllowedParams( $flags = 0 ) {
160 $result = array(
161 'forcelinkupdate' => false,
162 'forcerecursivelinkupdate' => false
163 );
164 if ( $flags ) {
165 $result += $this->getPageSet()->getFinalParams( $flags );
166 }
167
168 return $result;
169 }
170
171 public function getParamDescription() {
172 return $this->getPageSet()->getFinalParamDescription()
173 + array(
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',
177 );
178 }
179
180 public function getResultProperties() {
181 return array(
182 ApiBase::PROP_LIST => true,
183 '' => array(
184 'ns' => array(
185 ApiBase::PROP_TYPE => 'namespace',
186 ApiBase::PROP_NULLABLE => true
187 ),
188 'title' => array(
189 ApiBase::PROP_TYPE => 'string',
190 ApiBase::PROP_NULLABLE => true
191 ),
192 'pageid' => array(
193 ApiBase::PROP_TYPE => 'integer',
194 ApiBase::PROP_NULLABLE => true
195 ),
196 'revid' => array(
197 ApiBase::PROP_TYPE => 'integer',
198 ApiBase::PROP_NULLABLE => true
199 ),
200 'invalid' => 'boolean',
201 'special' => 'boolean',
202 'missing' => 'boolean',
203 'purged' => 'boolean',
204 'linkupdate' => 'boolean',
205 'iw' => array(
206 ApiBase::PROP_TYPE => 'string',
207 ApiBase::PROP_NULLABLE => true
208 ),
209 )
210 );
211 }
212
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.'
216 );
217 }
218
219 public function getPossibleErrors() {
220 return array_merge(
221 parent::getPossibleErrors(),
222 $this->getPageSet()->getFinalPossibleErrors()
223 );
224 }
225
226 public function getExamples() {
227 return array(
228 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
229 );
230 }
231
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/API:Purge';
234 }
235 }