r77693 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77692‎ | r77693 | r77694 >
Date:23:59, 3 December 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Resolves bug #26131 by always placing user scripts and styles at the very end of the page
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2374,11 +2374,12 @@
23752375 }
23762376 continue;
23772377 }
2378 - // Special handling for user and site groups; because users might change their stuff on-wiki like site or
2379 - // user pages, or user preferences; we need to find the highest timestamp of these user-changable modules so
2380 - // we can ensure cache misses on change
 2378+ // Special handling for user and site groups; because users might change their stuff
 2379+ // on-wiki like site or user pages, or user preferences; we need to find the highest
 2380+ // timestamp of these user-changable modules so we can ensure cache misses on change
23812381 if ( $group === 'user' || $group === 'site' ) {
2382 - // Create a fake request based on the one we are about to make so modules return correct times
 2382+ // Create a fake request based on the one we are about to make so modules return
 2383+ // correct times
23832384 $context = new ResourceLoaderContext( $resourceLoader, new FauxRequest( $query ) );
23842385 // Get the maximum timestamp
23852386 $timestamp = 1;
@@ -2425,26 +2426,32 @@
24262427 // Startup - this will immediately load jquery and mediawiki modules
24272428 $scripts = $this->makeResourceLoaderLink( $sk, 'startup', 'scripts', true );
24282429
2429 - // Configuration -- This could be merged together with the load and go, but makeGlobalVariablesScript returns a
2430 - // whole script tag -- grumble grumble...
 2430+ // Configuration -- This could be merged together with the load and go, but
 2431+ // makeGlobalVariablesScript returns a whole script tag -- grumble grumble...
24312432 $scripts .= Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n";
24322433
2433 - // Script and Messages "only"
2434 -
2435 - // Scripts
 2434+ // Script and Messages "only" requests
24362435 $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' );
2437 -
2438 - // Messages
24392436 $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' );
24402437
2441 - // Modules - let the client calculate dependencies and batch requests as it likes
 2438+ // Modules requests - let the client calculate dependencies and batch requests as it likes
24422439 if ( $this->getModules() ) {
24432440 $modules = FormatJson::encode( $this->getModules() );
24442441 $scripts .= Html::inlineScript(
2445 - "if ( window.mediaWiki ) { mediaWiki.loader.load( {$modules} ); mediaWiki.loader.go(); }"
 2442+ ResourceLoader::makeLoaderConditionalScript(
 2443+ "mediaWiki.loader.load( {$modules} ); mediaWiki.loader.go();"
 2444+ )
24462445 ) . "\n";
24472446 }
24482447
 2448+ // Legacy Scripts
 2449+ $scripts .= "\n" . $this->mScripts;
 2450+
 2451+ // Add site JS if enabled
 2452+ if ( $wgUseSiteJs ) {
 2453+ $scripts .= $this->makeResourceLoaderLink( $sk, 'site', 'scripts' );
 2454+ }
 2455+
24492456 // Add user JS if enabled - trying to load user.options as a bundle if possible
24502457 $userOptionsAdded = false;
24512458 if ( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) {
@@ -2453,20 +2460,16 @@
24542461 # XXX: additional security check/prompt?
24552462 $this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) );
24562463 } else {
2457 - $scripts .= $this->makeResourceLoaderLink( $sk, array( 'user', 'user.options' ), 'scripts' );
 2464+ $scripts .= $this->makeResourceLoaderLink(
 2465+ $sk, array( 'user', 'user.options' ), 'scripts'
 2466+ );
24582467 $userOptionsAdded = true;
24592468 }
24602469 }
24612470 if ( !$userOptionsAdded ) {
24622471 $scripts .= $this->makeResourceLoaderLink( $sk, 'user.options', 'scripts' );
24632472 }
2464 - $scripts .= "\n" . $this->mScripts;
2465 -
2466 - // Add site JS if enabled
2467 - if ( $wgUseSiteJs ) {
2468 - $scripts .= $this->makeResourceLoaderLink( $sk, 'site', 'scripts' );
2469 - }
2470 -
 2473+
24712474 return $scripts;
24722475 }
24732476
@@ -2584,9 +2587,15 @@
25852588 }
25862589 }
25872590 }
 2591+
 2592+ // Add styles to tags, pushing user modules to the end
 2593+ $styles = array( array(), array() );
 2594+ foreach ( $this->getModuleStyles() as $module ) {
 2595+ $styles[strpos( 'user', $module ) === 0 ? 1 : 0][] = $module;
 2596+ }
 2597+ $tags[] = $this->makeResourceLoaderLink( $sk, $styles[0], 'styles' );
 2598+ $tags[] = $this->makeResourceLoaderLink( $sk, $styles[1], 'styles' );
25882599
2589 - $tags[] = $this->makeResourceLoaderLink( $sk, $this->getModuleStyles(), 'styles' );
2590 -
25912600 return implode( "\n", $tags );
25922601 }
25932602

Follow-up revisions

RevisionCommit summaryAuthorDate
r77696Made use of Xml::encodeJsCall - improves on r77693.tparscal00:35, 4 December 2010
r77702Fixes r77696 where a few mistakes were made. Also improves on r77693 by using...tparscal00:48, 4 December 2010
r78018Improves on r77693 by ensuring site styles are loaded just before user styles.tparscal21:45, 7 December 2010
r78023Improves on r77693 by placing ResourceLoader "only styles" CSS links together...tparscal22:27, 7 December 2010

Comments

#Comment by Catrope (talk | contribs)   00:09, 4 December 2010
+					"mediaWiki.loader.load( {$modules} ); mediaWiki.loader.go();"

Should use Xml::encodeJsCall() or whatever it's called. Not introduced in this rev, to be fair.

+			$styles[strpos( 'user', $module ) === 0 ? 1 : 0][] = $module;

Ugh. Can't you use getGroup() here somehow? Named indices in $styles would also be more readable.

#Comment by Trevor Parscal (WMF) (talk | contribs)   00:11, 4 December 2010

I would need to initialize a context and.. Well, yes, but this was clearly a minimum effort :( I will see if I can do something more robust.

#Comment by Trevor Parscal (WMF) (talk | contribs)   00:36, 4 December 2010

r77696 resolves the Xml::encodeJsCall stuff.

#Comment by Trevor Parscal (WMF) (talk | contribs)   00:49, 4 December 2010

But it's got issues - see r77702 for fixes to those - and also some code that addresses you point about getGroup needing to be used.

Status & tagging log