PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / api / ApiProtect.php
blobd07301c520cb8270ab3b4c287397229c98dbc8fc
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 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
40 if ( $errors ) {
41 // We don't care about multiple errors, just report one of them
42 $this->dieUsageMsg( reset( $errors ) );
45 $expiry = (array)$params['expiry'];
46 if ( count( $expiry ) != count( $params['protections'] ) ) {
47 if ( count( $expiry ) == 1 ) {
48 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
49 } else {
50 $this->dieUsageMsg( array(
51 'toofewexpiries',
52 count( $expiry ),
53 count( $params['protections'] )
54 ) );
58 $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], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
78 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
81 if ( wfIsInfinity( $expiry[$i] ) ) {
82 $expiryarray[$p[0]] = 'infinity';
83 } else {
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' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ),
101 $cascade = $params['cascade'];
103 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
104 $this->setWatch( $watch, $titleObj, 'watchdefault' );
106 $status = $pageObj->doUpdateRestrictions(
107 $protections,
108 $expiryarray,
109 $cascade,
110 $params['reason'],
111 $this->getUser()
114 if ( !$status->isOK() ) {
115 $this->dieStatus( $status );
117 $res = array(
118 'title' => $titleObj->getPrefixedText(),
119 'reason' => $params['reason']
121 if ( $cascade ) {
122 $res['cascade'] = true;
124 $res['protections'] = $resultProtections;
125 $result = $this->getResult();
126 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
127 $result->addValue( null, $this->getModuleName(), $res );
130 public function mustBePosted() {
131 return true;
134 public function isWriteMode() {
135 return true;
138 public function getAllowedParams() {
139 return array(
140 'title' => array(
141 ApiBase::PARAM_TYPE => 'string',
143 'pageid' => array(
144 ApiBase::PARAM_TYPE => 'integer',
146 'protections' => array(
147 ApiBase::PARAM_ISMULTI => true,
148 ApiBase::PARAM_REQUIRED => true,
150 'expiry' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_ALLOW_DUPLICATES => true,
153 ApiBase::PARAM_DFLT => 'infinite',
155 'reason' => '',
156 'cascade' => false,
157 'watch' => array(
158 ApiBase::PARAM_DFLT => false,
159 ApiBase::PARAM_DEPRECATED => true,
161 'watchlist' => array(
162 ApiBase::PARAM_DFLT => 'preferences',
163 ApiBase::PARAM_TYPE => array(
164 'watch',
165 'unwatch',
166 'preferences',
167 'nochange'
173 public function needsToken() {
174 return 'csrf';
177 protected function getExamplesMessages() {
178 return array(
179 'action=protect&title=Main%20Page&token=123ABC&' .
180 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
181 => 'apihelp-protect-example-protect',
182 'action=protect&title=Main%20Page&token=123ABC&' .
183 'protections=edit=all|move=all&reason=Lifting%20restrictions'
184 => 'apihelp-protect-example-unprotect',
185 'action=protect&title=Main%20Page&token=123ABC&' .
186 'protections=&reason=Lifting%20restrictions'
187 => 'apihelp-protect-example-unprotect2',
191 public function getHelpUrls() {
192 return 'https://www.mediawiki.org/wiki/API:Protect';