MediaWiki
REL1_20
|
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 global $wgGroupPermissions; 00290 00291 $edit_restriction = isset( $this->mRestrictions['edit'] ) ? $this->mRestrictions['edit'] : ''; 00292 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' ); 00293 if ($this->mCascade && ($edit_restriction != 'protect') && 00294 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) ) 00295 $this->mCascade = false; 00296 00297 $status = $this->mArticle->doUpdateRestrictions( $this->mRestrictions, $expiry, $this->mCascade, $reasonstr, $wgUser ); 00298 00299 if ( !$status->isOK() ) { 00300 $this->show( $wgOut->parseInline( $status->getWikiText() ) ); 00301 return false; 00302 } 00303 00310 $errorMsg = ''; 00311 if( !wfRunHooks( 'ProtectionForm::save', array( $this->mArticle, &$errorMsg ) ) ) { 00312 if ( $errorMsg == '' ) { 00313 $errorMsg = array( 'hookaborted' ); 00314 } 00315 } 00316 if( $errorMsg != '' ) { 00317 $this->show( $errorMsg ); 00318 return false; 00319 } 00320 00321 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'mwProtectWatch' ) != $wgUser->isWatched( $this->mTitle ) ) { 00322 if ( $wgRequest->getCheck( 'mwProtectWatch' ) ) { 00323 WatchAction::doWatch( $this->mTitle, $wgUser ); 00324 } else { 00325 WatchAction::doUnwatch( $this->mTitle, $wgUser ); 00326 } 00327 } 00328 return true; 00329 } 00330 00336 function buildForm() { 00337 global $wgUser, $wgLang, $wgOut; 00338 00339 $mProtectreasonother = Xml::label( 00340 wfMessage( 'protectcomment' )->text(), 00341 'wpProtectReasonSelection' 00342 ); 00343 $mProtectreason = Xml::label( 00344 wfMessage( 'protect-otherreason' )->text(), 00345 'mwProtect-reason' 00346 ); 00347 00348 $out = ''; 00349 if( !$this->disabled ) { 00350 $wgOut->addModules( 'mediawiki.legacy.protect' ); 00351 $out .= Xml::openElement( 'form', array( 'method' => 'post', 00352 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 00353 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) ); 00354 } 00355 00356 $out .= Xml::openElement( 'fieldset' ) . 00357 Xml::element( 'legend', null, wfMessage( 'protect-legend' )->text() ) . 00358 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) . 00359 Xml::openElement( 'tbody' ); 00360 00361 foreach( $this->mRestrictions as $action => $selected ) { 00362 /* Not all languages have V_x <-> N_x relation */ 00363 $msg = wfMessage( 'restriction-' . $action ); 00364 $out .= "<tr><td>". 00365 Xml::openElement( 'fieldset' ) . 00366 Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) . 00367 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) . 00368 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>"; 00369 00370 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection', 00371 wfMessage( 'protect-dropdown' )->inContentLanguage()->text(), 00372 wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(), 00373 $this->mReasonSelection, 00374 'mwProtect-reason', 4 ); 00375 $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text(); 00376 00377 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled); 00378 00379 $mProtectexpiry = Xml::label( 00380 wfMessage( 'protectexpiry' )->text(), 00381 "mwProtectExpirySelection-$action" 00382 ); 00383 $mProtectother = Xml::label( 00384 wfMessage( 'protect-othertime' )->text(), 00385 "mwProtect-$action-expires" 00386 ); 00387 00388 $expiryFormOptions = ''; 00389 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) { 00390 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action], true ); 00391 $d = $wgLang->date( $this->mExistingExpiry[$action], true ); 00392 $t = $wgLang->time( $this->mExistingExpiry[$action], true ); 00393 $expiryFormOptions .= 00394 Xml::option( 00395 wfMessage( 'protect-existing-expiry', $timestamp, $d, $t )->text(), 00396 'existing', 00397 $this->mExpirySelection[$action] == 'existing' 00398 ) . "\n"; 00399 } 00400 00401 $expiryFormOptions .= Xml::option( 00402 wfMessage( 'protect-othertime-op' )->text(), 00403 "othertime" 00404 ) . "\n"; 00405 foreach( explode(',', $scExpiryOptions) as $option ) { 00406 if ( strpos($option, ":") === false ) { 00407 $show = $value = $option; 00408 } else { 00409 list($show, $value) = explode(":", $option); 00410 } 00411 $show = htmlspecialchars($show); 00412 $value = htmlspecialchars($value); 00413 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n"; 00414 } 00415 # Add expiry dropdown 00416 if( $showProtectOptions && !$this->disabled ) { 00417 $out .= " 00418 <table><tr> 00419 <td class='mw-label'> 00420 {$mProtectexpiry} 00421 </td> 00422 <td class='mw-input'>" . 00423 Xml::tags( 'select', 00424 array( 00425 'id' => "mwProtectExpirySelection-$action", 00426 'name' => "wpProtectExpirySelection-$action", 00427 'onchange' => "ProtectionForm.updateExpiryList(this)", 00428 'tabindex' => '2' ) + $this->disabledAttrib, 00429 $expiryFormOptions ) . 00430 "</td> 00431 </tr></table>"; 00432 } 00433 # Add custom expiry field 00434 $attribs = array( 'id' => "mwProtect-$action-expires", 00435 'onkeyup' => 'ProtectionForm.updateExpiry(this)', 00436 'onchange' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib; 00437 $out .= "<table><tr> 00438 <td class='mw-label'>" . 00439 $mProtectother . 00440 '</td> 00441 <td class="mw-input">' . 00442 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) . 00443 '</td> 00444 </tr></table>'; 00445 $out .= "</td></tr>" . 00446 Xml::closeElement( 'table' ) . 00447 Xml::closeElement( 'fieldset' ) . 00448 "</td></tr>"; 00449 } 00450 # Give extensions a chance to add items to the form 00451 wfRunHooks( 'ProtectionForm::buildForm', array($this->mArticle,&$out) ); 00452 00453 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00454 00455 // JavaScript will add another row with a value-chaining checkbox 00456 if( $this->mTitle->exists() ) { 00457 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) . 00458 Xml::openElement( 'tbody' ); 00459 $out .= '<tr> 00460 <td></td> 00461 <td class="mw-input">' . 00462 Xml::checkLabel( 00463 wfMessage( 'protect-cascade' )->text(), 00464 'mwProtect-cascade', 00465 'mwProtect-cascade', 00466 $this->mCascade, $this->disabledAttrib 00467 ) . 00468 "</td> 00469 </tr>\n"; 00470 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00471 } 00472 00473 # Add manual and custom reason field/selects as well as submit 00474 if( !$this->disabled ) { 00475 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) . 00476 Xml::openElement( 'tbody' ); 00477 $out .= " 00478 <tr> 00479 <td class='mw-label'> 00480 {$mProtectreasonother} 00481 </td> 00482 <td class='mw-input'> 00483 {$reasonDropDown} 00484 </td> 00485 </tr> 00486 <tr> 00487 <td class='mw-label'> 00488 {$mProtectreason} 00489 </td> 00490 <td class='mw-input'>" . 00491 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text', 00492 'id' => 'mwProtect-reason', 'maxlength' => 180 ) ) . 00493 // Limited maxlength as the database trims at 255 bytes and other texts 00494 // chosen by dropdown menus on this page are also included in this database field. 00495 // The byte limit of 180 bytes is enforced in javascript 00496 "</td> 00497 </tr>"; 00498 # Disallow watching is user is not logged in 00499 if( $wgUser->isLoggedIn() ) { 00500 $out .= " 00501 <tr> 00502 <td></td> 00503 <td class='mw-input'>" . 00504 Xml::checkLabel( wfMessage( 'watchthis' )->text(), 00505 'mwProtectWatch', 'mwProtectWatch', 00506 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) . 00507 "</td> 00508 </tr>"; 00509 } 00510 $out .= " 00511 <tr> 00512 <td></td> 00513 <td class='mw-submit'>" . 00514 Xml::submitButton( 00515 wfMessage( 'confirm' )->text(), 00516 array( 'id' => 'mw-Protect-submit' ) 00517 ) . 00518 "</td> 00519 </tr>\n"; 00520 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); 00521 } 00522 $out .= Xml::closeElement( 'fieldset' ); 00523 00524 if ( $wgUser->isAllowed( 'editinterface' ) ) { 00525 $title = Title::makeTitle( NS_MEDIAWIKI, 'Protect-dropdown' ); 00526 $link = Linker::link( 00527 $title, 00528 wfMessage( 'protect-edit-reasonlist' )->escaped(), 00529 array(), 00530 array( 'action' => 'edit' ) 00531 ); 00532 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>'; 00533 } 00534 00535 if ( !$this->disabled ) { 00536 $out .= Html::hidden( 'wpEditToken', $wgUser->getEditToken( array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ); 00537 $out .= Xml::closeElement( 'form' ); 00538 $wgOut->addScript( $this->buildCleanupScript() ); 00539 } 00540 00541 return $out; 00542 } 00543 00551 function buildSelector( $action, $selected ) { 00552 global $wgRestrictionLevels, $wgUser; 00553 00554 $levels = array(); 00555 foreach( $wgRestrictionLevels as $key ) { 00556 //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 00557 if( $key == 'sysop' ) { 00558 //special case, rewrite sysop to protect and editprotected 00559 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) && !$this->disabled ) 00560 continue; 00561 } else { 00562 if( !$wgUser->isAllowed($key) && !$this->disabled ) 00563 continue; 00564 } 00565 $levels[] = $key; 00566 } 00567 00568 $id = 'mwProtect-level-' . $action; 00569 $attribs = array( 00570 'id' => $id, 00571 'name' => $id, 00572 'size' => count( $levels ), 00573 'onchange' => 'ProtectionForm.updateLevels(this)', 00574 ) + $this->disabledAttrib; 00575 00576 $out = Xml::openElement( 'select', $attribs ); 00577 foreach( $levels as $key ) { 00578 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected ); 00579 } 00580 $out .= Xml::closeElement( 'select' ); 00581 return $out; 00582 } 00583 00590 private function getOptionLabel( $permission ) { 00591 if( $permission == '' ) { 00592 return wfMessage( 'protect-default' )->text(); 00593 } else { 00594 $msg = wfMessage( "protect-level-{$permission}" ); 00595 if( $msg->exists() ) { 00596 return $msg->text(); 00597 } 00598 return wfMessage( 'protect-fallback', $permission )->text(); 00599 } 00600 } 00601 00602 function buildCleanupScript() { 00603 global $wgRestrictionLevels, $wgGroupPermissions, $wgOut; 00604 00605 $cascadeableLevels = array(); 00606 foreach( $wgRestrictionLevels as $key ) { 00607 if ( ( isset( $wgGroupPermissions[$key]['protect'] ) && $wgGroupPermissions[$key]['protect'] ) 00608 || $key == 'protect' 00609 ) { 00610 $cascadeableLevels[] = $key; 00611 } 00612 } 00613 $options = array( 00614 'tableId' => 'mwProtectSet', 00615 'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(), 00616 'numTypes' => count( $this->mApplicableTypes ), 00617 'existingMatch' => count( array_unique( $this->mExistingExpiry ) ) === 1, 00618 ); 00619 00620 $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels ); 00621 $script = Xml::encodeJsCall( 'ProtectionForm.init', array( $options ) ); 00622 return Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ); 00623 } 00624 00631 function showLogExtract( &$out ) { 00632 # Show relevant lines from the protection log: 00633 $protectLogPage = new LogPage( 'protect' ); 00634 $out->addHTML( Xml::element( 'h2', null, $protectLogPage->getName()->text() ) ); 00635 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle ); 00636 # Let extensions add other relevant log extracts 00637 wfRunHooks( 'ProtectionForm::showLogExtract', array($this->mArticle,$out) ); 00638 } 00639 }