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;
32 function __construct( &$article ) {
33 global $wgRequest, $wgUser;
34 global $wgRestrictionTypes, $wgRestrictionLevels;
35 $this->mArticle
=& $article;
36 $this->mTitle
=& $article->mTitle
;
39 $this->mTitle
->loadRestrictions();
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 ) );
47 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
49 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
50 $this->mExpiry
= 'infinite';
51 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
54 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
58 // The form will be available in read-only to show levels.
59 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
60 $this->disabledAttrib
= $this->disabled
61 ?
array( 'disabled' => 'disabled' )
64 if( $wgRequest->wasPosted() ) {
65 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
66 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
67 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
69 foreach( $wgRestrictionTypes as $action ) {
70 $val = $wgRequest->getVal( "mwProtect-level-$action" );
71 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
72 $this->mRestrictions
[$action] = $val;
80 if( $wgRequest->wasPosted() ) {
83 $wgOut->redirect( $this->mTitle
->getFullUrl() );
90 function show( $err = null ) {
91 global $wgOut, $wgUser;
93 $wgOut->setRobotpolicy( 'noindex,nofollow' );
95 if( is_null( $this->mTitle
) ||
96 !$this->mTitle
->exists() ||
97 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
98 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
102 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
105 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
106 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
109 if ( $cascadeSources && count($cascadeSources) > 0 ) {
112 foreach ( $cascadeSources as $title ) {
113 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
116 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
118 $wgOut->addWikiText( $notice );
121 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
122 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
124 # Show an appropriate message if the user isn't allowed or able to change
125 # the protection settings at this time
126 if( $this->disabled
) {
127 if( $wgUser->isAllowed( 'protect' ) ) {
128 if( $wgUser->isBlocked() ) {
130 $message = 'protect-locked-blocked';
133 $message = 'protect-locked-dblock';
137 $message = 'protect-locked-access';
140 $message = 'protect-text';
142 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
144 $wgOut->addHTML( $this->buildForm() );
146 $this->showLogExtract( $wgOut );
150 global $wgRequest, $wgUser, $wgOut;
152 if( $this->disabled
) {
157 $token = $wgRequest->getVal( 'wpEditToken' );
158 if( !$wgUser->matchEditToken( $token ) ) {
159 $this->show( wfMsg( 'sessionfailure' ) );
163 if ( strlen( $this->mExpiry
) == 0 ) {
164 $this->mExpiry
= 'infinite';
167 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
168 $expiry = Block
::infinity();
170 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
171 $expiry = strtotime( $this->mExpiry
);
173 if ( $expiry < 0 ||
$expiry === false ) {
174 $this->show( wfMsg( 'protect_expiry_invalid' ) );
178 $expiry = wfTimestamp( TS_MW
, $expiry );
180 if ( $expiry < wfTimestampNow() ) {
181 $this->show( wfMsg( 'protect_expiry_old' ) );
187 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
189 throw new FatalError( "Unknown error at restriction save time." );
192 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
193 $this->mArticle
->doWatch();
194 } elseif( $this->mTitle
->userIsWatching() ) {
195 $this->mArticle
->doUnwatch();
201 function buildForm() {
205 if( !$this->disabled
) {
206 $out .= $this->buildScript();
207 // The submission needs to reenable the move permission selector
208 // if it's in locked mode, or some browsers won't submit the data.
209 $out .= wfOpenElement( 'form', array(
210 'id' => 'mw-Protect-Form',
211 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
213 'onsubmit' => 'protectEnable(true)' ) );
215 $out .= wfElement( 'input', array(
217 'name' => 'wpEditToken',
218 'value' => $wgUser->editToken() ) );
221 $out .= "<table id='mwProtectSet'>";
224 foreach( $this->mRestrictions
as $action => $required ) {
225 /* Not all languages have V_x <-> N_x relation */
226 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
230 foreach( $this->mRestrictions
as $action => $selected ) {
232 $out .= $this->buildSelector( $action, $selected );
237 // JavaScript will add another row with a value-chaining checkbox
239 $out .= "</tbody>\n";
240 $out .= "</table>\n";
245 global $wgEnableCascadingProtection;
246 if( $wgEnableCascadingProtection )
247 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
249 if( !$this->disabled
)
250 $out .= '<tr><td></td><td>' . $this->buildWatchInput() . "</td></tr>\n";
252 $out .= $this->buildExpiryInput();
254 if( !$this->disabled
) {
255 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
256 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
259 $out .= "</tbody>\n";
260 $out .= "</table>\n";
262 if ( !$this->disabled
) {
264 $out .= $this->buildCleanupScript();
270 function buildSelector( $action, $selected ) {
271 global $wgRestrictionLevels;
272 $id = 'mwProtect-level-' . $action;
276 'size' => count( $wgRestrictionLevels ),
277 'onchange' => 'protectLevelsUpdate(this)',
278 ) +
$this->disabledAttrib
;
280 $out = wfOpenElement( 'select', $attribs );
281 foreach( $wgRestrictionLevels as $key ) {
282 $out .= $this->buildOption( $key, $selected );
284 $out .= "</select>\n";
288 function buildOption( $key, $selected ) {
289 $text = ( $key == '' )
290 ?
wfMsg( 'protect-default' )
291 : wfMsg( "protect-level-$key" );
292 $selectedAttrib = ($selected == $key)
293 ?
array( 'selected' => 'selected' )
295 return wfElement( 'option',
296 array( 'value' => $key ) +
$selectedAttrib,
300 function buildReasonInput() {
301 $id = 'mwProtect-reason';
302 return wfElement( 'label', array(
305 wfMsg( 'protectcomment' ) ) .
307 wfElement( 'input', array(
311 'value' => $this->mReason
) );
314 function buildCascadeInput() {
315 $id = 'mwProtect-cascade';
316 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
320 function buildExpiryInput() {
321 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
323 . '<td><label for="expires">' . wfMsgWithLinks( 'protectexpiry' ) . '</label></td>'
324 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
328 function buildWatchInput() {
330 return Xml
::checkLabel(
331 wfMsg( 'watchthis' ),
334 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
338 function buildSubmit() {
339 return wfElement( 'input', array(
340 'id' => 'mw-Protect-submit',
342 'value' => wfMsg( 'confirm' ) ) );
345 function buildScript() {
346 global $wgStylePath, $wgStyleVersion;
347 return '<script type="text/javascript" src="' .
348 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
352 function buildCleanupScript() {
353 global $wgRestrictionLevels, $wgGroupPermissions;
354 $script = 'var wgCascadeableLevels=';
355 $CascadeableLevels = array();
356 foreach( $wgRestrictionLevels as $key ) {
357 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
358 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
361 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
362 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
363 return '<script type="text/javascript">' . $script . '</script>';
367 * @param OutputPage $out
370 function showLogExtract( &$out ) {
371 # Show relevant lines from the protection log:
372 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
373 $logViewer = new LogViewer(
376 array( 'page' => $this->mTitle
->getPrefixedText(),
377 'type' => 'protect' ) ) ) );
378 $logViewer->showList( $out );