5 * Created on Sep 1, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class ApiProtect
extends ApiBase
{
31 public function execute() {
34 $params = $this->extractRequestParams();
36 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
37 $titleObj = $pageObj->getTitle();
39 $this->checkTitleUserPermissions( $titleObj, 'protect' );
41 $user = $this->getUser();
42 $tags = $params['tags'];
44 // Check if user can add tags
45 if ( !is_null( $tags ) ) {
46 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $tags, $user );
47 if ( !$ableToTag->isOK() ) {
48 $this->dieStatus( $ableToTag );
52 $expiry = (array)$params['expiry'];
53 if ( count( $expiry ) != count( $params['protections'] ) ) {
54 if ( count( $expiry ) == 1 ) {
55 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
57 $this->dieWithError( [
58 'apierror-toofewexpiries',
60 count( $params['protections'] )
65 $restrictionTypes = $titleObj->getRestrictionTypes();
69 $resultProtections = [];
70 foreach ( $params['protections'] as $i => $prot ) {
71 $p = explode( '=', $prot );
72 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
74 if ( $titleObj->exists() && $p[0] == 'create' ) {
75 $this->dieWithError( 'apierror-create-titleexists' );
77 if ( !$titleObj->exists() && $p[0] != 'create' ) {
78 $this->dieWithError( 'apierror-missingtitle-createonly' );
81 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
82 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
84 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
85 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
88 if ( wfIsInfinity( $expiry[$i] ) ) {
89 $expiryarray[$p[0]] = 'infinity';
91 $exp = strtotime( $expiry[$i] );
92 if ( $exp < 0 ||
!$exp ) {
93 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
96 $exp = wfTimestamp( TS_MW
, $exp );
97 if ( $exp < wfTimestampNow() ) {
98 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
100 $expiryarray[$p[0]] = $exp;
102 $resultProtections[] = [
103 $p[0] => $protections[$p[0]],
104 'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601
, 'infinite' ),
108 $cascade = $params['cascade'];
110 $watch = $params['watch'] ?
'watch' : $params['watchlist'];
111 $this->setWatch( $watch, $titleObj, 'watchdefault' );
113 $status = $pageObj->doUpdateRestrictions(
122 if ( !$status->isOK() ) {
123 $this->dieStatus( $status );
126 'title' => $titleObj->getPrefixedText(),
127 'reason' => $params['reason']
130 $res['cascade'] = true;
132 $res['protections'] = $resultProtections;
133 $result = $this->getResult();
134 ApiResult
::setIndexedTagName( $res['protections'], 'protection' );
135 $result->addValue( null, $this->getModuleName(), $res );
138 public function mustBePosted() {
142 public function isWriteMode() {
146 public function getAllowedParams() {
149 ApiBase
::PARAM_TYPE
=> 'string',
152 ApiBase
::PARAM_TYPE
=> 'integer',
155 ApiBase
::PARAM_ISMULTI
=> true,
156 ApiBase
::PARAM_REQUIRED
=> true,
159 ApiBase
::PARAM_ISMULTI
=> true,
160 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
161 ApiBase
::PARAM_DFLT
=> 'infinite',
165 ApiBase
::PARAM_TYPE
=> 'tags',
166 ApiBase
::PARAM_ISMULTI
=> true,
170 ApiBase
::PARAM_DFLT
=> false,
171 ApiBase
::PARAM_DEPRECATED
=> true,
174 ApiBase
::PARAM_DFLT
=> 'preferences',
175 ApiBase
::PARAM_TYPE
=> [
185 public function needsToken() {
189 protected function getExamplesMessages() {
191 'action=protect&title=Main%20Page&token=123ABC&' .
192 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
193 => 'apihelp-protect-example-protect',
194 'action=protect&title=Main%20Page&token=123ABC&' .
195 'protections=edit=all|move=all&reason=Lifting%20restrictions'
196 => 'apihelp-protect-example-unprotect',
197 'action=protect&title=Main%20Page&token=123ABC&' .
198 'protections=&reason=Lifting%20restrictions'
199 => 'apihelp-protect-example-unprotect2',
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Protect';