Convert mediawiki.toc and mediawiki.user to using mw.cookie

* Remove redundant 'path' parameter (handled by mw.cookie)
* Remove redundant 'expires' parameter (handled by mw.cookie)
* Return value for absent cookie is now reliably 'null'.

This changes the cookie name due to mw.cookie adding the standard
cookie prefix. This will cause existing values to be lost. Make
use of this oppertunity to rename some cookie names.

* mw_hidetoc -> {wikiprefix} hidetoc
* mediaWiki.user.sessionId -> {wikiprefix} mwuser-session
* mediaWiki.user.bucket -> {wikiprefix} mwuser-bucket

Bug: T67384
Change-Id: I835063f40f742a56f8d137cbaabc77e51c60a2a9
This commit is contained in:
Yaroslav Melnychuk 2014-12-02 17:58:43 +02:00 committed by Krinkle
parent 2e81697565
commit a4d3d3b427
5 changed files with 16 additions and 23 deletions

View file

@ -209,7 +209,6 @@ return array(
'styles' => 'resources/src/jquery/jquery.confirmable.css',
'dependencies' => 'mediawiki.jqueryMsg',
),
// Use mediawiki.cookie in new code, rather than jquery.cookie.
'jquery.cookie' => array(
'scripts' => 'resources/lib/jquery/jquery.cookie.js',
'targets' => array( 'desktop', 'mobile' ),
@ -1030,7 +1029,7 @@ return array(
),
'mediawiki.toc' => array(
'scripts' => 'resources/src/mediawiki/mediawiki.toc.js',
'dependencies' => 'jquery.cookie',
'dependencies' => 'mediawiki.cookie',
'messages' => array( 'showtoc', 'hidetoc' ),
'targets' => array( 'desktop', 'mobile' ),
),
@ -1042,7 +1041,7 @@ return array(
'mediawiki.user' => array(
'scripts' => 'resources/src/mediawiki/mediawiki.user.js',
'dependencies' => array(
'jquery.cookie',
'mediawiki.cookie',
'mediawiki.api',
'user.options',
'user.tokens',

View file

@ -16,7 +16,7 @@
mw.cookie = {
/**
* Sets or deletes a cookie.
* Set or deletes a cookie.
*
* While this is natural in JavaScript, contrary to `WebResponse#setcookie` in PHP, the
* default values for the `options` properties only apply if that property isn't set
@ -101,13 +101,13 @@
},
/**
* Gets the value of a cookie.
* Get the value of a cookie.
*
* @param {string} key
* @param {string} [prefix=wgCookiePrefix] The prefix of the key. If `prefix` is
* `undefined` or `null`, then `wgCookiePrefix` is used
* @param {Mixed} [defaultValue=null]
* @return {string} If the cookie exists, then the value of the
* @return {string|null|Mixed} If the cookie exists, then the value of the
* cookie, otherwise `defaultValue`
*/
get: function ( key, prefix, defaultValue ) {

View file

@ -15,25 +15,19 @@
$tocList.slideDown( 'fast' );
$tocToggleLink.text( mw.msg( 'hidetoc' ) );
$toc.removeClass( 'tochidden' );
$.cookie( 'mw_hidetoc', null, {
expires: 30,
path: '/'
} );
mw.cookie.set( 'hidetoc', null );
} else {
$tocList.slideUp( 'fast' );
$tocToggleLink.text( mw.msg( 'showtoc' ) );
$toc.addClass( 'tochidden' );
$.cookie( 'mw_hidetoc', '1', {
expires: 30,
path: '/'
} );
mw.cookie.set( 'hidetoc', '1' );
}
}
// Only add it if there is a complete TOC and it doesn't
// have a toggle added already
if ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
hideToc = $.cookie( 'mw_hidetoc' ) === '1';
hideToc = mw.cookie.get( 'hidetoc' ) === '1';
$tocToggleLink = $( '<a href="#" id="togglelink"></a>' )
.text( hideToc ? mw.msg( 'showtoc' ) : mw.msg( 'hidetoc' ) )

View file

@ -159,10 +159,10 @@
* @return {string} Random session ID
*/
sessionId: function () {
var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
if ( sessionId === undefined || sessionId === null ) {
var sessionId = mw.cookie.get( 'mwuser-session' );
if ( sessionId === null ) {
sessionId = mw.user.generateRandomSessionId();
$.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
mw.cookie.set( 'mwuser-session', sessionId, { expires: null } );
}
return sessionId;
},
@ -208,7 +208,7 @@
expires: 30
}, options || {} );
cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
cookie = mw.cookie.get( 'mwuser-bucket:' + key );
// Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
@ -245,10 +245,10 @@
}
}
$.cookie(
'mediaWiki.user.bucket:' + key,
mw.cookie.set(
'mwuser-bucket:' + key,
version + ':' + bucket,
{ path: '/', expires: Number( options.expires ) }
{ expires: Number( options.expires ) * 86400 }
);
}

View file

@ -1,7 +1,7 @@
( function ( mw, $ ) {
QUnit.module( 'mediawiki.toc', QUnit.newMwEnvironment( {
setup: function () {
// Prevent live cookies like mw_hidetoc=1 from interferring with the test
// Prevent live cookies from interferring with the test
this.stub( $, 'cookie' ).returns( null );
}
} ) );