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