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