b2f5e2a6dd643269589e977ffada05c5eab2c930
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
25 * @todo document, briefly.
27 class ProtectionForm
{
28 var $mRestrictions = array();
30 var $mCascade = false;
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
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
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;
81 if( $wgRequest->wasPosted() ) {
84 $wgOut->redirect( $this->mTitle
->getFullUrl() );
91 function show( $err = null ) {
94 $wgOut->setRobotpolicy( 'noindex,nofollow' );
96 if( is_null( $this->mTitle
) ||
97 !$this->mTitle
->exists() ||
98 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
99 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
103 list( $cascadeSources, $restrictions ) = $this->mTitle
->getCascadeProtectionSources();
106 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
107 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
110 if ( $cascadeSources && count($cascadeSources) > 0 ) {
113 foreach ( $cascadeSources as $title ) {
114 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
117 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
119 $wgOut->addWikiText( $notice );
122 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
123 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
126 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
127 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
129 $wgOut->addHTML( $this->buildForm() );
131 $this->showLogExtract( $wgOut );
135 global $wgRequest, $wgUser, $wgOut;
137 if( $this->disabled
) {
142 $token = $wgRequest->getVal( 'wpEditToken' );
143 if( !$wgUser->matchEditToken( $token ) ) {
144 $this->show( wfMsg( 'sessionfailure' ) );
148 if ( strlen( $this->mExpiry
) == 0 ) {
149 $this->mExpiry
= 'infinite';
152 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
153 $expiry = Block
::infinity();
155 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
156 $expiry = strtotime( $this->mExpiry
);
158 if ( $expiry < 0 ||
$expiry === false ) {
159 $this->show( wfMsg( 'protect_expiry_invalid' ) );
163 $expiry = wfTimestamp( TS_MW
, $expiry );
165 if ( $expiry < wfTimestampNow() ) {
166 $this->show( wfMsg( 'protect_expiry_old' ) );
172 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
174 throw new FatalError( "Unknown error at restriction save time." );
179 function buildForm() {
183 if( !$this->disabled
) {
184 $out .= $this->buildScript();
185 // The submission needs to reenable the move permission selector
186 // if it's in locked mode, or some browsers won't submit the data.
187 $out .= wfOpenElement( 'form', array(
188 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
190 'onsubmit' => 'protectEnable(true)' ) );
192 $out .= wfElement( 'input', array(
194 'name' => 'wpEditToken',
195 'value' => $wgUser->editToken() ) );
198 $out .= "<table id='mwProtectSet'>";
201 foreach( $this->mRestrictions
as $action => $required ) {
202 /* Not all languages have V_x <-> N_x relation */
203 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
207 foreach( $this->mRestrictions
as $action => $selected ) {
209 $out .= $this->buildSelector( $action, $selected );
214 // JavaScript will add another row with a value-chaining checkbox
216 $out .= "</tbody>\n";
217 $out .= "</table>\n";
219 global $wgEnableCascadingProtection;
221 if ($wgEnableCascadingProtection)
222 $out .= $this->buildCascadeInput();
227 $out .= $this->buildExpiryInput();
229 if( !$this->disabled
) {
230 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
231 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
234 $out .= "</tbody>\n";
235 $out .= "</table>\n";
237 if ( !$this->disabled
) {
239 $out .= $this->buildCleanupScript();
245 function buildSelector( $action, $selected ) {
246 global $wgRestrictionLevels;
247 $id = 'mwProtect-level-' . $action;
251 'size' => count( $wgRestrictionLevels ),
252 'onchange' => 'protectLevelsUpdate(this)',
253 ) +
$this->disabledAttrib
;
255 $out = wfOpenElement( 'select', $attribs );
256 foreach( $wgRestrictionLevels as $key ) {
257 $out .= $this->buildOption( $key, $selected );
259 $out .= "</select>\n";
263 function buildOption( $key, $selected ) {
264 $text = ( $key == '' )
265 ?
wfMsg( 'protect-default' )
266 : wfMsg( "protect-level-$key" );
267 $selectedAttrib = ($selected == $key)
268 ?
array( 'selected' => 'selected' )
270 return wfElement( 'option',
271 array( 'value' => $key ) +
$selectedAttrib,
275 function buildReasonInput() {
276 $id = 'mwProtect-reason';
277 return wfElement( 'label', array(
280 wfMsg( 'protectcomment' ) ) .
282 wfElement( 'input', array(
286 'value' => $this->mReason
) );
289 function buildCascadeInput() {
290 $id = 'mwProtect-cascade';
291 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
295 function buildExpiryInput() {
296 $id = 'mwProtect-expiry';
298 $ci = "<tr> <td align=\"right\">";
299 $ci .= wfElement( 'label', array (
302 wfMsg( 'protectexpiry' ) );
303 $ci .= "</td> <td align=\"left\">";
304 $ci .= wfElement( 'input', array(
308 'value' => $this->mExpiry
) +
$this->disabledAttrib
);
314 function buildSubmit() {
315 return wfElement( 'input', array(
317 'value' => wfMsg( 'confirm' ) ) );
320 function buildScript() {
321 global $wgStylePath, $wgStyleVersion;
322 return '<script type="text/javascript" src="' .
323 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
327 function buildCleanupScript() {
328 global $wgRestrictionLevels, $wgGroupPermissions;
329 $script = 'var wgCascadeableLevels=';
330 $CascadeableLevels = array();
331 foreach( $wgRestrictionLevels as $key ) {
332 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
333 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
336 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
337 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
338 return '<script type="text/javascript">' . $script . '</script>';
342 * @param OutputPage $out
345 function showLogExtract( &$out ) {
346 # Show relevant lines from the deletion log:
347 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
348 $logViewer = new LogViewer(
351 array( 'page' => $this->mTitle
->getPrefixedText(),
352 'type' => 'protect' ) ) ) );
353 $logViewer->showList( $out );