Merge "Move up devunt's name to Developers"
[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 use MediaWiki\Logger\LoggerFactory;
28
29 /**
30 * API interface for page purging
31 * @ingroup API
32 */
33 class ApiPurge extends ApiBase {
34 private $mPageSet;
35
36 /**
37 * Purges the cache of a page
38 */
39 public function execute() {
40 $main = $this->getMain();
41 if ( !$main->isInternalMode() && !$main->getRequest()->wasPosted() ) {
42 $this->logFeatureUsage( 'purge-via-GET' );
43 $this->setWarning( 'Use of action=purge via GET is deprecated. Use POST instead.' );
44 }
45
46 $params = $this->extractRequestParams();
47
48 $continuationManager = new ApiContinuationManager( $this, [], [] );
49 $this->setContinuationManager( $continuationManager );
50
51 $forceLinkUpdate = $params['forcelinkupdate'];
52 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
53 $pageSet = $this->getPageSet();
54 $pageSet->execute();
55
56 $result = $pageSet->getInvalidTitlesAndRevisions();
57 $user = $this->getUser();
58
59 foreach ( $pageSet->getGoodTitles() as $title ) {
60 $r = [];
61 ApiQueryBase::addTitleInfo( $r, $title );
62 $page = WikiPage::factory( $title );
63 if ( !$user->pingLimiter( 'purge' ) ) {
64 $flags = WikiPage::PURGE_ALL;
65 if ( !$this->getRequest()->wasPosted() ) {
66 $flags ^= WikiPage::PURGE_GLOBAL_PCACHE; // skip DB_MASTER write
67 }
68 // Directly purge and skip the UI part of purge()
69 $page->doPurge( $flags );
70 $r['purged'] = true;
71 } else {
72 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
73 $this->setWarning( $error['info'] );
74 }
75
76 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
77 if ( !$user->pingLimiter( 'linkpurge' ) ) {
78 $popts = $page->makeParserOptions( 'canonical' );
79
80 # Parse content; note that HTML generation is only needed if we want to cache the result.
81 $content = $page->getContent( Revision::RAW );
82 if ( $content ) {
83 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
84 $p_result = $content->getParserOutput(
85 $title,
86 $page->getLatest(),
87 $popts,
88 $enableParserCache
89 );
90
91 # Logging to better see expensive usage patterns
92 if ( $forceRecursiveLinkUpdate ) {
93 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
94 "Recursive link purge enqueued for {title}",
95 [
96 'user' => $this->getUser()->getName(),
97 'title' => $title->getPrefixedText()
98 ]
99 );
100 }
101
102 # Update the links tables
103 $updates = $content->getSecondaryDataUpdates(
104 $title, null, $forceRecursiveLinkUpdate, $p_result );
105 foreach ( $updates as $update ) {
106 # Some extensions, like EventBus, need to know the user
107 # that performed the purge action, so set it here
108 if ( $update instanceof LinksUpdate ) {
109 $update->setTriggeringUser( $user );
110 }
111 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
112 }
113
114 $r['linkupdate'] = true;
115
116 if ( $enableParserCache ) {
117 $pcache = ParserCache::singleton();
118 $pcache->save( $p_result, $page, $popts );
119 }
120 }
121 } else {
122 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
123 $this->setWarning( $error['info'] );
124 $forceLinkUpdate = false;
125 }
126 }
127
128 $result[] = $r;
129 }
130 $apiResult = $this->getResult();
131 ApiResult::setIndexedTagName( $result, 'page' );
132 $apiResult->addValue( null, $this->getModuleName(), $result );
133
134 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
135 if ( $values ) {
136 $apiResult->addValue( null, 'normalized', $values );
137 }
138 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
139 if ( $values ) {
140 $apiResult->addValue( null, 'converted', $values );
141 }
142 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
143 if ( $values ) {
144 $apiResult->addValue( null, 'redirects', $values );
145 }
146
147 $this->setContinuationManager( null );
148 $continuationManager->setContinuationIntoResult( $apiResult );
149 }
150
151 /**
152 * Get a cached instance of an ApiPageSet object
153 * @return ApiPageSet
154 */
155 private function getPageSet() {
156 if ( !isset( $this->mPageSet ) ) {
157 $this->mPageSet = new ApiPageSet( $this );
158 }
159
160 return $this->mPageSet;
161 }
162
163 public function isWriteMode() {
164 return true;
165 }
166
167 public function mustBePosted() {
168 // Anonymous users are not allowed a non-POST request
169 return !$this->getUser()->isAllowed( 'purge' );
170 }
171
172 protected function getHelpFlags() {
173 $flags = parent::getHelpFlags();
174
175 // Claim that we must be posted for the purposes of help and paraminfo.
176 // @todo Remove this when self::mustBePosted() is updated for T145649
177 if ( !in_array( 'mustbeposted', $flags, true ) ) {
178 $flags[] = 'mustbeposted';
179 }
180
181 return $flags;
182 }
183
184 public function getAllowedParams( $flags = 0 ) {
185 $result = [
186 'forcelinkupdate' => false,
187 'forcerecursivelinkupdate' => false,
188 'continue' => [
189 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
190 ],
191 ];
192 if ( $flags ) {
193 $result += $this->getPageSet()->getFinalParams( $flags );
194 }
195
196 return $result;
197 }
198
199 protected function getExamplesMessages() {
200 return [
201 'action=purge&titles=Main_Page|API'
202 => 'apihelp-purge-example-simple',
203 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
204 => 'apihelp-purge-example-generator',
205 ];
206 }
207
208 public function getHelpUrls() {
209 return 'https://www.mediawiki.org/wiki/API:Purge';
210 }
211 }