]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiProtect.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / api / ApiProtect.php
1 <?php
2
3 /*
4  * Created on Sep 1, 2007
5  * API for MediaWiki 1.8+
6  *
7  * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  */
24
25 if (!defined('MEDIAWIKI')) {
26         // Eclipse helper - will be ignored in production
27         require_once ("ApiBase.php");
28 }
29
30 /**
31  * @ingroup API
32  */
33 class ApiProtect extends ApiBase {
34
35         public function __construct($main, $action) {
36                 parent :: __construct($main, $action);
37         }
38
39         public function execute() {
40                 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
41                 $params = $this->extractRequestParams();
42
43                 $titleObj = NULL;
44                 if(!isset($params['title']))
45                         $this->dieUsageMsg(array('missingparam', 'title'));
46                 if(!isset($params['token']))
47                         $this->dieUsageMsg(array('missingparam', 'token'));
48                 if(empty($params['protections']))
49                         $this->dieUsageMsg(array('missingparam', 'protections'));
50
51                 if(!$wgUser->matchEditToken($params['token']))
52                         $this->dieUsageMsg(array('sessionfailure'));
53
54                 $titleObj = Title::newFromText($params['title']);
55                 if(!$titleObj)
56                         $this->dieUsageMsg(array('invalidtitle', $params['title']));
57
58                 $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
59                 if($errors)
60                         // We don't care about multiple errors, just report one of them
61                         $this->dieUsageMsg(reset($errors));
62
63                 $expiry = (array)$params['expiry'];
64                 if(count($expiry) != count($params['protections']))
65                 {
66                         if(count($expiry) == 1)
67                                 $expiry = array_fill(0, count($params['protections']), $expiry[0]);
68                         else
69                                 $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections'])));
70                 }
71                         
72                 $protections = array();
73                 $expiryarray = array();
74                 $resultProtections = array();
75                 foreach($params['protections'] as $i => $prot)
76                 {
77                         $p = explode('=', $prot);
78                         $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
79                         if($titleObj->exists() && $p[0] == 'create')
80                                 $this->dieUsageMsg(array('create-titleexists'));
81                         if(!$titleObj->exists() && $p[0] != 'create')
82                                 $this->dieUsageMsg(array('missingtitles-createonly'));
83                         if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create')
84                                 $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
85                         if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all')
86                                 $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
87
88                         if(in_array($expiry[$i], array('infinite', 'indefinite', 'never')))
89                                 $expiryarray[$p[0]] = Block::infinity();
90                         else
91                         {
92                                 $exp = strtotime($expiry[$i]);
93                                 if($exp < 0 || $exp == false)
94                                         $this->dieUsageMsg(array('invalidexpiry', $expiry[$i]));
95
96                                 $exp = wfTimestamp(TS_MW, $exp);
97                                 if($exp < wfTimestampNow())
98                                         $this->dieUsageMsg(array('pastexpiry', $expiry[$i]));
99                                 $expiryarray[$p[0]] = $exp;
100                         }
101                         $resultProtections[] = array($p[0] => $protections[$p[0]],
102                                         'expiry' => ($expiryarray[$p[0]] == Block::infinity() ?
103                                                                 'infinite' :
104                                                                 wfTimestamp(TS_ISO_8601, $expiryarray[$p[0]])));
105                 }
106
107                 $cascade = $params['cascade'];
108                 $articleObj = new Article($titleObj);
109                 if($params['watch'])
110                         $articleObj->doWatch();
111                 if($titleObj->exists())
112                         $ok = $articleObj->updateRestrictions($protections, $params['reason'], $cascade, $expiryarray);
113                 else
114                         $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiryarray['create']);
115                 if(!$ok)
116                         // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
117                         // Just throw an unknown error in this case, as it's very likely to be a race condition
118                         $this->dieUsageMsg(array());
119                 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
120                 if($cascade)
121                         $res['cascade'] = '';
122                 $res['protections'] = $resultProtections;
123                 $this->getResult()->setIndexedTagName($res['protections'], 'protection');
124                 $this->getResult()->addValue(null, $this->getModuleName(), $res);
125         }
126
127         public function mustBePosted() { return true; }
128
129         public function isWriteMode() {
130                 return true;
131         }
132
133         public function getAllowedParams() {
134                 return array (
135                         'title' => null,
136                         'token' => null,
137                         'protections' => array(
138                                 ApiBase :: PARAM_ISMULTI => true
139                         ),
140                         'expiry' => array(
141                                 ApiBase :: PARAM_ISMULTI => true,
142                                 ApiBase :: PARAM_ALLOW_DUPLICATES => true,
143                                 ApiBase :: PARAM_DFLT => 'infinite',
144                         ),
145                         'reason' => '',
146                         'cascade' => false,
147                         'watch' => false,
148                 );
149         }
150
151         public function getParamDescription() {
152                 return array (
153                         'title' => 'Title of the page you want to (un)protect.',
154                         'token' => 'A protect token previously retrieved through prop=info',
155                         'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
156                         'expiry' => array('Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
157                                         'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.'),
158                         'reason' => 'Reason for (un)protecting (optional)',
159                         'cascade' => array('Enable cascading protection (i.e. protect pages included in this page)',
160                                         'Ignored if not all protection levels are \'sysop\' or \'protect\''),
161                         'watch' => 'If set, add the page being (un)protected to your watchlist',
162                 );
163         }
164
165         public function getDescription() {
166                 return array(
167                         'Change the protection level of a page.'
168                 );
169         }
170
171         protected function getExamples() {
172                 return array (
173                         'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
174                         'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
175                 );
176         }
177
178         public function getVersion() {
179                 return __CLASS__ . ': $Id: ApiProtect.php 48122 2009-03-07 12:58:41Z catrope $';
180         }
181 }