Back off from job types longer for DB read-only errors
[mediawiki.git] / includes / api / ApiProtect.php
blob746dc9a16bb7104ad5a8e3c279ae5be79cc25cb6
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 global $wgContLang;
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] );
56 } else {
57 $this->dieWithError( [
58 'apierror-toofewexpiries',
59 count( $expiry ),
60 count( $params['protections'] )
61 ] );
65 $restrictionTypes = $titleObj->getRestrictionTypes();
67 $protections = [];
68 $expiryarray = [];
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';
90 } else {
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(
114 $protections,
115 $expiryarray,
116 $cascade,
117 $params['reason'],
118 $user,
119 $tags
122 if ( !$status->isOK() ) {
123 $this->dieStatus( $status );
125 $res = [
126 'title' => $titleObj->getPrefixedText(),
127 'reason' => $params['reason']
129 if ( $cascade ) {
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() {
139 return true;
142 public function isWriteMode() {
143 return true;
146 public function getAllowedParams() {
147 return [
148 'title' => [
149 ApiBase::PARAM_TYPE => 'string',
151 'pageid' => [
152 ApiBase::PARAM_TYPE => 'integer',
154 'protections' => [
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_REQUIRED => true,
158 'expiry' => [
159 ApiBase::PARAM_ISMULTI => true,
160 ApiBase::PARAM_ALLOW_DUPLICATES => true,
161 ApiBase::PARAM_DFLT => 'infinite',
163 'reason' => '',
164 'tags' => [
165 ApiBase::PARAM_TYPE => 'tags',
166 ApiBase::PARAM_ISMULTI => true,
168 'cascade' => false,
169 'watch' => [
170 ApiBase::PARAM_DFLT => false,
171 ApiBase::PARAM_DEPRECATED => true,
173 'watchlist' => [
174 ApiBase::PARAM_DFLT => 'preferences',
175 ApiBase::PARAM_TYPE => [
176 'watch',
177 'unwatch',
178 'preferences',
179 'nochange'
185 public function needsToken() {
186 return 'csrf';
189 protected function getExamplesMessages() {
190 return [
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';