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 ) );
46 $this->mRestrictions
['robots'] = implode( ',', $this->mTitle
->getRestrictions( 'robots' ) );
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;
77 // Read checkboxex only if user is allowed to change robots policy, otherwise keep previous policy
78 if ( $wgUser->isAllowed( 'editrobots' ) ) {
79 $robotspolicy = $wgRequest->getBool( 'mwProtect-robots-noindex' ) ?
'noindex' : 'index';
80 $robotspolicy .= $wgRequest->getBool( 'mwProtect-robots-nofollow' ) ?
',nofollow' : ',follow';
81 // 'index,follow' is default, no need to set this explicitly at this point; is done at Article::View
82 $this->mRestrictions
['robots'] = ( $robotspolicy == 'index,follow' ) ?
'' : $robotspolicy;
89 if( $wgRequest->wasPosted() ) {
92 $wgOut->redirect( $this->mTitle
->getFullUrl() );
99 function show( $err = null ) {
100 global $wgOut, $wgUser;
102 $wgOut->setRobotpolicy( 'noindex,nofollow' );
104 if( is_null( $this->mTitle
) ||
105 !$this->mTitle
->exists() ||
106 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
107 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
111 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
114 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
115 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
118 if ( $cascadeSources && count($cascadeSources) > 0 ) {
121 foreach ( $cascadeSources as $title ) {
122 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
125 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
127 $wgOut->addWikiText( $notice );
130 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
131 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
133 # Show an appropriate message if the user isn't allowed or able to change
134 # the protection settings at this time
135 if( $this->disabled
) {
136 if( $wgUser->isAllowed( 'protect' ) ) {
137 if( $wgUser->isBlocked() ) {
139 $message = 'protect-locked-blocked';
142 $message = 'protect-locked-dblock';
146 $message = 'protect-locked-access';
149 $message = 'protect-text';
151 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
153 $wgOut->addHTML( $this->buildForm() );
155 $this->showLogExtract( $wgOut );
159 global $wgRequest, $wgUser, $wgOut;
161 if( $this->disabled
) {
166 $token = $wgRequest->getVal( 'wpEditToken' );
167 if( !$wgUser->matchEditToken( $token ) ) {
168 $this->show( wfMsg( 'sessionfailure' ) );
172 if ( strlen( $this->mExpiry
) == 0 ) {
173 $this->mExpiry
= 'infinite';
176 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
177 $expiry = Block
::infinity();
179 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
180 $expiry = strtotime( $this->mExpiry
);
182 if ( $expiry < 0 ||
$expiry === false ) {
183 $this->show( wfMsg( 'protect_expiry_invalid' ) );
187 $expiry = wfTimestamp( TS_MW
, $expiry );
189 if ( $expiry < wfTimestampNow() ) {
190 $this->show( wfMsg( 'protect_expiry_old' ) );
196 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
198 throw new FatalError( "Unknown error at restriction save time." );
201 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
202 $this->mArticle
->doWatch();
203 } elseif( $this->mTitle
->userIsWatching() ) {
204 $this->mArticle
->doUnwatch();
210 function buildForm() {
211 global $wgUser, $wgRestrictionTypes;
214 if( !$this->disabled
) {
215 $out .= $this->buildScript();
216 // The submission needs to reenable the move permission selector
217 // if it's in locked mode, or some browsers won't submit the data.
218 $out .= wfOpenElement( 'form', array(
219 'id' => 'mw-Protect-Form',
220 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
222 'onsubmit' => 'protectEnable(true)' ) );
224 $out .= wfElement( 'input', array(
226 'name' => 'wpEditToken',
227 'value' => $wgUser->editToken() ) );
230 $out .= "<table id='mwProtectSet'>";
233 foreach( $this->mRestrictions
as $action => $required ) {
234 /* Not all languages have V_x <-> N_x relation */
235 if ( in_array( $action, $wgRestrictionTypes ) )
236 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
240 foreach( $this->mRestrictions
as $action => $selected ) {
241 if ( in_array( $action, $wgRestrictionTypes ) ) {
243 $out .= $this->buildSelector( $action, $selected );
249 // JavaScript will add another row with a value-chaining checkbox
251 $out .= "</tbody>\n";
252 $out .= "</table>\n";
257 global $wgEnableCascadingProtection;
258 if( $wgEnableCascadingProtection )
259 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
261 if( !$this->disabled
)
262 $out .= '<tr><td></td><td>' . $this->buildWatchInput() . "</td></tr>\n";
264 $out .= $this->buildRobotsInput();
265 $out .= $this->buildExpiryInput();
267 if( !$this->disabled
) {
268 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
269 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
272 $out .= "</tbody>\n";
273 $out .= "</table>\n";
275 if ( !$this->disabled
) {
277 $out .= $this->buildCleanupScript();
283 function buildSelector( $action, $selected ) {
284 global $wgRestrictionLevels;
285 $id = 'mwProtect-level-' . $action;
289 'size' => count( $wgRestrictionLevels ),
290 'onchange' => 'protectLevelsUpdate(this)',
291 ) +
$this->disabledAttrib
;
293 $out = wfOpenElement( 'select', $attribs );
294 foreach( $wgRestrictionLevels as $key ) {
295 $out .= $this->buildOption( $key, $selected );
297 $out .= "</select>\n";
301 function buildOption( $key, $selected ) {
302 $text = ( $key == '' )
303 ?
wfMsg( 'protect-default' )
304 : wfMsg( "protect-level-$key" );
305 $selectedAttrib = ($selected == $key)
306 ?
array( 'selected' => 'selected' )
308 return wfElement( 'option',
309 array( 'value' => $key ) +
$selectedAttrib,
313 function buildReasonInput() {
314 $id = 'mwProtect-reason';
315 return wfElement( 'label', array(
318 wfMsg( 'protectcomment' ) ) .
320 wfElement( 'input', array(
324 'value' => $this->mReason
) );
327 function buildCascadeInput() {
328 $id = 'mwProtect-cascade';
329 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
333 function buildRobotsInput() {
335 $robotsallowed = $wgUser->isAllowed( 'editrobots' ) ?
array() : array( 'disabled' => 'disabled' );
336 $noindexset = ( isset( $this->mRestrictions
['robots'] ) && strstr( $this->mRestrictions
['robots'], 'noindex' ) ) ?
true : false;
337 $nofollowset = ( isset( $this->mRestrictions
['robots'] ) && strstr( $this->mRestrictions
['robots'], 'nofollow' ) ) ?
true : false;
338 $ret = "<tr><td align=\"right\">";
339 $ret .= Xml
::label( wfMsg( 'protect-robotspolicy' ), 'mwProtect-robots-label' );
340 $ret .= "</td> <td align=\"left\" width=\"60\">";
341 $ret .= Xml
::checkLabel( 'noindex', 'mwProtect-robots-noindex', 'mwProtect-robots-noindex', $noindexset, $robotsallowed );
342 $ret .= Xml
::checkLabel( 'nofollow', 'mwProtect-robots-nofollow', 'mwProtect-robots-nofollow', $nofollowset, $robotsallowed );
343 $ret .= "</td></tr>";
347 function buildExpiryInput() {
348 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
350 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
351 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
355 function buildWatchInput() {
357 return Xml
::checkLabel(
358 wfMsg( 'watchthis' ),
361 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
365 function buildSubmit() {
366 return wfElement( 'input', array(
367 'id' => 'mw-Protect-submit',
369 'value' => wfMsg( 'confirm' ) ) );
372 function buildScript() {
373 global $wgStylePath, $wgStyleVersion;
374 return '<script type="text/javascript" src="' .
375 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
379 function buildCleanupScript() {
380 global $wgRestrictionLevels, $wgGroupPermissions;
381 $script = 'var wgCascadeableLevels=';
382 $CascadeableLevels = array();
383 foreach( $wgRestrictionLevels as $key ) {
384 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
385 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
388 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
389 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
390 return '<script type="text/javascript">' . $script . '</script>';
394 * @param OutputPage $out
397 function showLogExtract( &$out ) {
398 # Show relevant lines from the protection log:
399 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
400 $logViewer = new LogViewer(
403 array( 'page' => $this->mTitle
->getPrefixedText(),
404 'type' => 'protect' ) ) ) );
405 $logViewer->showList( $out );