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