Merge "Added deprecation comment to constant that when used throws deprecation exception"
[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 $result array output
39 * @param $values array values to add
40 * @param $flag string the name of the boolean flag to mark this element
41 * @param $name string 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 $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 ) {
86 if ( !$this->getUser()->pingLimiter() ) {
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( $title, $page->getLatest(), $popts, $wgEnableParserCache );
94
95 # Update the links tables
96 $updates = $content->getSecondaryDataUpdates( $title, null, true, $p_result );
97 DataUpdate::runUpdates( $updates );
98
99 $r['linkupdate'] = '';
100
101 if ( $wgEnableParserCache ) {
102 $pcache = ParserCache::singleton();
103 $pcache->save( $p_result, $page, $popts );
104 }
105 } else {
106 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
107 $this->setWarning( $error['info'] );
108 $forceLinkUpdate = false;
109 }
110 }
111
112 $result[] = $r;
113 }
114 $apiResult = $this->getResult();
115 $apiResult->setIndexedTagName( $result, 'page' );
116 $apiResult->addValue( null, $this->getModuleName(), $result );
117
118 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
119 if ( $values ) {
120 $apiResult->addValue( null, 'normalized', $values );
121 }
122 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
123 if ( $values ) {
124 $apiResult->addValue( null, 'converted', $values );
125 }
126 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
127 if ( $values ) {
128 $apiResult->addValue( null, 'redirects', $values );
129 }
130 }
131
132 /**
133 * Get a cached instance of an ApiPageSet object
134 * @return ApiPageSet
135 */
136 private function getPageSet() {
137 if ( !isset( $this->mPageSet ) ) {
138 $this->mPageSet = new ApiPageSet( $this );
139 }
140 return $this->mPageSet;
141 }
142
143 public function isWriteMode() {
144 return true;
145 }
146
147 public function mustBePosted() {
148 // Anonymous users are not allowed a non-POST request
149 return !$this->getUser()->isAllowed( 'purge' );
150 }
151
152 public function getAllowedParams( $flags = 0 ) {
153 $result = array( 'forcelinkupdate' => false );
154 if ( $flags ) {
155 $result += $this->getPageSet()->getFinalParams( $flags );
156 }
157 return $result;
158 }
159
160 public function getParamDescription() {
161 return $this->getPageSet()->getParamDescription()
162 + array( 'forcelinkupdate' => 'Update the links tables' );
163 }
164
165 public function getResultProperties() {
166 return array(
167 ApiBase::PROP_LIST => true,
168 '' => array(
169 'ns' => array(
170 ApiBase::PROP_TYPE => 'namespace',
171 ApiBase::PROP_NULLABLE => true
172 ),
173 'title' => array(
174 ApiBase::PROP_TYPE => 'string',
175 ApiBase::PROP_NULLABLE => true
176 ),
177 'pageid' => array(
178 ApiBase::PROP_TYPE => 'integer',
179 ApiBase::PROP_NULLABLE => true
180 ),
181 'revid' => array(
182 ApiBase::PROP_TYPE => 'integer',
183 ApiBase::PROP_NULLABLE => true
184 ),
185 'invalid' => 'boolean',
186 'special' => 'boolean',
187 'missing' => 'boolean',
188 'purged' => 'boolean',
189 'linkupdate' => 'boolean',
190 'iw' => array(
191 ApiBase::PROP_TYPE => 'string',
192 ApiBase::PROP_NULLABLE => true
193 ),
194 )
195 );
196 }
197
198 public function getDescription() {
199 return array( 'Purge the cache for the given titles.',
200 'Requires a POST request if the user is not logged in.'
201 );
202 }
203
204 public function getPossibleErrors() {
205 return array_merge(
206 parent::getPossibleErrors(),
207 $this->getPageSet()->getPossibleErrors()
208 );
209 }
210
211 public function getExamples() {
212 return array(
213 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
214 );
215 }
216
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Purge';
219 }
220 }