123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?php
- class ProtectionForm {
-
- var $mRestrictions = array();
-
- var $mReason = '';
-
- var $mReasonSelection = '';
-
- var $mCascade = false;
-
- var $mExpiry = array();
-
- var $mExpirySelection = array();
-
- var $mPermErrors = array();
-
- var $mApplicableTypes = array();
-
- var $mExistingExpiry = array();
- function __construct( Article $article ) {
- global $wgRequest, $wgUser;
- global $wgRestrictionTypes, $wgRestrictionLevels;
- $this->mArticle = $article;
- $this->mTitle = $article->mTitle;
- $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
- $this->mCascade = $this->mTitle->areRestrictionsCascading();
-
- $this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser);
- $this->disabled = wfReadOnly() || $this->mPermErrors != array();
- $this->disabledAttrib = $this->disabled
- ? array( 'disabled' => 'disabled' )
- : array();
- $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
- $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' );
- $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
- foreach( $this->mApplicableTypes as $action ) {
-
-
- $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
- if ( !$this->mRestrictions[$action] ) {
-
- $existingExpiry = '';
- } else {
- $existingExpiry = $this->mTitle->getRestrictionExpiry( $action );
- }
- $this->mExistingExpiry[$action] = $existingExpiry;
- $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
- $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
- if ( $requestExpiry ) {
-
- $this->mExpiry[$action] = $requestExpiry;
- $this->mExpirySelection[$action] = 'othertime';
- } elseif ( $requestExpirySelection ) {
-
- $this->mExpiry[$action] = '';
- $this->mExpirySelection[$action] = $requestExpirySelection;
- } elseif ( $existingExpiry == 'infinity' ) {
-
- $this->mExpiry[$action] = '';
- $this->mExpirySelection[$action] = 'infinite';
- } elseif ( $existingExpiry ) {
-
- $this->mExpiry[$action] = '';
- $this->mExpirySelection[$action] = $existingExpiry;
- } else {
-
- $this->mExpiry[$action] = '';
- $this->mExpirySelection[$action] = 'infinite';
- }
- $val = $wgRequest->getVal( "mwProtect-level-$action" );
- if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
-
- if( $val == 'sysop' ) {
-
- if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
- continue;
- } else {
- if( !$wgUser->isAllowed($val) )
- continue;
- }
- $this->mRestrictions[$action] = $val;
- }
- }
- }
-
- function getExpiry( $action ) {
- if ( $this->mExpirySelection[$action] == 'existing' ) {
- return $this->mExistingExpiry[$action];
- } elseif ( $this->mExpirySelection[$action] == 'othertime' ) {
- $value = $this->mExpiry[$action];
- } else {
- $value = $this->mExpirySelection[$action];
- }
- if ( $value == 'infinite' || $value == 'indefinite' || $value == 'infinity' ) {
- $time = Block::infinity();
- } else {
- $unix = strtotime( $value );
- if ( !$unix || $unix === -1 ) {
- return false;
- }
-
-
- $time = wfTimestamp( TS_MW, $unix );
- }
- return $time;
- }
- function execute() {
- global $wgRequest, $wgOut;
- if( $wgRequest->wasPosted() ) {
- if( $this->save() ) {
- $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
- $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
- }
- } else {
- $this->show();
- }
- }
- function show( $err = null ) {
- global $wgOut, $wgUser;
- $wgOut->setRobotPolicy( 'noindex,nofollow' );
- if( is_null( $this->mTitle ) ||
- $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
- $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
- return;
- }
- list( $cascadeSources, ) = $this->mTitle->getCascadeProtectionSources();
- if ( "" != $err ) {
- $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
- $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
- }
- if ( $cascadeSources && count($cascadeSources) > 0 ) {
- $titles = '';
- foreach ( $cascadeSources as $title ) {
- $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
- }
- $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
- }
- $sk = $wgUser->getSkin();
- $titleLink = $sk->makeLinkObj( $this->mTitle );
- $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
- $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
-
-
- if( $this->disabled ) {
- if( wfReadOnly() ) {
- $wgOut->readOnlyPage();
- } elseif( $this->mPermErrors ) {
- $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
- }
- } else {
- $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
- }
- $wgOut->addHTML( $this->buildForm() );
- $this->showLogExtract( $wgOut );
- }
- function save() {
- global $wgRequest, $wgUser, $wgOut;
-
- if ( $this->disabled ) {
- $this->show();
- return false;
- }
- $token = $wgRequest->getVal( 'wpEditToken' );
- if ( !$wgUser->matchEditToken( $token ) ) {
- $this->show( wfMsg( 'sessionfailure' ) );
- return false;
- }
-
-
- $reasonstr = $this->mReasonSelection;
- if ( $reasonstr != 'other' && $this->mReason != '' ) {
-
- $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->mReason;
- } elseif ( $reasonstr == 'other' ) {
- $reasonstr = $this->mReason;
- }
- $expiry = array();
- foreach( $this->mApplicableTypes as $action ) {
- $expiry[$action] = $this->getExpiry( $action );
- if( empty($this->mRestrictions[$action]) )
- continue;
- if ( !$expiry[$action] ) {
- $this->show( wfMsg( 'protect_expiry_invalid' ) );
- return false;
- }
- if ( $expiry[$action] < wfTimestampNow() ) {
- $this->show( wfMsg( 'protect_expiry_old' ) );
- return false;
- }
- }
-
-
- global $wgGroupPermissions;
- $edit_restriction = $this->mRestrictions['edit'];
- $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
- if ($this->mCascade && ($edit_restriction != 'protect') &&
- !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
- $this->mCascade = false;
- if ($this->mTitle->exists()) {
- $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
- } else {
- $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry['create'] );
- }
- if( !$ok ) {
- throw new FatalError( "Unknown error at restriction save time." );
- }
- if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
- $this->mArticle->doWatch();
- } elseif( $this->mTitle->userIsWatching() ) {
- $this->mArticle->doUnwatch();
- }
- return $ok;
- }
-
- function buildForm() {
- global $wgUser, $wgLang;
- $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
- $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
- $out = '';
- if( !$this->disabled ) {
- $out .= $this->buildScript();
- $out .= Xml::openElement( 'form', array( 'method' => 'post',
- 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
- 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
- $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
- }
- $out .= Xml::openElement( 'fieldset' ) .
- Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
- Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
- Xml::openElement( 'tbody' );
- foreach( $this->mRestrictions as $action => $selected ) {
-
- $msg = wfMsg( 'restriction-' . $action );
- if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
- $msg = $action;
- }
- $out .= "<tr><td>".
- Xml::openElement( 'fieldset' ) .
- Xml::element( 'legend', null, $msg ) .
- Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
- "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
- $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
- wfMsgForContent( 'protect-dropdown' ),
- wfMsgForContent( 'protect-otherreason-op' ),
- $this->mReasonSelection,
- 'mwProtect-reason', 4 );
- $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
- $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
- $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
- $mProtectother = Xml::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
- $expiryFormOptions = '';
- if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) {
- $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action] );
- $d = $wgLang->date( $this->mExistingExpiry[$action] );
- $t = $wgLang->time( $this->mExistingExpiry[$action] );
- $expiryFormOptions .=
- Xml::option(
- wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
- 'existing',
- $this->mExpirySelection[$action] == 'existing'
- ) . "\n";
- }
-
- $expiryFormOptions .= Xml::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
- foreach( explode(',', $scExpiryOptions) as $option ) {
- if ( strpos($option, ":") === false ) {
- $show = $value = $option;
- } else {
- list($show, $value) = explode(":", $option);
- }
- $show = htmlspecialchars($show);
- $value = htmlspecialchars($value);
- $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n";
- }
-
- if( $showProtectOptions && !$this->disabled ) {
- $out .= "
- <table><tr>
- <td class='mw-label'>
- {$mProtectexpiry}
- </td>
- <td class='mw-input'>" .
- Xml::tags( 'select',
- array(
- 'id' => "mwProtectExpirySelection-$action",
- 'name' => "wpProtectExpirySelection-$action",
- 'onchange' => "ProtectionForm.updateExpiryList(this)",
- 'tabindex' => '2' ) + $this->disabledAttrib,
- $expiryFormOptions ) .
- "</td>
- </tr></table>";
- }
-
- $attribs = array( 'id' => "mwProtect-$action-expires",
- 'onkeyup' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib;
- $out .= "<table><tr>
- <td class='mw-label'>" .
- $mProtectother .
- '</td>
- <td class="mw-input">' .
- Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
- '</td>
- </tr></table>';
- $out .= "</td></tr>" .
- Xml::closeElement( 'table' ) .
- Xml::closeElement( 'fieldset' ) .
- "</td></tr>";
- }
- $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
-
- if( $this->mTitle->exists() ) {
- $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
- Xml::openElement( 'tbody' );
- $out .= '<tr>
- <td></td>
- <td class="mw-input">' .
- Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
- $this->mCascade, $this->disabledAttrib ) .
- "</td>
- </tr>\n";
- $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
- }
-
-
- if( !$this->disabled ) {
- $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
- Xml::openElement( 'tbody' );
- $out .= "
- <tr>
- <td class='mw-label'>
- {$mProtectreasonother}
- </td>
- <td class='mw-input'>
- {$reasonDropDown}
- </td>
- </tr>
- <tr>
- <td class='mw-label'>
- {$mProtectreason}
- </td>
- <td class='mw-input'>" .
- Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
- 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
- "</td>
- </tr>
- <tr>
- <td></td>
- <td class='mw-input'>" .
- Xml::checkLabel( wfMsg( 'watchthis' ),
- 'mwProtectWatch', 'mwProtectWatch',
- $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
- "</td>
- </tr>
- <tr>
- <td></td>
- <td class='mw-submit'>" .
- Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
- "</td>
- </tr>\n";
- $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
- }
- $out .= Xml::closeElement( 'fieldset' );
- if ( $wgUser->isAllowed( 'editinterface' ) ) {
- $linkTitle = Title::makeTitleSafe( NS_MEDIAWIKI, 'protect-dropdown' );
- $link = $wgUser->getSkin()->Link ( $linkTitle, wfMsgHtml( 'protect-edit-reasonlist' ) );
- $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
- }
- if ( !$this->disabled ) {
- $out .= Xml::closeElement( 'form' ) .
- $this->buildCleanupScript();
- }
- return $out;
- }
- function buildSelector( $action, $selected ) {
- global $wgRestrictionLevels, $wgUser;
- $levels = array();
- foreach( $wgRestrictionLevels as $key ) {
-
- if( $key == 'sysop' ) {
-
- if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
- continue;
- } else {
- if( !$wgUser->isAllowed($key) && !$this->disabled )
- continue;
- }
- $levels[] = $key;
- }
- $id = 'mwProtect-level-' . $action;
- $attribs = array(
- 'id' => $id,
- 'name' => $id,
- 'size' => count( $levels ),
- 'onchange' => 'ProtectionForm.updateLevels(this)',
- ) + $this->disabledAttrib;
- $out = Xml::openElement( 'select', $attribs );
- foreach( $levels as $key ) {
- $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
- }
- $out .= Xml::closeElement( 'select' );
- return $out;
- }
-
- private function getOptionLabel( $permission ) {
- if( $permission == '' ) {
- return wfMsg( 'protect-default' );
- } else {
- $key = "protect-level-{$permission}";
- $msg = wfMsg( $key );
- if( wfEmptyMsg( $key, $msg ) )
- $msg = wfMsg( 'protect-fallback', $permission );
- return $msg;
- }
- }
- function buildScript() {
- global $wgStylePath, $wgStyleVersion;
- return Xml::tags( 'script', array(
- 'type' => 'text/javascript',
- 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
- }
- function buildCleanupScript() {
- global $wgRestrictionLevels, $wgGroupPermissions;
- $script = 'var wgCascadeableLevels=';
- $CascadeableLevels = array();
- foreach( $wgRestrictionLevels as $key ) {
- if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
- $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
- }
- }
- $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
- $options = (object)array(
- 'tableId' => 'mw-protect-table-move',
- 'labelText' => wfMsg( 'protect-unchain' ),
- 'numTypes' => count($this->mApplicableTypes),
- 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry ) ),
- );
- $encOptions = Xml::encodeJsVar( $options );
- $script .= "ProtectionForm.init($encOptions)";
- return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
- }
-
- function showLogExtract( &$out ) {
-
- $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
- LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
- }
- }
|