4 var config
= require( './config.json' ),
9 expires
: config
.expires
,
14 * Manage cookies in a way that is syntactically and functionally similar
15 * to the `WebRequest#getCookie` and `WebResponse#setcookie` methods in PHP.
17 * @author Sam Smith <samsmith@wikimedia.org>
18 * @author Matthew Flaschen <mflaschen@wikimedia.org>
26 * Set or delete a cookie.
28 * **Note:** If explicitly passing `null` or `undefined` for an options key,
29 * that will override the default. This is natural in JavaScript, but noted
30 * here because it is contrary to MediaWiki's `WebResponse#setcookie()` method
34 * @param {string|null} value Value of cookie. If `value` is `null` then this method will
35 * instead remove a cookie by name of `key`.
36 * @param {Object|Date|number} [options] Options object, or expiry date
37 * @param {Date|number|null} [options.expires=wgCookieExpiration] The expiry date of the cookie,
38 * or lifetime in seconds. If `options.expires` is null or 0, then a session cookie is set.
39 * @param {string} [options.prefix=wgCookiePrefix] The prefix of the key
40 * @param {string} [options.domain=wgCookieDomain] The domain attribute of the cookie
41 * @param {string} [options.path=wgCookiePath] The path attribute of the cookie
42 * @param {boolean} [options.secure=false] Whether or not to include the secure attribute.
43 * (Does **not** use the wgCookieSecure configuration variable)
45 set: function ( key
, value
, options
) {
48 // The 'options' parameter may be a shortcut for the expiry.
49 if ( arguments
.length
> 2 && ( !options
|| options
instanceof Date
|| typeof options
=== 'number' ) ) {
50 options
= { expires
: options
};
53 options
= $.extend( {}, defaults
, options
);
56 key
= options
.prefix
+ key
;
57 // Don't pass invalid option to $.cookie
58 delete options
.prefix
;
60 if ( !options
.expires
) {
61 // Session cookie (null or zero)
62 // Normalize to absent (undefined) for $.cookie.
63 delete options
.expires
;
64 } else if ( typeof options
.expires
=== 'number' ) {
65 // Lifetime in seconds
67 date
.setTime( Number( date
) + ( options
.expires
* 1000 ) );
68 options
.expires
= date
;
71 if ( value
!== null ) {
72 value
= String( value
);
75 $.cookie( key
, value
, options
);
79 * Get the value of a cookie.
82 * @param {string} [prefix=wgCookiePrefix] The prefix of the key. If `prefix` is
83 * `undefined` or `null`, then `wgCookiePrefix` is used
84 * @param {Mixed} [defaultValue=null]
85 * @return {string|null|Mixed} If the cookie exists, then the value of the
86 * cookie, otherwise `defaultValue`
88 get: function ( key
, prefix
, defaultValue
) {
91 if ( prefix
=== undefined || prefix
=== null ) {
92 prefix
= defaults
.prefix
;
95 // Was defaultValue omitted?
96 if ( arguments
.length
< 3 ) {
100 result
= $.cookie( prefix
+ key
);
102 return result
!== null ? result
: defaultValue
;
106 if ( window
.QUnit
) {
108 setDefaults: function ( value
) {