From 488d368b86466d96f4d5c917ea203a7050e15bbe Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 13 Sep 2011 20:36:24 +0000 Subject: [PATCH] Followup r96978, clean up code duplication by factoring out the code building load.php requests into ResourceLoader::makeLoaderURL() and makeLoaderQuery() --- includes/OutputPage.php | 56 +++++++++-------- includes/resourceloader/ResourceLoader.php | 61 +++++++++++++++++++ .../resourceloader/ResourceLoaderModule.php | 42 +++++++------ 3 files changed, 112 insertions(+), 47 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index e24cc2fd17..7d0a552eb4 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2362,21 +2362,6 @@ $distantTemplates protected function makeResourceLoaderLink( $modules, $only, $useESI = false, array $extraQuery = array() ) { global $wgLoadScript, $wgResourceLoaderUseESI, $wgResourceLoaderInlinePrivateModules; - $baseQuery = array( - 'lang' => $this->getContext()->getLang()->getCode(), - 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false', - 'skin' => $this->getSkin()->getSkinName(), - ) + $extraQuery; - if ( $only !== ResourceLoaderModule::TYPE_COMBINED ) { - $baseQuery['only'] = $only; - } - // Propagate printable and handheld parameters if present - if ( $this->isPrintable() ) { - $baseQuery['printable'] = 1; - } - if ( $this->getRequest()->getBool( 'handheld' ) ) { - $baseQuery['handheld'] = 1; - } if ( !count( $modules ) ) { return ''; @@ -2422,14 +2407,26 @@ $distantTemplates $links = ''; foreach ( $groups as $group => $modules ) { - $query = $baseQuery; // Special handling for user-specific groups + $user = null; if ( ( $group === 'user' || $group === 'private' ) && $this->getUser()->isLoggedIn() ) { - $query['user'] = $this->getUser()->getName(); + $user = $this->getUser()->getName(); } // Create a fake request based on the one we are about to make so modules return // correct timestamp and emptiness data + $query = ResourceLoader::makeLoaderQuery( + array(), // modules; not determined yet + $this->getContext()->getLang()->getCode(), + $this->getSkin()->getSkinName(), + $user, + null, // version; not determined yet + ResourceLoader::inDebugMode(), + $only === ResourceLoaderModule::TYPE_COMBINED ? null : $only, + $this->isPrintable(), + $this->getRequest()->getBool( 'handheld' ), + $extraQuery + ); $context = new ResourceLoaderContext( $resourceLoader, new FauxRequest( $query ) ); // Drop modules that know they're empty foreach ( $modules as $key => $module ) { @@ -2442,8 +2439,6 @@ $distantTemplates continue; } - $query['modules'] = ResourceLoader::makePackedModulesString( array_keys( $modules ) ); - // Support inlining of private modules if configured as such if ( $group === 'private' && $wgResourceLoaderInlinePrivateModules ) { if ( $only == ResourceLoaderModule::TYPE_STYLES ) { @@ -2465,6 +2460,7 @@ $distantTemplates // timestamp of these user-changable modules so we can ensure cache misses on change // This should NOT be done for the site group (bug 27564) because anons get that too // and we shouldn't be putting timestamps in Squid-cached HTML + $version = null; if ( $group === 'user' ) { // Get the maximum timestamp $timestamp = 1; @@ -2472,15 +2468,21 @@ $distantTemplates $timestamp = max( $timestamp, $module->getModifiedTime( $context ) ); } // Add a version parameter so cache will break when things change - $query['version'] = wfTimestamp( TS_ISO_8601_BASIC, $timestamp ); + $version = wfTimestamp( TS_ISO_8601_BASIC, $timestamp ); } - // Make queries uniform in order - ksort( $query ); - - $url = wfAppendQuery( $wgLoadScript, $query ); - // Prevent the IE6 extension check from being triggered (bug 28840) - // by appending a character that's invalid in Windows extensions ('*') - $url .= '&*'; + + $url = ResourceLoader::makeLoaderURL( + array_keys( $modules ), + $this->getContext()->getLang()->getCode(), + $this->getSkin()->getSkinName(), + $user, + $version, + ResourceLoader::inDebugMode(), + $only === ResourceLoaderModule::TYPE_COMBINED ? null : $only, + $this->isPrintable(), + $this->getRequest()->getBool( 'handheld' ), + $extraQuery + ); if ( $useESI && $wgResourceLoaderUseESI ) { $esi = Xml::element( 'esi:include', array( 'src' => $url ) ); if ( $only == ResourceLoaderModule::TYPE_STYLES ) { diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 49111652e6..1128264a05 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -867,4 +867,65 @@ class ResourceLoader { return $retval = $wgRequest->getFuzzyBool( 'debug', $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) ); } + + /** + * Build a load.php URL + * @param $modules array of module names (strings) + * @param $lang string Language code + * @param $skin string Skin name + * @param $user string|null User name. If null, the &user= parameter is omitted + * @param $version string|null Versioning timestamp + * @param $debug bool Whether the request should be in debug mode + * @param $only string|null &only= parameter + * @param $printable bool Printable mode + * @param $handheld bool Handheld mode + * @param $extraQuery array Extra query parameters to add + * @return string URL to load.php. May be protocol-relative (if $wgLoadScript is procol-relative) + */ + public static function makeLoaderURL( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null, + $printable = false, $handheld = false, $extraQuery = array() ) { + global $wgLoadScript; + $query = self::makeLoaderQuery( $modules, $lang, $skin, $user, $version, $debug, + $only, $printable, $handheld, $extraQuery + ); + + // Prevent the IE6 extension check from being triggered (bug 28840) + // by appending a character that's invalid in Windows extensions ('*') + return wfAppendQuery( $wgLoadScript, $query ) . '&*'; + } + + /** + * Build a query array (array representation of query string) for load.php. Helper + * function for makeLoaderURL(). + * @return array + */ + public static function makeLoaderQuery( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null, + $printable = false, $handheld = false, $extraQuery = array() ) { + $query = array( + 'modules' => self::makePackedModulesString( $modules ), + 'lang' => $lang, + 'skin' => $skin, + 'debug' => $debug ? 'true' : 'false', + ); + if ( $user !== null ) { + $query['user'] = $user; + } + if ( $version !== null ) { + $query['version'] = $version; + } + if ( $only !== null ) { + $query['only'] = $only; + } + if ( $printable ) { + $query['printable'] = 1; + } + if ( $handheld ) { + $query['handheld'] = 1; + } + $query += $extraQuery; + + // Make queries uniform in order + ksort( $query ); + return $query; + } } diff --git a/includes/resourceloader/ResourceLoaderModule.php b/includes/resourceloader/ResourceLoaderModule.php index fb6a503ea3..14a92bd1a4 100644 --- a/includes/resourceloader/ResourceLoaderModule.php +++ b/includes/resourceloader/ResourceLoaderModule.php @@ -142,17 +142,18 @@ abstract class ResourceLoaderModule { * @return Array of URLs */ public function getScriptURLsForDebug( ResourceLoaderContext $context ) { - global $wgLoadScript; // TODO factor out to ResourceLoader static method and deduplicate from makeResourceLoaderLink() - $query = array( - 'modules' => $this->getName(), - 'only' => 'scripts', - 'skin' => $context->getSkin(), - 'user' => $context->getUser(), - 'debug' => 'true', - 'version' => $context->getVersion() + $url = ResourceLoader::makeLoaderURL( + array( $this->getName() ), + $context->getLanguage(), + $context->getSkin(), + $context->getUser(), + $context->getVersion(), + true, // debug + 'scripts', // only + $context->getRequest()->getBool( 'printable' ), + $context->getRequest()->getBool( 'handheld' ) ); - ksort( $query ); - return array( wfAppendQuery( $wgLoadScript, $query ) . '&*' ); + return array( $url ); } /** @@ -186,17 +187,18 @@ abstract class ResourceLoaderModule { * @return Array: array( mediaType => array( URL1, URL2, ... ), ... ) */ public function getStyleURLsForDebug( ResourceLoaderContext $context ) { - global $wgLoadScript; // TODO factor out to ResourceLoader static method and deduplicate from makeResourceLoaderLink() - $query = array( - 'modules' => $this->getName(), - 'only' => 'styles', - 'skin' => $context->getSkin(), - 'user' => $context->getUser(), - 'debug' => 'true', - 'version' => $context->getVersion() + $url = ResourceLoader::makeLoaderURL( + array( $this->getName() ), + $context->getLanguage(), + $context->getSkin(), + $context->getUser(), + $context->getVersion(), + true, // debug + 'styles', // only + $context->getRequest()->getBool( 'printable' ), + $context->getRequest()->getBool( 'handheld' ) ); - ksort( $query ); - return array( 'all' => array( wfAppendQuery( $wgLoadScript, $query ) . '&*' ) ); + return array( 'all' => array( $url ) ); } /** -- 2.20.1