* (bug 12148) Text highlight wasn't applied to cleanly deleted and added
[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 * @addtogroup SpecialPage
25 */
26 class ProtectionForm {
27 var $mRestrictions = array();
28 var $mReason = '';
29 var $mCascade = false;
30 var $mExpiry = null;
31 var $mPermErrors = array();
32
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38
39 if( $this->mTitle ) {
40 $this->mTitle->loadRestrictions();
41
42 foreach( $wgRestrictionTypes as $action ) {
43 // Fixme: this form currently requires individual selections,
44 // but the db allows multiples separated by commas.
45 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
46 }
47
48 $this->mCascade = $this->mTitle->areRestrictionsCascading();
49
50 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
51 $this->mExpiry = 'infinite';
52 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
53 $this->mExpiry = '';
54 } else {
55 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
56 }
57 }
58
59 // The form will be available in read-only to show levels.
60 $this->disabled = ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
61 $this->disabledAttrib = $this->disabled
62 ? array( 'disabled' => 'disabled' )
63 : array();
64
65 if( $wgRequest->wasPosted() ) {
66 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
67 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
68 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
69
70 foreach( $wgRestrictionTypes as $action ) {
71 $val = $wgRequest->getVal( "mwProtect-level-$action" );
72 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
73 $this->mRestrictions[$action] = $val;
74 }
75 }
76 }
77 }
78
79 function execute() {
80 global $wgRequest, $wgOut;
81 if( $wgRequest->wasPosted() ) {
82 if( $this->save() ) {
83 $article = new Article( $this->mTitle );
84 $q = $article->isRedirect() ? 'redirect=no' : '';
85 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
86 }
87 } else {
88 $this->show();
89 }
90 }
91
92 function show( $err = null ) {
93 global $wgOut, $wgUser;
94
95 $wgOut->setRobotpolicy( 'noindex,nofollow' );
96
97 if( is_null( $this->mTitle ) ||
98 !$this->mTitle->exists() ||
99 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
100 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
101 return;
102 }
103
104 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
105
106 if ( "" != $err ) {
107 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
108 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
109 }
110
111 if ( $cascadeSources && count($cascadeSources) > 0 ) {
112 $titles = '';
113
114 foreach ( $cascadeSources as $title ) {
115 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
116 }
117
118 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
119
120 $wgOut->addWikiText( $notice );
121 }
122
123 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
124 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
125
126 # Show an appropriate message if the user isn't allowed or able to change
127 # the protection settings at this time
128 if( $this->disabled ) {
129 $message = $wgOut->formatPermissionsErrorMessage( $this->mPermErrors );
130 } else {
131 $message = wfMsg( 'protect-text', wfEscapeWikiText( $this->mTitle->getPrefixedText() ) );
132 }
133 $wgOut->addWikiText( $message );
134
135 $wgOut->addHTML( $this->buildForm() );
136
137 $this->showLogExtract( $wgOut );
138 }
139
140 function save() {
141 global $wgRequest, $wgUser, $wgOut;
142
143 if( $this->disabled ) {
144 $this->show();
145 return false;
146 }
147
148 $token = $wgRequest->getVal( 'wpEditToken' );
149 if( !$wgUser->matchEditToken( $token ) ) {
150 $this->show( wfMsg( 'sessionfailure' ) );
151 return false;
152 }
153
154 if ( strlen( $this->mExpiry ) == 0 ) {
155 $this->mExpiry = 'infinite';
156 }
157
158 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
159 $expiry = Block::infinity();
160 } else {
161 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
162 $expiry = strtotime( $this->mExpiry );
163
164 if ( $expiry < 0 || $expiry === false ) {
165 $this->show( wfMsg( 'protect_expiry_invalid' ) );
166 return false;
167 }
168
169 $expiry = wfTimestamp( TS_MW, $expiry );
170
171 if ( $expiry < wfTimestampNow() ) {
172 $this->show( wfMsg( 'protect_expiry_old' ) );
173 return false;
174 }
175
176 }
177
178 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
179 # to a semi-protected page.
180 global $wgGroupPermissions;
181
182 $edit_restriction = $this->mRestrictions['edit'];
183
184 if ($this->mCascade && ($edit_restriction != 'protect') &&
185 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
186 $this->mCascade = false;
187
188 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
189 if( !$ok ) {
190 throw new FatalError( "Unknown error at restriction save time." );
191 }
192
193 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
194 $this->mArticle->doWatch();
195 } elseif( $this->mTitle->userIsWatching() ) {
196 $this->mArticle->doUnwatch();
197 }
198
199 return $ok;
200 }
201
202 function buildForm() {
203 global $wgUser;
204
205 $out = '';
206 if( !$this->disabled ) {
207 $out .= $this->buildScript();
208 // The submission needs to reenable the move permission selector
209 // if it's in locked mode, or some browsers won't submit the data.
210 $out .= wfOpenElement( 'form', array(
211 'id' => 'mw-Protect-Form',
212 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
213 'method' => 'post',
214 'onsubmit' => 'protectEnable(true)' ) );
215
216 $out .= wfElement( 'input', array(
217 'type' => 'hidden',
218 'name' => 'wpEditToken',
219 'value' => $wgUser->editToken() ) );
220 }
221
222 $out .= "<table id='mwProtectSet'>";
223 $out .= "<tbody>";
224 $out .= "<tr>\n";
225 foreach( $this->mRestrictions as $action => $required ) {
226 /* Not all languages have V_x <-> N_x relation */
227 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
228 }
229 $out .= "</tr>\n";
230 $out .= "<tr>\n";
231 foreach( $this->mRestrictions as $action => $selected ) {
232 $out .= "<td>\n";
233 $out .= $this->buildSelector( $action, $selected );
234 $out .= "</td>\n";
235 }
236 $out .= "</tr>\n";
237
238 // JavaScript will add another row with a value-chaining checkbox
239
240 $out .= "</tbody>\n";
241 $out .= "</table>\n";
242
243 $out .= "<table>\n";
244 $out .= "<tbody>\n";
245
246 global $wgEnableCascadingProtection;
247 if( $wgEnableCascadingProtection )
248 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
249
250 $out .= $this->buildExpiryInput();
251
252 if( !$this->disabled ) {
253 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
254 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
255 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
256 }
257
258 $out .= "</tbody>\n";
259 $out .= "</table>\n";
260
261 if ( !$this->disabled ) {
262 $out .= "</form>\n";
263 $out .= $this->buildCleanupScript();
264 }
265
266 return $out;
267 }
268
269 function buildSelector( $action, $selected ) {
270 global $wgRestrictionLevels;
271 $id = 'mwProtect-level-' . $action;
272 $attribs = array(
273 'id' => $id,
274 'name' => $id,
275 'size' => count( $wgRestrictionLevels ),
276 'onchange' => 'protectLevelsUpdate(this)',
277 ) + $this->disabledAttrib;
278
279 $out = wfOpenElement( 'select', $attribs );
280 foreach( $wgRestrictionLevels as $key ) {
281 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
282 }
283 $out .= "</select>\n";
284 return $out;
285 }
286
287 /**
288 * Prepare the label for a protection selector option
289 *
290 * @param string $permission Permission required
291 * @return string
292 */
293 private function getOptionLabel( $permission ) {
294 if( $permission == '' ) {
295 return wfMsg( 'protect-default' );
296 } else {
297 $key = "protect-level-{$permission}";
298 $msg = wfMsg( $key );
299 if( wfEmptyMsg( $key, $msg ) )
300 $msg = wfMsg( 'protect-fallback', $permission );
301 return $msg;
302 }
303 }
304
305 function buildReasonInput() {
306 $id = 'mwProtect-reason';
307 return wfElement( 'label', array(
308 'id' => "$id-label",
309 'for' => $id ),
310 wfMsg( 'protectcomment' ) ) .
311 '</td><td>' .
312 wfElement( 'input', array(
313 'size' => 60,
314 'maxlength' => 255,
315 'name' => $id,
316 'id' => $id,
317 'value' => $this->mReason ) );
318 }
319
320 function buildCascadeInput() {
321 $id = 'mwProtect-cascade';
322 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
323 return $ci;
324 }
325
326 function buildExpiryInput() {
327 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
328 return '<tr>'
329 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
330 . '<td>' . Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) . '</td>'
331 . '</tr>';
332 }
333
334 function buildWatchInput() {
335 global $wgUser;
336 return Xml::checkLabel(
337 wfMsg( 'watchthis' ),
338 'mwProtectWatch',
339 'mwProtectWatch',
340 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' )
341 );
342 }
343
344 function buildSubmit() {
345 return wfElement( 'input', array(
346 'id' => 'mw-Protect-submit',
347 'type' => 'submit',
348 'value' => wfMsg( 'confirm' ) ) );
349 }
350
351 function buildScript() {
352 global $wgStylePath, $wgStyleVersion;
353 return '<script type="text/javascript" src="' .
354 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
355 '"></script>';
356 }
357
358 function buildCleanupScript() {
359 global $wgRestrictionLevels, $wgGroupPermissions;
360 $script = 'var wgCascadeableLevels=';
361 $CascadeableLevels = array();
362 foreach( $wgRestrictionLevels as $key ) {
363 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
364 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
365 }
366 }
367 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
368 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
369 return '<script type="text/javascript">' . $script . '</script>';
370 }
371
372 /**
373 * @param OutputPage $out
374 * @access private
375 */
376 function showLogExtract( &$out ) {
377 # Show relevant lines from the protection log:
378 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
379 $logViewer = new LogViewer(
380 new LogReader(
381 new FauxRequest(
382 array( 'page' => $this->mTitle->getPrefixedText(),
383 'type' => 'protect' ) ) ) );
384 $logViewer->showList( $out );
385 }
386
387 }