Convert mediawiki.toc and mediawiki.user to using mw.cookie
authorYaroslav Melnychuk <yaroslavmelnuchuk@gmail.com>
Tue, 2 Dec 2014 15:58:43 +0000 (17:58 +0200)
committerGilles Dubuc <gdubuc@wikimedia.org>
Wed, 10 Jun 2015 09:11:01 +0000 (11:11 +0200)
* 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-sessionId
* mediaWiki.user.bucket -> {wikiprefix} mwuser-bucket

This is a re-submission of a4d3d3b427713, which was reverted due
to T101857. Commit amended to use "sessionId" instead of "session".

Bug: T67384
Change-Id: Ibe88778cf3b6db90b3875c89305ffba53ac84104

resources/Resources.php
resources/src/mediawiki/mediawiki.cookie.js
resources/src/mediawiki/mediawiki.toc.js
resources/src/mediawiki/mediawiki.user.js
tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js

index 644ff9c..43a75a2 100644 (file)
@@ -218,7 +218,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' ),
@@ -1058,7 +1057,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' ),
        ),
@@ -1070,7 +1069,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',
index 8f091e4..d260fca 100644 (file)
@@ -16,7 +16,7 @@
        mw.cookie = {
 
                /**
-                * Sets or deletes a cookie.
+                * Set or delete 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
                },
 
                /**
-                * 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 ) {
index 45338ea..78627fc 100644 (file)
                                $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' ) )
index 817c856..ec34ce6 100644 (file)
                 * @return {string} Random session ID
                 */
                sessionId: function () {
-                       var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
-                       if ( sessionId === undefined || sessionId === null ) {
+                       var sessionId = mw.cookie.get( 'mwuser-sessionId' );
+                       if ( sessionId === null ) {
                                sessionId = mw.user.generateRandomSessionId();
-                               $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
+                               mw.cookie.set( 'mwuser-sessionId', sessionId, { expires: null } );
                        }
                        return sessionId;
                },
                                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 ) {
                                        }
                                }
 
-                               $.cookie(
-                                       'mediaWiki.user.bucket:' + key,
+                               mw.cookie.set(
+                                       'mwuser-bucket:' + key,
                                        version + ':' + bucket,
-                                       { path: '/', expires: Number( options.expires ) }
+                                       { expires: Number( options.expires ) * 86400 }
                                );
                        }
 
index e43516b..89eb45f 100644 (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 );
                }
        } ) );