Avoid using the deprecated ParserCache::singleton()
[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 DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
97 }
98
99 $r['linkupdate'] = true;
100
101 if ( $enableParserCache ) {
102 $pcache = MediaWikiServices::getInstance()->getParserCache();
103 $pcache->save( $p_result, $page, $popts );
104 }
105 }
106 } else {
107 $this->addWarning( 'apierror-ratelimited' );
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 return true;
153 }
154
155 public function getAllowedParams( $flags = 0 ) {
156 $result = [
157 'forcelinkupdate' => false,
158 'forcerecursivelinkupdate' => false,
159 'continue' => [
160 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
161 ],
162 ];
163 if ( $flags ) {
164 $result += $this->getPageSet()->getFinalParams( $flags );
165 }
166
167 return $result;
168 }
169
170 protected function getExamplesMessages() {
171 return [
172 'action=purge&titles=Main_Page|API'
173 => 'apihelp-purge-example-simple',
174 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
175 => 'apihelp-purge-example-generator',
176 ];
177 }
178
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
181 }
182 }