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