Index: trunk/phase3/resources/Resources.php |
— | — | @@ -326,6 +326,7 @@ |
327 | 327 | 'mediawiki' => array( |
328 | 328 | 'scripts' => 'resources/mediawiki/mediawiki.js', |
329 | 329 | 'debugScripts' => 'resources/mediawiki/mediawiki.log.js', |
| 330 | + 'dependencies' => array( 'jquery.cookie' ), |
330 | 331 | 'debugRaw' => false |
331 | 332 | ), |
332 | 333 | 'mediawiki.util' => array( |
Index: trunk/phase3/resources/mediawiki/mediawiki.js |
— | — | @@ -225,7 +225,62 @@ |
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. It's uniqueness should not be depended on. |
| 245 | + * |
| 246 | + * @return string random set of 32 alpha-numeric characters |
| 247 | + */ |
| 248 | + function generateSessionId() { |
| 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 the current user's name or a random session ID automatically generated and kept in |
| 269 | + * a cookie. |
| 270 | + * |
| 271 | + * @return string user name or random session ID |
| 272 | + */ |
| 273 | + this.sessionId = function () { |
| 274 | + var name = that.name(); |
| 275 | + if ( name ) { |
| 276 | + return name; |
| 277 | + } |
| 278 | + var sessionId = $.cookie( 'mediaWiki.user.sessionId' ); |
| 279 | + if ( typeof sessionId == 'undefined' || sessionId == null ) { |
| 280 | + sessionId = generateSessionId(); |
| 281 | + $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': 30, 'path': '/' } ); |
| 282 | + } |
| 283 | + return sessionId; |
| 284 | + }; |
230 | 285 | } |
231 | 286 | |
232 | 287 | /* Public Members */ |