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 .= wfMsgForContent( 'colon-separator' ) . $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( empty($this->mRestrictions[$action]) )
00246 continue;
00247 if ( !$expiry[$action] ) {
00248 $this->show( wfMsg( 'protect_expiry_invalid' ) );
00249 return false;
00250 }
00251 if ( $expiry[$action] < wfTimestampNow() ) {
00252 $this->show( wfMsg( 'protect_expiry_old' ) );
00253 return false;
00254 }
00255 }
00256
00257 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
00258 # to a semi-protected page.
00259 global $wgGroupPermissions;
00260
00261 $edit_restriction = $this->mRestrictions['edit'];
00262 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
00263 if ($this->mCascade && ($edit_restriction != 'protect') &&
00264 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
00265 $this->mCascade = false;
00266
00267 if ($this->mTitle->exists()) {
00268 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
00269 } else {
00270 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry['create'] );
00271 }
00272
00273 if( !$ok ) {
00274 throw new FatalError( "Unknown error at restriction save time." );
00275 }
00276
00277 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
00278 $this->mArticle->doWatch();
00279 } elseif( $this->mTitle->userIsWatching() ) {
00280 $this->mArticle->doUnwatch();
00281 }
00282 return $ok;
00283 }
00284
00290 function buildForm() {
00291 global $wgUser, $wgLang;
00292
00293 $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
00294 $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
00295
00296 $out = '';
00297 if( !$this->disabled ) {
00298 $out .= $this->buildScript();
00299 $out .= Xml::openElement( 'form', array( 'method' => 'post',
00300 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
00301 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
00302 $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
00303 }
00304
00305 $out .= Xml::openElement( 'fieldset' ) .
00306 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
00307 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
00308 Xml::openElement( 'tbody' );
00309
00310 foreach( $this->mRestrictions as $action => $selected ) {
00311
00312 $msg = wfMsg( 'restriction-' . $action );
00313 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
00314 $msg = $action;
00315 }
00316 $out .= "<tr><td>".
00317 Xml::openElement( 'fieldset' ) .
00318 Xml::element( 'legend', null, $msg ) .
00319 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
00320 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
00321
00322 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
00323 wfMsgForContent( 'protect-dropdown' ),
00324 wfMsgForContent( 'protect-otherreason-op' ),
00325 $this->mReasonSelection,
00326 'mwProtect-reason', 4 );
00327 $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
00328
00329 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
00330
00331 $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
00332 $mProtectother = Xml::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
00333
00334 $expiryFormOptions = '';
00335 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) {
00336 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action] );
00337 $d = $wgLang->date( $this->mExistingExpiry[$action] );
00338 $t = $wgLang->time( $this->mExistingExpiry[$action] );
00339 $expiryFormOptions .=
00340 Xml::option(
00341 wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
00342 'existing',
00343 $this->mExpirySelection[$action] == 'existing'
00344 ) . "\n";
00345 }
00346
00347 $expiryFormOptions .= Xml::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
00348 foreach( explode(',', $scExpiryOptions) as $option ) {
00349 if ( strpos($option, ":") === false ) {
00350 $show = $value = $option;
00351 } else {
00352 list($show, $value) = explode(":", $option);
00353 }
00354 $show = htmlspecialchars($show);
00355 $value = htmlspecialchars($value);
00356 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n";
00357 }
00358 # Add expiry dropdown
00359 if( $showProtectOptions && !$this->disabled ) {
00360 $out .= "
00361 <table><tr>
00362 <td class='mw-label'>
00363 {$mProtectexpiry}
00364 </td>
00365 <td class='mw-input'>" .
00366 Xml::tags( 'select',
00367 array(
00368 'id' => "mwProtectExpirySelection-$action",
00369 'name' => "wpProtectExpirySelection-$action",
00370 'onchange' => "ProtectionForm.updateExpiryList(this)",
00371 'tabindex' => '2' ) + $this->disabledAttrib,
00372 $expiryFormOptions ) .
00373 "</td>
00374 </tr></table>";
00375 }
00376 # Add custom expiry field
00377 $attribs = array( 'id' => "mwProtect-$action-expires",
00378 'onkeyup' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib;
00379 $out .= "<table><tr>
00380 <td class='mw-label'>" .
00381 $mProtectother .
00382 '</td>
00383 <td class="mw-input">' .
00384 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
00385 '</td>
00386 </tr></table>';
00387 $out .= "</td></tr>" .
00388 Xml::closeElement( 'table' ) .
00389 Xml::closeElement( 'fieldset' ) .
00390 "</td></tr>";
00391 }
00392
00393 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00394
00395
00396 if( $this->mTitle->exists() ) {
00397 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
00398 Xml::openElement( 'tbody' );
00399 $out .= '<tr>
00400 <td></td>
00401 <td class="mw-input">' .
00402 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
00403 $this->mCascade, $this->disabledAttrib ) .
00404 "</td>
00405 </tr>\n";
00406 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00407 }
00408
00409 # Add manual and custom reason field/selects as well as submit
00410 if( !$this->disabled ) {
00411 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
00412 Xml::openElement( 'tbody' );
00413 $out .= "
00414 <tr>
00415 <td class='mw-label'>
00416 {$mProtectreasonother}
00417 </td>
00418 <td class='mw-input'>
00419 {$reasonDropDown}
00420 </td>
00421 </tr>
00422 <tr>
00423 <td class='mw-label'>
00424 {$mProtectreason}
00425 </td>
00426 <td class='mw-input'>" .
00427 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
00428 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
00429 "</td>
00430 </tr>
00431 <tr>
00432 <td></td>
00433 <td class='mw-input'>" .
00434 Xml::checkLabel( wfMsg( 'watchthis' ),
00435 'mwProtectWatch', 'mwProtectWatch',
00436 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
00437 "</td>
00438 </tr>
00439 <tr>
00440 <td></td>
00441 <td class='mw-submit'>" .
00442 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
00443 "</td>
00444 </tr>\n";
00445 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
00446 }
00447 $out .= Xml::closeElement( 'fieldset' );
00448
00449 if ( $wgUser->isAllowed( 'editinterface' ) ) {
00450 $linkTitle = Title::makeTitleSafe( NS_MEDIAWIKI, 'protect-dropdown' );
00451 $link = $wgUser->getSkin()->Link ( $linkTitle, wfMsgHtml( 'protect-edit-reasonlist' ) );
00452 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
00453 }
00454
00455 if ( !$this->disabled ) {
00456 $out .= Xml::closeElement( 'form' ) .
00457 $this->buildCleanupScript();
00458 }
00459
00460 return $out;
00461 }
00462
00463 function buildSelector( $action, $selected ) {
00464 global $wgRestrictionLevels, $wgUser;
00465
00466 $levels = array();
00467 foreach( $wgRestrictionLevels as $key ) {
00468
00469 if( $key == 'sysop' ) {
00470
00471 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
00472 continue;
00473 } else {
00474 if( !$wgUser->isAllowed($key) && !$this->disabled )
00475 continue;
00476 }
00477 $levels[] = $key;
00478 }
00479
00480 $id = 'mwProtect-level-' . $action;
00481 $attribs = array(
00482 'id' => $id,
00483 'name' => $id,
00484 'size' => count( $levels ),
00485 'onchange' => 'ProtectionForm.updateLevels(this)',
00486 ) + $this->disabledAttrib;
00487
00488 $out = Xml::openElement( 'select', $attribs );
00489 foreach( $levels as $key ) {
00490 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
00491 }
00492 $out .= Xml::closeElement( 'select' );
00493 return $out;
00494 }
00495
00502 private function getOptionLabel( $permission ) {
00503 if( $permission == '' ) {
00504 return wfMsg( 'protect-default' );
00505 } else {
00506 $key = "protect-level-{$permission}";
00507 $msg = wfMsg( $key );
00508 if( wfEmptyMsg( $key, $msg ) )
00509 $msg = wfMsg( 'protect-fallback', $permission );
00510 return $msg;
00511 }
00512 }
00513
00514 function buildScript() {
00515 global $wgStylePath, $wgStyleVersion;
00516 return Xml::tags( 'script', array(
00517 'type' => 'text/javascript',
00518 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
00519 }
00520
00521 function buildCleanupScript() {
00522 global $wgRestrictionLevels, $wgGroupPermissions;
00523 $script = 'var wgCascadeableLevels=';
00524 $CascadeableLevels = array();
00525 foreach( $wgRestrictionLevels as $key ) {
00526 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
00527 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
00528 }
00529 }
00530 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
00531 $options = (object)array(
00532 'tableId' => 'mw-protect-table-move',
00533 'labelText' => wfMsg( 'protect-unchain' ),
00534 'numTypes' => count($this->mApplicableTypes),
00535 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry ) ),
00536 );
00537 $encOptions = Xml::encodeJsVar( $options );
00538
00539 $script .= "ProtectionForm.init($encOptions)";
00540 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
00541 }
00542
00547 function showLogExtract( &$out ) {
00548 # Show relevant lines from the protection log:
00549 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
00550 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
00551 }
00552 }