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
21 * @addtogroup SpecialPage
24 class ProtectionForm
{
25 var $mRestrictions = array();
27 var $mCascade = false;
30 function __construct( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle
=& $article;
34 $this->mTitle
=& $article->mTitle
;
37 $this->mTitle
->loadRestrictions();
39 foreach( $wgRestrictionTypes as $action ) {
40 // Fixme: this form currently requires individual selections,
41 // but the db allows multiples separated by commas.
42 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
45 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
47 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
48 $this->mExpiry
= 'infinite';
49 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
52 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
56 // The form will be available in read-only to show levels.
57 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
58 $this->disabledAttrib
= $this->disabled
59 ?
array( 'disabled' => 'disabled' )
62 if( $wgRequest->wasPosted() ) {
63 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
64 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
65 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
67 foreach( $wgRestrictionTypes as $action ) {
68 $val = $wgRequest->getVal( "mwProtect-level-$action" );
69 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
70 $this->mRestrictions
[$action] = $val;
78 if( $wgRequest->wasPosted() ) {
81 $wgOut->redirect( $this->mTitle
->getFullUrl() );
88 function show( $err = null ) {
91 $wgOut->setRobotpolicy( 'noindex,nofollow' );
93 if( is_null( $this->mTitle
) ||
94 !$this->mTitle
->exists() ||
95 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
96 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
100 list( $cascadeSources, $restrictions ) = $this->mTitle
->getCascadeProtectionSources();
103 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
104 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
107 if ( $cascadeSources && count($cascadeSources) > 0 ) {
110 foreach ( $cascadeSources as $title ) {
111 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
114 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
116 $wgOut->addWikiText( $notice );
119 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
120 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
123 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
124 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
126 $wgOut->addHTML( $this->buildForm() );
128 $this->showLogExtract( $wgOut );
132 global $wgRequest, $wgUser, $wgOut;
134 if( $this->disabled
) {
139 $token = $wgRequest->getVal( 'wpEditToken' );
140 if( !$wgUser->matchEditToken( $token ) ) {
141 $this->show( wfMsg( 'sessionfailure' ) );
145 if ( strlen( $this->mExpiry
) == 0 ) {
146 $this->mExpiry
= 'infinite';
149 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
150 $expiry = Block
::infinity();
152 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
153 $expiry = strtotime( $this->mExpiry
);
155 if ( $expiry < 0 ||
$expiry === false ) {
156 $this->show( wfMsg( 'protect_expiry_invalid' ) );
160 $expiry = wfTimestamp( TS_MW
, $expiry );
162 if ( $expiry < wfTimestampNow() ) {
163 $this->show( wfMsg( 'protect_expiry_old' ) );
169 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
171 throw new FatalError( "Unknown error at restriction save time." );
176 function buildForm() {
180 if( !$this->disabled
) {
181 $out .= $this->buildScript();
182 // The submission needs to reenable the move permission selector
183 // if it's in locked mode, or some browsers won't submit the data.
184 $out .= wfOpenElement( 'form', array(
185 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
187 'onsubmit' => 'protectEnable(true)' ) );
189 $out .= wfElement( 'input', array(
191 'name' => 'wpEditToken',
192 'value' => $wgUser->editToken() ) );
195 $out .= "<table id='mwProtectSet'>";
198 foreach( $this->mRestrictions
as $action => $required ) {
199 /* Not all languages have V_x <-> N_x relation */
200 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
204 foreach( $this->mRestrictions
as $action => $selected ) {
206 $out .= $this->buildSelector( $action, $selected );
211 // JavaScript will add another row with a value-chaining checkbox
213 $out .= "</tbody>\n";
214 $out .= "</table>\n";
216 global $wgEnableCascadingProtection;
218 if ($wgEnableCascadingProtection)
219 $out .= $this->buildCascadeInput();
224 $out .= $this->buildExpiryInput();
226 if( !$this->disabled
) {
227 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
228 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
231 $out .= "</tbody>\n";
232 $out .= "</table>\n";
234 if ( !$this->disabled
) {
236 $out .= $this->buildCleanupScript();
242 function buildSelector( $action, $selected ) {
243 global $wgRestrictionLevels;
244 $id = 'mwProtect-level-' . $action;
248 'size' => count( $wgRestrictionLevels ),
249 'onchange' => 'protectLevelsUpdate(this)',
250 ) +
$this->disabledAttrib
;
252 $out = wfOpenElement( 'select', $attribs );
253 foreach( $wgRestrictionLevels as $key ) {
254 $out .= $this->buildOption( $key, $selected );
256 $out .= "</select>\n";
260 function buildOption( $key, $selected ) {
261 $text = ( $key == '' )
262 ?
wfMsg( 'protect-default' )
263 : wfMsg( "protect-level-$key" );
264 $selectedAttrib = ($selected == $key)
265 ?
array( 'selected' => 'selected' )
267 return wfElement( 'option',
268 array( 'value' => $key ) +
$selectedAttrib,
272 function buildReasonInput() {
273 $id = 'mwProtect-reason';
274 return wfElement( 'label', array(
277 wfMsg( 'protectcomment' ) ) .
279 wfElement( 'input', array(
283 'value' => $this->mReason
) );
286 function buildCascadeInput() {
287 $id = 'mwProtect-cascade';
288 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
292 function buildExpiryInput() {
293 $id = 'mwProtect-expiry';
295 $ci = "<tr> <td align=\"right\">";
296 $ci .= wfElement( 'label', array (
299 wfMsg( 'protectexpiry' ) );
300 $ci .= "</td> <td align=\"left\">";
301 $ci .= wfElement( 'input', array(
305 'value' => $this->mExpiry
) +
$this->disabledAttrib
);
311 function buildSubmit() {
312 return wfElement( 'input', array(
314 'value' => wfMsg( 'confirm' ) ) );
317 function buildScript() {
318 global $wgStylePath, $wgStyleVersion;
319 return '<script type="text/javascript" src="' .
320 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
324 function buildCleanupScript() {
325 global $wgRestrictionLevels, $wgGroupPermissions;
326 $script = 'var wgCascadeableLevels=';
327 $CascadeableLevels = array();
328 foreach( $wgRestrictionLevels as $key ) {
329 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
330 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
333 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
334 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
335 return '<script type="text/javascript">' . $script . '</script>';
339 * @param OutputPage $out
342 function showLogExtract( &$out ) {
343 # Show relevant lines from the deletion log:
344 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
345 $logViewer = new LogViewer(
348 array( 'page' => $this->mTitle
->getPrefixedText(),
349 'type' => 'protect' ) ) ) );
350 $logViewer->showList( $out );