Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiProtect.php
blob96c7240c3529a57ab2225e4402cbccd05814f1d2
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use ChangeTags;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\Permissions\RestrictionStore;
28 use MediaWiki\Title\Title;
29 use MediaWiki\User\Options\UserOptionsLookup;
30 use MediaWiki\Watchlist\WatchlistManager;
31 use Wikimedia\ParamValidator\ParamValidator;
33 /**
34 * @ingroup API
36 class ApiProtect extends ApiBase {
38 use ApiWatchlistTrait;
40 private RestrictionStore $restrictionStore;
42 public function __construct(
43 ApiMain $mainModule,
44 string $moduleName,
45 WatchlistManager $watchlistManager,
46 UserOptionsLookup $userOptionsLookup,
47 RestrictionStore $restrictionStore
48 ) {
49 parent::__construct( $mainModule, $moduleName );
50 $this->restrictionStore = $restrictionStore;
52 // Variables needed in ApiWatchlistTrait trait
53 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
54 $this->watchlistMaxDuration =
55 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
56 $this->watchlistManager = $watchlistManager;
57 $this->userOptionsLookup = $userOptionsLookup;
60 public function execute() {
61 $params = $this->extractRequestParams();
63 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
64 $titleObj = $pageObj->getTitle();
65 $this->getErrorFormatter()->setContextTitle( $titleObj );
67 $this->checkTitleUserPermissions( $titleObj, 'protect' );
69 $user = $this->getUser();
70 $tags = $params['tags'];
72 // Check if user can add tags
73 if ( $tags !== null ) {
74 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
75 if ( !$ableToTag->isOK() ) {
76 $this->dieStatus( $ableToTag );
80 $expiry = (array)$params['expiry'];
81 if ( count( $expiry ) != count( $params['protections'] ) ) {
82 if ( count( $expiry ) == 1 ) {
83 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
84 } else {
85 $this->dieWithError( [
86 'apierror-toofewexpiries',
87 count( $expiry ),
88 count( $params['protections'] )
89 ] );
93 $restrictionTypes = $this->restrictionStore->listApplicableRestrictionTypes( $titleObj );
94 $levels = $this->getPermissionManager()->getNamespaceRestrictionLevels(
95 $titleObj->getNamespace(),
96 $user
99 $protections = [];
100 $expiryarray = [];
101 $resultProtections = [];
102 foreach ( $params['protections'] as $i => $prot ) {
103 $p = explode( '=', $prot );
104 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
106 if ( $titleObj->exists() && $p[0] == 'create' ) {
107 $this->dieWithError( 'apierror-create-titleexists' );
109 if ( !$titleObj->exists() && $p[0] != 'create' ) {
110 $this->dieWithError( 'apierror-missingtitle-createonly' );
113 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
114 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
116 if ( !in_array( $p[1], $levels ) && $p[1] != 'all' ) {
117 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
120 if ( wfIsInfinity( $expiry[$i] ) ) {
121 $expiryarray[$p[0]] = 'infinity';
122 } else {
123 $exp = strtotime( $expiry[$i] );
124 if ( $exp < 0 || !$exp ) {
125 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
128 $exp = wfTimestamp( TS_MW, $exp );
129 if ( $exp < wfTimestampNow() ) {
130 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
132 $expiryarray[$p[0]] = $exp;
134 $resultProtections[] = [
135 $p[0] => $protections[$p[0]],
136 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
140 $cascade = $params['cascade'];
142 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
143 $watchlistExpiry = $this->getExpiryFromParams( $params );
144 $this->setWatch( $watch, $titleObj, $user, 'watchdefault', $watchlistExpiry );
146 $status = $pageObj->doUpdateRestrictions(
147 $protections,
148 $expiryarray,
149 $cascade,
150 $params['reason'],
151 $user,
152 $tags ?? []
155 if ( !$status->isOK() ) {
156 $this->dieStatus( $status );
158 $res = [
159 'title' => $titleObj->getPrefixedText(),
160 'reason' => $params['reason']
162 if ( $cascade ) {
163 $res['cascade'] = true;
165 $res['protections'] = $resultProtections;
166 $result = $this->getResult();
167 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
168 $result->addValue( null, $this->getModuleName(), $res );
171 public function mustBePosted() {
172 return true;
175 public function isWriteMode() {
176 return true;
179 public function getAllowedParams() {
180 return [
181 'title' => [
182 ParamValidator::PARAM_TYPE => 'string',
184 'pageid' => [
185 ParamValidator::PARAM_TYPE => 'integer',
187 'protections' => [
188 ParamValidator::PARAM_ISMULTI => true,
189 ParamValidator::PARAM_REQUIRED => true,
191 'expiry' => [
192 ParamValidator::PARAM_ISMULTI => true,
193 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
194 ParamValidator::PARAM_DEFAULT => 'infinite',
196 'reason' => '',
197 'tags' => [
198 ParamValidator::PARAM_TYPE => 'tags',
199 ParamValidator::PARAM_ISMULTI => true,
201 'cascade' => false,
202 'watch' => [
203 ParamValidator::PARAM_DEFAULT => false,
204 ParamValidator::PARAM_DEPRECATED => true,
206 ] + $this->getWatchlistParams();
209 public function needsToken() {
210 return 'csrf';
213 protected function getExamplesMessages() {
214 $title = Title::newMainPage()->getPrefixedText();
215 $mp = rawurlencode( $title );
217 return [
218 "action=protect&title={$mp}&token=123ABC&" .
219 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
220 => 'apihelp-protect-example-protect',
221 "action=protect&title={$mp}&token=123ABC&" .
222 'protections=edit=all|move=all&reason=Lifting%20restrictions'
223 => 'apihelp-protect-example-unprotect',
224 "action=protect&title={$mp}&token=123ABC&" .
225 'protections=&reason=Lifting%20restrictions'
226 => 'apihelp-protect-example-unprotect2',
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';
235 /** @deprecated class alias since 1.43 */
236 class_alias( ApiProtect::class, 'ApiProtect' );