Don't duplicate code, use wfAppendQuery
[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, $wgRestrictionTypes, $wgRestrictionLevels;
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 $expiryarray = array();
79 foreach($params['protections'] as $prot)
80 {
81 $p = explode('=', $prot);
82 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
83 if($titleObj->exists() && $p[0] == 'create')
84 $this->dieUsageMsg(array('create-titleexists'));
85 if(!$titleObj->exists() && $p[0] != 'create')
86 $this->dieUsageMsg(array('missingtitles-createonly'));
87 if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create')
88 $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
89 if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all')
90 $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
91 $expiryarray[$p[0]] = $expiry;
92 }
93
94 if($titleObj->exists()) {
95 $articleObj = new Article($titleObj);
96 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiryarray);
97 } else
98 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
99 if(!$ok)
100 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
101 // Just throw an unknown error in this case, as it's very likely to be a race condition
102 $this->dieUsageMsg(array());
103 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
104 if($expiry == Block::infinity())
105 $res['expiry'] = 'infinity';
106 else
107 $res['expiry'] = wfTimestamp(TS_ISO_8601, $expiry);
108
109 if($params['cascade'])
110 $res['cascade'] = '';
111 $res['protections'] = $protections;
112 $this->getResult()->addValue(null, $this->getModuleName(), $res);
113 }
114
115 public function mustBePosted() { return true; }
116
117 public function getAllowedParams() {
118 return array (
119 'title' => null,
120 'token' => null,
121 'protections' => array(
122 ApiBase :: PARAM_ISMULTI => true
123 ),
124 'expiry' => 'infinite',
125 'reason' => '',
126 'cascade' => false
127 );
128 }
129
130 public function getParamDescription() {
131 return array (
132 'title' => 'Title of the page you want to restore.',
133 'token' => 'A protect token previously retrieved through prop=info',
134 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
135 'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',
136 'reason' => 'Reason for (un)protecting (optional)',
137 'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'
138 );
139 }
140
141 public function getDescription() {
142 return array(
143 'Change the protection level of a page.'
144 );
145 }
146
147 protected function getExamples() {
148 return array (
149 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',
150 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
151 );
152 }
153
154 public function getVersion() {
155 return __CLASS__ . ': $Id$';
156 }
157 }