r69447 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69446‎ | r69447 | r69448 >
Date:22:40, 16 July 2010
Author:tparscal
Status:ok
Tags:
Comment:
Goodbye implicit base-package!
Modified paths:
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)
  • /branches/resourceloader/phase3/resources/Resources.php (modified) (history)
  • /branches/resourceloader/phase3/resources/test/index.html (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -32,8 +32,8 @@
3333 * 'loader' => 'resources/foo/loader.js',
3434 * // If you need any localized messages brought into the JavaScript environment, list the keys here
3535 * 'messages' => array( 'foo-hello', 'foo-goodbye' ),
36 - * // Base-only scripts are special scripts loaded in the base-package
37 - * 'base' => false,
 36+ * // Raw scripts are are loaded without using the loader and can not bundle loaders, styles and messages
 37+ * 'raw' => false,
3838 * // Debug-only scripts are special scripts that are only loaded when requested and while in debug mode
3939 * 'debug' => false,
4040 * ) );
@@ -154,7 +154,7 @@
155155 * 'style' => [string: path to file, optional],
156156 * 'loader' => [string: path to file, optional],
157157 * 'messages' => [array: message keys, optional],
158 - * 'base' => [boolean: include in base package only, optional],
 158+ * 'raw' => [boolean: include directly without any loading support, optional],
159159 * 'debug' => [boolean: include in debug mode only, optional],
160160 * )
161161 *
@@ -183,7 +183,7 @@
184184 'style' => null,
185185 'messages' => null,
186186 'loader' => null,
187 - 'base' => false,
 187+ 'raw' => false,
188188 'debug' => false,
189189 ), $options );
190190 // Validate script option
@@ -217,7 +217,6 @@
218218 * 'lang' => [string: language code, optional, code of default language by default],
219219 * 'skin' => [string: name of skin, optional, name of default skin by default],
220220 * 'dir' => [string: 'ltr' or 'rtl', optional, direction of lang by default],
221 - * 'base' => [boolean: true to include base-only scripts, optional, false by default],
222221 * 'debug' => [boolean: true to include debug-only scripts, optional, false by default],
223222 * )
224223 */
@@ -228,89 +227,81 @@
229228 'user' => $request->getBool( 'user', $wgUser->isLoggedIn() ),
230229 'lang' => $request->getVal( 'lang', $wgLang->getCode() ),
231230 'skin' => $request->getVal( 'skin', $wgDefaultSkin ),
232 - 'base' => $request->getBool( 'base' ),
233231 'debug' => $request->getBool( 'debug' ),
234232 ) );
235 - $modules = explode( '|', $request->getVal( 'modules' ) );
236233 // Get the direction from the requested language
237234 if ( !isset( $parameters['dir'] ) ) {
238235 $lang = $wgLang->factory( $parameters['lang'] );
239236 $parameters['dir'] = $lang->getDir();
240237 }
241 - // Optionally include all base-only scripts
242 - $base = array();
243 - if ( $parameters['base'] ) {
244 - foreach ( static::$modules as $name => $options ) {
245 - if ( $options['base'] ) {
246 - // Only include debug scripts in debug mode
247 - if ( $options['debug'] ) {
248 - if ( $parameters['debug'] ) {
249 - $base[] = $name;
250 - }
251 - } else {
252 - $base[] = $name;
 238+ // Get modules - filtering out any we don't know about
 239+ $modules = array();
 240+ foreach ( explode( '|', $request->getVal( 'modules' ) ) as $module ) {
 241+ if ( isset( static::$modules[$module] ) ) {
 242+ if ( static::$modules[$module]['debug'] ) {
 243+ if ( $parameters['debug'] ) {
 244+ $modules[] = $module;
253245 }
 246+ } else {
 247+ $modules[] = $module;
254248 }
255249 }
256250 }
257 - // Include requested modules which have been registered - ignoring any which have not been
258 - $other = array();
259 - foreach ( static::$modules as $name => $options ) {
260 - if ( in_array( $name, $modules ) && !in_array( $name, $base )) {
261 - $other[] = $name;
262 - }
263 - }
264251 // Use output buffering
265252 ob_start();
266 - // Optionally include base modules
267 - if ( $parameters['base'] ) {
268 - // Base modules
269 - foreach ( $base as $module ) {
 253+ // Output raw modules first
 254+ foreach ( $modules as $module ) {
 255+ if ( static::$modules[$module]['raw'] ) {
270256 readfile( static::$modules[$module]['script'] );
271257 }
272 - // All module loaders - keep track of which loaders have been included to prevent multiple modules with a
273 - // single loader causing the loader to be included more than once
 258+ }
 259+ // Special meta-information for the 'mw' module
 260+ if ( in_array( 'mw', $modules ) ) {
 261+ // Collect all loaders
274262 $loaders = array();
275263 foreach ( self::$modules as $name => $options ) {
276 - if ( $options['loader'] !== null && !in_array( $options['loader'], $loaders ) ) {
277 - readfile( $options['loader'] );
 264+ if ( $options['loader'] !== null ) {
278265 $loaders[] = $options['loader'];
279266 }
280267 }
281 - }
282 -
283 - /*
284 - * Skin::makeGlobalVariablesScript needs to be modified so that we still output the globals for now, but also
285 - * put them into the initial payload like this:
286 - *
287 - * // Sets the inital configuration
288 - * mw.config.set( { 'name': 'value', ... } );
289 - *
290 - * Also, the naming of these variables is horrible and sad, hopefully this can be worked on
291 - */
292 -
293 - // Other modules
294 - $blobs = static::messages( $parameters['lang'], $other );
295 - foreach ( $other as $module ) {
296 - // Script
297 - $script = file_get_contents( static::$modules[$module]['script'] );
298 - if ( !$parameters['debug'] ) {
299 - $script = static::filter( 'strip-debug', $script );
 268+ // Include each loader once
 269+ foreach ( array_unique( $loaders ) as $loader ) {
 270+ readfile( $loader );
300271 }
301 - // Style
302 - $style = static::$modules[$module]['style'] ? file_get_contents( static::$modules[$module]['style'] ) : '';
303 - if ( $style !== '' ) {
304 - if ( $parameters['dir'] == 'rtl' ) {
305 - $style = static::filter( 'flip-css', $style );
 272+ /*
 273+ * Skin::makeGlobalVariablesScript needs to be modified so that we still output the globals for now, but also
 274+ * put them into the initial payload like this:
 275+ *
 276+ * // Sets the inital configuration
 277+ * mw.config.set( { 'name': 'value', ... } );
 278+ *
 279+ * Also, the naming of these variables is horrible and sad, hopefully this can be worked on
 280+ */
 281+ }
 282+ // Output non-raw modules
 283+ $blobs = static::messages( $parameters['lang'], $modules );
 284+ foreach ( $modules as $module ) {
 285+ if ( !static::$modules[$module]['raw'] ) {
 286+ // Script
 287+ $script = file_get_contents( static::$modules[$module]['script'] );
 288+ if ( !$parameters['debug'] ) {
 289+ $script = static::filter( 'strip-debug', $script );
306290 }
307 - $style = Xml::escapeJsString(
308 - static::filter( 'minify-css', $style, static::$modules[$module]['style'] )
309 - );
 291+ // Style
 292+ $style = static::$modules[$module]['style'] ? file_get_contents( static::$modules[$module]['style'] ) : '';
 293+ if ( $style !== '' ) {
 294+ if ( $parameters['dir'] == 'rtl' ) {
 295+ $style = static::filter( 'flip-css', $style );
 296+ }
 297+ $style = Xml::escapeJsString(
 298+ static::filter( 'minify-css', $style, static::$modules[$module]['style'] )
 299+ );
 300+ }
 301+ // Messages
 302+ $messages = isset( $blobs[$module] ) ? $blobs[$module] : '{}';
 303+ // Output
 304+ echo "mw.loader.implement(\n'{$module}', function() { {$script} }, '{$style}', {$messages}\n);\n";
310305 }
311 - // Messages
312 - $messages = isset( $blobs[$module] ) ? $blobs[$module] : '{}';
313 - // Output
314 - echo "mw.loader.implement(\n'{$module}', function() { {$script} }, '{$style}', {$messages}\n);\n";
315306 }
316307 // Set headers -- when we support CSS only mode, this might change!
317308 header( 'Content-Type: text/javascript' );
Index: branches/resourceloader/phase3/resources/test/index.html
@@ -8,7 +8,8 @@
99 color: white;
1010 }
1111 </style>
12 - <script type="text/javascript" src="../../load.php?modules=test&base=1&debug=0"></script>
 12+ <script type="text/javascript" src="../../load.php?modules=jquery|mw|mw.debug&debug=1"></script>
 13+ <script type="text/javascript" src="../../load.php?modules=mw.util|test&debug=1"></script>
1314 <script>
1415 mw.config.set( 'wgScriptPath', '../..' );
1516 </script>
Index: branches/resourceloader/phase3/resources/Resources.php
@@ -3,19 +3,19 @@
44 ResourceLoader::register( array(
55 'jquery' => array(
66 'script' => 'resources/base/jquery-1.4.2.js',
7 - 'base' => true,
 7+ 'raw' => true,
88 ),
99 'mw' => array(
1010 'script' => 'resources/base/mw.js',
11 - 'base' => true,
 11+ 'raw' => true,
1212 ),
1313 'mw.util' => array(
1414 'script' => 'resources/base/mw/mw.util.js',
15 - 'base' => true,
 15+ 'raw' => true,
1616 ),
1717 'mw.debug' => array(
1818 'script' => 'resources/base/mw/mw.debug.js',
19 - 'base' => true,
 19+ 'raw' => true,
2020 'debug' => true
2121 ),
2222 'test' => array(

Status & tagging log