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() {
32 global $wgRestrictionLevels;
33 $params = $this->extractRequestParams();
35 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
36 $titleObj = $pageObj->getTitle();
38 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
40 // We don't care about multiple errors, just report one of them
41 $this->dieUsageMsg( reset( $errors ) );
44 $expiry = (array)$params['expiry'];
45 if ( count( $expiry ) != count( $params['protections'] ) ) {
46 if ( count( $expiry ) == 1 ) {
47 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
49 $this->dieUsageMsg( array(
52 count( $params['protections'] )
57 $restrictionTypes = $titleObj->getRestrictionTypes();
60 $protections = array();
61 $expiryarray = array();
62 $resultProtections = array();
63 foreach ( $params['protections'] as $i => $prot ) {
64 $p = explode( '=', $prot );
65 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
67 if ( $titleObj->exists() && $p[0] == 'create' ) {
68 $this->dieUsageMsg( 'create-titleexists' );
70 if ( !$titleObj->exists() && $p[0] != 'create' ) {
71 $this->dieUsageMsg( 'missingtitle-createonly' );
74 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
75 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
77 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
78 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
81 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
82 $expiryarray[$p[0]] = $db->getInfinity();
84 $exp = strtotime( $expiry[$i] );
85 if ( $exp < 0 ||
!$exp ) {
86 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
89 $exp = wfTimestamp( TS_MW
, $exp );
90 if ( $exp < wfTimestampNow() ) {
91 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
93 $expiryarray[$p[0]] = $exp;
95 $resultProtections[] = array(
96 $p[0] => $protections[$p[0]],
97 'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity()
99 : wfTimestamp( TS_ISO_8601
, $expiryarray[$p[0]] )
104 $cascade = $params['cascade'];
106 $watch = $params['watch'] ?
'watch' : $params['watchlist'];
107 $this->setWatch( $watch, $titleObj, 'watchdefault' );
109 $status = $pageObj->doUpdateRestrictions(
117 if ( !$status->isOK() ) {
118 $this->dieStatus( $status );
121 'title' => $titleObj->getPrefixedText(),
122 'reason' => $params['reason']
125 $res['cascade'] = '';
127 $res['protections'] = $resultProtections;
128 $result = $this->getResult();
129 $result->setIndexedTagName( $res['protections'], 'protection' );
130 $result->addValue( null, $this->getModuleName(), $res );
133 public function mustBePosted() {
137 public function isWriteMode() {
141 public function getAllowedParams() {
144 ApiBase
::PARAM_TYPE
=> 'string',
147 ApiBase
::PARAM_TYPE
=> 'integer',
150 ApiBase
::PARAM_TYPE
=> 'string',
151 ApiBase
::PARAM_REQUIRED
=> true
153 'protections' => array(
154 ApiBase
::PARAM_ISMULTI
=> true,
155 ApiBase
::PARAM_REQUIRED
=> true,
158 ApiBase
::PARAM_ISMULTI
=> true,
159 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
160 ApiBase
::PARAM_DFLT
=> 'infinite',
165 ApiBase
::PARAM_DFLT
=> false,
166 ApiBase
::PARAM_DEPRECATED
=> true,
168 'watchlist' => array(
169 ApiBase
::PARAM_DFLT
=> 'preferences',
170 ApiBase
::PARAM_TYPE
=> array(
180 public function getParamDescription() {
181 $p = $this->getModulePrefix();
184 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
185 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
186 'token' => 'A protect token previously retrieved through prop=info',
187 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
189 'Expiry timestamps. If only one timestamp is ' .
190 'set, it\'ll be used for all protections.',
191 'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.'
193 'reason' => 'Reason for (un)protecting',
195 'Enable cascading protection (i.e. protect pages included in this page)',
196 'Ignored if not all protection levels are \'sysop\' or \'protect\''
198 'watch' => 'If set, add the page being (un)protected to your watchlist',
199 'watchlist' => 'Unconditionally add or remove the page from your ' .
200 'watchlist, use preferences or do not change watch',
204 public function getResultProperties() {
208 'reason' => 'string',
209 'cascade' => 'boolean'
214 public function getDescription() {
215 return 'Change the protection level of a page.';
218 public function getPossibleErrors() {
219 return array_merge( parent
::getPossibleErrors(),
220 $this->getTitleOrPageIdErrorMessage(),
222 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
223 array( 'create-titleexists' ),
224 array( 'missingtitle-createonly' ),
225 array( 'protect-invalidaction', 'action' ),
226 array( 'protect-invalidlevel', 'level' ),
227 array( 'invalidexpiry', 'expiry' ),
228 array( 'pastexpiry', 'expiry' ),
233 public function needsToken() {
237 public function getTokenSalt() {
241 public function getExamples() {
243 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
244 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
245 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
246 'protections=edit=all|move=all&reason=Lifting%20restrictions'
250 public function getHelpUrls() {
251 return 'https://www.mediawiki.org/wiki/API:Protect';