MediaWiki
master
|
00001 <?php 00029 class ProtectionForm { 00031 var $mRestrictions = array(); 00032 00034 var $mReason = ''; 00035 00037 var $mReasonSelection = ''; 00038 00040 var $mCascade = false; 00041 00043 var $mExpiry = array(); 00044 00049 var $mExpirySelection = array(); 00050 00052 var $mPermErrors = array(); 00053 00055 var $mApplicableTypes = array(); 00056 00058 var $mExistingExpiry = array(); 00059 00060 function __construct( Page $article ) { 00061 global $wgUser; 00062 // Set instance variables. 00063 $this->mArticle = $article; 00064 $this->mTitle = $article->getTitle(); 00065 $this->mApplicableTypes = $this->mTitle->getRestrictionTypes(); 00066 00067 // Check if the form should be disabled. 00068 // If it is, the form will be available in read-only to show levels. 00069 $this->mPermErrors = $this->mTitle->getUserPermissionsErrors( 'protect', $wgUser ); 00070 if ( wfReadOnly() ) { 00071 $this->mPermErrors[] = array( 'readonlytext', wfReadOnlyReason() ); 00072 } 00073 $this->disabled = $this->mPermErrors != array(); 00074 $this->disabledAttrib = $this->disabled 00075 ? array( 'disabled' => 'disabled' ) 00076 : array(); 00077 00078 $this->loadData(); 00079 } 00080 00084 function loadData() { 00085 global $wgRequest, $wgUser; 00086 global $wgRestrictionLevels; 00087 00088 $this->mCascade = $this->mTitle->areRestrictionsCascading(); 00089 00090 $this->mReason = $wgRequest->getText( 'mwProtect-reason' ); 00091 $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' ); 00092 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade ); 00093 00094 foreach( $this->mApplicableTypes as $action ) { 00095 // @todo FIXME: This form currently requires individual selections, 00096 // but the db allows multiples separated by commas. 00097 00098 // Pull the actual restriction from the DB 00099 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) ); 00100 00101 if ( !$this->mRestrictions[$action] ) { 00102 // No existing expiry 00103 $existingExpiry = ''; 00104 } else { 00105 $existingExpiry = $this->mTitle->getRestrictionExpiry( $action ); 00106 } 00107 $this->mExistingExpiry[$action] = $existingExpiry; 00108 00109 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" ); 00110 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" ); 00111 00112 if ( $requestExpiry ) { 00113 // Custom expiry takes precedence 00114 $this->mExpiry[$action] = $requestExpiry; 00115 $this->mExpirySelection[$action] = 'othertime'; 00116 } elseif ( $requestExpirySelection ) { 00117 // Expiry selected from list 00118 $this->mExpiry[$action] = ''; 00119 $this->mExpirySelection[$action] = $requestExpirySelection; 00120 } elseif ( $existingExpiry == 'infinity' ) { 00121 // Existing expiry is infinite, use "infinite" in drop-down 00122 $this->mExpiry[$action] = ''; 00123 $this->mExpirySelection[$action] = 'infinite'; 00124 } elseif ( $existingExpiry ) { 00125 // Use existing expiry in its own list item 00126 $this->mExpiry[$action] = ''; 00127 $this->mExpirySelection[$action] = $existingExpiry; 00128 } else { 00129 // Final default: infinite 00130 $this->mExpiry[$action] = ''; 00131 $this->mExpirySelection[$action] = 'infinite'; 00132 } 00133 00134 $val = $wgRequest->getVal( "mwProtect-level-$action" ); 00135 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) { 00136 // Prevent users from setting levels that they cannot later unset 00137 if( $val == 'sysop' ) { 00138 // Special case, rewrite sysop to either protect and editprotected 00139 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) ) 00140 continue; 00141 } else { 00142 if( !$wgUser->isAllowed($val) ) 00143 continue; 00144 } 00145 $this->mRestrictions[$action] = $val; 00146 } 00147 } 00148 } 00149 00157 function getExpiry( $action ) { 00158 if ( $this->mExpirySelection[$action] == 'existing' ) { 00159 return $this->mExistingExpiry[$action]; 00160 } elseif ( $this->mExpirySelection[$action] == 'othertime' ) { 00161 $value = $this->mExpiry[$action]; 00162 } else { 00163 $value = $this->mExpirySelection[$action]; 00164 } 00165 if ( $value == 'infinite' || $value == 'indefinite' || $value == 'infinity' ) { 00166 $time = wfGetDB( DB_SLAVE )->getInfinity(); 00167 } else { 00168 $unix = strtotime( $value ); 00169 00170 if ( !$unix || $unix === -1 ) { 00171 return false; 00172 } 00173 00174 // @todo FIXME: Non-qualified absolute times are not in users specified timezone 00175 // and there isn't notice about it in the ui 00176 $time = wfTimestamp( TS_MW, $unix ); 00177 } 00178 return $time; 00179 } 00180 00184 function execute() { 00185 global $wgRequest, $wgOut; 00186 00187 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) { 00188 throw new ErrorPageError( 'protect-badnamespace-title', 'protect-badnamespace-text' ); 00189 } 00190 00191 if( $wgRequest->wasPosted() ) { 00192 if( $this->save() ) { 00193 $q = $this->mArticle->isRedirect() ? 'redirect=no' : ''; 00194 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) ); 00195 } 00196 } else { 00197 $this->show(); 00198 } 00199 } 00200 00206 function show( $err = null ) { 00207 global $wgOut; 00208 00209 $wgOut->setRobotPolicy( 'noindex,nofollow' ); 00210 00211 if ( is_array( $err ) ) { 00212 $wgOut->wrapWikiMsg( "<p class='error'>\n$1\n</p>\n", $err ); 00213 } elseif ( is_string( $err ) ) { 00214 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" ); 00215 } 00216 00217 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources(); 00218 if ( $cascadeSources && count($cascadeSources) > 0 ) { 00219 $titles = ''; 00220 00221 foreach ( $cascadeSources as $title ) { 00222 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n"; 00223 } 00224 00225 $wgOut->wrapWikiMsg( "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>", array( 'protect-cascadeon', count($cascadeSources) ) ); 00226 } 00227 00228 # Show an appropriate message if the user isn't allowed or able to change 00229 # the protection settings at this time 00230 if ( $this->disabled ) { 00231 $wgOut->setPageTitle( wfMessage( 'protect-title-notallowed', $this->mTitle->getPrefixedText() ) ); 00232 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors, 'protect' ) ); 00233 } else { 00234 $wgOut->setPageTitle( wfMessage( 'protect-title', $this->mTitle->getPrefixedText() ) ); 00235 $wgOut->addWikiMsg( 'protect-text', 00236 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ); 00237 } 00238 00239 $wgOut->addBacklinkSubtitle( $this->mTitle ); 00240 $wgOut->addHTML( $this->buildForm() ); 00241 $this->showLogExtract( $wgOut ); 00242 } 00243 00249 function save() { 00250 global $wgRequest, $wgUser, $wgOut; 00251 00252 # Permission check! 00253 if ( $this->disabled ) { 00254 $this->show(); 00255 return false; 00256 } 00257 00258 $token = $wgRequest->getVal( 'wpEditToken' ); 00259 if ( !$wgUser->matchEditToken( $token, array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ) { 00260 $this->show( array( 'sessionfailure' ) ); 00261 return false; 00262 } 00263 00264 # Create reason string. Use list and/or custom string. 00265 $reasonstr = $this->mReasonSelection; 00266 if ( $reasonstr != 'other' && $this->mReason != '' ) { 00267 // Entry from drop down menu + additional comment 00268 $reasonstr .= wfMessage( 'colon-separator' )->text() . $this->mReason; 00269 } elseif ( $reasonstr == 'other' ) { 00270 $reasonstr = $this->mReason; 00271 } 00272 $expiry = array(); 00273 foreach( $this->mApplicableTypes as $action ) { 00274 $expiry[$action] = $this->getExpiry( $action ); 00275 if( empty($this->mRestrictions[$action]) ) 00276 continue; // unprotected 00277 if ( !$expiry[$action] ) { 00278 $this->show( array( 'protect_expiry_invalid' ) ); 00279 return false; 00280 } 00281 if ( $expiry[$action] < wfTimestampNow() ) { 00282 $this->show( array( 'protect_expiry_old' ) ); 00283 return false; 00284 } 00285 } 00286 00287 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied 00288 # to a semi-protected page. 00289 $edit_restriction = isset( $this->mRestrictions['edit'] ) ? $this->mRestrictions['edit'] : ''; 00290 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' ); 00291 if ($this->mCascade && ($edit_restriction != 'protect') && 00292 !User::groupHasPermission( $edit_restriction, 'protect' ) ) 00293 $this->mCascade = false; 00294 00295 $status = $this->mArticle->doUpdateRestrictions( $this->mRestrictions, $expiry, $this->mCascade, $reasonstr, $wgUser ); 00296 00297 if ( !$status->isOK() ) { 00298 $this->show( $wgOut->parseInline( $status->getWikiText() ) ); 00299 return false; 00300 } 00301 00308 $errorMsg = ''; 00309 if( !wfRunHooks( 'ProtectionForm::save', array( $this->mArticle, &$errorMsg ) ) ) { 00310 if ( $errorMsg == '' ) { 00311 $errorMsg = array( 'hookaborted' ); 00312 } 00313 } 00314 if( $errorMsg != '' ) { 00315 $this->show( $errorMsg ); 00316 return false; 00317 } 00318 00319 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'mwProtectWatch' ) != $wgUser->isWatched( $this->mTitle ) ) { 00320 if ( $wgRequest->getCheck( 'mwProtectWatch' ) ) { 00321 WatchAction::doWatch( $this->mTitle, $wgUser ); 00322 } else { 00323 WatchAction::doUnwatch( $this->mTitle, $wgUser ); 00324 } 00325 } 00326 return true; 00327 } 00328 00334 function buildForm() { 00335 global $wgUser, $wgLang, $wgOut; 00336 00337 $mProtectreasonother = Xml::label( 00338 wfMessage( 'protectcomment' )->text(), 00339 'wpProtectReasonSelection' 00340 ); 00341 $mProtectreason = Xml::label( 00342 wfMessage( 'protect-otherreason' )->text(), 00343 'mwProtect-reason' 00344 ); 00345 00346 $out = ''; 00347 if( !$this->disabled ) { 00348 $wgOut->addModules( 'mediawiki.legacy.protect' ); 00349 $out .= Xml::openElement( 'form', array( 'method' => 'post', 00350 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 00351 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) ); 00352 } 00353 00354 $out .= Xml::openElement( 'fieldset' ) . 00355 Xml::element( 'legend', null, wfMessage( 'protect-legend' )->text() ) . 00356 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) . 00357 Xml::openElement( 'tbody' ); 00358 00359 foreach( $this->mRestrictions as $action => $selected ) { 00360 /* Not all languages have V_x <-> N_x relation */ 00361 $msg = wfMessage( 'restriction-' . $action ); 00362 $out .= "<tr><td>". 00363 Xml::openElement( 'fieldset' ) . 00364 Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) . 00365 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) . 00366 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>"; 00367 00368 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection', 00369 wfMessage( 'protect-dropdown' )->inContentLanguage()->text(), 00370 wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(), 00371 $this->mReasonSelection, 00372 'mwProtect-reason', 4 ); 00373 $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text(); 00374 00375 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled); 00376 00377 $mProtectexpiry = Xml::label( 00378 wfMessage( 'protectexpiry' )->text(), 00379 "mwProtectExpirySelection-$action" 00380 ); 00381 $mProtectother = Xml::label( 00382 wfMessage( 'protect-othertime' )->text(), 00383 "mwProtect-$action-expires" 00384 ); 00385 00386 $expiryFormOptions = ''; 00387 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) { 00388 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action], true ); 00389 $d = $wgLang->date( $this->mExistingExpiry[$action], true ); 00390 $t = $wgLang->time( $this->mExistingExpiry[$action], true ); 00391 $expiryFormOptions .= 00392 Xml::option( 00393 wfMessage( 'protect-existing-expiry', $timestamp, $d, $t )->text(), 00394 'existing', 00395 $this->mExpirySelection[$action] == 'existing' 00396 ) . "\n"; 00397 } 00398 00399 $expiryFormOptions .= Xml::option( 00400 wfMessage( 'protect-othertime-op' )->text(), 00401 "othertime" 00402 ) . "\n"; 00403 foreach( explode(',', $scExpiryOptions) as $option ) { 00404 if ( strpos($option, ":") === false ) { 00405 $show = $value = $option; 00406 } else { 00407 list($show, $value) = explode(":", $option); 00408 } 00409 $show = htmlspecialchars($show); 00410 $value = htmlspecialchars($value); 00411 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n"; 00412 } 00413 # Add expiry dropdown 00414 if( $showProtectOptions && !$this->disabled ) { 00415 $out .= " 00416 <table><tr> 00417 <td class='mw-label'> 00418 {$mProtectexpiry} 00419 </td> 00420 <td class='mw-input'>" . 00421 Xml::tags( 'select', 00422 array( 00423 'id' => "mwProtectExpirySelection-$action", 00424 'name' => "wpProtectExpirySelection-$action", 00425 'onchange' => "ProtectionForm.updateExpiryList(this)", 00426 'tabindex' => '2' ) + $this->disabledAttrib, 00427 $expiryFormOptions ) . 00428 "</td> 00429 </tr></table>"; 00430 } 00431 # Add custom expiry field 00432 $attribs = array( 'id' => "mwProtect-$action-expires", 00433 'onkeyup' => 'ProtectionForm.updateExpiry(this)', 00434 'onchange' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib; 00435 $out .= "<table><tr> 00436 <td class='mw-label'>" . 00437 $mProtectother . 00438 '</td> 00439 <td class="mw-input">' . 00440 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) . 00441 '</td> 00442 </tr></table>'; 00443 $out .= "</td></tr>" . 00444 Xml::closeElement( 'table' ) . 00445 Xml::closeElement( 'fieldset' ) . 00446 "</td></tr>"; 00447 } 00448 # Give extensions a chance to add items to the form 00449 wfRunHooks( 'ProtectionForm::buildForm', array($this->mArticle,&$out) ); 00450 00451 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00452 00453 // JavaScript will add another row with a value-chaining checkbox 00454 if( $this->mTitle->exists() ) { 00455 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) . 00456 Xml::openElement( 'tbody' ); 00457 $out .= '<tr> 00458 <td></td> 00459 <td class="mw-input">' . 00460 Xml::checkLabel( 00461 wfMessage( 'protect-cascade' )->text(), 00462 'mwProtect-cascade', 00463 'mwProtect-cascade', 00464 $this->mCascade, $this->disabledAttrib 00465 ) . 00466 "</td> 00467 </tr>\n"; 00468 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00469 } 00470 00471 # Add manual and custom reason field/selects as well as submit 00472 if( !$this->disabled ) { 00473 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) . 00474 Xml::openElement( 'tbody' ); 00475 $out .= " 00476 <tr> 00477 <td class='mw-label'> 00478 {$mProtectreasonother} 00479 </td> 00480 <td class='mw-input'> 00481 {$reasonDropDown} 00482 </td> 00483 </tr> 00484 <tr> 00485 <td class='mw-label'> 00486 {$mProtectreason} 00487 </td> 00488 <td class='mw-input'>" . 00489 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text', 00490 'id' => 'mwProtect-reason', 'maxlength' => 180 ) ) . 00491 // Limited maxlength as the database trims at 255 bytes and other texts 00492 // chosen by dropdown menus on this page are also included in this database field. 00493 // The byte limit of 180 bytes is enforced in javascript 00494 "</td> 00495 </tr>"; 00496 # Disallow watching is user is not logged in 00497 if( $wgUser->isLoggedIn() ) { 00498 $out .= " 00499 <tr> 00500 <td></td> 00501 <td class='mw-input'>" . 00502 Xml::checkLabel( wfMessage( 'watchthis' )->text(), 00503 'mwProtectWatch', 'mwProtectWatch', 00504 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) . 00505 "</td> 00506 </tr>"; 00507 } 00508 $out .= " 00509 <tr> 00510 <td></td> 00511 <td class='mw-submit'>" . 00512 Xml::submitButton( 00513 wfMessage( 'confirm' )->text(), 00514 array( 'id' => 'mw-Protect-submit' ) 00515 ) . 00516 "</td> 00517 </tr>\n"; 00518 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00519 } 00520 $out .= Xml::closeElement( 'fieldset' ); 00521 00522 if ( $wgUser->isAllowed( 'editinterface' ) ) { 00523 $title = Title::makeTitle( NS_MEDIAWIKI, 'Protect-dropdown' ); 00524 $link = Linker::link( 00525 $title, 00526 wfMessage( 'protect-edit-reasonlist' )->escaped(), 00527 array(), 00528 array( 'action' => 'edit' ) 00529 ); 00530 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>'; 00531 } 00532 00533 if ( !$this->disabled ) { 00534 $out .= Html::hidden( 'wpEditToken', $wgUser->getEditToken( array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ); 00535 $out .= Xml::closeElement( 'form' ); 00536 $wgOut->addScript( $this->buildCleanupScript() ); 00537 } 00538 00539 return $out; 00540 } 00541 00549 function buildSelector( $action, $selected ) { 00550 global $wgRestrictionLevels, $wgUser; 00551 00552 $levels = array(); 00553 foreach( $wgRestrictionLevels as $key ) { 00554 //don't let them choose levels above their own (aka so they can still unprotect and edit the page). but only when the form isn't disabled 00555 if( $key == 'sysop' ) { 00556 //special case, rewrite sysop to protect and editprotected 00557 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) && !$this->disabled ) 00558 continue; 00559 } else { 00560 if( !$wgUser->isAllowed($key) && !$this->disabled ) 00561 continue; 00562 } 00563 $levels[] = $key; 00564 } 00565 00566 $id = 'mwProtect-level-' . $action; 00567 $attribs = array( 00568 'id' => $id, 00569 'name' => $id, 00570 'size' => count( $levels ), 00571 'onchange' => 'ProtectionForm.updateLevels(this)', 00572 ) + $this->disabledAttrib; 00573 00574 $out = Xml::openElement( 'select', $attribs ); 00575 foreach( $levels as $key ) { 00576 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected ); 00577 } 00578 $out .= Xml::closeElement( 'select' ); 00579 return $out; 00580 } 00581 00588 private function getOptionLabel( $permission ) { 00589 if( $permission == '' ) { 00590 return wfMessage( 'protect-default' )->text(); 00591 } else { 00592 $msg = wfMessage( "protect-level-{$permission}" ); 00593 if( $msg->exists() ) { 00594 return $msg->text(); 00595 } 00596 return wfMessage( 'protect-fallback', $permission )->text(); 00597 } 00598 } 00599 00600 function buildCleanupScript() { 00601 global $wgRestrictionLevels, $wgOut; 00602 00603 $cascadeableLevels = array(); 00604 foreach( $wgRestrictionLevels as $key ) { 00605 if ( User::groupHasPermission( $key, 'protect' ) 00606 || $key == 'protect' 00607 ) { 00608 $cascadeableLevels[] = $key; 00609 } 00610 } 00611 $options = array( 00612 'tableId' => 'mwProtectSet', 00613 'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(), 00614 'numTypes' => count( $this->mApplicableTypes ), 00615 'existingMatch' => count( array_unique( $this->mExistingExpiry ) ) === 1, 00616 ); 00617 00618 $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels ); 00619 $script = Xml::encodeJsCall( 'ProtectionForm.init', array( $options ) ); 00620 return Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ); 00621 } 00622 00629 function showLogExtract( &$out ) { 00630 # Show relevant lines from the protection log: 00631 $protectLogPage = new LogPage( 'protect' ); 00632 $out->addHTML( Xml::element( 'h2', null, $protectLogPage->getName()->text() ) ); 00633 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle ); 00634 # Let extensions add other relevant log extracts 00635 wfRunHooks( 'ProtectionForm::showLogExtract', array($this->mArticle,$out) ); 00636 } 00637 }