aaaand one more
[lhc/web/wiklou.git] / includes / ProtectionForm.php
index e349fba..788b565 100644 (file)
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
- *
- * @addtogroup SpecialPage
  */
 
+/**
+ * @todo document, briefly.
+ * @addtogroup SpecialPage
+ */
 class ProtectionForm {
        var $mRestrictions = array();
        var $mReason = '';
@@ -28,7 +30,7 @@ class ProtectionForm {
        var $mExpiry = null;
 
        function __construct( &$article ) {
-               global $wgRequest, $wgUser, $wgLang;
+               global $wgRequest, $wgUser;
                global $wgRestrictionTypes, $wgRestrictionLevels;
                $this->mArticle =& $article;
                $this->mTitle =& $article->mTitle;
@@ -49,7 +51,7 @@ class ProtectionForm {
                        } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
                                $this->mExpiry = '';
                        } else {
-                               $this->mExpiry = $wgLang->timeanddate( $this->mTitle->mRestrictionsExpiry );
+                               $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
                        }
                }
 
@@ -70,17 +72,23 @@ class ProtectionForm {
                                        $this->mRestrictions[$action] = $val;
                                }
                        }
-
+               }
+       }
+       
+       function execute() {
+               global $wgRequest;
+               if( $wgRequest->wasPosted() ) {
                        if( $this->save() ) {
                                global $wgOut;
                                $wgOut->redirect( $this->mTitle->getFullUrl() );
-                               return;
                        }
+               } else {
+                       $this->show();
                }
        }
 
        function show( $err = null ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
@@ -91,7 +99,7 @@ class ProtectionForm {
                        return;
                }
 
-               $cascadeSources = $this->mTitle->getCascadeProtectionSources();
+               list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
 
                if ( "" != $err ) {
                        $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
@@ -105,7 +113,7 @@ class ProtectionForm {
                                $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
                        }
 
-                       $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
+                       $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
 
                        $wgOut->addWikiText( $notice );
                }
@@ -113,9 +121,25 @@ class ProtectionForm {
                $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
                $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
 
-               $wgOut->addWikiText(
-                       wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
-                               wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
+               # Show an appropriate message if the user isn't allowed or able to change
+               # the protection settings at this time
+               if( $this->disabled ) {
+                       if( $wgUser->isAllowed( 'protect' ) ) {
+                               if( $wgUser->isBlocked() ) {
+                                       # Blocked
+                                       $message = 'protect-locked-blocked';
+                               } else {
+                                       # Database lock
+                                       $message = 'protect-locked-dblock';
+                               }
+                       } else {
+                               # Permission error
+                               $message = 'protect-locked-access';
+                       }
+               } else {
+                       $message = 'protect-text';
+               }
+               $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
 
                $wgOut->addHTML( $this->buildForm() );
 
@@ -124,17 +148,16 @@ class ProtectionForm {
 
        function save() {
                global $wgRequest, $wgUser, $wgOut;
-               if( !$wgRequest->wasPosted() ) {
-                       return false;
-               }
-
+               
                if( $this->disabled ) {
+                       $this->show();
                        return false;
                }
 
                $token = $wgRequest->getVal( 'wpEditToken' );
                if( !$wgUser->matchEditToken( $token ) ) {
-                       throw new FatalError( wfMsg( 'sessionfailure' ) );
+                       $this->show( wfMsg( 'sessionfailure' ) );
+                       return false;
                }
 
                if ( strlen( $this->mExpiry ) == 0 ) {
@@ -149,10 +172,16 @@ class ProtectionForm {
 
                        if ( $expiry < 0 || $expiry === false ) {
                                $this->show( wfMsg( 'protect_expiry_invalid' ) );
-                               return;
+                               return false;
                        }
 
                        $expiry = wfTimestamp( TS_MW, $expiry );
+
+                       if ( $expiry < wfTimestampNow() ) {
+                               $this->show( wfMsg( 'protect_expiry_old' ) );
+                               return false;
+                       }
+
                }
 
                $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
@@ -171,6 +200,7 @@ class ProtectionForm {
                        // The submission needs to reenable the move permission selector
                        // if it's in locked mode, or some browsers won't submit the data.
                        $out .= wfOpenElement( 'form', array(
+                               'id' => 'mw-Protect-Form',
                                'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
                                'method' => 'post',
                                'onsubmit' => 'protectEnable(true)' ) );
@@ -202,14 +232,14 @@ class ProtectionForm {
                $out .= "</tbody>\n";
                $out .= "</table>\n";
 
-               $out .= "<table>\n";
-               $out .= "<tbody>\n";
-
                global $wgEnableCascadingProtection;
 
                if ($wgEnableCascadingProtection)
                        $out .= $this->buildCascadeInput();
 
+               $out .= "<table>\n";
+               $out .= "<tbody>\n";
+
                $out .= $this->buildExpiryInput();
 
                if( !$this->disabled ) {
@@ -268,13 +298,13 @@ class ProtectionForm {
                        wfElement( 'input', array(
                                'size' => 60,
                                'name' => $id,
-                               'id' => $id ) );
+                               'id' => $id,
+                               'value' => $this->mReason ) );
        }
 
        function buildCascadeInput() {
                $id = 'mwProtect-cascade';
-               $ci = "<tr>" . wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib) . "</tr>";
-
+               $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
                return $ci;
        }
 
@@ -286,12 +316,12 @@ class ProtectionForm {
                                'id' => "$id-label",
                                'for' => $id ),
                                wfMsg( 'protectexpiry' ) );
-               $ci .= "</td> <td aligh=\"left\">";
+               $ci .= "</td> <td align=\"left\">";
                $ci .= wfElement( 'input', array(
                                'size' => 60,
                                'name' => $id,
                                'id' => $id,
-                               'value' => $this->mExpiry ) );
+                               'value' => $this->mExpiry ) + $this->disabledAttrib );
                $ci .= "</td></tr>";
 
                return $ci;
@@ -299,6 +329,7 @@ class ProtectionForm {
 
        function buildSubmit() {
                return wfElement( 'input', array(
+                       'id' => 'mw-Protect-submit',
                        'type' => 'submit',
                        'value' => wfMsg( 'confirm' ) ) );
        }
@@ -311,8 +342,17 @@ class ProtectionForm {
        }
 
        function buildCleanupScript() {
-               return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
-                       wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
+               global $wgRestrictionLevels, $wgGroupPermissions;
+               $script = 'var wgCascadeableLevels=';
+               $CascadeableLevels = array();
+               foreach( $wgRestrictionLevels as $key ) {
+                       if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
+                               $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
+                       }
+               }
+               $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
+               $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
+               return '<script type="text/javascript">' . $script . '</script>';
        }
 
        /**
@@ -320,7 +360,7 @@ class ProtectionForm {
         * @access private
         */
        function showLogExtract( &$out ) {
-               # Show relevant lines from the deletion log:
+               # Show relevant lines from the protection log:
                $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
                $logViewer = new LogViewer(
                        new LogReader(