API: Log all deprecated parameter uses to api-feature-usage.log
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 1, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiProtect extends ApiBase {
31 public function execute() {
32 global $wgContLang;
33
34 $params = $this->extractRequestParams();
35
36 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
37 $titleObj = $pageObj->getTitle();
38
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 ) );
43 }
44
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 ) );
55 }
56 }
57
58 $restrictionTypes = $titleObj->getRestrictionTypes();
59 $db = $this->getDB();
60
61 $protections = array();
62 $expiryarray = array();
63 $resultProtections = array();
64 foreach ( $params['protections'] as $i => $prot ) {
65 $p = explode( '=', $prot );
66 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
67
68 if ( $titleObj->exists() && $p[0] == 'create' ) {
69 $this->dieUsageMsg( 'create-titleexists' );
70 }
71 if ( !$titleObj->exists() && $p[0] != 'create' ) {
72 $this->dieUsageMsg( 'missingtitle-createonly' );
73 }
74
75 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
76 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
77 }
78 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
79 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
80 }
81
82 if ( wfIsInfinity( $expiry[$i] ) ) {
83 $expiryarray[$p[0]] = 'infinity';
84 } else {
85 $exp = strtotime( $expiry[$i] );
86 if ( $exp < 0 || !$exp ) {
87 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
88 }
89
90 $exp = wfTimestamp( TS_MW, $exp );
91 if ( $exp < wfTimestampNow() ) {
92 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
93 }
94 $expiryarray[$p[0]] = $exp;
95 }
96 $resultProtections[] = array(
97 $p[0] => $protections[$p[0]],
98 'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ),
99 );
100 }
101
102 $cascade = $params['cascade'];
103
104 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
105 $this->setWatch( $watch, $titleObj, 'watchdefault' );
106
107 $status = $pageObj->doUpdateRestrictions(
108 $protections,
109 $expiryarray,
110 $cascade,
111 $params['reason'],
112 $this->getUser()
113 );
114
115 if ( !$status->isOK() ) {
116 $this->dieStatus( $status );
117 }
118 $res = array(
119 'title' => $titleObj->getPrefixedText(),
120 'reason' => $params['reason']
121 );
122 if ( $cascade ) {
123 $res['cascade'] = true;
124 }
125 $res['protections'] = $resultProtections;
126 $result = $this->getResult();
127 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
128 $result->addValue( null, $this->getModuleName(), $res );
129 }
130
131 public function mustBePosted() {
132 return true;
133 }
134
135 public function isWriteMode() {
136 return true;
137 }
138
139 public function getAllowedParams() {
140 return array(
141 'title' => array(
142 ApiBase::PARAM_TYPE => 'string',
143 ),
144 'pageid' => array(
145 ApiBase::PARAM_TYPE => 'integer',
146 ),
147 'protections' => array(
148 ApiBase::PARAM_ISMULTI => true,
149 ApiBase::PARAM_REQUIRED => true,
150 ),
151 'expiry' => array(
152 ApiBase::PARAM_ISMULTI => true,
153 ApiBase::PARAM_ALLOW_DUPLICATES => true,
154 ApiBase::PARAM_DFLT => 'infinite',
155 ),
156 'reason' => '',
157 'cascade' => false,
158 'watch' => array(
159 ApiBase::PARAM_DFLT => false,
160 ApiBase::PARAM_DEPRECATED => true,
161 ),
162 'watchlist' => array(
163 ApiBase::PARAM_DFLT => 'preferences',
164 ApiBase::PARAM_TYPE => array(
165 'watch',
166 'unwatch',
167 'preferences',
168 'nochange'
169 ),
170 ),
171 );
172 }
173
174 public function needsToken() {
175 return 'csrf';
176 }
177
178 protected function getExamplesMessages() {
179 return array(
180 'action=protect&title=Main%20Page&token=123ABC&' .
181 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
182 => 'apihelp-protect-example-protect',
183 'action=protect&title=Main%20Page&token=123ABC&' .
184 'protections=edit=all|move=all&reason=Lifting%20restrictions'
185 => 'apihelp-protect-example-unprotect',
186 'action=protect&title=Main%20Page&token=123ABC&' .
187 'protections=&reason=Lifting%20restrictions'
188 => 'apihelp-protect-example-unprotect2',
189 );
190 }
191
192 public function getHelpUrls() {
193 return 'https://www.mediawiki.org/wiki/API:Protect';
194 }
195 }