3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@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
26 class ApiProtect
extends ApiBase
{
27 public function execute() {
28 $params = $this->extractRequestParams();
30 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
31 $titleObj = $pageObj->getTitle();
33 $this->checkTitleUserPermissions( $titleObj, 'protect' );
35 $user = $this->getUser();
36 $tags = $params['tags'];
38 // Check if user can add tags
39 if ( !is_null( $tags ) ) {
40 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $tags, $user );
41 if ( !$ableToTag->isOK() ) {
42 $this->dieStatus( $ableToTag );
46 $expiry = (array)$params['expiry'];
47 if ( count( $expiry ) != count( $params['protections'] ) ) {
48 if ( count( $expiry ) == 1 ) {
49 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
51 $this->dieWithError( [
52 'apierror-toofewexpiries',
54 count( $params['protections'] )
59 $restrictionTypes = $titleObj->getRestrictionTypes();
63 $resultProtections = [];
64 foreach ( $params['protections'] as $i => $prot ) {
65 $p = explode( '=', $prot );
66 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
68 if ( $titleObj->exists() && $p[0] == 'create' ) {
69 $this->dieWithError( 'apierror-create-titleexists' );
71 if ( !$titleObj->exists() && $p[0] != 'create' ) {
72 $this->dieWithError( 'apierror-missingtitle-createonly' );
75 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
76 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
78 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
79 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
82 if ( wfIsInfinity( $expiry[$i] ) ) {
83 $expiryarray[$p[0]] = 'infinity';
85 $exp = strtotime( $expiry[$i] );
86 if ( $exp < 0 ||
!$exp ) {
87 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
90 $exp = wfTimestamp( TS_MW
, $exp );
91 if ( $exp < wfTimestampNow() ) {
92 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
94 $expiryarray[$p[0]] = $exp;
96 $resultProtections[] = [
97 $p[0] => $protections[$p[0]],
98 'expiry' => ApiResult
::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
102 $cascade = $params['cascade'];
104 $watch = $params['watch'] ?
'watch' : $params['watchlist'];
105 $this->setWatch( $watch, $titleObj, 'watchdefault' );
107 $status = $pageObj->doUpdateRestrictions(
116 if ( !$status->isOK() ) {
117 $this->dieStatus( $status );
120 'title' => $titleObj->getPrefixedText(),
121 'reason' => $params['reason']
124 $res['cascade'] = true;
126 $res['protections'] = $resultProtections;
127 $result = $this->getResult();
128 ApiResult
::setIndexedTagName( $res['protections'], 'protection' );
129 $result->addValue( null, $this->getModuleName(), $res );
132 public function mustBePosted() {
136 public function isWriteMode() {
140 public function getAllowedParams() {
143 ApiBase
::PARAM_TYPE
=> 'string',
146 ApiBase
::PARAM_TYPE
=> 'integer',
149 ApiBase
::PARAM_ISMULTI
=> true,
150 ApiBase
::PARAM_REQUIRED
=> true,
153 ApiBase
::PARAM_ISMULTI
=> true,
154 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
155 ApiBase
::PARAM_DFLT
=> 'infinite',
159 ApiBase
::PARAM_TYPE
=> 'tags',
160 ApiBase
::PARAM_ISMULTI
=> true,
164 ApiBase
::PARAM_DFLT
=> false,
165 ApiBase
::PARAM_DEPRECATED
=> true,
168 ApiBase
::PARAM_DFLT
=> 'preferences',
169 ApiBase
::PARAM_TYPE
=> [
179 public function needsToken() {
183 protected function getExamplesMessages() {
185 'action=protect&title=Main%20Page&token=123ABC&' .
186 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
187 => 'apihelp-protect-example-protect',
188 'action=protect&title=Main%20Page&token=123ABC&' .
189 'protections=edit=all|move=all&reason=Lifting%20restrictions'
190 => 'apihelp-protect-example-unprotect',
191 'action=protect&title=Main%20Page&token=123ABC&' .
192 'protections=&reason=Lifting%20restrictions'
193 => 'apihelp-protect-example-unprotect2',
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';