r75060 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75059‎ | r75060 | r75061 >
Date:00:22, 20 October 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Whitespace, comments and general cleanup.
Modified paths:
  • /trunk/phase3/includes/resourceloader/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/resourceloader/ResourceLoader.php
@@ -25,33 +25,35 @@
2626 /**
2727 * Dynamic JavaScript and CSS resource loading system.
2828 *
29 - * Most of the documention is on the MediaWiki documentation wiki starting at
 29+ * Most of the documention is on the MediaWiki documentation wiki starting at:
3030 * http://www.mediawiki.org/wiki/ResourceLoader
3131 */
3232 class ResourceLoader {
3333
3434 /* Protected Static Members */
3535
36 - // @var array list of module name/ResourceLoaderModule object pairs
 36+ /** @var {array} List of module name/ResourceLoaderModule object pairs */
3737 protected $modules = array();
3838
3939 /* Protected Methods */
40 -
 40+
4141 /**
42 - * Loads information stored in the database about modules
 42+ * Loads information stored in the database about modules.
4343 *
 44+ * This method grabs modules dependencies from the database and updates modules objects.
 45+ *
4446 * This is not inside the module code because it's so much more performant to request all of the information at once
45 - * than it is to have each module requests it's own information.
46 - *
47 - * This method grab modules dependencies from the database and initialize modules object.
48 - * A first pass compute dependencies, a second one the blob mtime.
49 - *
50 - * @param $modules Array List of module names to preload information for
51 - * @param $context ResourceLoaderContext Context to load the information within
 47+ * than it is to have each module requests it's own information. This sacrifice of modularity yields a profound
 48+ * performance improvement.
 49+ *
 50+ * A first pass calculates dependent file modified times, a second one calculates message blob modified times.
 51+ *
 52+ * @param {array} $modules List of module names to preload information for
 53+ * @param {ResourceLoaderContext} $context Context to load the information within
5254 */
5355 protected function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
5456 if ( !count( $modules ) ) {
55 - return; # or Database*::select() will explode
 57+ return; // or else Database*::select() will explode, plus it's cheaper!
5658 }
5759 $dbr = wfGetDb( DB_SLAVE );
5860 $skin = $context->getSkin();
@@ -72,6 +74,7 @@
7375 );
7476 $modulesWithDeps[] = $row->md_module;
7577 }
 78+
7679 // Register the absence of a dependency row too
7780 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
7881 $this->modules[$name]->setFileDependencies( $skin, array() );
@@ -103,44 +106,40 @@
104107 }
105108
106109 /**
107 - * Runs text (js,CSS) through a filter, caching the filtered result for future calls.
 110+ * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
108111 *
109112 * Availables filters are:
110113 * - minify-js \see JSMin::minify
111114 * - minify-css \see CSSMin::minify
112115 * - flip-css \see CSSJanus::transform
113 - * When the filter names does not exist, text is returned as is.
114 - *
115 - * @param $filter String: name of filter to run
116 - * @param $data String: text to filter, such as JavaScript or CSS text
117 - * @param $file String: path to file being filtered, (optional: only required for CSS to resolve paths)
118 - * @return String: filtered data
 116+ *
 117+ * If data is empty, only whitespace or the filter was unknown, data is returned unmodified.
 118+ *
 119+ * @param {string} $filter Name of filter to run
 120+ * @param {string} $data Text to filter, such as JavaScript or CSS text
 121+ * @return {string} Filtered data
119122 */
120123 protected function filter( $filter, $data ) {
121124 global $wgMemc;
 125+
122126 wfProfileIn( __METHOD__ );
123127
124 - // For empty or whitespace-only things, don't do any processing
125 - # FIXME: we should return the data unfiltered if $filter is not supported.
126 - # that would save up a md5 computation and one memcached get.
127 - if ( trim( $data ) === '' ) {
 128+ // For empty/whitespace-only data or for unknown filters, don't perform any caching or processing
 129+ if ( trim( $data ) === '' || !in_array( $filter, array( 'minify-js', 'minify-css', 'flip-css' ) ) ) {
128130 wfProfileOut( __METHOD__ );
129131 return $data;
130132 }
131133
132 - // Try memcached
 134+ // Try for Memcached hit
133135 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
134 - $cached = $wgMemc->get( $key );
135 -
136 - if ( $cached !== false && $cached !== null ) {
 136+ if ( is_string( $cache = $wgMemc->get( $key ) ) ) {
137137 wfProfileOut( __METHOD__ );
138 - return $cached;
 138+ return $cache;
139139 }
140140
141 - // Run the filter
 141+ // Run the filter - we've already verified one of these will work
142142 try {
143143 switch ( $filter ) {
144 - # note: if adding a new filter. Please update method documentation above.
145144 case 'minify-js':
146145 $result = JSMin::minify( $data );
147146 break;
@@ -150,26 +149,23 @@
151150 case 'flip-css':
152151 $result = CSSJanus::transform( $data, true, false );
153152 break;
154 - default:
155 - // Don't cache anything, just pass right through
156 - wfProfileOut( __METHOD__ );
157 - return $data;
158153 }
159154 } catch ( Exception $exception ) {
160 - throw new MWException( 'Filter threw an exception: ' . $exception->getMessage() );
 155+ throw new MWException( 'ResourceLoader filter error. Exception was thrown: ' . $exception->getMessage() );
161156 }
162157
163 - // Save filtered text to memcached
 158+ // Save filtered text to Memcached
164159 $wgMemc->set( $key, $result );
165160
166161 wfProfileOut( __METHOD__ );
 162+
167163 return $result;
168164 }
169165
170166 /* Methods */
171167
172168 /**
173 - * Registers core modules and runs registration hooks
 169+ * Registers core modules and runs registration hooks.
174170 */
175171 public function __construct() {
176172 global $IP;
@@ -186,23 +182,17 @@
187183
188184 /**
189185 * Registers a module with the ResourceLoader system.
190 - *
191 - * Note that registering the same object under multiple names is not supported
192 - * and may silently fail in all kinds of interesting ways.
193 - *
194 - * @param $name Mixed: string of name of module or array of name/object pairs
195 - * @param $object ResourceLoaderModule: module object (optional when using
196 - * multiple-registration calling style)
197 - * @return Boolean: false if there were any errors, in which case one or more
198 - * modules were not registered
199 - *
200 - * @todo We need much more clever error reporting, not just in detailing what
201 - * happened, but in bringing errors to the client in a way that they can
202 - * easily see them if they want to, such as by using FireBug
 186+ *
 187+ * @param {mixed} $name string of name of module or array of name/object pairs
 188+ * @param {ResourceLoaderModule} $object module object (optional when using multiple-registration calling style)
 189+ * @throws {MWException} If a duplicate module registration is attempted
 190+ * @throws {MWException} If something other than a ResourceLoaderModule is being registered
 191+ * @return {bool} false if there were any errors, in which case one or more modules were not registered
203192 */
204193 public function register( $name, ResourceLoaderModule $object = null ) {
 194+
205195 wfProfileIn( __METHOD__ );
206 -
 196+
207197 // Allow multiple modules to be registered in one call
208198 if ( is_array( $name ) && !isset( $object ) ) {
209199 foreach ( $name as $key => $value ) {
@@ -210,20 +200,23 @@
211201 }
212202
213203 wfProfileOut( __METHOD__ );
 204+
214205 return;
215206 }
216207
217208 // Disallow duplicate registrations
218209 if ( isset( $this->modules[$name] ) ) {
219210 // A module has already been registered by this name
220 - throw new MWException( 'Another module has already been registered as ' . $name );
 211+ throw new MWException(
 212+ 'ResourceLoader duplicate registration error. Another module has already been registered as ' . $name
 213+ );
221214 }
222 -
 215+
223216 // Validate the input (type hinting lets null through)
224217 if ( !( $object instanceof ResourceLoaderModule ) ) {
225 - throw new MWException( 'Invalid ResourceLoader module error. Instances of ResourceLoaderModule expected.' );
 218+ throw new MWException( 'ResourceLoader invalid module error. Instances of ResourceLoaderModule expected.' );
226219 }
227 -
 220+
228221 // Attach module
229222 $this->modules[$name] = $object;
230223 $object->setName( $name );
@@ -234,36 +227,35 @@
235228 /**
236229 * Gets a map of all modules and their options
237230 *
238 - * @return Array: array( modulename => ResourceLoaderModule )
 231+ * @return {array} array( modulename => ResourceLoaderModule )
239232 */
240233 public function getModules() {
241234 return $this->modules;
242235 }
243236
244237 /**
245 - * Get the ResourceLoaderModule object for a given module name
 238+ * Get the ResourceLoaderModule object for a given module name.
246239 *
247 - * @param $name String: module name
248 - * @return mixed ResourceLoaderModule or null if not registered
 240+ * @param {string} $name module name
 241+ * @return {mixed} ResourceLoaderModule if module has been registered, null otherwise
249242 */
250243 public function getModule( $name ) {
251244 return isset( $this->modules[$name] ) ? $this->modules[$name] : null;
252245 }
253246
254247 /**
255 - * Outputs a response to a resource load-request, including a content-type header
 248+ * Outputs a response to a resource load-request, including a content-type header.
256249 *
257 - * @param $context ResourceLoaderContext object
 250+ * @param {ResourceLoaderContext} $context Context in which a response should be formed
258251 */
259252 public function respond( ResourceLoaderContext $context ) {
260253 global $wgResourceLoaderMaxage, $wgCacheEpoch;
261254
262255 wfProfileIn( __METHOD__ );
263 -
 256+
264257 // Split requested modules into two groups, modules and missing
265258 $modules = array();
266259 $missing = array();
267 -
268260 foreach ( $context->getModules() as $name ) {
269261 if ( isset( $this->modules[$name] ) ) {
270262 $modules[$name] = $this->modules[$name];
@@ -272,14 +264,12 @@
273265 }
274266 }
275267
276 - // If a version wasn't specified we need a shorter expiry time for updates to
277 - // propagate to clients quickly
 268+ // If a version wasn't specified we need a shorter expiry time for updates to propagate to clients quickly
278269 if ( is_null( $context->getVersion() ) ) {
279270 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
280271 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
281272 }
282 - // If a version was specified we can use a longer expiry time since changing
283 - // version numbers causes cache misses
 273+ // If a version was specified we can use a longer expiry time since changing version numbers causes cache misses
284274 else {
285275 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
286276 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
@@ -288,9 +278,9 @@
289279 // Preload information needed to the mtime calculation below
290280 $this->preloadModuleInfo( array_keys( $modules ), $context );
291281
292 - // To send Last-Modified and support If-Modified-Since, we need to detect
293 - // the last modified time
294282 wfProfileIn( __METHOD__.'-getModifiedTime' );
 283+
 284+ // To send Last-Modified and support If-Modified-Since, we need to detect the last modified time
295285 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
296286 foreach ( $modules as $module ) {
297287 // Bypass squid cache if the request includes any private modules
@@ -300,6 +290,7 @@
301291 // Calculate maximum modified time
302292 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
303293 }
 294+
304295 wfProfileOut( __METHOD__.'-getModifiedTime' );
305296
306297 header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ? 'text/css' : 'text/javascript' ) );
@@ -315,11 +306,15 @@
316307 wfProfileOut( __METHOD__ );
317308 return;
318309 }
319 -
 310+
 311+ // Generate a response
320312 $response = $this->makeModuleResponse( $context, $modules, $missing );
 313+
 314+ // Tack on PHP warnings as a comment in debug mode
321315 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
322316 $response .= "/*\n$warnings\n*/";
323317 }
 318+
324319 // Clear any warnings from the buffer
325320 ob_clean();
326321 echo $response;
@@ -328,10 +323,12 @@
329324 }
330325
331326 /**
332 - *
333 - * @param $context ResourceLoaderContext
334 - * @param $modules Array array( modulename => ResourceLoaderModule )
335 - * @param $missing Unavailables modules (Default null)
 327+ * Generates code for a response
 328+ *
 329+ * @param {ResourceLoaderContext} $context Context in which to generate a response
 330+ * @param {array} $modules List of module objects keyed by module name
 331+ * @param {array} $missing List of unavailables modules (optional)
 332+ * @return {string} Response data
336333 */
337334 public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = null ) {
338335 // Pre-fetch blobs
@@ -341,6 +338,7 @@
342339 // Generate output
343340 $out = '';
344341 foreach ( $modules as $name => $module ) {
 342+
345343 wfProfileIn( __METHOD__ . '-' . $name );
346344
347345 // Scripts
@@ -384,7 +382,7 @@
385383 $out .= self::makeLoaderImplementScript( $name, $scripts, $styles, $messages );
386384 break;
387385 }
388 -
 386+
389387 wfProfileOut( __METHOD__ . '-' . $name );
390388 }
391389
@@ -410,9 +408,9 @@
411409 }
412410 }
413411 }
414 -
 412+
415413 /* Static Methods */
416 -
 414+
417415 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
418416 if ( is_array( $scripts ) ) {
419417 $scripts = implode( $scripts, "\n" );
Index: trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php
@@ -23,7 +23,7 @@
2424 defined( 'MEDIAWIKI' ) || die( 1 );
2525
2626 /**
27 - * ResourceLoader module based on local JS/CSS files
 27+ * ResourceLoader module based on local JavaScript/CSS files.
2828 */
2929 class ResourceLoaderFileModule extends ResourceLoaderModule {
3030
@@ -55,7 +55,7 @@
5656 /* Methods */
5757
5858 /**
59 - * Construct a new module from an options array.
 59+ * Constructs a new module from an options array.
6060 *
6161 * @param {array} $options Options array. If not given or empty, an empty module will be constructed
6262 * @param {string} $basePath base path to prepend to all paths in $options
@@ -121,7 +121,7 @@
122122 }
123123
124124 /**
125 - * Gets all scripts for a given context concatenated together
 125+ * Gets all scripts for a given context concatenated together.
126126 *
127127 * @param {ResourceLoaderContext} $context Context in which to generate script
128128 * @return {string} JavaScript code for $context
@@ -137,7 +137,7 @@
138138 }
139139
140140 /**
141 - * Gets loader script
 141+ * Gets loader script.
142142 *
143143 * @return {string} JavaScript code to be added to startup module
144144 */
@@ -149,7 +149,7 @@
150150 }
151151
152152 /**
153 - * Gets all styles for a given context concatenated together
 153+ * Gets all styles for a given context concatenated together.
154154 *
155155 * @param {ResourceLoaderContext} $context Context in which to generate styles
156156 * @return {string} CSS code for $context
@@ -185,7 +185,7 @@
186186 }
187187
188188 /**
189 - * Gets list of message keys used by this module
 189+ * Gets list of message keys used by this module.
190190 *
191191 * @return {array} List of message keys
192192 */
@@ -194,7 +194,7 @@
195195 }
196196
197197 /**
198 - * Gets the name of the group this module should be loaded in
 198+ * Gets the name of the group this module should be loaded in.
199199 *
200200 * @return {string} Group name
201201 */
@@ -203,7 +203,7 @@
204204 }
205205
206206 /**
207 - * Gets list of names of modules this module depends on
 207+ * Gets list of names of modules this module depends on.
208208 *
209209 * @return {array} List of module names
210210 */
@@ -212,10 +212,12 @@
213213 }
214214
215215 /**
216 - * Get the last modified timestamp of this module, which is calculated as the highest last modified timestamp of its
217 - * constituent files and the files it depends on. This function is context-sensitive, only performing calculations
218 - * on files relevant to the given language, skin and debug mode.
 216+ * Get the last modified timestamp of this module.
219217 *
 218+ * Last modified timestamps are calculated from the highest last modified timestamp of this module's constituent
 219+ * files as well as the files it depends on. This function is context-sensitive, only performing calculations on
 220+ * files relevant to the given language, skin and debug mode.
 221+ *
220222 * @param {ResourceLoaderContext} $context Context in which to calculate the modified time
221223 * @return {integer} UNIX timestamp
222224 * @see {ResourceLoaderModule::getFileDependencies}
@@ -262,7 +264,7 @@
263265 /* Protected Members */
264266
265267 /**
266 - * Prefixes each file path in a list
 268+ * Prefixes each file path in a list.
267269 *
268270 * @param {array} $list List of file paths in any combination of index/path or path/options pairs
269271 * @param {string} $prefix String to prepend to each file path in $list
@@ -283,7 +285,7 @@
284286 }
285287
286288 /**
287 - * Collates file paths by option (where provided)
 289+ * Collates file paths by option (where provided).
288290 *
289291 * @param {array} $list List of file paths in any combination of index/path or path/options pairs
290292 * @return {array} List of file paths, collated by $option
@@ -310,7 +312,7 @@
311313 }
312314
313315 /**
314 - * Gets a list of element that match a key, optionally using a fallback key
 316+ * Gets a list of element that match a key, optionally using a fallback key.
315317 *
316318 * @param {array} $map Map of lists to select from
317319 * @param {string} $key Key to look for in $map
@@ -327,7 +329,7 @@
328330 }
329331
330332 /**
331 - * Get the contents of a list of JavaScript files
 333+ * Gets the contents of a list of JavaScript files.
332334 *
333335 * @param {array} $scripts List of file paths to scripts to read, remap and concetenate
334336 * @return {string} Concatenated and remapped JavaScript data from $scripts
@@ -340,7 +342,7 @@
341343 }
342344
343345 /**
344 - * Get the contents of a list of CSS files
 346+ * Gets the contents of a list of CSS files.
345347 *
346348 * @param {array} $styles List of file paths to styles to read, remap and concetenate
347349 * @return {array} List of concatenated and remapped CSS data from $styles, keyed by media type
@@ -359,7 +361,7 @@
360362 }
361363
362364 /**
363 - * Reads a script file
 365+ * Reads a script file.
364366 *
365367 * This method can be used as a callback for array_map()
366368 *
@@ -373,7 +375,7 @@
374376 }
375377
376378 /**
377 - * Reads a style file
 379+ * Reads a style file.
378380 *
379381 * This method can be used as a callback for array_map()
380382 *
@@ -389,7 +391,7 @@
390392 }
391393
392394 /**
393 - * Resolve a file name
 395+ * Resolves a file name.
394396 *
395397 * This method can be used as a callback for array_map()
396398 *

Comments

#Comment by Hashar (talk | contribs)   06:29, 20 October 2010

code change in filter() looks good. Thanks for implementing my notes and fixing my crappy english 8-)

Status & tagging log