MediaWiki
REL1_21
|
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 $wgOut->addBacklinkSubtitle( $this->mTitle ); 00211 00212 if ( is_array( $err ) ) { 00213 $wgOut->wrapWikiMsg( "<p class='error'>\n$1\n</p>\n", $err ); 00214 } elseif ( is_string( $err ) ) { 00215 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" ); 00216 } 00217 00218 if ( $this->mTitle->getRestrictionTypes() === array() ) { 00219 // No restriction types available for the current title 00220 // this might happen if an extension alters the available types 00221 $wgOut->setPageTitle( wfMessage( 'protect-norestrictiontypes-title', $this->mTitle->getPrefixedText() ) ); 00222 $wgOut->addWikiText( wfMessage( 'protect-norestrictiontypes-text' )->text() ); 00223 00224 // Show the log in case protection was possible once 00225 $this->showLogExtract( $wgOut ); 00226 // return as there isn't anything else we can do 00227 return; 00228 } 00229 00230 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources(); 00231 if ( $cascadeSources && count( $cascadeSources ) > 0 ) { 00232 $titles = ''; 00233 00234 foreach ( $cascadeSources as $title ) { 00235 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n"; 00236 } 00237 00238 $wgOut->wrapWikiMsg( "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>", array( 'protect-cascadeon', count( $cascadeSources ) ) ); 00239 } 00240 00241 # Show an appropriate message if the user isn't allowed or able to change 00242 # the protection settings at this time 00243 if ( $this->disabled ) { 00244 $wgOut->setPageTitle( wfMessage( 'protect-title-notallowed', $this->mTitle->getPrefixedText() ) ); 00245 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors, 'protect' ) ); 00246 } else { 00247 $wgOut->setPageTitle( wfMessage( 'protect-title', $this->mTitle->getPrefixedText() ) ); 00248 $wgOut->addWikiMsg( 'protect-text', 00249 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ); 00250 } 00251 00252 $wgOut->addHTML( $this->buildForm() ); 00253 $this->showLogExtract( $wgOut ); 00254 } 00255 00261 function save() { 00262 global $wgRequest, $wgUser, $wgOut; 00263 00264 # Permission check! 00265 if ( $this->disabled ) { 00266 $this->show(); 00267 return false; 00268 } 00269 00270 $token = $wgRequest->getVal( 'wpEditToken' ); 00271 if ( !$wgUser->matchEditToken( $token, array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ) { 00272 $this->show( array( 'sessionfailure' ) ); 00273 return false; 00274 } 00275 00276 # Create reason string. Use list and/or custom string. 00277 $reasonstr = $this->mReasonSelection; 00278 if ( $reasonstr != 'other' && $this->mReason != '' ) { 00279 // Entry from drop down menu + additional comment 00280 $reasonstr .= wfMessage( 'colon-separator' )->text() . $this->mReason; 00281 } elseif ( $reasonstr == 'other' ) { 00282 $reasonstr = $this->mReason; 00283 } 00284 $expiry = array(); 00285 foreach( $this->mApplicableTypes as $action ) { 00286 $expiry[$action] = $this->getExpiry( $action ); 00287 if( empty( $this->mRestrictions[$action] ) ) 00288 continue; // unprotected 00289 if ( !$expiry[$action] ) { 00290 $this->show( array( 'protect_expiry_invalid' ) ); 00291 return false; 00292 } 00293 if ( $expiry[$action] < wfTimestampNow() ) { 00294 $this->show( array( 'protect_expiry_old' ) ); 00295 return false; 00296 } 00297 } 00298 00299 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied 00300 # to a semi-protected page. 00301 $edit_restriction = isset( $this->mRestrictions['edit'] ) ? $this->mRestrictions['edit'] : ''; 00302 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' ); 00303 if ( $this->mCascade && ($edit_restriction != 'protect') && 00304 !User::groupHasPermission( $edit_restriction, 'protect' ) ) 00305 $this->mCascade = false; 00306 00307 $status = $this->mArticle->doUpdateRestrictions( $this->mRestrictions, $expiry, $this->mCascade, $reasonstr, $wgUser ); 00308 00309 if ( !$status->isOK() ) { 00310 $this->show( $wgOut->parseInline( $status->getWikiText() ) ); 00311 return false; 00312 } 00313 00320 $errorMsg = ''; 00321 if( !wfRunHooks( 'ProtectionForm::save', array( $this->mArticle, &$errorMsg ) ) ) { 00322 if ( $errorMsg == '' ) { 00323 $errorMsg = array( 'hookaborted' ); 00324 } 00325 } 00326 if( $errorMsg != '' ) { 00327 $this->show( $errorMsg ); 00328 return false; 00329 } 00330 00331 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'mwProtectWatch' ) != $wgUser->isWatched( $this->mTitle ) ) { 00332 if ( $wgRequest->getCheck( 'mwProtectWatch' ) ) { 00333 WatchAction::doWatch( $this->mTitle, $wgUser ); 00334 } else { 00335 WatchAction::doUnwatch( $this->mTitle, $wgUser ); 00336 } 00337 } 00338 return true; 00339 } 00340 00346 function buildForm() { 00347 global $wgUser, $wgLang, $wgOut; 00348 00349 $mProtectreasonother = Xml::label( 00350 wfMessage( 'protectcomment' )->text(), 00351 'wpProtectReasonSelection' 00352 ); 00353 $mProtectreason = Xml::label( 00354 wfMessage( 'protect-otherreason' )->text(), 00355 'mwProtect-reason' 00356 ); 00357 00358 $out = ''; 00359 if( !$this->disabled ) { 00360 $wgOut->addModules( 'mediawiki.legacy.protect' ); 00361 $out .= Xml::openElement( 'form', array( 'method' => 'post', 00362 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 00363 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) ); 00364 } 00365 00366 $out .= Xml::openElement( 'fieldset' ) . 00367 Xml::element( 'legend', null, wfMessage( 'protect-legend' )->text() ) . 00368 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) . 00369 Xml::openElement( 'tbody' ); 00370 00371 foreach( $this->mRestrictions as $action => $selected ) { 00372 /* Not all languages have V_x <-> N_x relation */ 00373 $msg = wfMessage( 'restriction-' . $action ); 00374 $out .= "<tr><td>". 00375 Xml::openElement( 'fieldset' ) . 00376 Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) . 00377 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) . 00378 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>"; 00379 00380 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection', 00381 wfMessage( 'protect-dropdown' )->inContentLanguage()->text(), 00382 wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(), 00383 $this->mReasonSelection, 00384 'mwProtect-reason', 4 ); 00385 $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text(); 00386 00387 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled); 00388 00389 $mProtectexpiry = Xml::label( 00390 wfMessage( 'protectexpiry' )->text(), 00391 "mwProtectExpirySelection-$action" 00392 ); 00393 $mProtectother = Xml::label( 00394 wfMessage( 'protect-othertime' )->text(), 00395 "mwProtect-$action-expires" 00396 ); 00397 00398 $expiryFormOptions = ''; 00399 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) { 00400 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action], true ); 00401 $d = $wgLang->date( $this->mExistingExpiry[$action], true ); 00402 $t = $wgLang->time( $this->mExistingExpiry[$action], true ); 00403 $expiryFormOptions .= 00404 Xml::option( 00405 wfMessage( 'protect-existing-expiry', $timestamp, $d, $t )->text(), 00406 'existing', 00407 $this->mExpirySelection[$action] == 'existing' 00408 ) . "\n"; 00409 } 00410 00411 $expiryFormOptions .= Xml::option( 00412 wfMessage( 'protect-othertime-op' )->text(), 00413 "othertime" 00414 ) . "\n"; 00415 foreach( explode( ',', $scExpiryOptions ) as $option ) { 00416 if ( strpos( $option, ":" ) === false ) { 00417 $show = $value = $option; 00418 } else { 00419 list( $show, $value ) = explode( ":", $option ); 00420 } 00421 $show = htmlspecialchars( $show ); 00422 $value = htmlspecialchars( $value ); 00423 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n"; 00424 } 00425 # Add expiry dropdown 00426 if( $showProtectOptions && !$this->disabled ) { 00427 $out .= " 00428 <table><tr> 00429 <td class='mw-label'> 00430 {$mProtectexpiry} 00431 </td> 00432 <td class='mw-input'>" . 00433 Xml::tags( 'select', 00434 array( 00435 'id' => "mwProtectExpirySelection-$action", 00436 'name' => "wpProtectExpirySelection-$action", 00437 'onchange' => "ProtectionForm.updateExpiryList(this)", 00438 'tabindex' => '2' ) + $this->disabledAttrib, 00439 $expiryFormOptions ) . 00440 "</td> 00441 </tr></table>"; 00442 } 00443 # Add custom expiry field 00444 $attribs = array( 'id' => "mwProtect-$action-expires", 00445 'onkeyup' => 'ProtectionForm.updateExpiry(this)', 00446 'onchange' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib; 00447 $out .= "<table><tr> 00448 <td class='mw-label'>" . 00449 $mProtectother . 00450 '</td> 00451 <td class="mw-input">' . 00452 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) . 00453 '</td> 00454 </tr></table>'; 00455 $out .= "</td></tr>" . 00456 Xml::closeElement( 'table' ) . 00457 Xml::closeElement( 'fieldset' ) . 00458 "</td></tr>"; 00459 } 00460 # Give extensions a chance to add items to the form 00461 wfRunHooks( 'ProtectionForm::buildForm', array( $this->mArticle, &$out ) ); 00462 00463 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00464 00465 // JavaScript will add another row with a value-chaining checkbox 00466 if( $this->mTitle->exists() ) { 00467 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) . 00468 Xml::openElement( 'tbody' ); 00469 $out .= '<tr> 00470 <td></td> 00471 <td class="mw-input">' . 00472 Xml::checkLabel( 00473 wfMessage( 'protect-cascade' )->text(), 00474 'mwProtect-cascade', 00475 'mwProtect-cascade', 00476 $this->mCascade, $this->disabledAttrib 00477 ) . 00478 "</td> 00479 </tr>\n"; 00480 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00481 } 00482 00483 # Add manual and custom reason field/selects as well as submit 00484 if( !$this->disabled ) { 00485 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) . 00486 Xml::openElement( 'tbody' ); 00487 $out .= " 00488 <tr> 00489 <td class='mw-label'> 00490 {$mProtectreasonother} 00491 </td> 00492 <td class='mw-input'> 00493 {$reasonDropDown} 00494 </td> 00495 </tr> 00496 <tr> 00497 <td class='mw-label'> 00498 {$mProtectreason} 00499 </td> 00500 <td class='mw-input'>" . 00501 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text', 00502 'id' => 'mwProtect-reason', 'maxlength' => 180 ) ) . 00503 // Limited maxlength as the database trims at 255 bytes and other texts 00504 // chosen by dropdown menus on this page are also included in this database field. 00505 // The byte limit of 180 bytes is enforced in javascript 00506 "</td> 00507 </tr>"; 00508 # Disallow watching is user is not logged in 00509 if( $wgUser->isLoggedIn() ) { 00510 $out .= " 00511 <tr> 00512 <td></td> 00513 <td class='mw-input'>" . 00514 Xml::checkLabel( wfMessage( 'watchthis' )->text(), 00515 'mwProtectWatch', 'mwProtectWatch', 00516 $wgUser->isWatched( $this->mTitle ) || $wgUser->getOption( 'watchdefault' ) ) . 00517 "</td> 00518 </tr>"; 00519 } 00520 $out .= " 00521 <tr> 00522 <td></td> 00523 <td class='mw-submit'>" . 00524 Xml::submitButton( 00525 wfMessage( 'confirm' )->text(), 00526 array( 'id' => 'mw-Protect-submit' ) 00527 ) . 00528 "</td> 00529 </tr>\n"; 00530 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00531 } 00532 $out .= Xml::closeElement( 'fieldset' ); 00533 00534 if ( $wgUser->isAllowed( 'editinterface' ) ) { 00535 $title = Title::makeTitle( NS_MEDIAWIKI, 'Protect-dropdown' ); 00536 $link = Linker::link( 00537 $title, 00538 wfMessage( 'protect-edit-reasonlist' )->escaped(), 00539 array(), 00540 array( 'action' => 'edit' ) 00541 ); 00542 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>'; 00543 } 00544 00545 if ( !$this->disabled ) { 00546 $out .= Html::hidden( 'wpEditToken', $wgUser->getEditToken( array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ); 00547 $out .= Xml::closeElement( 'form' ); 00548 $wgOut->addScript( $this->buildCleanupScript() ); 00549 } 00550 00551 return $out; 00552 } 00553 00561 function buildSelector( $action, $selected ) { 00562 global $wgRestrictionLevels, $wgUser; 00563 00564 $levels = array(); 00565 foreach( $wgRestrictionLevels as $key ) { 00566 //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 00567 if( $key == 'sysop' ) { 00568 //special case, rewrite sysop to protect and editprotected 00569 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) && !$this->disabled ) 00570 continue; 00571 } else { 00572 if( !$wgUser->isAllowed( $key ) && !$this->disabled ) 00573 continue; 00574 } 00575 $levels[] = $key; 00576 } 00577 00578 $id = 'mwProtect-level-' . $action; 00579 $attribs = array( 00580 'id' => $id, 00581 'name' => $id, 00582 'size' => count( $levels ), 00583 'onchange' => 'ProtectionForm.updateLevels(this)', 00584 ) + $this->disabledAttrib; 00585 00586 $out = Xml::openElement( 'select', $attribs ); 00587 foreach( $levels as $key ) { 00588 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected ); 00589 } 00590 $out .= Xml::closeElement( 'select' ); 00591 return $out; 00592 } 00593 00600 private function getOptionLabel( $permission ) { 00601 if( $permission == '' ) { 00602 return wfMessage( 'protect-default' )->text(); 00603 } else { 00604 $msg = wfMessage( "protect-level-{$permission}" ); 00605 if( $msg->exists() ) { 00606 return $msg->text(); 00607 } 00608 return wfMessage( 'protect-fallback', $permission )->text(); 00609 } 00610 } 00611 00612 function buildCleanupScript() { 00613 global $wgRestrictionLevels, $wgOut; 00614 00615 $cascadeableLevels = array(); 00616 foreach( $wgRestrictionLevels as $key ) { 00617 if ( User::groupHasPermission( $key, 'protect' ) 00618 || $key == 'protect' 00619 ) { 00620 $cascadeableLevels[] = $key; 00621 } 00622 } 00623 $options = array( 00624 'tableId' => 'mwProtectSet', 00625 'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(), 00626 'numTypes' => count( $this->mApplicableTypes ), 00627 'existingMatch' => count( array_unique( $this->mExistingExpiry ) ) === 1, 00628 ); 00629 00630 $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels ); 00631 $script = Xml::encodeJsCall( 'ProtectionForm.init', array( $options ) ); 00632 return Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ); 00633 } 00634 00641 function showLogExtract( &$out ) { 00642 # Show relevant lines from the protection log: 00643 $protectLogPage = new LogPage( 'protect' ); 00644 $out->addHTML( Xml::element( 'h2', null, $protectLogPage->getName()->text() ) ); 00645 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle ); 00646 # Let extensions add other relevant log extracts 00647 wfRunHooks( 'ProtectionForm::showLogExtract', array( $this->mArticle, $out ) ); 00648 } 00649 }