Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / api / ApiProtect.php
blobc74f890a57c5446aa625e8bc9ef6078b1d1d4963
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiProtect extends ApiBase {
31 public function execute() {
32 $params = $this->extractRequestParams();
34 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
35 $titleObj = $pageObj->getTitle();
37 $this->checkTitleUserPermissions( $titleObj, 'protect' );
39 $user = $this->getUser();
40 $tags = $params['tags'];
42 // Check if user can add tags
43 if ( !is_null( $tags ) ) {
44 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
45 if ( !$ableToTag->isOK() ) {
46 $this->dieStatus( $ableToTag );
50 $expiry = (array)$params['expiry'];
51 if ( count( $expiry ) != count( $params['protections'] ) ) {
52 if ( count( $expiry ) == 1 ) {
53 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
54 } else {
55 $this->dieWithError( [
56 'apierror-toofewexpiries',
57 count( $expiry ),
58 count( $params['protections'] )
59 ] );
63 $restrictionTypes = $titleObj->getRestrictionTypes();
65 $protections = [];
66 $expiryarray = [];
67 $resultProtections = [];
68 foreach ( $params['protections'] as $i => $prot ) {
69 $p = explode( '=', $prot );
70 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
72 if ( $titleObj->exists() && $p[0] == 'create' ) {
73 $this->dieWithError( 'apierror-create-titleexists' );
75 if ( !$titleObj->exists() && $p[0] != 'create' ) {
76 $this->dieWithError( 'apierror-missingtitle-createonly' );
79 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
80 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
82 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
83 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
86 if ( wfIsInfinity( $expiry[$i] ) ) {
87 $expiryarray[$p[0]] = 'infinity';
88 } else {
89 $exp = strtotime( $expiry[$i] );
90 if ( $exp < 0 || !$exp ) {
91 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
94 $exp = wfTimestamp( TS_MW, $exp );
95 if ( $exp < wfTimestampNow() ) {
96 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
98 $expiryarray[$p[0]] = $exp;
100 $resultProtections[] = [
101 $p[0] => $protections[$p[0]],
102 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
106 $cascade = $params['cascade'];
108 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
109 $this->setWatch( $watch, $titleObj, 'watchdefault' );
111 $status = $pageObj->doUpdateRestrictions(
112 $protections,
113 $expiryarray,
114 $cascade,
115 $params['reason'],
116 $user,
117 $tags
120 if ( !$status->isOK() ) {
121 $this->dieStatus( $status );
123 $res = [
124 'title' => $titleObj->getPrefixedText(),
125 'reason' => $params['reason']
127 if ( $cascade ) {
128 $res['cascade'] = true;
130 $res['protections'] = $resultProtections;
131 $result = $this->getResult();
132 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
133 $result->addValue( null, $this->getModuleName(), $res );
136 public function mustBePosted() {
137 return true;
140 public function isWriteMode() {
141 return true;
144 public function getAllowedParams() {
145 return [
146 'title' => [
147 ApiBase::PARAM_TYPE => 'string',
149 'pageid' => [
150 ApiBase::PARAM_TYPE => 'integer',
152 'protections' => [
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_REQUIRED => true,
156 'expiry' => [
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_ALLOW_DUPLICATES => true,
159 ApiBase::PARAM_DFLT => 'infinite',
161 'reason' => '',
162 'tags' => [
163 ApiBase::PARAM_TYPE => 'tags',
164 ApiBase::PARAM_ISMULTI => true,
166 'cascade' => false,
167 'watch' => [
168 ApiBase::PARAM_DFLT => false,
169 ApiBase::PARAM_DEPRECATED => true,
171 'watchlist' => [
172 ApiBase::PARAM_DFLT => 'preferences',
173 ApiBase::PARAM_TYPE => [
174 'watch',
175 'unwatch',
176 'preferences',
177 'nochange'
183 public function needsToken() {
184 return 'csrf';
187 protected function getExamplesMessages() {
188 return [
189 'action=protect&title=Main%20Page&token=123ABC&' .
190 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
191 => 'apihelp-protect-example-protect',
192 'action=protect&title=Main%20Page&token=123ABC&' .
193 'protections=edit=all|move=all&reason=Lifting%20restrictions'
194 => 'apihelp-protect-example-unprotect',
195 'action=protect&title=Main%20Page&token=123ABC&' .
196 'protections=&reason=Lifting%20restrictions'
197 => 'apihelp-protect-example-unprotect2',
201 public function getHelpUrls() {
202 return 'https://www.mediawiki.org/wiki/API:Protect';