Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / api / ApiPurge.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Logger\LoggerFactory;
22
23 /**
24 * API interface for page purging
25 * @ingroup API
26 */
27 class ApiPurge extends ApiBase {
28 private $mPageSet = null;
29
30 /**
31 * Purges the cache of a page
32 */
33 public function execute() {
34 $params = $this->extractRequestParams();
35
36 $continuationManager = new ApiContinuationManager( $this, [], [] );
37 $this->setContinuationManager( $continuationManager );
38
39 $forceLinkUpdate = $params['forcelinkupdate'];
40 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
41 $pageSet = $this->getPageSet();
42 $pageSet->execute();
43
44 $result = $pageSet->getInvalidTitlesAndRevisions();
45 $user = $this->getUser();
46
47 foreach ( $pageSet->getGoodTitles() as $title ) {
48 $r = [];
49 ApiQueryBase::addTitleInfo( $r, $title );
50 $page = WikiPage::factory( $title );
51 if ( !$user->pingLimiter( 'purge' ) ) {
52 // Directly purge and skip the UI part of purge()
53 $page->doPurge();
54 $r['purged'] = true;
55 } else {
56 $this->addWarning( 'apierror-ratelimited' );
57 }
58
59 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
60 if ( !$user->pingLimiter( 'linkpurge' ) ) {
61 # Logging to better see expensive usage patterns
62 if ( $forceRecursiveLinkUpdate ) {
63 LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
64 "Recursive link purge enqueued for {title}",
65 [
66 'user' => $this->getUser()->getName(),
67 'title' => $title->getPrefixedText()
68 ]
69 );
70 }
71
72 $page->updateParserCache( [
73 'causeAction' => 'api-purge',
74 'causeAgent' => $this->getUser()->getName(),
75 ] );
76 $page->doSecondaryDataUpdates( [
77 'recursive' => $forceRecursiveLinkUpdate,
78 'causeAction' => 'api-purge',
79 'causeAgent' => $this->getUser()->getName(),
80 'defer' => DeferredUpdates::PRESEND,
81 ] );
82 $r['linkupdate'] = true;
83 } else {
84 $this->addWarning( 'apierror-ratelimited' );
85 $forceLinkUpdate = false;
86 }
87 }
88
89 $result[] = $r;
90 }
91 $apiResult = $this->getResult();
92 ApiResult::setIndexedTagName( $result, 'page' );
93 $apiResult->addValue( null, $this->getModuleName(), $result );
94
95 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
96 if ( $values ) {
97 $apiResult->addValue( null, 'normalized', $values );
98 }
99 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
100 if ( $values ) {
101 $apiResult->addValue( null, 'converted', $values );
102 }
103 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
104 if ( $values ) {
105 $apiResult->addValue( null, 'redirects', $values );
106 }
107
108 $this->setContinuationManager( null );
109 $continuationManager->setContinuationIntoResult( $apiResult );
110 }
111
112 /**
113 * Get a cached instance of an ApiPageSet object
114 * @return ApiPageSet
115 */
116 private function getPageSet() {
117 if ( $this->mPageSet === null ) {
118 $this->mPageSet = new ApiPageSet( $this );
119 }
120
121 return $this->mPageSet;
122 }
123
124 public function isWriteMode() {
125 return true;
126 }
127
128 public function mustBePosted() {
129 return true;
130 }
131
132 public function getAllowedParams( $flags = 0 ) {
133 $result = [
134 'forcelinkupdate' => false,
135 'forcerecursivelinkupdate' => false,
136 'continue' => [
137 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
138 ],
139 ];
140 if ( $flags ) {
141 $result += $this->getPageSet()->getFinalParams( $flags );
142 }
143
144 return $result;
145 }
146
147 protected function getExamplesMessages() {
148 return [
149 'action=purge&titles=Main_Page|API'
150 => 'apihelp-purge-example-simple',
151 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
152 => 'apihelp-purge-example-generator',
153 ];
154 }
155
156 public function getHelpUrls() {
157 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
158 }
159 }