Merge "Remove HTML back-compat styles from I5fe0543e"
[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 * Purges the cache of a page
37 */
38 public function execute() {
39 $params = $this->extractRequestParams();
40
41 $forceLinkUpdate = $params['forcelinkupdate'];
42 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
43 $pageSet = $this->getPageSet();
44 $pageSet->execute();
45
46 $result = $pageSet->getInvalidTitlesAndRevisions();
47
48 foreach ( $pageSet->getGoodTitles() as $title ) {
49 $r = array();
50 ApiQueryBase::addTitleInfo( $r, $title );
51 $page = WikiPage::factory( $title );
52 $page->doPurge(); // Directly purge and skip the UI part of purge().
53 $r['purged'] = '';
54
55 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
56 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
57 global $wgEnableParserCache;
58
59 $popts = $page->makeParserOptions( 'canonical' );
60
61 # Parse content; note that HTML generation is only needed if we want to cache the result.
62 $content = $page->getContent( Revision::RAW );
63 $p_result = $content->getParserOutput(
64 $title,
65 $page->getLatest(),
66 $popts,
67 $wgEnableParserCache
68 );
69
70 # Update the links tables
71 $updates = $content->getSecondaryDataUpdates(
72 $title, null, $forceRecursiveLinkUpdate, $p_result );
73 DataUpdate::runUpdates( $updates );
74
75 $r['linkupdate'] = '';
76
77 if ( $wgEnableParserCache ) {
78 $pcache = ParserCache::singleton();
79 $pcache->save( $p_result, $page, $popts );
80 }
81 } else {
82 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
83 $this->setWarning( $error['info'] );
84 $forceLinkUpdate = false;
85 }
86 }
87
88 $result[] = $r;
89 }
90 $apiResult = $this->getResult();
91 $apiResult->setIndexedTagName( $result, 'page' );
92 $apiResult->addValue( null, $this->getModuleName(), $result );
93
94 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
95 if ( $values ) {
96 $apiResult->addValue( null, 'normalized', $values );
97 }
98 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
99 if ( $values ) {
100 $apiResult->addValue( null, 'converted', $values );
101 }
102 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
103 if ( $values ) {
104 $apiResult->addValue( null, 'redirects', $values );
105 }
106 }
107
108 /**
109 * Get a cached instance of an ApiPageSet object
110 * @return ApiPageSet
111 */
112 private function getPageSet() {
113 if ( !isset( $this->mPageSet ) ) {
114 $this->mPageSet = new ApiPageSet( $this );
115 }
116
117 return $this->mPageSet;
118 }
119
120 public function isWriteMode() {
121 return true;
122 }
123
124 public function mustBePosted() {
125 // Anonymous users are not allowed a non-POST request
126 return !$this->getUser()->isAllowed( 'purge' );
127 }
128
129 public function getAllowedParams( $flags = 0 ) {
130 $result = array(
131 'forcelinkupdate' => false,
132 'forcerecursivelinkupdate' => false
133 );
134 if ( $flags ) {
135 $result += $this->getPageSet()->getFinalParams( $flags );
136 }
137
138 return $result;
139 }
140
141 public function getParamDescription() {
142 return $this->getPageSet()->getFinalParamDescription()
143 + array(
144 'forcelinkupdate' => 'Update the links tables',
145 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
146 'the links tables for any page that uses this page as a template',
147 );
148 }
149
150 public function getResultProperties() {
151 return array(
152 ApiBase::PROP_LIST => true,
153 '' => array(
154 'ns' => array(
155 ApiBase::PROP_TYPE => 'namespace',
156 ApiBase::PROP_NULLABLE => true
157 ),
158 'title' => array(
159 ApiBase::PROP_TYPE => 'string',
160 ApiBase::PROP_NULLABLE => true
161 ),
162 'pageid' => array(
163 ApiBase::PROP_TYPE => 'integer',
164 ApiBase::PROP_NULLABLE => true
165 ),
166 'revid' => array(
167 ApiBase::PROP_TYPE => 'integer',
168 ApiBase::PROP_NULLABLE => true
169 ),
170 'invalid' => 'boolean',
171 'special' => 'boolean',
172 'missing' => 'boolean',
173 'purged' => 'boolean',
174 'linkupdate' => 'boolean',
175 'iw' => array(
176 ApiBase::PROP_TYPE => 'string',
177 ApiBase::PROP_NULLABLE => true
178 ),
179 )
180 );
181 }
182
183 public function getDescription() {
184 return array( 'Purge the cache for the given titles.',
185 'Requires a POST request if the user is not logged in.'
186 );
187 }
188
189 public function getPossibleErrors() {
190 return array_merge(
191 parent::getPossibleErrors(),
192 $this->getPageSet()->getFinalPossibleErrors()
193 );
194 }
195
196 public function getExamples() {
197 return array(
198 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
199 );
200 }
201
202 public function getHelpUrls() {
203 return 'https://www.mediawiki.org/wiki/API:Purge';
204 }
205 }