mediawiki.inspect: add method for grepping loaded modules
authorOri Livneh <ori@wikimedia.org>
Tue, 26 Nov 2013 22:30:28 +0000 (14:30 -0800)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 20 Mar 2014 03:21:06 +0000 (04:21 +0100)
Example usage:

>>> mw.inspect.grep(':hover .settings-text')
["ext.uls.init"]
>>> mw.inspect.grep('options.expire')
["jquery.cookie", "mediawiki.user"]

Use cases:
* You're debugging a layout bug and want to trace a CSS rule to the module
  which inserts it.
* A JavaScript error contains a distinct and plausibly unique substring (such
  as a variable name), and you want to identify the module that is causing the
  error.

You can re-load the page with debug=1, but you still need to decipher load.php
URLs for non-file modules, and you might not be able to reproduce the issue in
debug mode. Alternately, you can grep a full clone of the production branch
that the wiki is running (assuming you have one handy), but that won't help you
if the module is a gadget.

Change-Id: Ie5f0e7d1c7022f8d399e895f157db8acefc8abee

resources/mediawiki/mediawiki.inspect.js

index e76141d..62fa410 100644 (file)
                                }
                                return [stats];
                        }
+               },
+
+               /**
+                * Perform a substring search across the JavaScript and CSS source code
+                * of all loaded modules and return an array of the names of the
+                * modules that matched.
+                *
+                * @param {string|RegExp} pattern String or regexp to match.
+                * @return {Array} Array of the names of modules that matched.
+                */
+               grep: function ( pattern ) {
+                       if ( typeof pattern.test !== 'function' ) {
+                               // Based on Y.Escape.regex from YUI v3.15.0
+                               pattern = new RegExp( pattern.replace( /[\-$\^*()+\[\]{}|\\,.?\s]/g, '\\$&' ), 'g' );
+                       }
+
+                       return $.grep( inspect.getLoadedModules(), function ( moduleName ) {
+                               var module = mw.loader.moduleRegistry[moduleName];
+
+                               // Grep module's JavaScript
+                               if ( $.isFunction( module.script ) && pattern.test( module.script.toString() ) ) {
+                                       return true;
+                               }
+
+                               // Grep module's CSS
+                               if (
+                                       $.isPlainObject( module.style ) && $.isArray( module.style.css )
+                                       && pattern.test( module.style.css.join( '' ) )
+                               ) {
+                                       // Module's CSS source matches
+                                       return true;
+                               }
+
+                               return false;
+                       } );
                }
        };