r75911 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75910‎ | r75911 | r75912 >
Date:07:58, 3 November 2010
Author:tstarling
Status:ok (Comments)
Tags:
Comment:
Resource loader minor changes. Fix for r73668 etc.

* Break long lines.
* Convert long or unnecessary ternary operator usages to if/else.
* Fixed excessively clever assignment expressions.
* Rename $cache to $cacheEntry.
* Removed unnecessary web invocation guards. Their perlish form was making me uncomfortable. BTW, unlike in Perl, die() is not a function, it's a special case in the PHP grammar which very roughly simulates the Perl syntax:

die "x"; // works
0 || die("x"); // works
0 || (die); // works
0 || (die "x"); // fail!
Modified paths:
  • /trunk/phase3/includes/resourceloader/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderContext.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderSiteModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderStartUpModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderUserModule.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderUserOptionsModule.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/resourceloader/ResourceLoaderSiteModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Module for site customizations
2826 */
@@ -45,7 +43,10 @@
4644 'Print.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'print' ),
4745 );
4846 if ( $wgHandheldStyle ) {
49 - $pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'handheld' );
 47+ $pages['Handheld.css'] = array(
 48+ 'ns' => NS_MEDIAWIKI,
 49+ 'type' => 'style',
 50+ 'media' => 'handheld' );
5051 }
5152 return $pages;
5253 }
Index: trunk/phase3/includes/resourceloader/ResourceLoader.php
@@ -20,8 +20,6 @@
2121 * @author Trevor Parscal
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Dynamic JavaScript and CSS resource loading system.
2826 *
@@ -40,10 +38,12 @@
4139 /**
4240 * Loads information stored in the database about modules.
4341 *
44 - * This method grabs modules dependencies from the database and updates modules objects.
 42+ * This method grabs modules dependencies from the database and updates modules
 43+ * objects.
4544 *
46 - * This is not inside the module code because it's so much more performant to request all of the information at once
47 - * than it is to have each module requests its own information. This sacrifice of modularity yields a profound
 45+ * This is not inside the module code because it's so much more performant to
 46+ * request all of the information at once than it is to have each module
 47+ * requests its own information. This sacrifice of modularity yields a profound
4848 * performance improvement.
4949 *
5050 * @param $modules Array: list of module names to preload information for
@@ -111,7 +111,8 @@
112112 * - minify-css \see CSSMin::minify
113113 * - flip-css \see CSSJanus::transform
114114 *
115 - * If $data is empty, only contains whitespace or the filter was unknown, $data is returned unmodified.
 115+ * If $data is empty, only contains whitespace or the filter was unknown,
 116+ * $data is returned unmodified.
116117 *
117118 * @param $filter String: name of filter to run
118119 * @param $data String: text to filter, such as JavaScript or CSS text
@@ -122,17 +123,21 @@
123124
124125 wfProfileIn( __METHOD__ );
125126
126 - // For empty/whitespace-only data or for unknown filters, don't perform any caching or processing
127 - if ( trim( $data ) === '' || !in_array( $filter, array( 'minify-js', 'minify-css', 'flip-css' ) ) ) {
 127+ // For empty/whitespace-only data or for unknown filters, don't perform
 128+ // any caching or processing
 129+ if ( trim( $data ) === ''
 130+ || !in_array( $filter, array( 'minify-js', 'minify-css', 'flip-css' ) ) )
 131+ {
128132 wfProfileOut( __METHOD__ );
129133 return $data;
130134 }
131135
132136 // Try for Memcached hit
133137 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
134 - if ( is_string( $cache = $wgMemc->get( $key ) ) ) {
 138+ $cacheEntry = $wgMemc->get( $key );
 139+ if ( is_string( $cacheEntry ) ) {
135140 wfProfileOut( __METHOD__ );
136 - return $cache;
 141+ return $cacheEntry;
137142 }
138143
139144 // Run the filter - we've already verified one of these will work
@@ -149,7 +154,8 @@
150155 break;
151156 }
152157 } catch ( Exception $exception ) {
153 - throw new MWException( 'ResourceLoader filter error. Exception was thrown: ' . $exception->getMessage() );
 158+ throw new MWException( 'ResourceLoader filter error. ' .
 159+ 'Exception was thrown: ' . $exception->getMessage() );
154160 }
155161
156162 // Save filtered text to Memcached
@@ -182,10 +188,13 @@
183189 * Registers a module with the ResourceLoader system.
184190 *
185191 * @param $name Mixed: string of name of module or array of name/object pairs
186 - * @param $object ResourceLoaderModule: module object (optional when using multiple-registration calling style)
 192+ * @param $object ResourceLoaderModule: module object (optional when using
 193+ * multiple-registration calling style)
187194 * @throws MWException If a duplicate module registration is attempted
188 - * @throws MWException If something other than a ResourceLoaderModule is being registered
189 - * @return Boolean: false if there were any errors, in which case one or more modules were not registered
 195+ * @throws MWException If something other than a ResourceLoaderModule is being
 196+ * registered
 197+ * @return Boolean: false if there were any errors, in which case one or more
 198+ * modules were not registered
190199 */
191200 public function register( $name, ResourceLoaderModule $object = null ) {
192201
@@ -206,13 +215,15 @@
207216 if ( isset( $this->modules[$name] ) ) {
208217 // A module has already been registered by this name
209218 throw new MWException(
210 - 'ResourceLoader duplicate registration error. Another module has already been registered as ' . $name
 219+ 'ResourceLoader duplicate registration error. ' .
 220+ 'Another module has already been registered as ' . $name
211221 );
212222 }
213223
214224 // Validate the input (type hinting lets null through)
215225 if ( !( $object instanceof ResourceLoaderModule ) ) {
216 - throw new MWException( 'ResourceLoader invalid module error. Instances of ResourceLoaderModule expected.' );
 226+ throw new MWException( 'ResourceLoader invalid module error. ' .
 227+ 'Instances of ResourceLoaderModule expected.' );
217228 }
218229
219230 // Attach module
@@ -262,12 +273,14 @@
263274 }
264275 }
265276
266 - // If a version wasn't specified we need a shorter expiry time for updates to propagate to clients quickly
 277+ // If a version wasn't specified we need a shorter expiry time for updates
 278+ // to propagate to clients quickly
267279 if ( is_null( $context->getVersion() ) ) {
268280 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
269281 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
270282 }
271 - // If a version was specified we can use a longer expiry time since changing version numbers causes cache misses
 283+ // If a version was specified we can use a longer expiry time since changing
 284+ // version numbers causes cache misses
272285 else {
273286 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
274287 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
@@ -278,7 +291,8 @@
279292
280293 wfProfileIn( __METHOD__.'-getModifiedTime' );
281294
282 - // To send Last-Modified and support If-Modified-Since, we need to detect the last modified time
 295+ // To send Last-Modified and support If-Modified-Since, we need to detect
 296+ // the last modified time
283297 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
284298 foreach ( $modules as $module ) {
285299 // Bypass squid cache if the request includes any private modules
@@ -291,7 +305,11 @@
292306
293307 wfProfileOut( __METHOD__.'-getModifiedTime' );
294308
295 - header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ? 'text/css' : 'text/javascript' ) );
 309+ if ( $context->getOnly() === 'styles' ) {
 310+ header( 'Content-Type: text/css' );
 311+ } else {
 312+ header( 'Content-Type: text/javascript' );
 313+ }
296314 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
297315 if ( $context->getDebug() ) {
298316 header( 'Cache-Control: must-revalidate' );
@@ -332,10 +350,15 @@
333351 * @param $missing Array: list of unavailable modules (optional)
334352 * @return String: response data
335353 */
336 - public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = array() ) {
 354+ public function makeModuleResponse( ResourceLoaderContext $context,
 355+ array $modules, $missing = array() )
 356+ {
337357 // Pre-fetch blobs
338 - $blobs = $context->shouldIncludeMessages() ?
339 - MessageBlobStore::get( $this, $modules, $context->getLanguage() ) : array();
 358+ if ( $context->shouldIncludeMessages() ) {
 359+ $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
 360+ } else {
 361+ $blobs = array();
 362+ }
340363
341364 // Generate output
342365 $out = '';
@@ -351,9 +374,10 @@
352375
353376 // Styles
354377 $styles = array();
355 - if ( $context->shouldIncludeStyles() && ( count( $styles = $module->getStyles( $context ) ) ) ) {
 378+ if ( $context->shouldIncludeStyles() ) {
 379+ $styles = $module->getStyles( $context );
356380 // Flip CSS on a per-module basis
357 - if ( $this->modules[$name]->getFlip( $context ) ) {
 381+ if ( $styles && $this->modules[$name]->getFlip( $context ) ) {
358382 foreach ( $styles as $media => $style ) {
359383 $styles[$media] = $this->filter( 'flip-css', $style );
360384 }
@@ -375,7 +399,8 @@
376400 $out .= self::makeMessageSetScript( $messages );
377401 break;
378402 default:
379 - // Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
 403+ // Minify CSS before embedding in mediaWiki.loader.implement call
 404+ // (unless in debug mode)
380405 if ( !$context->getDebug() ) {
381406 foreach ( $styles as $media => $style ) {
382407 $styles[$media] = $this->filter( 'minify-css', $style );
@@ -391,8 +416,11 @@
392417 // Update module states
393418 if ( $context->shouldIncludeScripts() ) {
394419 // Set the state of modules loaded as only scripts to ready
395 - if ( count( $modules ) && $context->getOnly() === 'scripts' && !isset( $modules['startup'] ) ) {
396 - $out .= self::makeLoaderStateScript( array_fill_keys( array_keys( $modules ), 'ready' ) );
 420+ if ( count( $modules ) && $context->getOnly() === 'scripts'
 421+ && !isset( $modules['startup'] ) )
 422+ {
 423+ $out .= self::makeLoaderStateScript(
 424+ array_fill_keys( array_keys( $modules ), 'ready' ) );
397425 }
398426 // Set the state of modules which were requested but unavailable as missing
399427 if ( is_array( $missing ) && count( $missing ) ) {
@@ -462,7 +490,9 @@
463491 "( '$name', $version, $dependencies, $group );\n";
464492 }
465493
466 - public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) {
 494+ public static function makeLoaderRegisterScript( $name, $version = null,
 495+ $dependencies = null, $group = null )
 496+ {
467497 if ( is_array( $name ) ) {
468498 $registrations = FormatJson::encode( $name );
469499 return "mediaWiki.loader.register( $registrations );\n";
Index: trunk/phase3/includes/resourceloader/ResourceLoaderUserOptionsModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Module for user preference customizations
2826 */
@@ -80,7 +78,8 @@
8179 // Build CSS rules
8280 $rules = array();
8381 if ( $options['underline'] < 2 ) {
84 - $rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
 82+ $rules[] = "a { text-decoration: " .
 83+ ( $options['underline'] ? 'underline' : 'none' ) . "; }";
8584 }
8685 if ( $options['highlightbroken'] ) {
8786 $rules[] = "a.new, #quickbar a.new { color: #ba0000; }\n";
Index: trunk/phase3/includes/resourceloader/ResourceLoaderContext.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Object passed around to modules which contains information about the state
2826 * of a specific loader request
Index: trunk/phase3/includes/resourceloader/ResourceLoaderUserModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Module for user customizations
2826 */
@@ -36,9 +34,11 @@
3735 $username = $context->getUser();
3836 return array(
3937 "$username/common.js" => array( 'ns' => NS_USER, 'type' => 'script' ),
40 - "$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER, 'type' => 'script' ),
 38+ "$username/" . $context->getSkin() . '.js' =>
 39+ array( 'ns' => NS_USER, 'type' => 'script' ),
4140 "$username/common.css" => array( 'ns' => NS_USER, 'type' => 'style' ),
42 - "$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER, 'type' => 'style' ),
 41+ "$username/" . $context->getSkin() . '.css' =>
 42+ array( 'ns' => NS_USER, 'type' => 'style' ),
4343 );
4444 }
4545 return array();
Index: trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * ResourceLoader module based on local JavaScript/CSS files.
2826 */
Index: trunk/phase3/includes/resourceloader/ResourceLoaderModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 /**
2725 * Abstraction for resource loader modules, with name registration and maxage functionality.
2826 */
@@ -160,9 +158,11 @@
161159 ), __METHOD__
162160 );
163161 if ( !is_null( $deps ) ) {
164 - return $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
 162+ $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
 163+ } else {
 164+ $this->fileDeps[$skin] = array();
165165 }
166 - return $this->fileDeps[$skin] = array();
 166+ return $this->fileDeps[$skin];
167167 }
168168
169169 /**
Index: trunk/phase3/includes/resourceloader/ResourceLoaderStartUpModule.php
@@ -20,8 +20,6 @@
2121 * @author Roan Kattouw
2222 */
2323
24 -defined( 'MEDIAWIKI' ) || die( 1 );
25 -
2624 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
2725 /* Protected Members */
2826
@@ -107,7 +105,8 @@
108106 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
109107 $deps = $module->getDependencies();
110108 $group = $module->getGroup();
111 - $version = wfTimestamp( TS_ISO_8601_BASIC, round( $module->getModifiedTime( $context ), -2 ) );
 109+ $version = wfTimestamp( TS_ISO_8601_BASIC,
 110+ round( $module->getModifiedTime( $context ), -2 ) );
112111 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
113112 }
114113 // Automatically register module
@@ -118,8 +117,8 @@
119118 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
120119 $registrations[] = array( $name, $mtime );
121120 }
122 - // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies)
123 - // to mediaWiki.loader.register()
 121+ // Modules with dependencies but no group pass three arguments
 122+ // (name, timestamp, dependencies) to mediaWiki.loader.register()
124123 else if ( $module->getGroup() === null ) {
125124 $registrations[] = array(
126125 $name, $mtime, $module->getDependencies() );
@@ -163,11 +162,18 @@
164163 // Startup function
165164 $configuration = FormatJson::encode( $this->getConfig( $context ) );
166165 $registrations = self::getModuleRegistrations( $context );
167 - $out .= "var startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};";
 166+ $out .= "var startUp = function() {\n" .
 167+ "\t$registrations\n" .
 168+ "\tmediaWiki.config.set( $configuration );" .
 169+ "\n};";
168170
169171 // Conditional script injection
170 - $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
171 - $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete isCompatible;";
 172+ $scriptTag = Xml::escapeJsString(
 173+ Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
 174+ $out .= "if ( isCompatible() ) {\n" .
 175+ "\tdocument.write( '$scriptTag' );\n" .
 176+ "}\n" .
 177+ "delete isCompatible;";
172178 }
173179
174180 return $out;
@@ -182,8 +188,9 @@
183189 }
184190 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
185191
186 - // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
187 - // before making changes to this code!
 192+ // ATTENTION!: Because of the line above, this is not going to cause
 193+ // infinite recursion - think carefully before making changes to this
 194+ // code!
188195 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
189196 foreach ( $context->getResourceLoader()->getModules() as $module ) {
190197 $time = max( $time, $module->getModifiedTime( $context ) );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r73668Added early exits when files are used before webstarttparscal17:31, 24 September 2010

Comments

#Comment by Trevor Parscal (WMF) (talk | contribs)   17:15, 3 November 2010

Thanks for the help and good advice!

Status & tagging log