Don't queue refreshLinks jobs on null edit
[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() ) {
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 return $this->mPageSet;
143 }
144
145 public function isWriteMode() {
146 return true;
147 }
148
149 public function mustBePosted() {
150 // Anonymous users are not allowed a non-POST request
151 return !$this->getUser()->isAllowed( 'purge' );
152 }
153
154 public function getAllowedParams( $flags = 0 ) {
155 $result = array(
156 'forcelinkupdate' => false,
157 'forcerecursivelinkupdate' => false
158 );
159 if ( $flags ) {
160 $result += $this->getPageSet()->getFinalParams( $flags );
161 }
162 return $result;
163 }
164
165 public function getParamDescription() {
166 return $this->getPageSet()->getFinalParamDescription()
167 + array(
168 'forcelinkupdate' => 'Update the links tables',
169 'forcerecursivelinkupdate' => 'Update the links table, and update ' .
170 'the links tables for any page that uses this page as a template',
171 );
172 }
173
174 public function getResultProperties() {
175 return array(
176 ApiBase::PROP_LIST => true,
177 '' => array(
178 'ns' => array(
179 ApiBase::PROP_TYPE => 'namespace',
180 ApiBase::PROP_NULLABLE => true
181 ),
182 'title' => array(
183 ApiBase::PROP_TYPE => 'string',
184 ApiBase::PROP_NULLABLE => true
185 ),
186 'pageid' => array(
187 ApiBase::PROP_TYPE => 'integer',
188 ApiBase::PROP_NULLABLE => true
189 ),
190 'revid' => array(
191 ApiBase::PROP_TYPE => 'integer',
192 ApiBase::PROP_NULLABLE => true
193 ),
194 'invalid' => 'boolean',
195 'special' => 'boolean',
196 'missing' => 'boolean',
197 'purged' => 'boolean',
198 'linkupdate' => 'boolean',
199 'iw' => array(
200 ApiBase::PROP_TYPE => 'string',
201 ApiBase::PROP_NULLABLE => true
202 ),
203 )
204 );
205 }
206
207 public function getDescription() {
208 return array( 'Purge the cache for the given titles.',
209 'Requires a POST request if the user is not logged in.'
210 );
211 }
212
213 public function getPossibleErrors() {
214 return array_merge(
215 parent::getPossibleErrors(),
216 $this->getPageSet()->getFinalPossibleErrors()
217 );
218 }
219
220 public function getExamples() {
221 return array(
222 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
223 );
224 }
225
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/API:Purge';
228 }
229 }