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