r68430 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68429‎ | r68430 | r68431 >
Date:21:20, 22 June 2010
Author:tparscal
Status:ok
Tags:
Comment:
Beginings of the configuration system and utilities.
Modified paths:
  • /branches/resourceloader/phase3/resources/mw/mw.js (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/resources/mw/mw.js
@@ -6,20 +6,124 @@
77
88 /* Public Members */
99
 10+ 'util': new ( function() {
 11+
 12+ /* Private Members */
 13+
 14+ var that = this;
 15+
 16+ /* Public Functions */
 17+
 18+ /**
 19+ * Builds a url string from an object containing any of the following components:
 20+ *
 21+ * Component Example
 22+ * scheme "http"
 23+ * server "www.domain.com"
 24+ * path "path/to/my/file.html"
 25+ * query "this=th�t?" or { 'this': 'th�t?' }
 26+ * fragment "place_on_the_page"
 27+ *
 28+ * Results in: "http://www.domain.com/path/to/my/file.html?this=th%E5t#place_on_the_page"
 29+ */
 30+ this.buildUrlString = function( components ) {
 31+ var url = '';
 32+ if ( typeof components['scheme'] === 'string' ) {
 33+ url += components['scheme'] + '://';
 34+ }
 35+ if ( typeof components['server'] === 'string' ) {
 36+ url += components['server'] + '/';
 37+ }
 38+ if ( typeof components['path'] === 'string' ) {
 39+ url += components['path'];
 40+ }
 41+ if ( typeof components['query'] === 'string' ) {
 42+ url += '?' + components['path'];
 43+ } else if ( typeof components['query'] === 'object' ) {
 44+ url += '?' + that.buildQueryString( components['path'] );
 45+ }
 46+ if ( typeof components['fragment'] === 'string' ) {
 47+ url += '#' + components['fragment'];
 48+ }
 49+ return url;
 50+ };
 51+ /**
 52+ * Builds a query string from an object with key and values
 53+ */
 54+ this.buildQueryString = function( parameters ) {
 55+ // RFC 3986 compliant URI component encoder
 56+ function encode( string ) {
 57+ return encodeURIComponent( string )
 58+ .replace(/!/g, '%21')
 59+ .replace(/'/g, '%27')
 60+ .replace(/\(/g, '%28')
 61+ .replace(/\)/g, '%29')
 62+ .replace(/\*/g, '%2A');
 63+ }
 64+ var parts = [];
 65+ for ( p in parameters ) {
 66+ parts[parts.length] = encode( key ) + '=' + encode( parameters[p] );
 67+ }
 68+ return parts.join( '&' );
 69+ };
 70+ } )(),
1071 /**
 72+ * Configuration system
 73+ */
 74+ 'config': new ( function() {
 75+
 76+ /* Private Members */
 77+
 78+ var that = this;
 79+ // List of configuration values
 80+ var values = {};
 81+
 82+ /* Public Functions */
 83+
 84+ /**
 85+ * Sets one or multiple configuration values using a key and a value or an object of keys and values
 86+ */
 87+ this.set = function( keys, value ) {
 88+ if ( typeof keys === 'object' ) {
 89+ for ( key in keys ) {
 90+ values[key] = keys[key];
 91+ }
 92+ } else if ( typeof keys === 'string' && typeof value !== 'undefined' ) {
 93+ values[keys] = value;
 94+ }
 95+ };
 96+ /**
 97+ * Gets one or multiple configuration values using a key and an optional fallback or an array of keys
 98+ */
 99+ this.get = function( keys, fallback ) {
 100+ if ( typeof keys === 'object' ) {
 101+ var result = {};
 102+ for ( key in keys ) {
 103+ result[key] = typeof values[keys] === 'undefined' ? null, values[keys];
 104+ }
 105+ return result;
 106+ } else if ( typeof values[keys] === 'undefined' ) {
 107+ return typeof fallback !== 'undefined' ? fallback : null;
 108+ } else {
 109+ return values[keys];
 110+ }
 111+ };
 112+ } ),
 113+ /**
11114 * Localization system
12115 */
13116 'msg': new ( function() {
14117
15118 /* Private Members */
16 -
 119+
17120 var that = this;
 121+ // List of localized messages
18122 var messages = {};
19123
20124 /* Public Functions */
21125
22126 this.set = function( keys, value ) {
23 - if ( typeof key === 'object' ) {
 127+ if ( typeof keys === 'object' ) {
24128 for ( key in keys ) {
25129 messages[key] = keys[key];
26130 }
@@ -109,11 +213,19 @@
110214 }
111215 // Handle the batch
112216 if ( batch.length ) {
 217+ // Always order module alphabetically to help reduce cache misses for otherwise identical content
 218+ batch.sort();
113219 // It may be more performant to do this with an Ajax call, but that's limited to same-domain, so we can
114220 // either auto-detect (if there really is any benefit) or just use this method, which is safe either way
115221 var script = document.createElement( 'script' );
116222 script.type = 'text/javascript';
117 - script.src = 'load.php?modules=' + batch.join( '|' );
 223+ // Build and set the request URL
 224+ var query = mw.config.get( [ 'user', 'skin', 'space', 'view', 'language' ] );
 225+ query.modules = batch.join( '|' );
 226+ script.src = mw.util.buildUrlString( {
 227+ 'path': 'load.php',
 228+ 'query': query
 229+ } );
118230 // Good browsers
119231 script.onload = work;
120232 // Bad browsers (IE 6 & 7)

Status & tagging log