r96999 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96998‎ | r96999 | r97000 >
Date:20:36, 13 September 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
Followup r96978, clean up code duplication by factoring out the code building load.php requests into ResourceLoader::makeLoaderURL() and makeLoaderQuery()
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderModule.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2362,21 +2362,6 @@
23632363 protected function makeResourceLoaderLink( $modules, $only, $useESI = false, array $extraQuery = array() ) {
23642364 global $wgLoadScript, $wgResourceLoaderUseESI,
23652365 $wgResourceLoaderInlinePrivateModules;
2366 - $baseQuery = array(
2367 - 'lang' => $this->getContext()->getLang()->getCode(),
2368 - 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
2369 - 'skin' => $this->getSkin()->getSkinName(),
2370 - ) + $extraQuery;
2371 - if ( $only !== ResourceLoaderModule::TYPE_COMBINED ) {
2372 - $baseQuery['only'] = $only;
2373 - }
2374 - // Propagate printable and handheld parameters if present
2375 - if ( $this->isPrintable() ) {
2376 - $baseQuery['printable'] = 1;
2377 - }
2378 - if ( $this->getRequest()->getBool( 'handheld' ) ) {
2379 - $baseQuery['handheld'] = 1;
2380 - }
23812366
23822367 if ( !count( $modules ) ) {
23832368 return '';
@@ -2422,14 +2407,26 @@
24232408
24242409 $links = '';
24252410 foreach ( $groups as $group => $modules ) {
2426 - $query = $baseQuery;
24272411 // Special handling for user-specific groups
 2412+ $user = null;
24282413 if ( ( $group === 'user' || $group === 'private' ) && $this->getUser()->isLoggedIn() ) {
2429 - $query['user'] = $this->getUser()->getName();
 2414+ $user = $this->getUser()->getName();
24302415 }
24312416
24322417 // Create a fake request based on the one we are about to make so modules return
24332418 // correct timestamp and emptiness data
 2419+ $query = ResourceLoader::makeLoaderQuery(
 2420+ array(), // modules; not determined yet
 2421+ $this->getContext()->getLang()->getCode(),
 2422+ $this->getSkin()->getSkinName(),
 2423+ $user,
 2424+ null, // version; not determined yet
 2425+ ResourceLoader::inDebugMode(),
 2426+ $only === ResourceLoaderModule::TYPE_COMBINED ? null : $only,
 2427+ $this->isPrintable(),
 2428+ $this->getRequest()->getBool( 'handheld' ),
 2429+ $extraQuery
 2430+ );
24342431 $context = new ResourceLoaderContext( $resourceLoader, new FauxRequest( $query ) );
24352432 // Drop modules that know they're empty
24362433 foreach ( $modules as $key => $module ) {
@@ -2442,8 +2439,6 @@
24432440 continue;
24442441 }
24452442
2446 - $query['modules'] = ResourceLoader::makePackedModulesString( array_keys( $modules ) );
2447 -
24482443 // Support inlining of private modules if configured as such
24492444 if ( $group === 'private' && $wgResourceLoaderInlinePrivateModules ) {
24502445 if ( $only == ResourceLoaderModule::TYPE_STYLES ) {
@@ -2465,6 +2460,7 @@
24662461 // timestamp of these user-changable modules so we can ensure cache misses on change
24672462 // This should NOT be done for the site group (bug 27564) because anons get that too
24682463 // and we shouldn't be putting timestamps in Squid-cached HTML
 2464+ $version = null;
24692465 if ( $group === 'user' ) {
24702466 // Get the maximum timestamp
24712467 $timestamp = 1;
@@ -2472,15 +2468,21 @@
24732469 $timestamp = max( $timestamp, $module->getModifiedTime( $context ) );
24742470 }
24752471 // Add a version parameter so cache will break when things change
2476 - $query['version'] = wfTimestamp( TS_ISO_8601_BASIC, $timestamp );
 2472+ $version = wfTimestamp( TS_ISO_8601_BASIC, $timestamp );
24772473 }
2478 - // Make queries uniform in order
2479 - ksort( $query );
2480 -
2481 - $url = wfAppendQuery( $wgLoadScript, $query );
2482 - // Prevent the IE6 extension check from being triggered (bug 28840)
2483 - // by appending a character that's invalid in Windows extensions ('*')
2484 - $url .= '&*';
 2474+
 2475+ $url = ResourceLoader::makeLoaderURL(
 2476+ array_keys( $modules ),
 2477+ $this->getContext()->getLang()->getCode(),
 2478+ $this->getSkin()->getSkinName(),
 2479+ $user,
 2480+ $version,
 2481+ ResourceLoader::inDebugMode(),
 2482+ $only === ResourceLoaderModule::TYPE_COMBINED ? null : $only,
 2483+ $this->isPrintable(),
 2484+ $this->getRequest()->getBool( 'handheld' ),
 2485+ $extraQuery
 2486+ );
24852487 if ( $useESI && $wgResourceLoaderUseESI ) {
24862488 $esi = Xml::element( 'esi:include', array( 'src' => $url ) );
24872489 if ( $only == ResourceLoaderModule::TYPE_STYLES ) {
Index: trunk/phase3/includes/resourceloader/ResourceLoader.php
@@ -867,4 +867,65 @@
868868 return $retval = $wgRequest->getFuzzyBool( 'debug',
869869 $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
870870 }
 871+
 872+ /**
 873+ * Build a load.php URL
 874+ * @param $modules array of module names (strings)
 875+ * @param $lang string Language code
 876+ * @param $skin string Skin name
 877+ * @param $user string|null User name. If null, the &user= parameter is omitted
 878+ * @param $version string|null Versioning timestamp
 879+ * @param $debug bool Whether the request should be in debug mode
 880+ * @param $only string|null &only= parameter
 881+ * @param $printable bool Printable mode
 882+ * @param $handheld bool Handheld mode
 883+ * @param $extraQuery array Extra query parameters to add
 884+ * @return string URL to load.php. May be protocol-relative (if $wgLoadScript is procol-relative)
 885+ */
 886+ public static function makeLoaderURL( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
 887+ $printable = false, $handheld = false, $extraQuery = array() ) {
 888+ global $wgLoadScript;
 889+ $query = self::makeLoaderQuery( $modules, $lang, $skin, $user, $version, $debug,
 890+ $only, $printable, $handheld, $extraQuery
 891+ );
 892+
 893+ // Prevent the IE6 extension check from being triggered (bug 28840)
 894+ // by appending a character that's invalid in Windows extensions ('*')
 895+ return wfAppendQuery( $wgLoadScript, $query ) . '&*';
 896+ }
 897+
 898+ /**
 899+ * Build a query array (array representation of query string) for load.php. Helper
 900+ * function for makeLoaderURL().
 901+ * @return array
 902+ */
 903+ public static function makeLoaderQuery( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
 904+ $printable = false, $handheld = false, $extraQuery = array() ) {
 905+ $query = array(
 906+ 'modules' => self::makePackedModulesString( $modules ),
 907+ 'lang' => $lang,
 908+ 'skin' => $skin,
 909+ 'debug' => $debug ? 'true' : 'false',
 910+ );
 911+ if ( $user !== null ) {
 912+ $query['user'] = $user;
 913+ }
 914+ if ( $version !== null ) {
 915+ $query['version'] = $version;
 916+ }
 917+ if ( $only !== null ) {
 918+ $query['only'] = $only;
 919+ }
 920+ if ( $printable ) {
 921+ $query['printable'] = 1;
 922+ }
 923+ if ( $handheld ) {
 924+ $query['handheld'] = 1;
 925+ }
 926+ $query += $extraQuery;
 927+
 928+ // Make queries uniform in order
 929+ ksort( $query );
 930+ return $query;
 931+ }
871932 }
Index: trunk/phase3/includes/resourceloader/ResourceLoaderModule.php
@@ -142,17 +142,18 @@
143143 * @return Array of URLs
144144 */
145145 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
146 - global $wgLoadScript; // TODO factor out to ResourceLoader static method and deduplicate from makeResourceLoaderLink()
147 - $query = array(
148 - 'modules' => $this->getName(),
149 - 'only' => 'scripts',
150 - 'skin' => $context->getSkin(),
151 - 'user' => $context->getUser(),
152 - 'debug' => 'true',
153 - 'version' => $context->getVersion()
 146+ $url = ResourceLoader::makeLoaderURL(
 147+ array( $this->getName() ),
 148+ $context->getLanguage(),
 149+ $context->getSkin(),
 150+ $context->getUser(),
 151+ $context->getVersion(),
 152+ true, // debug
 153+ 'scripts', // only
 154+ $context->getRequest()->getBool( 'printable' ),
 155+ $context->getRequest()->getBool( 'handheld' )
154156 );
155 - ksort( $query );
156 - return array( wfAppendQuery( $wgLoadScript, $query ) . '&*' );
 157+ return array( $url );
157158 }
158159
159160 /**
@@ -186,17 +187,18 @@
187188 * @return Array: array( mediaType => array( URL1, URL2, ... ), ... )
188189 */
189190 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
190 - global $wgLoadScript; // TODO factor out to ResourceLoader static method and deduplicate from makeResourceLoaderLink()
191 - $query = array(
192 - 'modules' => $this->getName(),
193 - 'only' => 'styles',
194 - 'skin' => $context->getSkin(),
195 - 'user' => $context->getUser(),
196 - 'debug' => 'true',
197 - 'version' => $context->getVersion()
 191+ $url = ResourceLoader::makeLoaderURL(
 192+ array( $this->getName() ),
 193+ $context->getLanguage(),
 194+ $context->getSkin(),
 195+ $context->getUser(),
 196+ $context->getVersion(),
 197+ true, // debug
 198+ 'styles', // only
 199+ $context->getRequest()->getBool( 'printable' ),
 200+ $context->getRequest()->getBool( 'handheld' )
198201 );
199 - ksort( $query );
200 - return array( 'all' => array( wfAppendQuery( $wgLoadScript, $query ) . '&*' ) );
 202+ return array( 'all' => array( $url ) );
201203 }
202204
203205 /**

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r96978Fix the fixme on r88053: dependency handling was broken in debug mode in cert...catrope17:13, 13 September 2011

Comments

#Comment by Krinkle (talk | contribs)   21:07, 13 September 2011

Looks pretty good. One thing though. Do we really want to introduce two new methods with a list of 10 arguments ? Perhaps change it to have fewer arguments.

#Comment by Trevor Parscal (WMF) (talk | contribs)   22:42, 13 September 2011

You should just pass the context in for most of these, because it's primarily just extracting data out of the context - the method may change, need more data, not need other things, etc. If you passed the context in first, you could get it down to 3 or so arguments, which is more manageable.

#Comment by Catrope (talk | contribs)   10:01, 14 September 2011

The problem with that is that I don't have a context in OutputPage::makeResourceLoaderLink(). In fact, I have to call makerLoaderQuery() there to obtain a fake context in the first place.

#Comment by Krinkle (talk | contribs)   22:56, 13 September 2011

The rev to which this is a follow up (r96978) introduced ResourceLoaderModule getScriptURLsForDebug(), that one takes a single argument (RequestLoaderContext). Interesting one :)

Status & tagging log