Merge "Add method for inspecting module dependency relations"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 31 Oct 2013 04:44:34 +0000 (04:44 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 31 Oct 2013 04:44:34 +0000 (04:44 +0000)
1  2 
resources/mediawiki/mediawiki.inspect.js

         */
        var inspect = {
  
+               /**
+                * Return a map of all dependency relationships between loaded modules.
+                *
+                * @return {Object} Maps module names to objects. Each sub-object has
+                *  two properties, 'requires' and 'requiredBy'.
+                */
+               getDependencyGraph: function () {
+                       var modules = inspect.getLoadedModules(), graph = {};
+                       $.each( modules, function ( moduleIndex, moduleName ) {
+                               var dependencies = mw.loader.moduleRegistry[moduleName].dependencies || [];
+                               graph[moduleName] = graph[moduleName] || { requiredBy: [] };
+                               graph[moduleName].requires = dependencies;
+                               $.each( dependencies, function ( depIndex, depName ) {
+                                       graph[depName] = graph[depName] || { requiredBy: [] };
+                                       graph[depName].requiredBy.push( moduleName );
+                               } );
+                       } );
+                       return graph;
+               },
                /**
                 * Calculate the byte size of a ResourceLoader module.
                 *
                 */
                auditSelectors: function ( css ) {
                        var selectors = { total: 0, matched: 0 },
 -                              style = document.createElement( 'style' );
 +                              style = document.createElement( 'style' ),
 +                              sheet, rules;
  
                        style.textContent = css;
                        document.body.appendChild( style );
 -                      $.each( style.sheet.cssRules, function ( index, rule ) {
 +                      // Standards-compliant browsers use .sheet.cssRules, IE8 uses .styleSheet.rules…
 +                      sheet = style.sheet || style.styleSheet;
 +                      rules = sheet.cssRules || sheet.rules;
 +                      $.each( rules, function ( index, rule ) {
                                selectors.total++;
                                if ( document.querySelector( rule.selectorText ) !== null ) {
                                        selectors.matched++;
                        try {
                                // Bartosz made me put this here.
                                if ( window.opera ) { throw window.opera; }
 -                              console.table( data );
 +                              // Use Function.prototype#call to force an exception on Firefox,
 +                              // which doesn't define console#table but doesn't complain if you
 +                              // try to invoke it.
 +                              console.table.call( console.table, data );
                                return;
                        } catch (e) {}
                        try {
 -                              console.log( JSON.stringify( data, null, 2 ) );
 +                              console.log( $.toJSON( data, null, 2 ) );
                                return;
                        } catch (e) {}
                        mw.log( data );
                                } );
                                sortByProperty( modules, 'allSelectors', true );
                                return modules;
-                       },
+                       }
                }
        };