Merge "Remove wfCheckEntropy() as unused and deprecated in 1.27"
[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
28 /**
29 * API interface for page purging
30 * @ingroup API
31 */
32 class ApiPurge extends ApiBase {
33 private $mPageSet;
34
35 /**
36 * Purges the cache of a page
37 */
38 public function execute() {
39 $params = $this->extractRequestParams();
40
41 $continuationManager = new ApiContinuationManager( $this, [], [] );
42 $this->setContinuationManager( $continuationManager );
43
44 $forceLinkUpdate = $params['forcelinkupdate'];
45 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
46 $pageSet = $this->getPageSet();
47 $pageSet->execute();
48
49 $result = $pageSet->getInvalidTitlesAndRevisions();
50 $user = $this->getUser();
51
52 foreach ( $pageSet->getGoodTitles() as $title ) {
53 $r = [];
54 ApiQueryBase::addTitleInfo( $r, $title );
55 $page = WikiPage::factory( $title );
56 if ( !$user->pingLimiter( 'purge' ) ) {
57 $page->doPurge(); // Directly purge and skip the UI part of purge().
58 $r['purged'] = true;
59 } else {
60 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
61 $this->setWarning( $error['info'] );
62 }
63
64 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
65 if ( !$user->pingLimiter( 'linkpurge' ) ) {
66 $popts = $page->makeParserOptions( 'canonical' );
67
68 # Parse content; note that HTML generation is only needed if we want to cache the result.
69 $content = $page->getContent( Revision::RAW );
70 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
71 $p_result = $content->getParserOutput(
72 $title,
73 $page->getLatest(),
74 $popts,
75 $enableParserCache
76 );
77
78 # Update the links tables
79 $updates = $content->getSecondaryDataUpdates(
80 $title, null, $forceRecursiveLinkUpdate, $p_result );
81 DataUpdate::runUpdates( $updates );
82
83 $r['linkupdate'] = true;
84
85 if ( $enableParserCache ) {
86 $pcache = ParserCache::singleton();
87 $pcache->save( $p_result, $page, $popts );
88 }
89 } else {
90 $error = $this->parseMsg( [ 'actionthrottledtext' ] );
91 $this->setWarning( $error['info'] );
92 $forceLinkUpdate = false;
93 }
94 }
95
96 $result[] = $r;
97 }
98 $apiResult = $this->getResult();
99 ApiResult::setIndexedTagName( $result, 'page' );
100 $apiResult->addValue( null, $this->getModuleName(), $result );
101
102 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
103 if ( $values ) {
104 $apiResult->addValue( null, 'normalized', $values );
105 }
106 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
107 if ( $values ) {
108 $apiResult->addValue( null, 'converted', $values );
109 }
110 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
111 if ( $values ) {
112 $apiResult->addValue( null, 'redirects', $values );
113 }
114
115 $this->setContinuationManager( null );
116 $continuationManager->setContinuationIntoResult( $apiResult );
117 }
118
119 /**
120 * Get a cached instance of an ApiPageSet object
121 * @return ApiPageSet
122 */
123 private function getPageSet() {
124 if ( !isset( $this->mPageSet ) ) {
125 $this->mPageSet = new ApiPageSet( $this );
126 }
127
128 return $this->mPageSet;
129 }
130
131 public function isWriteMode() {
132 return true;
133 }
134
135 public function mustBePosted() {
136 // Anonymous users are not allowed a non-POST request
137 return !$this->getUser()->isAllowed( 'purge' );
138 }
139
140 public function getAllowedParams( $flags = 0 ) {
141 $result = [
142 'forcelinkupdate' => false,
143 'forcerecursivelinkupdate' => false,
144 'continue' => [
145 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
146 ],
147 ];
148 if ( $flags ) {
149 $result += $this->getPageSet()->getFinalParams( $flags );
150 }
151
152 return $result;
153 }
154
155 protected function getExamplesMessages() {
156 return [
157 'action=purge&titles=Main_Page|API'
158 => 'apihelp-purge-example-simple',
159 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
160 => 'apihelp-purge-example-generator',
161 ];
162 }
163
164 public function getHelpUrls() {
165 return 'https://www.mediawiki.org/wiki/API:Purge';
166 }
167 }