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
{
32 public function __construct( $main, $action ) {
33 parent
::__construct( $main, $action );
36 public function execute() {
37 global $wgRestrictionLevels;
38 $params = $this->extractRequestParams();
40 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
42 if ( isset( $params['title'] ) ) {
43 $titleObj = Title
::newFromText( $params['title'] );
45 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
47 $pageObj = WikiPage
::factory( $titleObj );
48 $pageObj->loadPageData( 'fromdbmaster' );
49 } elseif ( isset( $params['pageid'] ) ) {
50 $pageObj = WikiPage
::newFromID( $params['pageid'] );
52 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
54 $titleObj = $pageObj->getTitle();
57 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
59 // We don't care about multiple errors, just report one of them
60 $this->dieUsageMsg( reset( $errors ) );
63 $expiry = (array)$params['expiry'];
64 if ( count( $expiry ) != count( $params['protections'] ) ) {
65 if ( count( $expiry ) == 1 ) {
66 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
68 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
72 $restrictionTypes = $titleObj->getRestrictionTypes();
73 $dbr = wfGetDB( DB_SLAVE
);
75 $protections = array();
76 $expiryarray = array();
77 $resultProtections = array();
78 foreach ( $params['protections'] as $i => $prot ) {
79 $p = explode( '=', $prot );
80 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
82 if ( $titleObj->exists() && $p[0] == 'create' ) {
83 $this->dieUsageMsg( 'create-titleexists' );
85 if ( !$titleObj->exists() && $p[0] != 'create' ) {
86 $this->dieUsageMsg( 'missingtitle-createonly' );
89 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
90 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
92 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
93 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
96 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
97 $expiryarray[$p[0]] = $dbr->getInfinity();
99 $exp = strtotime( $expiry[$i] );
100 if ( $exp < 0 ||
!$exp ) {
101 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
104 $exp = wfTimestamp( TS_MW
, $exp );
105 if ( $exp < wfTimestampNow() ) {
106 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
108 $expiryarray[$p[0]] = $exp;
110 $resultProtections[] = array( $p[0] => $protections[$p[0]],
111 'expiry' => ( $expiryarray[$p[0]] == $dbr->getInfinity() ?
113 wfTimestamp( TS_ISO_8601
, $expiryarray[$p[0]] ) ) );
116 $cascade = $params['cascade'];
118 $watch = $params['watch'] ?
'watch' : $params['watchlist'];
119 $this->setWatch( $watch, $titleObj );
121 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
123 if ( !$status->isOK() ) {
124 $errors = $status->getErrorsArray();
125 $this->dieUsageMsg( $errors[0] );
128 'title' => $titleObj->getPrefixedText(),
129 'reason' => $params['reason']
132 $res['cascade'] = '';
134 $res['protections'] = $resultProtections;
135 $result = $this->getResult();
136 $result->setIndexedTagName( $res['protections'], 'protection' );
137 $result->addValue( null, $this->getModuleName(), $res );
140 public function mustBePosted() {
144 public function isWriteMode() {
148 public function getAllowedParams() {
151 ApiBase
::PARAM_TYPE
=> 'string',
154 ApiBase
::PARAM_TYPE
=> 'integer',
157 'protections' => array(
158 ApiBase
::PARAM_ISMULTI
=> true,
159 ApiBase
::PARAM_REQUIRED
=> true,
162 ApiBase
::PARAM_ISMULTI
=> true,
163 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
164 ApiBase
::PARAM_DFLT
=> 'infinite',
169 ApiBase
::PARAM_DFLT
=> false,
170 ApiBase
::PARAM_DEPRECATED
=> true,
172 'watchlist' => array(
173 ApiBase
::PARAM_DFLT
=> 'preferences',
174 ApiBase
::PARAM_TYPE
=> array(
184 public function getParamDescription() {
185 $p = $this->getModulePrefix();
187 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
188 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
189 'token' => 'A protect token previously retrieved through prop=info',
190 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
191 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
192 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
193 'reason' => 'Reason for (un)protecting (optional)',
194 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
195 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
196 'watch' => 'If set, add the page being (un)protected to your watchlist',
197 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
201 public function getDescription() {
202 return 'Change the protection level of a page';
205 public function getPossibleErrors() {
206 return array_merge( parent
::getPossibleErrors(),
207 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
209 array( 'invalidtitle', 'title' ),
210 array( 'nosuchpageid', 'pageid' ),
211 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
212 array( 'create-titleexists' ),
213 array( 'missingtitle-createonly' ),
214 array( 'protect-invalidaction', 'action' ),
215 array( 'protect-invalidlevel', 'level' ),
216 array( 'invalidexpiry', 'expiry' ),
217 array( 'pastexpiry', 'expiry' ),
222 public function needsToken() {
226 public function getTokenSalt() {
230 public function getExamples() {
232 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
233 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
237 public function getHelpUrls() {
238 return 'https://www.mediawiki.org/wiki/API:Protect';
241 public function getVersion() {
242 return __CLASS__
. ': $Id$';