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
|
|
|
$user = $this->getUser(); |
46
|
|
|
$tags = $params['tags']; |
47
|
|
|
|
48
|
|
|
// Check if user can add tags |
49
|
|
View Code Duplication |
if ( !is_null( $tags ) ) { |
50
|
|
|
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user ); |
51
|
|
|
if ( !$ableToTag->isOK() ) { |
52
|
|
|
$this->dieStatus( $ableToTag ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$expiry = (array)$params['expiry']; |
57
|
|
|
if ( count( $expiry ) != count( $params['protections'] ) ) { |
58
|
|
|
if ( count( $expiry ) == 1 ) { |
59
|
|
|
$expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] ); |
60
|
|
|
} else { |
61
|
|
|
$this->dieUsageMsg( [ |
62
|
|
|
'toofewexpiries', |
63
|
|
|
count( $expiry ), |
64
|
|
|
count( $params['protections'] ) |
65
|
|
|
] ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$restrictionTypes = $titleObj->getRestrictionTypes(); |
70
|
|
|
|
71
|
|
|
$protections = []; |
72
|
|
|
$expiryarray = []; |
73
|
|
|
$resultProtections = []; |
74
|
|
|
foreach ( $params['protections'] as $i => $prot ) { |
75
|
|
|
$p = explode( '=', $prot ); |
76
|
|
|
$protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] ); |
77
|
|
|
|
78
|
|
|
if ( $titleObj->exists() && $p[0] == 'create' ) { |
79
|
|
|
$this->dieUsageMsg( 'create-titleexists' ); |
80
|
|
|
} |
81
|
|
|
if ( !$titleObj->exists() && $p[0] != 'create' ) { |
82
|
|
|
$this->dieUsageMsg( 'missingtitle-createonly' ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) { |
86
|
|
|
$this->dieUsageMsg( [ 'protect-invalidaction', $p[0] ] ); |
87
|
|
|
} |
88
|
|
|
if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) { |
89
|
|
|
$this->dieUsageMsg( [ 'protect-invalidlevel', $p[1] ] ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( wfIsInfinity( $expiry[$i] ) ) { |
93
|
|
|
$expiryarray[$p[0]] = 'infinity'; |
94
|
|
|
} else { |
95
|
|
|
$exp = strtotime( $expiry[$i] ); |
96
|
|
|
if ( $exp < 0 || !$exp ) { |
97
|
|
|
$this->dieUsageMsg( [ 'invalidexpiry', $expiry[$i] ] ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$exp = wfTimestamp( TS_MW, $exp ); |
101
|
|
|
if ( $exp < wfTimestampNow() ) { |
102
|
|
|
$this->dieUsageMsg( [ 'pastexpiry', $expiry[$i] ] ); |
103
|
|
|
} |
104
|
|
|
$expiryarray[$p[0]] = $exp; |
105
|
|
|
} |
106
|
|
|
$resultProtections[] = [ |
107
|
|
|
$p[0] => $protections[$p[0]], |
108
|
|
|
'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ), |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$cascade = $params['cascade']; |
113
|
|
|
|
114
|
|
|
$watch = $params['watch'] ? 'watch' : $params['watchlist']; |
115
|
|
|
$this->setWatch( $watch, $titleObj, 'watchdefault' ); |
116
|
|
|
|
117
|
|
|
$status = $pageObj->doUpdateRestrictions( |
118
|
|
|
$protections, |
119
|
|
|
$expiryarray, |
120
|
|
|
$cascade, |
121
|
|
|
$params['reason'], |
122
|
|
|
$user, |
123
|
|
|
$tags |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
if ( !$status->isOK() ) { |
127
|
|
|
$this->dieStatus( $status ); |
128
|
|
|
} |
129
|
|
|
$res = [ |
130
|
|
|
'title' => $titleObj->getPrefixedText(), |
131
|
|
|
'reason' => $params['reason'] |
132
|
|
|
]; |
133
|
|
|
if ( $cascade ) { |
134
|
|
|
$res['cascade'] = true; |
135
|
|
|
} |
136
|
|
|
$res['protections'] = $resultProtections; |
137
|
|
|
$result = $this->getResult(); |
138
|
|
|
ApiResult::setIndexedTagName( $res['protections'], 'protection' ); |
139
|
|
|
$result->addValue( null, $this->getModuleName(), $res ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function mustBePosted() { |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function isWriteMode() { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getAllowedParams() { |
151
|
|
|
return [ |
152
|
|
|
'title' => [ |
153
|
|
|
ApiBase::PARAM_TYPE => 'string', |
154
|
|
|
], |
155
|
|
|
'pageid' => [ |
156
|
|
|
ApiBase::PARAM_TYPE => 'integer', |
157
|
|
|
], |
158
|
|
|
'protections' => [ |
159
|
|
|
ApiBase::PARAM_ISMULTI => true, |
160
|
|
|
ApiBase::PARAM_REQUIRED => true, |
161
|
|
|
], |
162
|
|
|
'expiry' => [ |
163
|
|
|
ApiBase::PARAM_ISMULTI => true, |
164
|
|
|
ApiBase::PARAM_ALLOW_DUPLICATES => true, |
165
|
|
|
ApiBase::PARAM_DFLT => 'infinite', |
166
|
|
|
], |
167
|
|
|
'reason' => '', |
168
|
|
|
'tags' => [ |
169
|
|
|
ApiBase::PARAM_TYPE => 'tags', |
170
|
|
|
ApiBase::PARAM_ISMULTI => true, |
171
|
|
|
], |
172
|
|
|
'cascade' => false, |
173
|
|
|
'watch' => [ |
174
|
|
|
ApiBase::PARAM_DFLT => false, |
175
|
|
|
ApiBase::PARAM_DEPRECATED => true, |
176
|
|
|
], |
177
|
|
|
'watchlist' => [ |
178
|
|
|
ApiBase::PARAM_DFLT => 'preferences', |
179
|
|
|
ApiBase::PARAM_TYPE => [ |
180
|
|
|
'watch', |
181
|
|
|
'unwatch', |
182
|
|
|
'preferences', |
183
|
|
|
'nochange' |
184
|
|
|
], |
185
|
|
|
], |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function needsToken() { |
190
|
|
|
return 'csrf'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function getExamplesMessages() { |
194
|
|
|
return [ |
195
|
|
|
'action=protect&title=Main%20Page&token=123ABC&' . |
196
|
|
|
'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never' |
197
|
|
|
=> 'apihelp-protect-example-protect', |
198
|
|
|
'action=protect&title=Main%20Page&token=123ABC&' . |
199
|
|
|
'protections=edit=all|move=all&reason=Lifting%20restrictions' |
200
|
|
|
=> 'apihelp-protect-example-unprotect', |
201
|
|
|
'action=protect&title=Main%20Page&token=123ABC&' . |
202
|
|
|
'protections=&reason=Lifting%20restrictions' |
203
|
|
|
=> 'apihelp-protect-example-unprotect2', |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getHelpUrls() { |
208
|
|
|
return 'https://www.mediawiki.org/wiki/API:Protect'; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|