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