Remove score display from search engine
[mediawiki.git] / includes / api / ApiProtect.php
blobb9f97e3536a48296545a3f779a87b859cb794958
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 $params = $this->extractRequestParams();
34 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
35 $titleObj = $pageObj->getTitle();
37 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
38 if ( $errors ) {
39 // We don't care about multiple errors, just report one of them
40 $this->dieUsageMsg( reset( $errors ) );
43 $expiry = (array)$params['expiry'];
44 if ( count( $expiry ) != count( $params['protections'] ) ) {
45 if ( count( $expiry ) == 1 ) {
46 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
47 } else {
48 $this->dieUsageMsg( array(
49 'toofewexpiries',
50 count( $expiry ),
51 count( $params['protections'] )
52 ) );
56 $restrictionTypes = $titleObj->getRestrictionTypes();
57 $db = $this->getDB();
59 $protections = array();
60 $expiryarray = array();
61 $resultProtections = array();
62 foreach ( $params['protections'] as $i => $prot ) {
63 $p = explode( '=', $prot );
64 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
66 if ( $titleObj->exists() && $p[0] == 'create' ) {
67 $this->dieUsageMsg( 'create-titleexists' );
69 if ( !$titleObj->exists() && $p[0] != 'create' ) {
70 $this->dieUsageMsg( 'missingtitle-createonly' );
73 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
74 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
76 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
77 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
80 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'infinity', 'never' ) ) ) {
81 $expiryarray[$p[0]] = $db->getInfinity();
82 } else {
83 $exp = strtotime( $expiry[$i] );
84 if ( $exp < 0 || !$exp ) {
85 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
88 $exp = wfTimestamp( TS_MW, $exp );
89 if ( $exp < wfTimestampNow() ) {
90 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
92 $expiryarray[$p[0]] = $exp;
94 $resultProtections[] = array(
95 $p[0] => $protections[$p[0]],
96 'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity()
97 ? 'infinite'
98 : wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] )
103 $cascade = $params['cascade'];
105 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
106 $this->setWatch( $watch, $titleObj, 'watchdefault' );
108 $status = $pageObj->doUpdateRestrictions(
109 $protections,
110 $expiryarray,
111 $cascade,
112 $params['reason'],
113 $this->getUser()
116 if ( !$status->isOK() ) {
117 $this->dieStatus( $status );
119 $res = array(
120 'title' => $titleObj->getPrefixedText(),
121 'reason' => $params['reason']
123 if ( $cascade ) {
124 $res['cascade'] = '';
126 $res['protections'] = $resultProtections;
127 $result = $this->getResult();
128 $result->setIndexedTagName( $res['protections'], 'protection' );
129 $result->addValue( null, $this->getModuleName(), $res );
132 public function mustBePosted() {
133 return true;
136 public function isWriteMode() {
137 return true;
140 public function getAllowedParams() {
141 return array(
142 'title' => array(
143 ApiBase::PARAM_TYPE => 'string',
145 'pageid' => array(
146 ApiBase::PARAM_TYPE => 'integer',
148 'token' => array(
149 ApiBase::PARAM_TYPE => 'string',
150 ApiBase::PARAM_REQUIRED => true
152 'protections' => array(
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_REQUIRED => true,
156 'expiry' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_ALLOW_DUPLICATES => true,
159 ApiBase::PARAM_DFLT => 'infinite',
161 'reason' => '',
162 'cascade' => false,
163 'watch' => array(
164 ApiBase::PARAM_DFLT => false,
165 ApiBase::PARAM_DEPRECATED => true,
167 'watchlist' => array(
168 ApiBase::PARAM_DFLT => 'preferences',
169 ApiBase::PARAM_TYPE => array(
170 'watch',
171 'unwatch',
172 'preferences',
173 'nochange'
179 public function getParamDescription() {
180 $p = $this->getModulePrefix();
182 return array(
183 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
184 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
185 'token' => 'A protect token previously retrieved through prop=info',
186 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
187 'expiry' => array(
188 'Expiry timestamps. If only one timestamp is ' .
189 'set, it\'ll be used for all protections.',
190 'Use \'infinite\', \'indefinite\', \'infinity\' or \'never\', for a never-expiring protection.'
192 'reason' => 'Reason for (un)protecting',
193 'cascade' => array(
194 'Enable cascading protection (i.e. protect pages included in this page)',
195 'Ignored if not all protection levels are \'sysop\' or \'protect\''
197 'watch' => 'If set, add the page being (un)protected to your watchlist',
198 'watchlist' => 'Unconditionally add or remove the page from your ' .
199 'watchlist, use preferences or do not change watch',
203 public function getResultProperties() {
204 return array(
205 '' => array(
206 'title' => 'string',
207 'reason' => 'string',
208 'cascade' => 'boolean'
213 public function getDescription() {
214 return 'Change the protection level of a page.';
217 public function getPossibleErrors() {
218 return array_merge( parent::getPossibleErrors(),
219 $this->getTitleOrPageIdErrorMessage(),
220 array(
221 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
222 array( 'create-titleexists' ),
223 array( 'missingtitle-createonly' ),
224 array( 'protect-invalidaction', 'action' ),
225 array( 'protect-invalidlevel', 'level' ),
226 array( 'invalidexpiry', 'expiry' ),
227 array( 'pastexpiry', 'expiry' ),
232 public function needsToken() {
233 return true;
236 public function getTokenSalt() {
237 return '';
240 public function getExamples() {
241 return array(
242 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
243 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
244 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
245 'protections=edit=all|move=all&reason=Lifting%20restrictions'
249 public function getHelpUrls() {
250 return 'https://www.mediawiki.org/wiki/API:Protect';