00001 <?php
00025 class ProtectionForm {
00027 var $mRestrictions = array();
00028
00030 var $mReason = '';
00031
00033 var $mReasonSelection = '';
00034
00036 var $mCascade = false;
00037
00039 var $mExpiry = array();
00040
00045 var $mExpirySelection = array();
00046
00048 var $mPermErrors = array();
00049
00051 var $mApplicableTypes = array();
00052
00054 var $mExistingExpiry = array();
00055
00056 function __construct( Article $article ) {
00057 global $wgRequest, $wgUser;
00058 global $wgRestrictionTypes, $wgRestrictionLevels;
00059 $this->mArticle = $article;
00060 $this->mTitle = $article->mTitle;
00061 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
00062
00063 $this->mCascade = $this->mTitle->areRestrictionsCascading();
00064
00065
00066 $this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser);
00067 $this->disabled = wfReadOnly() || $this->mPermErrors != array();
00068 $this->disabledAttrib = $this->disabled
00069 ? array( 'disabled' => 'disabled' )
00070 : array();
00071
00072 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
00073 $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' );
00074 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
00075
00076 foreach( $this->mApplicableTypes as $action ) {
00077
00078
00079 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
00080
00081 if ( !$this->mRestrictions[$action] ) {
00082
00083 $existingExpiry = '';
00084 } else {
00085 $existingExpiry = $this->mTitle->getRestrictionExpiry( $action );
00086 }
00087 $this->mExistingExpiry[$action] = $existingExpiry;
00088
00089 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
00090 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
00091
00092 if ( $requestExpiry ) {
00093
00094 $this->mExpiry[$action] = $requestExpiry;
00095 $this->mExpirySelection[$action] = 'othertime';
00096 } elseif ( $requestExpirySelection ) {
00097
00098 $this->mExpiry[$action] = '';
00099 $this->mExpirySelection[$action] = $requestExpirySelection;
00100 } elseif ( $existingExpiry == 'infinity' ) {
00101
00102 $this->mExpiry[$action] = '';
00103 $this->mExpirySelection[$action] = 'infinite';
00104 } elseif ( $existingExpiry ) {
00105
00106 $this->mExpiry[$action] = '';
00107 $this->mExpirySelection[$action] = $existingExpiry;
00108 } else {
00109
00110 $this->mExpiry[$action] = '';
00111 $this->mExpirySelection[$action] = 'infinite';
00112 }
00113
00114 $val = $wgRequest->getVal( "mwProtect-level-$action" );
00115 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
00116
00117 if( $val == 'sysop' ) {
00118
00119 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
00120 continue;
00121 } else {
00122 if( !$wgUser->isAllowed($val) )
00123 continue;
00124 }
00125 $this->mRestrictions[$action] = $val;
00126 }
00127 }
00128 }
00129
00134 function getExpiry( $action ) {
00135 if ( $this->mExpirySelection[$action] == 'existing' ) {
00136 return $this->mExistingExpiry[$action];
00137 } elseif ( $this->mExpirySelection[$action] == 'othertime' ) {
00138 $value = $this->mExpiry[$action];
00139 } else {
00140 $value = $this->mExpirySelection[$action];
00141 }
00142 if ( $value == 'infinite' || $value == 'indefinite' || $value == 'infinity' ) {
00143 $time = Block::infinity();
00144 } else {
00145 $unix = strtotime( $value );
00146
00147 if ( !$unix || $unix === -1 ) {
00148 return false;
00149 }
00150
00151
00152
00153 $time = wfTimestamp( TS_MW, $unix );
00154 }
00155 return $time;
00156 }
00157
00158 function execute() {
00159 global $wgRequest, $wgOut;
00160 if( $wgRequest->wasPosted() ) {
00161 if( $this->save() ) {
00162 $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
00163 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
00164 }
00165 } else {
00166 $this->show();
00167 }
00168 }
00169
00170 function show( $err = null ) {
00171 global $wgOut, $wgUser;
00172
00173 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00174
00175 if( is_null( $this->mTitle ) ||
00176 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
00177 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
00178 return;
00179 }
00180
00181 list( $cascadeSources, ) = $this->mTitle->getCascadeProtectionSources();
00182
00183 if ( "" != $err ) {
00184 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
00185 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
00186 }
00187
00188 if ( $cascadeSources && count($cascadeSources) > 0 ) {
00189 $titles = '';
00190
00191 foreach ( $cascadeSources as $title ) {
00192 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
00193 }
00194
00195 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
00196 }
00197
00198 $sk = $wgUser->getSkin();
00199 $titleLink = $sk->makeLinkObj( $this->mTitle );
00200 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
00201 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
00202
00203 # Show an appropriate message if the user isn't allowed or able to change
00204 # the protection settings at this time
00205 if( $this->disabled ) {
00206 if( wfReadOnly() ) {
00207 $wgOut->readOnlyPage();
00208 } elseif( $this->mPermErrors ) {
00209 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
00210 }
00211 } else {
00212 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
00213 }
00214
00215 $wgOut->addHTML( $this->buildForm() );
00216
00217 $this->showLogExtract( $wgOut );
00218 }
00219
00220 function save() {
00221 global $wgRequest, $wgUser, $wgOut;
00222 # Permission check!
00223 if ( $this->disabled ) {
00224 $this->show();
00225 return false;
00226 }
00227
00228 $token = $wgRequest->getVal( 'wpEditToken' );
00229 if ( !$wgUser->matchEditToken( $token ) ) {
00230 $this->show( wfMsg( 'sessionfailure' ) );
00231 return false;
00232 }
00233
00234 # Create reason string. Use list and/or custom string.
00235 $reasonstr = $this->mReasonSelection;
00236 if ( $reasonstr != 'other' && $this->mReason != '' ) {
00237
00238 $reasonstr .= ': ' . $this->mReason;
00239 } elseif ( $reasonstr == 'other' ) {
00240 $reasonstr = $this->mReason;
00241 }
00242 $expiry = array();
00243 foreach( $this->mApplicableTypes as $action ) {
00244 $expiry[$action] = $this->getExpiry( $action );
00245 if ( !$expiry[$action] ) {
00246 $this->show( wfMsg( 'protect_expiry_invalid' ) );
00247 return false;
00248 }
00249 if ( $expiry[$action] < wfTimestampNow() ) {
00250 $this->show( wfMsg( 'protect_expiry_old' ) );
00251 return false;
00252 }
00253 }
00254
00255 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
00256 # to a semi-protected page.
00257 global $wgGroupPermissions;
00258
00259 $edit_restriction = $this->mRestrictions['edit'];
00260 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
00261 if ($this->mCascade && ($edit_restriction != 'protect') &&
00262 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
00263 $this->mCascade = false;
00264
00265 if ($this->mTitle->exists()) {
00266 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
00267 } else {
00268 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry['create'] );
00269 }
00270
00271 if( !$ok ) {
00272 throw new FatalError( "Unknown error at restriction save time." );
00273 }
00274
00275 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
00276 $this->mArticle->doWatch();
00277 } elseif( $this->mTitle->userIsWatching() ) {
00278 $this->mArticle->doUnwatch();
00279 }
00280 return $ok;
00281 }
00282
00288 function buildForm() {
00289 global $wgUser, $wgLang;
00290
00291 $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
00292 $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
00293
00294 $out = '';
00295 if( !$this->disabled ) {
00296 $out .= $this->buildScript();
00297 $out .= Xml::openElement( 'form', array( 'method' => 'post',
00298 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
00299 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
00300 $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
00301 }
00302
00303 $out .= Xml::openElement( 'fieldset' ) .
00304 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
00305 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
00306 Xml::openElement( 'tbody' );
00307
00308 foreach( $this->mRestrictions as $action => $selected ) {
00309
00310 $msg = wfMsg( 'restriction-' . $action );
00311 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
00312 $msg = $action;
00313 }
00314 $out .= "<tr><td>".
00315 Xml::openElement( 'fieldset' ) .
00316 Xml::element( 'legend', null, $msg ) .
00317 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
00318 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
00319
00320 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
00321 wfMsgForContent( 'protect-dropdown' ),
00322 wfMsgForContent( 'protect-otherreason-op' ),
00323 $this->mReasonSelection,
00324 'mwProtect-reason', 4 );
00325 $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
00326
00327 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
00328
00329 $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
00330 $mProtectother = Xml::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
00331
00332 $expiryFormOptions = '';
00333 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) {
00334 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action] );
00335 $d = $wgLang->date( $this->mExistingExpiry[$action] );
00336 $t = $wgLang->time( $this->mExistingExpiry[$action] );
00337 $expiryFormOptions .=
00338 Xml::option(
00339 wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
00340 'existing',
00341 $this->mExpirySelection[$action] == 'existing'
00342 ) . "\n";
00343 }
00344
00345 $expiryFormOptions .= Xml::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
00346 foreach( explode(',', $scExpiryOptions) as $option ) {
00347 if ( strpos($option, ":") === false ) {
00348 $show = $value = $option;
00349 } else {
00350 list($show, $value) = explode(":", $option);
00351 }
00352 $show = htmlspecialchars($show);
00353 $value = htmlspecialchars($value);
00354 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n";
00355 }
00356 # Add expiry dropdown
00357 if( $showProtectOptions && !$this->disabled ) {
00358 $out .= "
00359 <table><tr>
00360 <td class='mw-label'>
00361 {$mProtectexpiry}
00362 </td>
00363 <td class='mw-input'>" .
00364 Xml::tags( 'select',
00365 array(
00366 'id' => "mwProtectExpirySelection-$action",
00367 'name' => "wpProtectExpirySelection-$action",
00368 'onchange' => "ProtectionForm.updateExpiryList(this)",
00369 'tabindex' => '2' ) + $this->disabledAttrib,
00370 $expiryFormOptions ) .
00371 "</td>
00372 </tr></table>";
00373 }
00374 # Add custom expiry field
00375 $attribs = array( 'id' => "mwProtect-$action-expires", 'onkeyup' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib;
00376 $out .= "<table><tr>
00377 <td class='mw-label'>" .
00378 $mProtectother .
00379 '</td>
00380 <td class="mw-input">' .
00381 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
00382 '</td>
00383 </tr></table>';
00384 $out .= "</td></tr>" .
00385 Xml::closeElement( 'table' ) .
00386 Xml::closeElement( 'fieldset' ) .
00387 "</td></tr>";
00388 }
00389
00390 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00391
00392
00393 if( $this->mTitle->exists() ) {
00394 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
00395 Xml::openElement( 'tbody' );
00396 $out .= '<tr>
00397 <td></td>
00398 <td class="mw-input">' .
00399 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
00400 $this->mCascade, $this->disabledAttrib ) .
00401 "</td>
00402 </tr>\n";
00403 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00404 }
00405
00406 # Add manual and custom reason field/selects as well as submit
00407 if( !$this->disabled ) {
00408 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
00409 Xml::openElement( 'tbody' );
00410 $out .= "
00411 <tr>
00412 <td class='mw-label'>
00413 {$mProtectreasonother}
00414 </td>
00415 <td class='mw-input'>
00416 {$reasonDropDown}
00417 </td>
00418 </tr>
00419 <tr>
00420 <td class='mw-label'>
00421 {$mProtectreason}
00422 </td>
00423 <td class='mw-input'>" .
00424 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
00425 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
00426 "</td>
00427 </tr>
00428 <tr>
00429 <td></td>
00430 <td class='mw-input'>" .
00431 Xml::checkLabel( wfMsg( 'watchthis' ),
00432 'mwProtectWatch', 'mwProtectWatch',
00433 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
00434 "</td>
00435 </tr>
00436 <tr>
00437 <td></td>
00438 <td class='mw-submit'>" .
00439 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
00440 "</td>
00441 </tr>\n";
00442 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00443 }
00444 $out .= Xml::closeElement( 'fieldset' );
00445
00446 if ( $wgUser->isAllowed( 'editinterface' ) ) {
00447 $linkTitle = Title::makeTitleSafe( NS_MEDIAWIKI, 'protect-dropdown' );
00448 $link = $wgUser->getSkin()->Link ( $linkTitle, wfMsgHtml( 'protect-edit-reasonlist' ) );
00449 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
00450 }
00451
00452 if ( !$this->disabled ) {
00453 $out .= Xml::closeElement( 'form' ) .
00454 $this->buildCleanupScript();
00455 }
00456
00457 return $out;
00458 }
00459
00460 function buildSelector( $action, $selected ) {
00461 global $wgRestrictionLevels, $wgUser;
00462
00463 $levels = array();
00464 foreach( $wgRestrictionLevels as $key ) {
00465
00466 if( $key == 'sysop' ) {
00467
00468 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
00469 continue;
00470 } else {
00471 if( !$wgUser->isAllowed($key) && !$this->disabled )
00472 continue;
00473 }
00474 $levels[] = $key;
00475 }
00476
00477 $id = 'mwProtect-level-' . $action;
00478 $attribs = array(
00479 'id' => $id,
00480 'name' => $id,
00481 'size' => count( $levels ),
00482 'onchange' => 'ProtectionForm.updateLevels(this)',
00483 ) + $this->disabledAttrib;
00484
00485 $out = Xml::openElement( 'select', $attribs );
00486 foreach( $levels as $key ) {
00487 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
00488 }
00489 $out .= Xml::closeElement( 'select' );
00490 return $out;
00491 }
00492
00499 private function getOptionLabel( $permission ) {
00500 if( $permission == '' ) {
00501 return wfMsg( 'protect-default' );
00502 } else {
00503 $key = "protect-level-{$permission}";
00504 $msg = wfMsg( $key );
00505 if( wfEmptyMsg( $key, $msg ) )
00506 $msg = wfMsg( 'protect-fallback', $permission );
00507 return $msg;
00508 }
00509 }
00510
00511 function buildScript() {
00512 global $wgStylePath, $wgStyleVersion;
00513 return Xml::tags( 'script', array(
00514 'type' => 'text/javascript',
00515 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
00516 }
00517
00518 function buildCleanupScript() {
00519 global $wgRestrictionLevels, $wgGroupPermissions;
00520 $script = 'var wgCascadeableLevels=';
00521 $CascadeableLevels = array();
00522 foreach( $wgRestrictionLevels as $key ) {
00523 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
00524 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
00525 }
00526 }
00527 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
00528 $options = (object)array(
00529 'tableId' => 'mw-protect-table-move',
00530 'labelText' => wfMsg( 'protect-unchain' ),
00531 'numTypes' => count($this->mApplicableTypes),
00532 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry ) ),
00533 );
00534 $encOptions = Xml::encodeJsVar( $options );
00535
00536 $script .= "ProtectionForm.init($encOptions)";
00537 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
00538 }
00539
00544 function showLogExtract( &$out ) {
00545 # Show relevant lines from the protection log:
00546 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
00547 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
00548 }
00549 }