* fixing DismissSiteNotice so that it works properly with javascript-disabled browsers
authorRyan Schmidt <skizzerz@users.mediawiki.org>
Sun, 5 Oct 2008 21:28:11 +0000 (21:28 +0000)
committerRyan Schmidt <skizzerz@users.mediawiki.org>
Sun, 5 Oct 2008 21:28:11 +0000 (21:28 +0000)
* make DismissSiteNotice honor cookie configuration variables
* adding missing name to CREDITS

CREDITS
includes/GlobalFunctions.php
includes/SpecialPage.php
includes/specials/SpecialDismissnotice.php [new file with mode: 0644]
languages/messages/MessagesEn.php

diff --git a/CREDITS b/CREDITS
index 2c418e1..c91eb79 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -18,6 +18,7 @@ following names for their contribution to the product.
 * Greg Sabino Mullane
 * Guy Van den Broeck
 * Hojjat
+* Jack Phoenix
 * Jon Harald Søby
 * Leon Weber
 * Matt Johnston
index 2402204..6034237 100644 (file)
@@ -1864,11 +1864,14 @@ function wfGetNamespaceNotice() {
  * @return $siteNotice The site-wide notice as set by $wgSiteNotice or MediaWiki:Sitenotice interface message
  */
 function wfGetSiteNotice() {
-       global $wgUser, $wgSiteNotice, $wgMajorSiteNoticeID;
+       global $wgUser, $wgMajorSiteNoticeID, $wgTitle;
+       global $wgCookiePrefix, $wgCookieExpiration, $wgCookiePath;
        $fname = 'wfGetSiteNotice';
        wfProfileIn( $fname );
        $siteNotice = '';
        $loggedIn = false;
+       $spTitle = SpecialPage::getTitleFor('DismissNotice');
+       $spUrl = $spTitle->getFullURL( array( 'returnto' => $wgTitle->getPrefixedURL() ) );
        
        if( $wgUser instanceOf User && $wgUser->isLoggedIn() ) {
                $loggedIn = true;
@@ -1884,53 +1887,35 @@ function wfGetSiteNotice() {
                }
        }
        
-       $encNotice = Xml::escapeJsString($siteNotice);
-       $encClose = Xml::escapeJsString( wfMsg( 'sitenotice_close' ) );
+       $msgClose = wfMsg( 'sitenotice_close' );
        $id = intval( $wgMajorSiteNoticeID ) . "." . intval( wfMsgForContent( 'sitenotice_id' ) );
 
        if( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice ) ) ) {
                if( $loggedIn ) {
+                       //it is hidden
+                       if( isset($_COOKIE[$wgCookiePrefix . 'DismissSiteNotice']) && $_COOKIE[$wgCookiePrefix . 'DismissSiteNotice'] == $id )
+                               return '';
                        $siteNotice = <<<EOT
+<table width="100%" id="mw-dismissable-notice"><tr><td width="80%">$siteNotice</td>
+<td width="20%" align="right">[<a id="dismissLink" href="$spUrl">$msgClose</a>]</td></tr></table>
 <script type="text/javascript" language="JavaScript">
 <!--
-var cookieName = "dismissSiteNotice=";
+var cookieName = "{$wgCookiePrefix}DismissSiteNotice=";
 var cookiePos = document.cookie.indexOf(cookieName);
-var siteNoticeID = "$id";
-var siteNoticeValue = "$encNotice";
-var cookieValue = "";
-var msgClose = "$encClose";
-
-if (cookiePos > -1) {
-       cookiePos = cookiePos + cookieName.length;
-       var endPos = document.cookie.indexOf(";", cookiePos);
-       if (endPos > -1) {
-               cookieValue = document.cookie.substring(cookiePos, endPos);
-       } else {
-               cookieValue = document.cookie.substring(cookiePos);
-       }
-}
-if (cookieValue != siteNoticeID) {
-       function dismissNotice() {
+var siteNoticeID = "{$id}";
+
+var dismissLink = document.getElementById('dismissLink');
+dismissLink.href = "javascript:dismissNotice();";
+
+function dismissNotice() {
                var date = new Date();
-               date.setTime(date.getTime() + 30*86400*1000);
-               document.cookie = cookieName + siteNoticeID + "; expires="+date.toGMTString() + "; path=/";
+               date.setTime(date.getTime() + {$wgCookieExpiration});
+               document.cookie = cookieName + siteNoticeID + "; expires="+date.toGMTString() + "; path={$wgCookiePath}";
                var element = document.getElementById('siteNotice');
                element.parentNode.removeChild(element);
-       }
-       document.writeln('<table width="100%" id="mw-dismissable-notice"><tr><td width="80%">'+siteNoticeValue+'</td>');
-       document.writeln('<td width="20%" align="right">[<a href="javascript:dismissNotice();">'+msgClose+'</a>]</td></tr></table>');
 }
 -->
 </script>
-EOT;
-               } else {
-                       // Don't allow anons to dismiss the site notice
-                       $siteNotice = <<<EOT
-<script type="text/javascript" language="JavaScript">
-<!--
-document.writeln("$encNotice");
--->
-</script>
 EOT;
                }
                if( !$siteNotice ) {
index cf16546..37650bb 100644 (file)
@@ -157,7 +157,8 @@ class SpecialPage
                'Randomredirect'            => 'SpecialRandomredirect',
                'Withoutinterwiki'          => array( 'SpecialPage', 'Withoutinterwiki' ),
                'Filepath'                  => array( 'SpecialPage', 'Filepath' ),
-
+               'Dismissnotice'                         => array( 'UnlistedSpecialPage', 'Dismissnotice' ),
+               
                'Mypage'                    => array( 'SpecialMypage' ),
                'Mytalk'                    => array( 'SpecialMytalk' ),
                'Mycontributions'           => array( 'SpecialMycontributions' ),
diff --git a/includes/specials/SpecialDismissnotice.php b/includes/specials/SpecialDismissnotice.php
new file mode 100644 (file)
index 0000000..6099a87
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+function wfSpecialDismissnotice() {
+       global $wgOut, $wgTitle, $wgUser, $wgRequest, $wgMajorSiteNoticeID;
+       global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain, $wgCookieExpiration;
+       
+       # Logged-out users cannot dismiss notice
+       if($wgUser->isAnon()) {
+               $wgOut->addWikiText( wfMsg('dismissnotice-nologin') );
+               return;
+       }
+       
+       # Set the cookie and redirect back to where they came from (or Main Page if they just typed it in the URL)
+       $id = intval( $wgMajorSiteNoticeID ) . "." . intval( wfMsgForContent( 'sitenotice_id' ) );
+       #not using WebResponse's setcookie method because the cookie cannot be httpOnly
+       setcookie( $wgCookiePrefix . 'DismissSiteNotice',
+               $id,
+               time() + $wgCookieExpiration,
+               $wgCookiePath,
+               $wgCookieDomain,
+               false,
+               false
+       );
+       
+       $titleObj = Title::newFromText( $wgRequest->getVal('returnto') );
+       if ( !$titleObj instanceof Title ) {
+               $titleObj = Title::newMainPage();
+       }
+
+       $wgOut->redirect( $titleObj->getFullURL() );
+}
\ No newline at end of file
index 24ee31b..73293d9 100644 (file)
@@ -441,6 +441,7 @@ $specialPageAliases = array(
        'LinkSearch'                => array( 'LinkSearch' ),
        'DeletedContributions'      => array( 'DeletedContributions' ),
        'Nuke'                      => array( 'Nuke' ),
+       'DismissNotice'                         => array( 'DismissNotice' ),
 );
 
 /**
@@ -3687,4 +3688,7 @@ Input the username or IP to get a list of pages to delete.',
 'nuke-submit-delete' => 'Delete selected',
 'right-nuke'         => 'Mass delete pages',
 
+# Special:DismissNotice
+'dismissnotice'         => 'Dismiss site notice',
+'dismissnotice-nologin' => 'You must be [[Special:UserLogin|logged in]] to dismiss the site notice.',
 );