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.
25 class ProtectionForm
{
26 var $mRestrictions = array();
28 var $mCascade = false;
30 var $mPermErrors = array();
31 var $mApplicableTypes = array();
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
38 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
41 $this->mTitle
->loadRestrictions();
43 foreach( $this->mApplicableTypes
as $action ) {
44 // Fixme: this form currently requires individual selections,
45 // but the db allows multiples separated by commas.
46 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
49 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
51 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
52 $this->mExpiry
= 'infinite';
53 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
56 // FIXME: this format is not user friendly
57 $this->mExpiry
= wfTimestamp( TS_ISO_8601
, $this->mTitle
->mRestrictionsExpiry
);
61 // The form will be available in read-only to show levels.
62 $this->disabled
= wfReadOnly() ||
($this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib
= $this->disabled
64 ?
array( 'disabled' => 'disabled' )
67 if( $wgRequest->wasPosted() ) {
68 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
69 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
70 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
72 foreach( $this->mApplicableTypes
as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 $this->mRestrictions
[$action] = $val;
82 global $wgRequest, $wgOut;
83 if( $wgRequest->wasPosted() ) {
85 $article = new Article( $this->mTitle
);
86 $q = $article->isRedirect() ?
'redirect=no' : '';
87 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
94 function show( $err = null ) {
95 global $wgOut, $wgUser;
97 $wgOut->setRobotpolicy( 'noindex,nofollow' );
99 if( is_null( $this->mTitle
) ||
100 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
101 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
105 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
108 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
109 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
112 if ( $cascadeSources && count($cascadeSources) > 0 ) {
115 foreach ( $cascadeSources as $title ) {
116 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
119 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
122 $sk = $wgUser->getSkin();
123 $titleLink = $sk->makeLinkObj( $this->mTitle
);
124 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
125 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
127 # Show an appropriate message if the user isn't allowed or able to change
128 # the protection settings at this time
129 if( $this->disabled
) {
131 $wgOut->readOnlyPage();
132 } elseif( $this->mPermErrors
) {
133 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
136 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
139 $wgOut->addHTML( $this->buildForm() );
141 $this->showLogExtract( $wgOut );
145 global $wgRequest, $wgUser, $wgOut;
147 if( $this->disabled
) {
152 $token = $wgRequest->getVal( 'wpEditToken' );
153 if( !$wgUser->matchEditToken( $token ) ) {
154 $this->show( wfMsg( 'sessionfailure' ) );
158 if ( strlen( $this->mExpiry
) == 0 ) {
159 $this->mExpiry
= 'infinite';
162 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
163 $expiry = Block
::infinity();
165 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
166 $expiry = strtotime( $this->mExpiry
);
168 if ( $expiry < 0 ||
$expiry === false ) {
169 $this->show( wfMsg( 'protect_expiry_invalid' ) );
173 // Fixme: non-qualified absolute times are not in users specified timezone
174 // and there isn't notice about it in the ui
175 $expiry = wfTimestamp( TS_MW
, $expiry );
177 if ( $expiry < wfTimestampNow() ) {
178 $this->show( wfMsg( 'protect_expiry_old' ) );
184 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
185 # to a semi-protected page.
186 global $wgGroupPermissions;
188 $edit_restriction = $this->mRestrictions
['edit'];
190 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
191 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
192 $this->mCascade
= false;
194 if ($this->mTitle
->exists()) {
195 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
197 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $this->mReason
, $expiry );
201 throw new FatalError( "Unknown error at restriction save time." );
204 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
205 $this->mArticle
->doWatch();
206 } elseif( $this->mTitle
->userIsWatching() ) {
207 $this->mArticle
->doUnwatch();
214 * Build the input form
216 * @return $out string HTML form
218 function buildForm() {
222 if( !$this->disabled
) {
223 $out .= $this->buildScript();
224 // The submission needs to reenable the move permission selector
225 // if it's in locked mode, or some browsers won't submit the data.
226 $out .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
227 Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
230 $out .= Xml
::openElement( 'fieldset' ) .
231 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
232 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
233 Xml
::openElement( 'tbody' ) .
236 foreach( $this->mRestrictions
as $action => $required ) {
237 /* Not all languages have V_x <-> N_x relation */
238 $label = Xml
::element( 'label',
239 array( 'for' => "mwProtect-level-$action" ),
240 wfMsg( 'restriction-' . $action ) );
241 $out .= "<th>$label</th>";
245 foreach( $this->mRestrictions
as $action => $selected ) {
247 $this->buildSelector( $action, $selected ) .
252 // JavaScript will add another row with a value-chaining checkbox
254 $out .= Xml
::closeElement( 'tbody' ) .
255 Xml
::closeElement( 'table' ) .
256 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
257 Xml
::openElement( 'tbody' );
259 global $wgEnableCascadingProtection;
260 if( $wgEnableCascadingProtection && $this->mTitle
->exists() ) {
263 <td class="mw-input">' .
264 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade
, $this->disabledAttrib
) .
269 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
271 <td class='mw-label'>" .
272 Xml
::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
274 <td class="mw-input">' .
275 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
279 if( !$this->disabled
) {
280 $id = 'mwProtect-reason';
282 <td class='mw-label'>" .
283 Xml
::label( wfMsg( 'protectcomment' ), $id ) .
285 <td class="mw-input">' .
286 Xml
::input( $id, 60, $this->mReason
, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
291 <td class='mw-input'>" .
292 Xml
::checkLabel( wfMsg( 'watchthis' ),
293 'mwProtectWatch', 'mwProtectWatch',
294 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
299 <td class='mw-submit'>" .
300 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
305 $out .= Xml
::closeElement( 'tbody' ) .
306 Xml
::closeElement( 'table' ) .
307 Xml
::closeElement( 'fieldset' );
309 if ( !$this->disabled
) {
310 $out .= Xml
::closeElement( 'form' ) .
311 $this->buildCleanupScript();
317 function buildSelector( $action, $selected ) {
318 global $wgRestrictionLevels;
319 $id = 'mwProtect-level-' . $action;
323 'size' => count( $wgRestrictionLevels ),
324 'onchange' => 'protectLevelsUpdate(this)',
325 ) +
$this->disabledAttrib
;
327 $out = Xml
::openElement( 'select', $attribs );
328 foreach( $wgRestrictionLevels as $key ) {
329 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
331 $out .= Xml
::closeElement( 'select' );
336 * Prepare the label for a protection selector option
338 * @param string $permission Permission required
341 private function getOptionLabel( $permission ) {
342 if( $permission == '' ) {
343 return wfMsg( 'protect-default' );
345 $key = "protect-level-{$permission}";
346 $msg = wfMsg( $key );
347 if( wfEmptyMsg( $key, $msg ) )
348 $msg = wfMsg( 'protect-fallback', $permission );
353 function buildScript() {
354 global $wgStylePath, $wgStyleVersion;
355 return Xml
::tags( 'script', array(
356 'type' => 'text/javascript',
357 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
360 function buildCleanupScript() {
361 global $wgRestrictionLevels, $wgGroupPermissions;
362 $script = 'var wgCascadeableLevels=';
363 $CascadeableLevels = array();
364 foreach( $wgRestrictionLevels as $key ) {
365 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
366 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
369 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
370 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
371 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
375 * @param OutputPage $out
378 function showLogExtract( &$out ) {
379 # Show relevant lines from the protection log:
380 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
381 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );