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)
committerKrinkle <krinklemail@gmail.com>
Wed, 13 May 2015 19:05:33 +0000 (19:05 +0000)
* 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

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 ae5b3f9..30ab739 100644 (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',
index 8f091e4..0c0095c 100644 (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
                },
 
                /**
-                * 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..c42eb9a 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-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;
                },
                                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 );
                }
        } ) );