Merge "Rename mediawiki.action.history.diff to mediawiki.diff.styles"
[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 $params = $this->extractRequestParams();
41
42 $continuationManager = new ApiContinuationManager( $this, [], [] );
43 $this->setContinuationManager( $continuationManager );
44
45 $forceLinkUpdate = $params['forcelinkupdate'];
46 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
47 $pageSet = $this->getPageSet();
48 $pageSet->execute();
49
50 $result = $pageSet->getInvalidTitlesAndRevisions();
51 $user = $this->getUser();
52
53 foreach ( $pageSet->getGoodTitles() as $title ) {
54 $r = [];
55 ApiQueryBase::addTitleInfo( $r, $title );
56 $page = WikiPage::factory( $title );
57 if ( !$user->pingLimiter( 'purge' ) ) {
58 $page->doPurge(); // Directly purge and skip the UI part of purge().
59 $r['purged'] = true;
60 } else {
61 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
62 $this->setWarning( $error['info'] );
63 }
64
65 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
66 if ( !$user->pingLimiter( 'linkpurge' ) ) {
67 $popts = $page->makeParserOptions( 'canonical' );
68
69 # Parse content; note that HTML generation is only needed if we want to cache the result.
70 $content = $page->getContent( Revision::RAW );
71 if ( $content ) {
72 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
73 $p_result = $content->getParserOutput(
74 $title,
75 $page->getLatest(),
76 $popts,
77 $enableParserCache
78 );
79
80 # Logging to better see expensive usage patterns
81 if ( $forceRecursiveLinkUpdate ) {
82 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
83 "Recursive link purge enqueued for {title}",
84 [
85 'user' => $this->getUser()->getName(),
86 'title' => $title->getPrefixedText()
87 ]
88 );
89 }
90
91 # Update the links tables
92 $updates = $content->getSecondaryDataUpdates(
93 $title, null, $forceRecursiveLinkUpdate, $p_result );
94 foreach ( $updates as $update ) {
95 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
96 }
97
98 $r['linkupdate'] = true;
99
100 if ( $enableParserCache ) {
101 $pcache = ParserCache::singleton();
102 $pcache->save( $p_result, $page, $popts );
103 }
104 }
105 } else {
106 $error = $this->parseMsg( [ '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 $this->setContinuationManager( null );
132 $continuationManager->setContinuationIntoResult( $apiResult );
133 }
134
135 /**
136 * Get a cached instance of an ApiPageSet object
137 * @return ApiPageSet
138 */
139 private function getPageSet() {
140 if ( !isset( $this->mPageSet ) ) {
141 $this->mPageSet = new ApiPageSet( $this );
142 }
143
144 return $this->mPageSet;
145 }
146
147 public function isWriteMode() {
148 return true;
149 }
150
151 public function mustBePosted() {
152 // Anonymous users are not allowed a non-POST request
153 return !$this->getUser()->isAllowed( 'purge' );
154 }
155
156 public function getAllowedParams( $flags = 0 ) {
157 $result = [
158 'forcelinkupdate' => false,
159 'forcerecursivelinkupdate' => false,
160 'continue' => [
161 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
162 ],
163 ];
164 if ( $flags ) {
165 $result += $this->getPageSet()->getFinalParams( $flags );
166 }
167
168 return $result;
169 }
170
171 protected function getExamplesMessages() {
172 return [
173 'action=purge&titles=Main_Page|API'
174 => 'apihelp-purge-example-simple',
175 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
176 => 'apihelp-purge-example-generator',
177 ];
178 }
179
180 public function getHelpUrls() {
181 return 'https://www.mediawiki.org/wiki/API:Purge';
182 }
183 }