Adding messages for new global group 'sysadmin'. Brion, Tim, Kate and JeLuF are all...
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 */
21
22 /**
23 * @todo document, briefly.
24 */
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29 var $mExpiry = null;
30 var $mPermErrors = array();
31 var $mApplicableTypes = array();
32
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
39
40 if( $this->mTitle ) {
41 $this->mTitle->loadRestrictions();
42
43 foreach( $this->mApplicableTypes as $action ) {
44 // Fixme: this form currently requires individual selections,
45 // but the db allows multiples separated by commas.
46 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
47 }
48
49 $this->mCascade = $this->mTitle->areRestrictionsCascading();
50
51 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
52 $this->mExpiry = 'infinite';
53 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
54 $this->mExpiry = '';
55 } else {
56 // FIXME: this format is not user friendly
57 $this->mExpiry = wfTimestamp( TS_ISO_8601, $this->mTitle->mRestrictionsExpiry );
58 }
59 }
60
61 // The form will be available in read-only to show levels.
62 $this->disabled = wfReadOnly() || ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib = $this->disabled
64 ? array( 'disabled' => 'disabled' )
65 : array();
66
67 if( $wgRequest->wasPosted() ) {
68 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
69 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
70 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
71
72 foreach( $this->mApplicableTypes as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 //prevent users from setting levels that they cannot later unset
76 if( $val == 'sysop' ) {
77 //special case, rewrite sysop to either protect and editprotected
78 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
79 continue;
80 } else {
81 if( !$wgUser->isAllowed($val) )
82 continue;
83 }
84 $this->mRestrictions[$action] = $val;
85 }
86 }
87 }
88 }
89
90 function execute() {
91 global $wgRequest, $wgOut;
92 if( $wgRequest->wasPosted() ) {
93 if( $this->save() ) {
94 $article = new Article( $this->mTitle );
95 $q = $article->isRedirect() ? 'redirect=no' : '';
96 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
97 }
98 } else {
99 $this->show();
100 }
101 }
102
103 function show( $err = null ) {
104 global $wgOut, $wgUser;
105
106 $wgOut->setRobotPolicy( 'noindex,nofollow' );
107
108 if( is_null( $this->mTitle ) ||
109 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
110 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
111 return;
112 }
113
114 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
115
116 if ( "" != $err ) {
117 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
118 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
119 }
120
121 if ( $cascadeSources && count($cascadeSources) > 0 ) {
122 $titles = '';
123
124 foreach ( $cascadeSources as $title ) {
125 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
126 }
127
128 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
129 }
130
131 $sk = $wgUser->getSkin();
132 $titleLink = $sk->makeLinkObj( $this->mTitle );
133 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
134 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
135
136 # Show an appropriate message if the user isn't allowed or able to change
137 # the protection settings at this time
138 if( $this->disabled ) {
139 if( wfReadOnly() ) {
140 $wgOut->readOnlyPage();
141 } elseif( $this->mPermErrors ) {
142 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
143 }
144 } else {
145 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
146 }
147
148 $wgOut->addHTML( $this->buildForm() );
149
150 $this->showLogExtract( $wgOut );
151 }
152
153 function save() {
154 global $wgRequest, $wgUser, $wgOut;
155
156 if( $this->disabled ) {
157 $this->show();
158 return false;
159 }
160
161 $token = $wgRequest->getVal( 'wpEditToken' );
162 if( !$wgUser->matchEditToken( $token ) ) {
163 $this->show( wfMsg( 'sessionfailure' ) );
164 return false;
165 }
166
167 if ( strlen( $this->mExpiry ) == 0 ) {
168 $this->mExpiry = 'infinite';
169 }
170
171 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
172 $expiry = Block::infinity();
173 } else {
174 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
175 $expiry = strtotime( $this->mExpiry );
176
177 if ( $expiry < 0 || $expiry === false ) {
178 $this->show( wfMsg( 'protect_expiry_invalid' ) );
179 return false;
180 }
181
182 // Fixme: non-qualified absolute times are not in users specified timezone
183 // and there isn't notice about it in the ui
184 $expiry = wfTimestamp( TS_MW, $expiry );
185
186 if ( $expiry < wfTimestampNow() ) {
187 $this->show( wfMsg( 'protect_expiry_old' ) );
188 return false;
189 }
190
191 }
192
193 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
194 # to a semi-protected page.
195 global $wgGroupPermissions;
196
197 $edit_restriction = $this->mRestrictions['edit'];
198
199 if ($this->mCascade && ($edit_restriction != 'protect') &&
200 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
201 $this->mCascade = false;
202
203 if ($this->mTitle->exists()) {
204 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
205 } else {
206 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $this->mReason, $expiry );
207 }
208
209 if( !$ok ) {
210 throw new FatalError( "Unknown error at restriction save time." );
211 }
212
213 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
214 $this->mArticle->doWatch();
215 } elseif( $this->mTitle->userIsWatching() ) {
216 $this->mArticle->doUnwatch();
217 }
218
219 return $ok;
220 }
221
222 /**
223 * Build the input form
224 *
225 * @return $out string HTML form
226 */
227 function buildForm() {
228 global $wgUser;
229
230 $out = '';
231 if( !$this->disabled ) {
232 $out .= $this->buildScript();
233 // The submission needs to reenable the move permission selector
234 // if it's in locked mode, or some browsers won't submit the data.
235 $out .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
236 Xml::hidden( 'wpEditToken',$wgUser->editToken() );
237 }
238
239 $out .= Xml::openElement( 'fieldset' ) .
240 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
241 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
242 Xml::openElement( 'tbody' ) .
243 "<tr>\n";
244
245 foreach( $this->mRestrictions as $action => $required ) {
246 /* Not all languages have V_x <-> N_x relation */
247 $label = Xml::element( 'label',
248 array( 'for' => "mwProtect-level-$action" ),
249 wfMsg( 'restriction-' . $action ) );
250 $out .= "<th>$label</th>";
251 }
252 $out .= "</tr>
253 <tr>\n";
254 foreach( $this->mRestrictions as $action => $selected ) {
255 $out .= "<td>" .
256 $this->buildSelector( $action, $selected ) .
257 "</td>";
258 }
259 $out .= "</tr>\n";
260
261 // JavaScript will add another row with a value-chaining checkbox
262
263 $out .= Xml::closeElement( 'tbody' ) .
264 Xml::closeElement( 'table' ) .
265 Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
266 Xml::openElement( 'tbody' );
267
268 if( $this->mTitle->exists() ) {
269 $out .= '<tr>
270 <td></td>
271 <td class="mw-input">' .
272 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade, $this->disabledAttrib ) .
273 "</td>
274 </tr>\n";
275 }
276
277 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
278 $out .= "<tr>
279 <td class='mw-label'>" .
280 Xml::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
281 '</td>
282 <td class="mw-input">' .
283 Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) .
284 '</td>
285 </tr>';
286
287 if( !$this->disabled ) {
288 $id = 'mwProtect-reason';
289 $out .= "<tr>
290 <td class='mw-label'>" .
291 Xml::label( wfMsg( 'protectcomment' ), $id ) .
292 '</td>
293 <td class="mw-input">' .
294 Xml::input( $id, 60, $this->mReason, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
295 "</td>
296 </tr>
297 <tr>
298 <td></td>
299 <td class='mw-input'>" .
300 Xml::checkLabel( wfMsg( 'watchthis' ),
301 'mwProtectWatch', 'mwProtectWatch',
302 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
303 "</td>
304 </tr>
305 <tr>
306 <td></td>
307 <td class='mw-submit'>" .
308 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
309 "</td>
310 </tr>\n";
311 }
312
313 $out .= Xml::closeElement( 'tbody' ) .
314 Xml::closeElement( 'table' ) .
315 Xml::closeElement( 'fieldset' );
316
317 if ( !$this->disabled ) {
318 $out .= Xml::closeElement( 'form' ) .
319 $this->buildCleanupScript();
320 }
321
322 return $out;
323 }
324
325 function buildSelector( $action, $selected ) {
326 global $wgRestrictionLevels, $wgUser;
327 $id = 'mwProtect-level-' . $action;
328 $attribs = array(
329 'id' => $id,
330 'name' => $id,
331 'size' => count( $wgRestrictionLevels ),
332 'onchange' => 'protectLevelsUpdate(this)',
333 ) + $this->disabledAttrib;
334
335 $out = Xml::openElement( 'select', $attribs );
336 foreach( $wgRestrictionLevels as $key ) {
337 //don't let them choose levels above their own (aka so they can still unprotect and edit the page). but only when the form isn't disabled
338 if( $key == 'sysop' ) {
339 //special case, rewrite sysop to protect and editprotected
340 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
341 continue;
342 } else {
343 if( !$wgUser->isAllowed($key) && !$this->disabled )
344 continue;
345 }
346 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
347 }
348 $out .= Xml::closeElement( 'select' );
349 return $out;
350 }
351
352 /**
353 * Prepare the label for a protection selector option
354 *
355 * @param string $permission Permission required
356 * @return string
357 */
358 private function getOptionLabel( $permission ) {
359 if( $permission == '' ) {
360 return wfMsg( 'protect-default' );
361 } else {
362 $key = "protect-level-{$permission}";
363 $msg = wfMsg( $key );
364 if( wfEmptyMsg( $key, $msg ) )
365 $msg = wfMsg( 'protect-fallback', $permission );
366 return $msg;
367 }
368 }
369
370 function buildScript() {
371 global $wgStylePath, $wgStyleVersion;
372 return Xml::tags( 'script', array(
373 'type' => 'text/javascript',
374 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
375 }
376
377 function buildCleanupScript() {
378 global $wgRestrictionLevels, $wgGroupPermissions;
379 $script = 'var wgCascadeableLevels=';
380 $CascadeableLevels = array();
381 foreach( $wgRestrictionLevels as $key ) {
382 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
383 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
384 }
385 }
386 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
387 $script .= 'protectInitialize("mwProtectSet","' . Xml::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes) . '")';
388 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
389 }
390
391 /**
392 * @param OutputPage $out
393 * @access private
394 */
395 function showLogExtract( &$out ) {
396 # Show relevant lines from the protection log:
397 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
398 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
399 }
400 }