Merge "Add attributes parameter to ShowSearchHitTitle"
[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 use MediaWiki\MediaWikiServices;
29
30 /**
31 * API interface for page purging
32 * @ingroup API
33 */
34 class ApiPurge extends ApiBase {
35 private $mPageSet;
36
37 /**
38 * Purges the cache of a page
39 */
40 public function execute() {
41 $params = $this->extractRequestParams();
42
43 $continuationManager = new ApiContinuationManager( $this, [], [] );
44 $this->setContinuationManager( $continuationManager );
45
46 $forceLinkUpdate = $params['forcelinkupdate'];
47 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
48 $pageSet = $this->getPageSet();
49 $pageSet->execute();
50
51 $result = $pageSet->getInvalidTitlesAndRevisions();
52 $user = $this->getUser();
53
54 foreach ( $pageSet->getGoodTitles() as $title ) {
55 $r = [];
56 ApiQueryBase::addTitleInfo( $r, $title );
57 $page = WikiPage::factory( $title );
58 if ( !$user->pingLimiter( 'purge' ) ) {
59 // Directly purge and skip the UI part of purge()
60 $page->doPurge();
61 $r['purged'] = true;
62 } else {
63 $this->addWarning( 'apierror-ratelimited' );
64 }
65
66 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
67 if ( !$user->pingLimiter( 'linkpurge' ) ) {
68 $popts = $page->makeParserOptions( 'canonical' );
69
70 # Parse content; note that HTML generation is only needed if we want to cache the result.
71 $content = $page->getContent( Revision::RAW );
72 if ( $content ) {
73 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
74 $p_result = $content->getParserOutput(
75 $title,
76 $page->getLatest(),
77 $popts,
78 $enableParserCache
79 );
80
81 # Logging to better see expensive usage patterns
82 if ( $forceRecursiveLinkUpdate ) {
83 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
84 "Recursive link purge enqueued for {title}",
85 [
86 'user' => $this->getUser()->getName(),
87 'title' => $title->getPrefixedText()
88 ]
89 );
90 }
91
92 # Update the links tables
93 $updates = $content->getSecondaryDataUpdates(
94 $title, null, $forceRecursiveLinkUpdate, $p_result );
95 foreach ( $updates as $update ) {
96 $update->setCause( 'api-purge', $this->getUser()->getName() );
97 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
98 }
99
100 $r['linkupdate'] = true;
101
102 if ( $enableParserCache ) {
103 $pcache = MediaWikiServices::getInstance()->getParserCache();
104 $pcache->save( $p_result, $page, $popts );
105 }
106 }
107 } else {
108 $this->addWarning( 'apierror-ratelimited' );
109 $forceLinkUpdate = false;
110 }
111 }
112
113 $result[] = $r;
114 }
115 $apiResult = $this->getResult();
116 ApiResult::setIndexedTagName( $result, 'page' );
117 $apiResult->addValue( null, $this->getModuleName(), $result );
118
119 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
120 if ( $values ) {
121 $apiResult->addValue( null, 'normalized', $values );
122 }
123 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
124 if ( $values ) {
125 $apiResult->addValue( null, 'converted', $values );
126 }
127 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
128 if ( $values ) {
129 $apiResult->addValue( null, 'redirects', $values );
130 }
131
132 $this->setContinuationManager( null );
133 $continuationManager->setContinuationIntoResult( $apiResult );
134 }
135
136 /**
137 * Get a cached instance of an ApiPageSet object
138 * @return ApiPageSet
139 */
140 private function getPageSet() {
141 if ( !isset( $this->mPageSet ) ) {
142 $this->mPageSet = new ApiPageSet( $this );
143 }
144
145 return $this->mPageSet;
146 }
147
148 public function isWriteMode() {
149 return true;
150 }
151
152 public function mustBePosted() {
153 return true;
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/Special:MyLanguage/API:Purge';
182 }
183 }