r85616 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85615‎ | r85616 | r85617 >
Date:12:07, 7 April 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
For bug 27488: move the startup script, jquery+mediawiki and the mw.config.set() call for configuration variables back to the <head> . Let modules control whether they're loaded in the <head> ('top') or at the bottom of the <body> ('bottom') through the position parameter/property

Also rearranges the loading order a little bit such that only=messages comes before only=scripts, and config comes before everything except startup and jquery+mediawiki
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderModule.php (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -390,15 +390,17 @@
391391 * Filter an array of modules to remove insufficiently trustworthy members, and modules
392392 * which are no longer registered (eg a page is cached before an extension is disabled)
393393 * @param $modules Array
 394+ * @param $position String if not null, only return modules with this position
394395 * @return Array
395396 */
396 - protected function filterModules( $modules, $type = ResourceLoaderModule::TYPE_COMBINED ){
 397+ protected function filterModules( $modules, $position = null, $type = ResourceLoaderModule::TYPE_COMBINED ){
397398 $resourceLoader = $this->getResourceLoader();
398399 $filteredModules = array();
399400 foreach( $modules as $val ){
400401 $module = $resourceLoader->getModule( $val );
401402 if( $module instanceof ResourceLoaderModule
402 - && $module->getOrigin() <= $this->getAllowedModules( $type ) )
 403+ && $module->getOrigin() <= $this->getAllowedModules( $type )
 404+ && ( is_null( $position ) || $module->getPosition() == $position ) )
403405 {
404406 $filteredModules[] = $val;
405407 }
@@ -410,12 +412,13 @@
411413 * Get the list of modules to include on this page
412414 *
413415 * @param $filter Bool whether to filter out insufficiently trustworthy modules
 416+ * @param $position String if not null, only return modules with this position
414417 * @return Array of module names
415418 */
416 - public function getModules( $filter = false, $param = 'mModules' ) {
 419+ public function getModules( $filter = false, $position = null, $param = 'mModules' ) {
417420 $modules = array_values( array_unique( $this->$param ) );
418421 return $filter
419 - ? $this->filterModules( $modules )
 422+ ? $this->filterModules( $modules, $position )
420423 : $modules;
421424 }
422425
@@ -434,8 +437,8 @@
435438 * Get the list of module JS to include on this page
436439 * @return array of module names
437440 */
438 - public function getModuleScripts( $filter = false ) {
439 - return $this->getModules( $filter, 'mModuleScripts' );
 441+ public function getModuleScripts( $filter = false, $position = null ) {
 442+ return $this->getModules( $filter, $position, 'mModuleScripts' );
440443 }
441444
442445 /**
@@ -454,8 +457,8 @@
455458 *
456459 * @return Array of module names
457460 */
458 - public function getModuleStyles( $filter = false ) {
459 - return $this->getModules( $filter, 'mModuleStyles' );
 461+ public function getModuleStyles( $filter = false, $position = null ) {
 462+ return $this->getModules( $filter, $position, 'mModuleStyles' );
460463 }
461464
462465 /**
@@ -474,8 +477,8 @@
475478 *
476479 * @return Array of module names
477480 */
478 - public function getModuleMessages( $filter = false ) {
479 - return $this->getModules( $filter, 'mModuleMessages' );
 481+ public function getModuleMessages( $filter = false, $position = null ) {
 482+ return $this->getModules( $filter, $position, 'mModuleMessages' );
480483 }
481484
482485 /**
@@ -2344,6 +2347,7 @@
23452348 $ret .= implode( "\n", array(
23462349 $this->getHeadLinks( $sk, true ),
23472350 $this->buildCssLinks( $sk ),
 2351+ $this->getHeadScripts( $sk ),
23482352 $this->getHeadItems()
23492353 ) );
23502354
@@ -2589,36 +2593,68 @@
25902594 }
25912595
25922596 /**
2593 - * Gets the global variables and mScripts; also adds userjs to the end if
2594 - * enabled. Despite the name, these scripts are no longer put in the
2595 - * <head> but at the bottom of the <body>
 2597+ * JS stuff to put in the <head>. This is the startup module, config
 2598+ * vars and modules marked with position 'top'
25962599 *
25972600 * @param $sk Skin object to use
25982601 * @return String: HTML fragment
25992602 */
26002603 function getHeadScripts( Skin $sk ) {
2601 - global $wgUseSiteJs, $wgAllowUserJs;
2602 -
26032604 // Startup - this will immediately load jquery and mediawiki modules
26042605 $scripts = $this->makeResourceLoaderLink( $sk, 'startup', ResourceLoaderModule::TYPE_SCRIPTS, true );
2605 -
2606 - // Script and Messages "only" requests
2607 - $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true ), ResourceLoaderModule::TYPE_SCRIPTS );
2608 - $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true ), ResourceLoaderModule::TYPE_MESSAGES );
2609 -
2610 - // Modules requests - let the client calculate dependencies and batch requests as it likes
2611 - $loader = '';
2612 - if ( $this->getModules( true ) ) {
2613 - $loader = Xml::encodeJsCall( 'mw.loader.load', array( $this->getModules( true ) ) ) .
2614 - Xml::encodeJsCall( 'mw.loader.go', array() );
2615 - }
26162606
 2607+ // Load config before anything else
26172608 $scripts .= Html::inlineScript(
26182609 ResourceLoader::makeLoaderConditionalScript(
2619 - ResourceLoader::makeConfigSetScript( $this->getJSVars() ) . $loader
 2610+ ResourceLoader::makeConfigSetScript( $this->getJSVars() )
26202611 )
26212612 );
 2613+
 2614+ // Script and Messages "only" requests marked for top inclusion
 2615+ // Messages should go first
 2616+ $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true, 'top' ), ResourceLoaderModule::TYPE_MESSAGES );
 2617+ $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true, 'top' ), ResourceLoaderModule::TYPE_SCRIPTS );
26222618
 2619+ // Modules requests - let the client calculate dependencies and batch requests as it likes
 2620+ // Only load modules that have marked themselves for loading at the top
 2621+ $modules = $this->getModules( true, 'top' );
 2622+ if ( $modules ) {
 2623+ $scripts .= Html::inlineScript(
 2624+ ResourceLoader::makeLoaderConditionalScript(
 2625+ Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) .
 2626+ Xml::encodeJsCall( 'mw.loader.go', array() )
 2627+ )
 2628+ );
 2629+ }
 2630+
 2631+ return $scripts;
 2632+ }
 2633+
 2634+ /**
 2635+ * JS stuff to put at the bottom of the <body>: modules marked with position 'bottom',
 2636+ * legacy scripts ($this->mScripts), user preferences, site JS and user JS
 2637+ */
 2638+ function getBottomScripts( Skin $sk ) {
 2639+ global $wgUseSiteJs, $wgAllowUserJs;
 2640+
 2641+ // Script and Messages "only" requests marked for bottom inclusion
 2642+ // Messages should go first
 2643+ $scripts = $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true, 'bottom' ), ResourceLoaderModule::TYPE_MESSAGES );
 2644+ $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true, 'bottom' ), ResourceLoaderModule::TYPE_SCRIPTS );
 2645+
 2646+ // Modules requests - let the client calculate dependencies and batch requests as it likes
 2647+ // Only load modules that have marked themselves for loading at the top
 2648+ $modules = $this->getModules( true, 'bottom' );
 2649+ if ( $modules ) {
 2650+ $scripts .= Html::inlineScript(
 2651+ ResourceLoader::makeLoaderConditionalScript(
 2652+ Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) .
 2653+ // the go() call is unnecessary if we inserted top modules, but we don't know for sure that we did
 2654+ Xml::encodeJsCall( 'mw.loader.go', array() )
 2655+ )
 2656+ );
 2657+ }
 2658+
26232659 // Legacy Scripts
26242660 $scripts .= "\n" . $this->mScripts;
26252661
@@ -2645,7 +2681,7 @@
26462682 }
26472683 }
26482684 $scripts .= $this->makeResourceLoaderLink( $sk, $userScripts, ResourceLoaderModule::TYPE_SCRIPTS );
2649 -
 2685+
26502686 return $scripts;
26512687 }
26522688
Index: trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php
@@ -78,6 +78,8 @@
7979 protected $messages = array();
8080 /** String: Name of group to load this module in */
8181 protected $group;
 82+ /** String: Position on the page to load this module at */
 83+ protected $position = 'bottom';
8284 /** Boolean: Link to raw files in debug mode */
8385 protected $debugRaw = true;
8486 /**
@@ -138,6 +140,8 @@
139141 * 'messages' => [array of message key strings],
140142 * // Group which this module should be loaded together with
141143 * 'group' => [group name string],
 144+ * // Position on the page to load this module at
 145+ * 'position' => ['bottom' (default) or 'top']
142146 * )
143147 * @endcode
144148 */
@@ -189,6 +193,7 @@
190194 break;
191195 // Single strings
192196 case 'group':
 197+ case 'position':
193198 case 'localBasePath':
194199 case 'remoteBasePath':
195200 $this->{$member} = (string) $option;
@@ -295,6 +300,10 @@
296301 public function getGroup() {
297302 return $this->group;
298303 }
 304+
 305+ public function getPosition() {
 306+ return $this->position;
 307+ }
299308
300309 /**
301310 * Gets list of names of modules this module depends on.
Index: trunk/phase3/includes/resourceloader/ResourceLoaderModule.php
@@ -159,6 +159,15 @@
160160 // Stub, override expected
161161 return null;
162162 }
 163+
 164+ /**
 165+ * Where on the HTML page should this module's JS be loaded?
 166+ * 'top': in the <head>
 167+ * 'bottom': at the bottom of the <body>
 168+ */
 169+ public function getPosition() {
 170+ return 'bottom';
 171+ }
163172
164173 /**
165174 * Get the loader JS for this module, if set.
Index: trunk/phase3/includes/Skin.php
@@ -738,7 +738,10 @@
739739 * @return String HTML-wrapped JS code to be put before </body>
740740 */
741741 function bottomScripts( $out ) {
742 - $bottomScriptText = "\n" . $out->getHeadScripts( $this );
 742+ // TODO and the suckage continues. This function is really just a wrapper around
 743+ // OutputPage::getBottomScripts() which takes a Skin param. This should be cleaned
 744+ // up at some point
 745+ $bottomScriptText = $out->getBottomScripts( $this );
743746 wfRunHooks( 'SkinAfterBottomScripts', array( $this, &$bottomScriptText ) );
744747
745748 return $bottomScriptText;
Index: trunk/phase3/resources/Resources.php
@@ -537,6 +537,7 @@
538538 'remoteBasePath' => $GLOBALS['wgStylePath'],
539539 'localBasePath' => "{$GLOBALS['IP']}/skins",
540540 'dependencies' => 'mediawiki.legacy.wikibits',
 541+ 'position' => 'top',
541542 ),
542543 'mediawiki.legacy.edit' => array(
543544 'scripts' => 'common/edit.js',

Follow-up revisions

RevisionCommit summaryAuthorDate
r85617Fix copypaste fail in r85616catrope13:14, 7 April 2011
r910891.17wmf1: MFT r85616, r85617. This required meticulous conflict resolution du...catrope18:06, 29 June 2011
r108184ResourceLoader: Add an experimental option to move the main module loading qu...catrope23:32, 5 January 2012

Comments

#Comment by Krinkle (talk | contribs)   12:30, 7 April 2011
+	function getBottomScripts( Skin $sk ) {
+		// Only load modules that have marked themselves for loading at the top

"at the top", bottom, right ?

#Comment by Catrope (talk | contribs)   13:15, 7 April 2011

Yeah, copypaste fail. Fixed in r85617.

#Comment by Krinkle (talk | contribs)   07:48, 26 May 2011

Could you look at the TODO you added here ? How urgent / accurate is it towards the current HEAD ?

#Comment by Catrope (talk | contribs)   19:21, 26 May 2011

It's not urgent. Ugly, but low priority.

#Comment by 😂 (talk | contribs)   06:01, 2 July 2011

Removing 1.18 tag, this was before the branch point.

Status & tagging log