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 // FIXME: this format is not user friendly
58 $this->mExpiry
= wfTimestamp( TS_ISO_8601
, $this->mTitle
->mRestrictionsExpiry
);
62 // The form will be available in read-only to show levels.
63 $this->disabled
= wfReadOnly() ||
($this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser)) != array();
64 $this->disabledAttrib
= $this->disabled
65 ?
array( 'disabled' => 'disabled' )
68 if( $wgRequest->wasPosted() ) {
69 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
70 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
71 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
73 foreach( $this->mApplicableTypes
as $action ) {
74 $val = $wgRequest->getVal( "mwProtect-level-$action" );
75 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
76 $this->mRestrictions
[$action] = $val;
83 global $wgRequest, $wgOut;
84 if( $wgRequest->wasPosted() ) {
86 $article = new Article( $this->mTitle
);
87 $q = $article->isRedirect() ?
'redirect=no' : '';
88 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
95 function show( $err = null ) {
96 global $wgOut, $wgUser;
98 $wgOut->setRobotpolicy( 'noindex,nofollow' );
100 if( is_null( $this->mTitle
) ||
101 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
102 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
106 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
109 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
110 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
113 if ( $cascadeSources && count($cascadeSources) > 0 ) {
116 foreach ( $cascadeSources as $title ) {
117 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
120 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
123 $sk = $wgUser->getSkin();
124 $titleLink = $sk->makeLinkObj( $this->mTitle
);
125 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
126 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
128 # Show an appropriate message if the user isn't allowed or able to change
129 # the protection settings at this time
130 if( $this->disabled
) {
132 $wgOut->readOnlyPage();
133 } elseif( $this->mPermErrors
) {
134 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
137 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
140 $wgOut->addHTML( $this->buildForm() );
142 $this->showLogExtract( $wgOut );
146 global $wgRequest, $wgUser, $wgOut;
148 if( $this->disabled
) {
153 $token = $wgRequest->getVal( 'wpEditToken' );
154 if( !$wgUser->matchEditToken( $token ) ) {
155 $this->show( wfMsg( 'sessionfailure' ) );
159 if ( strlen( $this->mExpiry
) == 0 ) {
160 $this->mExpiry
= 'infinite';
163 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
164 $expiry = Block
::infinity();
166 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
167 $expiry = strtotime( $this->mExpiry
);
169 if ( $expiry < 0 ||
$expiry === false ) {
170 $this->show( wfMsg( 'protect_expiry_invalid' ) );
174 // Fixme: non-qualified absolute times are not in users specified timezone
175 // and there isn't notice about it in the ui
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();
215 * Build the input form
217 * @return $out string HTML form
219 function buildForm() {
223 if( !$this->disabled
) {
224 $out .= $this->buildScript();
225 // The submission needs to reenable the move permission selector
226 // if it's in locked mode, or some browsers won't submit the data.
227 $out .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
228 Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
231 $out .= Xml
::openElement( 'fieldset' ) .
232 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
233 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
234 Xml
::openElement( 'tbody' ) .
237 foreach( $this->mRestrictions
as $action => $required ) {
238 /* Not all languages have V_x <-> N_x relation */
239 $label = Xml
::element( 'label',
240 array( 'for' => "mwProtect-level-$action" ),
241 wfMsg( 'restriction-' . $action ) );
242 $out .= "<th>$label</th>";
246 foreach( $this->mRestrictions
as $action => $selected ) {
248 $this->buildSelector( $action, $selected ) .
253 // JavaScript will add another row with a value-chaining checkbox
255 $out .= Xml
::closeElement( 'tbody' ) .
256 Xml
::closeElement( 'table' ) .
257 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
258 Xml
::openElement( 'tbody' );
260 global $wgEnableCascadingProtection;
261 if( $wgEnableCascadingProtection && $this->mTitle
->exists() ) {
264 <td class="mw-input">' .
265 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade
, $this->disabledAttrib
) .
270 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
272 <td class='mw-label'>" .
273 Xml
::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
275 <td class="mw-input">' .
276 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
280 if( !$this->disabled
) {
281 $id = 'mwProtect-reason';
283 <td class='mw-label'>" .
284 Xml
::label( wfMsg( 'protectcomment' ), $id ) .
286 <td class="mw-input">' .
287 Xml
::input( $id, 60, $this->mReason
, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
292 <td class='mw-input'>" .
293 Xml
::checkLabel( wfMsg( 'watchthis' ),
294 'mwProtectWatch', 'mwProtectWatch',
295 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
300 <td class='mw-submit'>" .
301 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
306 $out .= Xml
::closeElement( 'tbody' ) .
307 Xml
::closeElement( 'table' ) .
308 Xml
::closeElement( 'fieldset' );
310 if ( !$this->disabled
) {
311 $out .= Xml
::closeElement( 'form' ) .
312 $this->buildCleanupScript();
318 function buildSelector( $action, $selected ) {
319 global $wgRestrictionLevels;
320 $id = 'mwProtect-level-' . $action;
324 'size' => count( $wgRestrictionLevels ),
325 'onchange' => 'protectLevelsUpdate(this)',
326 ) +
$this->disabledAttrib
;
328 $out = Xml
::openElement( 'select', $attribs );
329 foreach( $wgRestrictionLevels as $key ) {
330 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
332 $out .= Xml
::closeElement( 'select' );
337 * Prepare the label for a protection selector option
339 * @param string $permission Permission required
342 private function getOptionLabel( $permission ) {
343 if( $permission == '' ) {
344 return wfMsg( 'protect-default' );
346 $key = "protect-level-{$permission}";
347 $msg = wfMsg( $key );
348 if( wfEmptyMsg( $key, $msg ) )
349 $msg = wfMsg( 'protect-fallback', $permission );
354 function buildScript() {
355 global $wgStylePath, $wgStyleVersion;
356 return Xml
::tags( 'script', array(
357 'type' => 'text/javascript',
358 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
361 function buildCleanupScript() {
362 global $wgRestrictionLevels, $wgGroupPermissions;
363 $script = 'var wgCascadeableLevels=';
364 $CascadeableLevels = array();
365 foreach( $wgRestrictionLevels as $key ) {
366 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
367 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
370 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
371 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
372 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
376 * @param OutputPage $out
379 function showLogExtract( &$out ) {
380 # Show relevant lines from the protection log:
381 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
382 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );