Tweaks for r40686, r40687:
[lhc/web/wiklou.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;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsageMsg(array('missingparam', 'title'));
47 if(!isset($params['token']))
48 $this->dieUsageMsg(array('missingparam', 'token'));
49 if(!isset($params['protections']) || empty($params['protections']))
50 $this->dieUsageMsg(array('missingparam', 'protections'));
51
52 if(!$wgUser->matchEditToken($params['token']))
53 $this->dieUsageMsg(array('sessionfailure'));
54
55 $titleObj = Title::newFromText($params['title']);
56 if(!$titleObj)
57 $this->dieUsageMsg(array('invalidtitle', $params['title']));
58
59 $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
60 if(!empty($errors))
61 // We don't care about multiple errors, just report one of them
62 $this->dieUsageMsg(current($errors));
63
64 if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
65 $expiry = Block::infinity();
66 else
67 {
68 $expiry = strtotime($params['expiry']);
69 if($expiry < 0 || $expiry == false)
70 $this->dieUsageMsg(array('invalidexpiry'));
71
72 $expiry = wfTimestamp(TS_MW, $expiry);
73 if($expiry < wfTimestampNow())
74 $this->dieUsageMsg(array('pastexpiry'));
75 }
76
77 $protections = array();
78 foreach($params['protections'] as $prot)
79 {
80 $p = explode('=', $prot);
81 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
82 if($titleObj->exists() && $p[0] == 'create')
83 $this->dieUsageMsg(array('create-titleexists'));
84 if(!$titleObj->exists() && $p[0] != 'create')
85 $this->dieUsageMsg(array('missingtitles-createonly'));
86 }
87
88 if($titleObj->exists()) {
89 $articleObj = new Article($titleObj);
90 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
91 } else
92 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
93 if(!$ok)
94 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
95 // Just throw an unknown error in this case, as it's very likely to be a race condition
96 $this->dieUsageMsg(array());
97 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
98 if($expiry == Block::infinity())
99 $res['expiry'] = 'infinity';
100 else
101 $res['expiry'] = wfTimestamp(TS_ISO_8601, $expiry);
102
103 if($params['cascade'])
104 $res['cascade'] = '';
105 $res['protections'] = $protections;
106 $this->getResult()->addValue(null, $this->getModuleName(), $res);
107 }
108
109 public function mustBePosted() { return true; }
110
111 public function getAllowedParams() {
112 return array (
113 'title' => null,
114 'token' => null,
115 'protections' => array(
116 ApiBase :: PARAM_ISMULTI => true
117 ),
118 'expiry' => 'infinite',
119 'reason' => '',
120 'cascade' => false
121 );
122 }
123
124 public function getParamDescription() {
125 return array (
126 'title' => 'Title of the page you want to restore.',
127 'token' => 'A protect token previously retrieved through prop=info',
128 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
129 'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',
130 'reason' => 'Reason for (un)protecting (optional)',
131 'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'
132 );
133 }
134
135 public function getDescription() {
136 return array(
137 'Change the protection level of a page.'
138 );
139 }
140
141 protected function getExamples() {
142 return array (
143 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',
144 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
145 );
146 }
147
148 public function getVersion() {
149 return __CLASS__ . ': $Id$';
150 }
151 }