Note that restrictions are rights, not groups. So now, cascading protection will...
[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 $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
95 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
96 }
97 } else {
98 $this->show();
99 }
100 }
101
102 function show( $err = null ) {
103 global $wgOut, $wgUser;
104
105 $wgOut->setRobotpolicy( 'noindex,nofollow' );
106
107 if( is_null( $this->mTitle ) ||
108 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
109 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
110 return;
111 }
112
113 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
114
115 if ( "" != $err ) {
116 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
117 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
118 }
119
120 if ( $cascadeSources && count($cascadeSources) > 0 ) {
121 $titles = '';
122
123 foreach ( $cascadeSources as $title ) {
124 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
125 }
126
127 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
128 }
129
130 $sk = $wgUser->getSkin();
131 $titleLink = $sk->makeLinkObj( $this->mTitle );
132 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
133 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
134
135 # Show an appropriate message if the user isn't allowed or able to change
136 # the protection settings at this time
137 if( $this->disabled ) {
138 if( wfReadOnly() ) {
139 $wgOut->readOnlyPage();
140 } elseif( $this->mPermErrors ) {
141 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
142 }
143 } else {
144 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
145 }
146
147 $wgOut->addHTML( $this->buildForm() );
148
149 $this->showLogExtract( $wgOut );
150 }
151
152 function save() {
153 global $wgRequest, $wgUser, $wgOut;
154
155 if( $this->disabled ) {
156 $this->show();
157 return false;
158 }
159
160 $token = $wgRequest->getVal( 'wpEditToken' );
161 if( !$wgUser->matchEditToken( $token ) ) {
162 $this->show( wfMsg( 'sessionfailure' ) );
163 return false;
164 }
165
166 if ( strlen( $this->mExpiry ) == 0 ) {
167 $this->mExpiry = 'infinite';
168 }
169
170 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
171 $expiry = Block::infinity();
172 } else {
173 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
174 $expiry = strtotime( $this->mExpiry );
175
176 if ( $expiry < 0 || $expiry === false ) {
177 $this->show( wfMsg( 'protect_expiry_invalid' ) );
178 return false;
179 }
180
181 // Fixme: non-qualified absolute times are not in users specified timezone
182 // and there isn't notice about it in the ui
183 $expiry = wfTimestamp( TS_MW, $expiry );
184
185 if ( $expiry < wfTimestampNow() ) {
186 $this->show( wfMsg( 'protect_expiry_old' ) );
187 return false;
188 }
189
190 }
191
192 # NOTE : verification of cascading protection in semi-protection mode
193 # is in Article::updateRestriction()
194
195 if( $this->mTitle->exists() ){
196 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
197 } else {
198 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $this->mReason, $expiry );
199 }
200
201 if( !$ok ) {
202 throw new FatalError( "Unknown error at restriction save time." );
203 }
204
205 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
206 $this->mArticle->doWatch();
207 } elseif( $this->mTitle->userIsWatching() ) {
208 $this->mArticle->doUnwatch();
209 }
210
211 return $ok;
212 }
213
214 /**
215 * Build the input form
216 *
217 * @return $out string HTML form
218 */
219 function buildForm() {
220 global $wgUser;
221
222 $out = '';
223 if( !$this->disabled ) {
224 $out .= $this->buildScript();
225 // The submission needs to reenable the move permission selector
226 // if it's in locked mode, or some browsers won't submit the data.
227 $out .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
228 Xml::hidden( 'wpEditToken',$wgUser->editToken() );
229 }
230
231 $out .= Xml::openElement( 'fieldset' ) .
232 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
233 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
234 Xml::openElement( 'tbody' ) .
235 "<tr>\n";
236
237 foreach( $this->mRestrictions as $action => $required ) {
238 /* Not all languages have V_x <-> N_x relation */
239 $label = Xml::element( 'label',
240 array( 'for' => "mwProtect-level-$action" ),
241 wfMsg( 'restriction-' . $action ) );
242 $out .= "<th>$label</th>";
243 }
244 $out .= "</tr>
245 <tr>\n";
246 foreach( $this->mRestrictions as $action => $selected ) {
247 $out .= "<td>" .
248 $this->buildSelector( $action, $selected ) .
249 "</td>";
250 }
251 $out .= "</tr>\n";
252
253 // JavaScript will add another row with a value-chaining checkbox
254
255 $out .= Xml::closeElement( 'tbody' ) .
256 Xml::closeElement( 'table' ) .
257 Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
258 Xml::openElement( 'tbody' );
259
260 if( $this->mTitle->exists() ) {
261 $out .= '<tr>
262 <td></td>
263 <td class="mw-input">' .
264 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade, $this->disabledAttrib ) .
265 "</td>
266 </tr>\n";
267 }
268
269 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
270 $out .= "<tr>
271 <td class='mw-label'>" .
272 Xml::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
273 '</td>
274 <td class="mw-input">' .
275 Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) .
276 '</td>
277 </tr>';
278
279 if( !$this->disabled ) {
280 $id = 'mwProtect-reason';
281 $out .= "<tr>
282 <td class='mw-label'>" .
283 Xml::label( wfMsg( 'protectcomment' ), $id ) .
284 '</td>
285 <td class="mw-input">' .
286 Xml::input( $id, 60, $this->mReason, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
287 "</td>
288 </tr>
289 <tr>
290 <td></td>
291 <td class='mw-input'>" .
292 Xml::checkLabel( wfMsg( 'watchthis' ),
293 'mwProtectWatch', 'mwProtectWatch',
294 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
295 "</td>
296 </tr>
297 <tr>
298 <td></td>
299 <td class='mw-submit'>" .
300 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
301 "</td>
302 </tr>\n";
303 }
304
305 $out .= Xml::closeElement( 'tbody' ) .
306 Xml::closeElement( 'table' ) .
307 Xml::closeElement( 'fieldset' );
308
309 if ( !$this->disabled ) {
310 $out .= Xml::closeElement( 'form' ) .
311 $this->buildCleanupScript();
312 }
313
314 return $out;
315 }
316
317 function buildSelector( $action, $selected ) {
318 global $wgRestrictionLevels, $wgUser;
319 $id = 'mwProtect-level-' . $action;
320 $attribs = array(
321 'id' => $id,
322 'name' => $id,
323 'size' => count( $wgRestrictionLevels ),
324 'onchange' => 'protectLevelsUpdate(this)',
325 ) + $this->disabledAttrib;
326
327 $out = Xml::openElement( 'select', $attribs );
328 foreach( $wgRestrictionLevels as $key ) {
329 //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
330 if( $key == 'sysop' ) {
331 //special case, rewrite sysop to protect and editprotected
332 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && $this->disabled )
333 continue;
334 } else {
335 if( !$wgUser->isAllowed($key) && !$this->disabled )
336 continue;
337 }
338 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
339 }
340 $out .= Xml::closeElement( 'select' );
341 return $out;
342 }
343
344 /**
345 * Prepare the label for a protection selector option
346 *
347 * @param string $permission Permission required
348 * @return string
349 */
350 private function getOptionLabel( $permission ) {
351 if( $permission == '' ) {
352 return wfMsg( 'protect-default' );
353 } else {
354 $key = "protect-level-{$permission}";
355 $msg = wfMsg( $key );
356 if( wfEmptyMsg( $key, $msg ) )
357 $msg = wfMsg( 'protect-fallback', $permission );
358 return $msg;
359 }
360 }
361
362 function buildScript() {
363 global $wgStylePath, $wgStyleVersion;
364 return Xml::tags( 'script', array(
365 'type' => 'text/javascript',
366 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
367 }
368
369 function buildCleanupScript() {
370 global $wgRestrictionLevels, $wgGroupPermissions;
371 $script = 'var wgCascadeableLevels=';
372 $CascadeableLevels = array();
373 foreach( $wgRestrictionLevels as $key ) {
374 $canCascade = true;
375 $check = $key == 'sysop' ? 'protect' : $key;
376 foreach( $wgGroupPermissions as $group => $rights ){
377 if( isset( $rights[$check] ) && $rights[$check] && !( isset( $rights['protect'] ) && $rights['protect'] ) ){
378 $canCascade = false;
379 break;
380 }
381 }
382 if( $key != '' && $canCascade ) {
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 }