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();
32 var $mApplicableTypes = array();
34 function __construct( &$article ) {
35 global $wgRequest, $wgUser;
36 global $wgRestrictionTypes, $wgRestrictionLevels;
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
42 $this->mTitle
->loadRestrictions();
44 foreach( $this->mApplicableTypes
as $action ) {
45 // Fixme: this form currently requires individual selections,
46 // but the db allows multiples separated by commas.
47 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
50 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
52 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
53 $this->mExpiry
= 'infinite';
54 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
57 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
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' )
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' );
72 foreach( $this->mApplicableTypes
as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 $this->mRestrictions
[$action] = $val;
82 global $wgRequest, $wgOut;
83 if( $wgRequest->wasPosted() ) {
85 $article = new Article( $this->mTitle
);
86 $q = $article->isRedirect() ?
'redirect=no' : '';
87 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
94 function show( $err = null ) {
95 global $wgOut, $wgUser;
97 $wgOut->setRobotpolicy( 'noindex,nofollow' );
99 if( is_null( $this->mTitle
) ||
100 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
101 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
105 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
108 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
109 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
112 if ( $cascadeSources && count($cascadeSources) > 0 ) {
115 foreach ( $cascadeSources as $title ) {
116 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
119 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
121 $wgOut->addWikiText( $notice );
124 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
125 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
127 # Show an appropriate message if the user isn't allowed or able to change
128 # the protection settings at this time
129 if( $this->disabled
) {
131 $wgOut->readOnlyPage();
134 if( $this->mPermErrors
) {
135 $message = $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
);
138 $message = wfMsg( 'protect-text', wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) );
140 $wgOut->addWikiText( $message );
142 $wgOut->addHTML( $this->buildForm() );
144 $this->showLogExtract( $wgOut );
148 global $wgRequest, $wgUser, $wgOut;
150 if( $this->disabled
) {
155 $token = $wgRequest->getVal( 'wpEditToken' );
156 if( !$wgUser->matchEditToken( $token ) ) {
157 $this->show( wfMsg( 'sessionfailure' ) );
161 if ( strlen( $this->mExpiry
) == 0 ) {
162 $this->mExpiry
= 'infinite';
165 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
166 $expiry = Block
::infinity();
168 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
169 $expiry = strtotime( $this->mExpiry
);
171 if ( $expiry < 0 ||
$expiry === false ) {
172 $this->show( wfMsg( 'protect_expiry_invalid' ) );
176 $expiry = wfTimestamp( TS_MW
, $expiry );
178 if ( $expiry < wfTimestampNow() ) {
179 $this->show( wfMsg( 'protect_expiry_old' ) );
185 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
186 # to a semi-protected page.
187 global $wgGroupPermissions;
189 $edit_restriction = $this->mRestrictions
['edit'];
191 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
192 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
193 $this->mCascade
= false;
195 if ($this->mTitle
->exists()) {
196 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
198 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $this->mReason
, $expiry );
202 throw new FatalError( "Unknown error at restriction save time." );
205 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
206 $this->mArticle
->doWatch();
207 } elseif( $this->mTitle
->userIsWatching() ) {
208 $this->mArticle
->doUnwatch();
214 function buildForm() {
218 if( !$this->disabled
) {
219 $out .= $this->buildScript();
220 // The submission needs to reenable the move permission selector
221 // if it's in locked mode, or some browsers won't submit the data.
222 $out .= wfOpenElement( 'form', array(
223 'id' => 'mw-Protect-Form',
224 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
226 'onsubmit' => 'protectEnable(true)' ) );
228 $out .= wfElement( 'input', array(
230 'name' => 'wpEditToken',
231 'value' => $wgUser->editToken() ) );
234 $out .= "<table id='mwProtectSet'>";
238 foreach( $this->mRestrictions
as $action => $required ) {
239 /* Not all languages have V_x <-> N_x relation */
240 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
244 foreach( $this->mRestrictions
as $action => $selected ) {
246 $out .= $this->buildSelector( $action, $selected );
251 // JavaScript will add another row with a value-chaining checkbox
253 $out .= "</tbody>\n";
254 $out .= "</table>\n";
259 global $wgEnableCascadingProtection;
260 if( $wgEnableCascadingProtection && $this->mTitle
->exists() )
261 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
263 $out .= $this->buildExpiryInput();
265 if( !$this->disabled
) {
266 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
267 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
268 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
271 $out .= "</tbody>\n";
272 $out .= "</table>\n";
274 if ( !$this->disabled
) {
276 $out .= $this->buildCleanupScript();
282 function buildSelector( $action, $selected ) {
283 global $wgRestrictionLevels;
284 $id = 'mwProtect-level-' . $action;
288 'size' => count( $wgRestrictionLevels ),
289 'onchange' => 'protectLevelsUpdate(this)',
290 ) +
$this->disabledAttrib
;
292 $out = wfOpenElement( 'select', $attribs );
293 foreach( $wgRestrictionLevels as $key ) {
294 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
296 $out .= "</select>\n";
301 * Prepare the label for a protection selector option
303 * @param string $permission Permission required
306 private function getOptionLabel( $permission ) {
307 if( $permission == '' ) {
308 return wfMsg( 'protect-default' );
310 $key = "protect-level-{$permission}";
311 $msg = wfMsg( $key );
312 if( wfEmptyMsg( $key, $msg ) )
313 $msg = wfMsg( 'protect-fallback', $permission );
318 function buildReasonInput() {
319 $id = 'mwProtect-reason';
320 return wfElement( 'label', array(
323 wfMsg( 'protectcomment' ) ) .
325 wfElement( 'input', array(
330 'value' => $this->mReason
) );
333 function buildCascadeInput() {
334 $id = 'mwProtect-cascade';
335 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
339 function buildExpiryInput() {
340 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
342 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
343 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
345 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
346 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
350 function buildWatchInput() {
352 return Xml
::checkLabel(
353 wfMsg( 'watchthis' ),
356 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
360 function buildSubmit() {
361 return wfElement( 'input', array(
362 'id' => 'mw-Protect-submit',
364 'value' => wfMsg( 'confirm' ) ) );
367 function buildScript() {
368 global $wgStylePath, $wgStyleVersion;
369 return '<script type="text/javascript" src="' .
370 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
374 function buildCleanupScript() {
375 global $wgRestrictionLevels, $wgGroupPermissions;
376 $script = 'var wgCascadeableLevels=';
377 $CascadeableLevels = array();
378 foreach( $wgRestrictionLevels as $key ) {
379 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
380 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
383 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
384 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
385 return '<script type="text/javascript">' . $script . '</script>';
389 * @param OutputPage $out
392 function showLogExtract( &$out ) {
393 # Show relevant lines from the protection log:
394 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
395 $logViewer = new LogViewer(
398 array( 'page' => $this->mTitle
->getPrefixedText(),
399 'type' => 'protect' ) ) ) );
400 $logViewer->showList( $out );