Index: branches/wmf/1.17wmf1/resources/mediawiki/mediawiki.js |
— | — | @@ -225,7 +225,91 @@ |
226 | 226 | * User object |
227 | 227 | */ |
228 | 228 | function User() { |
| 229 | + |
| 230 | + /* Private Members */ |
| 231 | + |
| 232 | + var that = this; |
| 233 | + |
| 234 | + /* Public Members */ |
| 235 | + |
229 | 236 | this.options = new Map(); |
| 237 | + |
| 238 | + /* Public Methods */ |
| 239 | + |
| 240 | + /* |
| 241 | + * Generates a random user session ID (32 alpha-numeric characters). |
| 242 | + * |
| 243 | + * This information would potentially be stored in a cookie to identify a user during a |
| 244 | + * session or series of sessions. It's uniqueness should not be depended on. |
| 245 | + * |
| 246 | + * @return string random set of 32 alpha-numeric characters |
| 247 | + */ |
| 248 | + function generateId() { |
| 249 | + var id = ''; |
| 250 | + var seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'; |
| 251 | + for ( var i = 0, r; i < 32; i++ ) { |
| 252 | + r = Math.floor( Math.random() * seed.length ); |
| 253 | + id += seed.substring( r, r + 1 ); |
| 254 | + } |
| 255 | + return id; |
| 256 | + } |
| 257 | + |
| 258 | + /* |
| 259 | + * Gets the current user's name. |
| 260 | + * |
| 261 | + * @return mixed user name string or null if users is anonymous |
| 262 | + */ |
| 263 | + this.name = function() { |
| 264 | + return mediaWiki.config.get( 'wgUserName' ); |
| 265 | + }; |
| 266 | + |
| 267 | + /* |
| 268 | + * Gets a random session ID automatically generated and kept in a cookie. |
| 269 | + * |
| 270 | + * This ID is ephemeral for everyone, staying in their browser only until they close |
| 271 | + * their browser. |
| 272 | + * |
| 273 | + * Do not use this method before the first call to mediaWiki.loader.go(), it depends on |
| 274 | + * jquery.cookie, which is added to the first pay-load just after mediaWiki is defined, but |
| 275 | + * won't be loaded until the first call to go(). |
| 276 | + * |
| 277 | + * @return string user name or random session ID |
| 278 | + */ |
| 279 | + this.sessionId = function () { |
| 280 | + var sessionId = $.cookie( 'mediaWiki.user.sessionId' ); |
| 281 | + if ( typeof sessionId == 'undefined' || sessionId == null ) { |
| 282 | + sessionId = generateId(); |
| 283 | + $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } ); |
| 284 | + } |
| 285 | + return sessionId; |
| 286 | + }; |
| 287 | + |
| 288 | + /* |
| 289 | + * Gets the current user's name or a random ID automatically generated and kept in a cookie. |
| 290 | + * |
| 291 | + * This ID is persistent for anonymous users, staying in their browser up to 1 year. The |
| 292 | + * expiration time is reset each time the ID is queried, so in most cases this ID will |
| 293 | + * persist until the browser's cookies are cleared or the user doesn't visit for 1 year. |
| 294 | + * |
| 295 | + * Do not use this method before the first call to mediaWiki.loader.go(), it depends on |
| 296 | + * jquery.cookie, which is added to the first pay-load just after mediaWiki is defined, but |
| 297 | + * won't be loaded until the first call to go(). |
| 298 | + * |
| 299 | + * @return string user name or random session ID |
| 300 | + */ |
| 301 | + this.id = function() { |
| 302 | + var name = that.name(); |
| 303 | + if ( name ) { |
| 304 | + return name; |
| 305 | + } |
| 306 | + var id = $.cookie( 'mediaWiki.user.id' ); |
| 307 | + if ( typeof id == 'undefined' || id == null ) { |
| 308 | + id = generateId(); |
| 309 | + } |
| 310 | + // Set cookie if not set, or renew it if already set |
| 311 | + $.cookie( 'mediaWiki.user.id', id, { 'expires': 365, 'path': '/' } ); |
| 312 | + return id; |
| 313 | + }; |
230 | 314 | } |
231 | 315 | |
232 | 316 | /* Public Members */ |
— | — | @@ -1022,6 +1106,9 @@ |
1023 | 1107 | delete startUp; |
1024 | 1108 | } |
1025 | 1109 | |
| 1110 | +// Add jQuery Cookie to initial payload (used in mediaWiki.user) |
| 1111 | +mediaWiki.loader.load( 'jquery.cookie' ); |
| 1112 | + |
1026 | 1113 | // Alias $j to jQuery for backwards compatibility |
1027 | 1114 | window.$j = jQuery; |
1028 | 1115 | window.mw = mediaWiki; |
Property changes on: branches/wmf/1.17wmf1/resources/mediawiki/mediawiki.js |
___________________________________________________________________ |
Modified: svn:mergeinfo |
1029 | 1116 | Merged /trunk/phase3/resources/mediawiki/mediawiki.js:r78104,78344,78347,78350,78365,78539,78886 |