3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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.
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.
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
23 * @todo document, briefly.
24 * @addtogroup SpecialPage
26 class ProtectionForm
{
27 var $mRestrictions = array();
29 var $mCascade = false;
31 var $mPermErrors = array();
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
40 $this->mTitle
->loadRestrictions();
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 ) );
48 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
50 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
51 $this->mExpiry
= 'infinite';
52 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
55 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
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' )
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' );
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;
80 global $wgRequest, $wgOut;
81 if( $wgRequest->wasPosted() ) {
83 $article = new Article( $this->mTitle
);
84 $q = $article->isRedirect() ?
'redirect=no' : '';
85 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
92 function show( $err = null ) {
93 global $wgOut, $wgUser;
95 $wgOut->setRobotpolicy( 'noindex,nofollow' );
97 if( is_null( $this->mTitle
) ||
98 !$this->mTitle
->exists() ||
99 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
100 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
104 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
107 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
108 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
111 if ( $cascadeSources && count($cascadeSources) > 0 ) {
114 foreach ( $cascadeSources as $title ) {
115 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
118 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
120 $wgOut->addWikiText( $notice );
123 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
124 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
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
);
131 $message = wfMsg( 'protect-text', wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) );
133 $wgOut->addWikiText( $message );
135 $wgOut->addHTML( $this->buildForm() );
137 $this->showLogExtract( $wgOut );
141 global $wgRequest, $wgUser, $wgOut;
143 if( $this->disabled
) {
148 $token = $wgRequest->getVal( 'wpEditToken' );
149 if( !$wgUser->matchEditToken( $token ) ) {
150 $this->show( wfMsg( 'sessionfailure' ) );
154 if ( strlen( $this->mExpiry
) == 0 ) {
155 $this->mExpiry
= 'infinite';
158 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
159 $expiry = Block
::infinity();
161 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
162 $expiry = strtotime( $this->mExpiry
);
164 if ( $expiry < 0 ||
$expiry === false ) {
165 $this->show( wfMsg( 'protect_expiry_invalid' ) );
169 $expiry = wfTimestamp( TS_MW
, $expiry );
171 if ( $expiry < wfTimestampNow() ) {
172 $this->show( wfMsg( 'protect_expiry_old' ) );
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;
182 $edit_restriction = $this->mRestrictions
['edit'];
184 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
185 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
186 $this->mCascade
= false;
188 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
190 throw new FatalError( "Unknown error at restriction save time." );
193 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
194 $this->mArticle
->doWatch();
195 } elseif( $this->mTitle
->userIsWatching() ) {
196 $this->mArticle
->doUnwatch();
202 function buildForm() {
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' ),
214 'onsubmit' => 'protectEnable(true)' ) );
216 $out .= wfElement( 'input', array(
218 'name' => 'wpEditToken',
219 'value' => $wgUser->editToken() ) );
222 $out .= "<table id='mwProtectSet'>";
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";
231 foreach( $this->mRestrictions
as $action => $selected ) {
233 $out .= $this->buildSelector( $action, $selected );
238 // JavaScript will add another row with a value-chaining checkbox
240 $out .= "</tbody>\n";
241 $out .= "</table>\n";
246 global $wgEnableCascadingProtection;
247 if( $wgEnableCascadingProtection )
248 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
250 $out .= $this->buildExpiryInput();
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";
258 $out .= "</tbody>\n";
259 $out .= "</table>\n";
261 if ( !$this->disabled
) {
263 $out .= $this->buildCleanupScript();
269 function buildSelector( $action, $selected ) {
270 global $wgRestrictionLevels;
271 $id = 'mwProtect-level-' . $action;
275 'size' => count( $wgRestrictionLevels ),
276 'onchange' => 'protectLevelsUpdate(this)',
277 ) +
$this->disabledAttrib
;
279 $out = wfOpenElement( 'select', $attribs );
280 foreach( $wgRestrictionLevels as $key ) {
281 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
283 $out .= "</select>\n";
288 * Prepare the label for a protection selector option
290 * @param string $permission Permission required
293 private function getOptionLabel( $permission ) {
294 if( $permission == '' ) {
295 return wfMsg( 'protect-default' );
297 $key = "protect-level-{$permission}";
298 $msg = wfMsg( $key );
299 if( wfEmptyMsg( $key, $msg ) )
300 $msg = wfMsg( 'protect-fallback', $permission );
305 function buildReasonInput() {
306 $id = 'mwProtect-reason';
307 return wfElement( 'label', array(
310 wfMsg( 'protectcomment' ) ) .
312 wfElement( 'input', array(
317 'value' => $this->mReason
) );
320 function buildCascadeInput() {
321 $id = 'mwProtect-cascade';
322 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
326 function buildExpiryInput() {
327 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
329 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
330 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
334 function buildWatchInput() {
336 return Xml
::checkLabel(
337 wfMsg( 'watchthis' ),
340 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
344 function buildSubmit() {
345 return wfElement( 'input', array(
346 'id' => 'mw-Protect-submit',
348 'value' => wfMsg( 'confirm' ) ) );
351 function buildScript() {
352 global $wgStylePath, $wgStyleVersion;
353 return '<script type="text/javascript" src="' .
354 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
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) . "'";
367 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
368 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
369 return '<script type="text/javascript">' . $script . '</script>';
373 * @param OutputPage $out
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(
382 array( 'page' => $this->mTitle
->getPrefixedText(),
383 'type' => 'protect' ) ) ) );
384 $logViewer->showList( $out );