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
23 namespace MediaWiki\Api
;
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
;
36 class ApiProtect
extends ApiBase
{
38 use ApiWatchlistTrait
;
40 private RestrictionStore
$restrictionStore;
42 public function __construct(
45 WatchlistManager
$watchlistManager,
46 UserOptionsLookup
$userOptionsLookup,
47 RestrictionStore
$restrictionStore
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] );
85 $this->dieWithError( [
86 'apierror-toofewexpiries',
88 count( $params['protections'] )
93 $restrictionTypes = $this->restrictionStore
->listApplicableRestrictionTypes( $titleObj );
94 $levels = $this->getPermissionManager()->getNamespaceRestrictionLevels(
95 $titleObj->getNamespace(),
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';
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(
155 if ( !$status->isOK() ) {
156 $this->dieStatus( $status );
159 'title' => $titleObj->getPrefixedText(),
160 'reason' => $params['reason']
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() {
175 public function isWriteMode() {
179 public function getAllowedParams() {
182 ParamValidator
::PARAM_TYPE
=> 'string',
185 ParamValidator
::PARAM_TYPE
=> 'integer',
188 ParamValidator
::PARAM_ISMULTI
=> true,
189 ParamValidator
::PARAM_REQUIRED
=> true,
192 ParamValidator
::PARAM_ISMULTI
=> true,
193 ParamValidator
::PARAM_ALLOW_DUPLICATES
=> true,
194 ParamValidator
::PARAM_DEFAULT
=> 'infinite',
198 ParamValidator
::PARAM_TYPE
=> 'tags',
199 ParamValidator
::PARAM_ISMULTI
=> true,
203 ParamValidator
::PARAM_DEFAULT
=> false,
204 ParamValidator
::PARAM_DEPRECATED
=> true,
206 ] +
$this->getWatchlistParams();
209 public function needsToken() {
213 protected function getExamplesMessages() {
214 $title = Title
::newMainPage()->getPrefixedText();
215 $mp = rawurlencode( $title );
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' );