Merge "resourceloader: Use state "error" instead of "missing" in case of exceptions"
[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
34 private $mPageSet;
35
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
42 */
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;
52 }
53 if ( $flag !== null ) {
54 $v[$flag] = '';
55 }
56 $result[] = $v;
57 }
58 }
59
60 /**
61 * Purges the cache of a page
62 */
63 public function execute() {
64 $params = $this->extractRequestParams();
65
66 $forceLinkUpdate = $params['forcelinkupdate'];
67 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
68 $pageSet = $this->getPageSet();
69 $pageSet->execute();
70
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() );
78
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'] = '';
85
86 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
87 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
88 global $wgEnableParserCache;
89
90 $popts = $page->makeParserOptions( 'canonical' );
91
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 );
95
96 # Update the links tables
97 $updates = $content->getSecondaryDataUpdates(
98 $title, null, $forceRecursiveLinkUpdate, $p_result );
99 DataUpdate::runUpdates( $updates );
100
101 $r['linkupdate'] = '';
102
103 if ( $wgEnableParserCache ) {
104 $pcache = ParserCache::singleton();
105 $pcache->save( $p_result, $page, $popts );
106 }
107 } else {
108 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
109 $this->setWarning( $error['info'] );
110 $forceLinkUpdate = false;
111 }
112 }
113
114 $result[] = $r;
115 }
116 $apiResult = $this->getResult();
117 $apiResult->setIndexedTagName( $result, 'page' );
118 $apiResult->addValue( null, $this->getModuleName(), $result );
119
120 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
121 if ( $values ) {
122 $apiResult->addValue( null, 'normalized', $values );
123 }
124 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
125 if ( $values ) {
126 $apiResult->addValue( null, 'converted', $values );
127 }
128 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
129 if ( $values ) {
130 $apiResult->addValue( null, 'redirects', $values );
131 }
132 }
133
134 /**
135 * Get a cached instance of an ApiPageSet object
136 * @return ApiPageSet
137 */
138 private function getPageSet() {
139 if ( !isset( $this->mPageSet ) ) {
140 $this->mPageSet = new ApiPageSet( $this );
141 }
142
143 return $this->mPageSet;
144 }
145
146 public function isWriteMode() {
147 return true;
148 }
149
150 public function mustBePosted() {
151 // Anonymous users are not allowed a non-POST request
152 return !$this->getUser()->isAllowed( 'purge' );
153 }
154
155 public function getAllowedParams( $flags = 0 ) {
156 $result = array(
157 'forcelinkupdate' => false,
158 'forcerecursivelinkupdate' => false
159 );
160 if ( $flags ) {
161 $result += $this->getPageSet()->getFinalParams( $flags );
162 }
163
164 return $result;
165 }
166
167 public function getParamDescription() {
168 return $this->getPageSet()->getFinalParamDescription()
169 + array(
170 'forcelinkupdate' => 'Update the links tables',
171 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
172 'the links tables for any page that uses this page as a template',
173 );
174 }
175
176 public function getResultProperties() {
177 return array(
178 ApiBase::PROP_LIST => true,
179 '' => array(
180 'ns' => array(
181 ApiBase::PROP_TYPE => 'namespace',
182 ApiBase::PROP_NULLABLE => true
183 ),
184 'title' => array(
185 ApiBase::PROP_TYPE => 'string',
186 ApiBase::PROP_NULLABLE => true
187 ),
188 'pageid' => array(
189 ApiBase::PROP_TYPE => 'integer',
190 ApiBase::PROP_NULLABLE => true
191 ),
192 'revid' => array(
193 ApiBase::PROP_TYPE => 'integer',
194 ApiBase::PROP_NULLABLE => true
195 ),
196 'invalid' => 'boolean',
197 'special' => 'boolean',
198 'missing' => 'boolean',
199 'purged' => 'boolean',
200 'linkupdate' => 'boolean',
201 'iw' => array(
202 ApiBase::PROP_TYPE => 'string',
203 ApiBase::PROP_NULLABLE => true
204 ),
205 )
206 );
207 }
208
209 public function getDescription() {
210 return array( 'Purge the cache for the given titles.',
211 'Requires a POST request if the user is not logged in.'
212 );
213 }
214
215 public function getPossibleErrors() {
216 return array_merge(
217 parent::getPossibleErrors(),
218 $this->getPageSet()->getFinalPossibleErrors()
219 );
220 }
221
222 public function getExamples() {
223 return array(
224 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
225 );
226 }
227
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/API:Purge';
230 }
231 }