Remove $wgUseDataURLs as promised by the comment in DefaultSettings.php . Data URL...
[lhc/web/wiklou.git] / includes / ResourceLoaderModule.php
index d6a65ca..bda0795 100644 (file)
  * @author Roan Kattouw
  */
 
+defined( 'MEDIAWIKI' ) || die( 1 );
+
 /**
- * Interface for resource loader modules, with name registration and maxage functionality.
+ * Abstraction for resource loader modules, with name registration and maxage functionality.
  */
 abstract class ResourceLoaderModule {
+       
        /* Protected Members */
 
        protected $name = null;
+       
+       // In-object cache for file dependencies
+       protected $fileDeps = array();
+       // In-object cache for message blob mtime
+       protected $msgBlobMtime = array();
 
        /* Methods */
 
@@ -50,30 +58,6 @@ abstract class ResourceLoaderModule {
                $this->name = $name;
        }
 
-       /**
-        * The maximum number of seconds to cache this module for in the
-        * client-side (browser) cache. Override this only if you have a good
-        * reason not to use $wgResourceLoaderClientMaxage.
-        *
-        * @return Integer: cache maxage in seconds
-        */
-       public function getClientMaxage() {
-               global $wgResourceLoaderClientMaxage;
-               return $wgResourceLoaderClientMaxage;
-       }
-
-       /**
-        * The maximum number of seconds to cache this module for in the
-        * server-side (Squid / proxy) cache. Override this only if you have a
-        * good reason not to use $wgResourceLoaderServerMaxage.
-        *
-        * @return Integer: cache maxage in seconds
-        */
-       public function getServerMaxage() {
-               global $wgResourceLoaderServerMaxage;
-               return $wgResourceLoaderServerMaxage;
-       }
-
        /**
         * Get whether CSS for this module should be flipped
         */
@@ -81,8 +65,6 @@ abstract class ResourceLoaderModule {
                return $context->getDirection() === 'rtl';
        }
 
-       /* Abstract Methods */
-
        /**
         * Get all JS for this module for a given language and skin.
         * Includes all relevant JS except loader scripts.
@@ -90,7 +72,10 @@ abstract class ResourceLoaderModule {
         * @param $context ResourceLoaderContext object
         * @return String: JS
         */
-       public abstract function getScript( ResourceLoaderContext $context );
+       public function getScript( ResourceLoaderContext $context ) {
+               // Stub, override expected
+               return '';
+       }
 
        /**
         * Get all CSS for this module for a given skin.
@@ -98,7 +83,10 @@ abstract class ResourceLoaderModule {
         * @param $context ResourceLoaderContext object
         * @return array: strings of CSS keyed by media type
         */
-       public abstract function getStyles( ResourceLoaderContext $context );
+       public function getStyles( ResourceLoaderContext $context ) {
+               // Stub, override expected
+               return '';
+       }
 
        /**
         * Get the messages needed for this module.
@@ -107,14 +95,30 @@ abstract class ResourceLoaderModule {
         *
         * @return array of message keys. Keys may occur more than once
         */
-       public abstract function getMessages();
+       public function getMessages() {
+               // Stub, override expected
+               return array();
+       }
+       
+       /**
+        * Get the group this module is in.
+        * 
+        * @return string of group name
+        */
+       public function getGroup() {
+               // Stub, override expected
+               return null;
+       }
 
        /**
         * Get the loader JS for this module, if set.
         *
         * @return Mixed: loader JS (string) or false if no custom loader set
         */
-       public abstract function getLoaderScript();
+       public function getLoaderScript() {
+               // Stub, override expected
+               return false;
+       }
 
        /**
         * Get a list of modules this module depends on.
@@ -131,8 +135,78 @@ abstract class ResourceLoaderModule {
         * loader script, see getLoaderScript()
         * @return Array of module names (strings)
         */
-       public abstract function getDependencies();
+       public function getDependencies() {
+               // Stub, override expected
+               return array();
+       }
+       
+       /**
+        * Get the files this module depends on indirectly for a given skin.
+        * Currently these are only image files referenced by the module's CSS.
+        *
+        * @param $skin String: skin name
+        * @return array of files
+        */
+       public function getFileDependencies( $skin ) {
+               // Try in-object cache first
+               if ( isset( $this->fileDeps[$skin] ) ) {
+                       return $this->fileDeps[$skin];
+               }
 
+               $dbr = wfGetDB( DB_SLAVE );
+               $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
+                               'md_module' => $this->getName(),
+                               'md_skin' => $skin,
+                       ), __METHOD__
+               );
+               if ( !is_null( $deps ) ) {
+                       return $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
+               }
+               return $this->fileDeps[$skin] = array();
+       }
+       
+       /**
+        * Set preloaded file dependency information. Used so we can load this
+        * information for all modules at once.
+        * @param $skin string Skin name
+        * @param $deps array Array of file names
+        */
+       public function setFileDependencies( $skin, $deps ) {
+               $this->fileDeps[$skin] = $deps;
+       }
+       
+       /**
+        * Get the last modification timestamp of the message blob for this
+        * module in a given language.
+        * @param $lang string Language code
+        * @return int UNIX timestamp, or 0 if no blob found
+        */
+       public function getMsgBlobMtime( $lang ) {
+               if ( !count( $this->getMessages() ) )
+                       return 0;
+               
+               $dbr = wfGetDB( DB_SLAVE );
+               $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
+                               'mr_resource' => $this->getName(),
+                               'mr_lang' => $lang
+                       ), __METHOD__
+               );
+               $this->msgBlobMtime[$lang] = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
+               return $this->msgBlobMtime[$lang];
+       }
+       
+       /**
+        * Set a preloaded message blob last modification timestamp. Used so we
+        * can load this information for all modules at once.
+        * @param $lang string Language code
+        * @param $mtime int UNIX timestamp or 0 if there is no such blob
+        */
+       public function setMsgBlobMtime( $lang, $mtime ) {
+               $this->msgBlobMtime[$lang] = $mtime;
+       }
+       
+       /* Abstract Methods */
+       
        /**
         * Get this module's last modification timestamp for a given
         * combination of language, skin and debug mode flag. This is typically
@@ -143,7 +217,10 @@ abstract class ResourceLoaderModule {
         * @param $context ResourceLoaderContext object
         * @return int UNIX timestamp
         */
-       public abstract function getModifiedTime( ResourceLoaderContext $context );
+       public function getModifiedTime( ResourceLoaderContext $context ) {
+               // 0 would mean now
+               return 1;
+       }
 }
 
 /**
@@ -155,6 +232,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected $scripts = array();
        protected $styles = array();
        protected $messages = array();
+       protected $group;
        protected $dependencies = array();
        protected $debugScripts = array();
        protected $languageScripts = array();
@@ -179,7 +257,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *      array(
         *              // Required module options (mutually exclusive)
         *              'scripts' => 'dir/script.js' | array( 'dir/script1.js', 'dir/script2.js' ... ),
-        *
+        * 
         *              // Optional module options
         *              'languageScripts' => array(
         *                      '[lang name]' => 'dir/lang.js' | '[lang name]' => array( 'dir/lang1.js', 'dir/lang2.js' ... )
@@ -199,6 +277,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *                      ...
         *              ),
         *              'messages' => array( 'message1', 'message2' ... ),
+        *              'group' => 'stuff',
         *      )
         */
        public function __construct( $options = array() ) {
@@ -213,6 +292,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                case 'messages':
                                        $this->messages = (array)$value;
                                        break;
+                               case 'group':
+                                       $this->group = (string)$value;
+                                       break;
                                case 'dependencies':
                                        $this->dependencies = (array)$value;
                                        break;
@@ -262,7 +344,16 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        public function addMessages( $messages ) {
                $this->messages = array_merge( $this->messages, (array)$messages );
        }
-
+       
+       /**
+        * Sets the group of this module.
+        *
+        * @param $group string group name
+        */
+       public function setGroup( $group ) {
+               $this->group = $group;
+       }
+       
        /**
         * Add dependencies. Dependency information is taken into account when
         * loading a module on the client side. When adding a module on the
@@ -389,7 +480,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                // Only store if modified
                if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
                        $encFiles = FormatJson::encode( $files );
-                       $dbw = wfGetDb( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                        $dbw->replace( 'module_deps',
                                array( array( 'md_module', 'md_skin' ) ), array(
                                        'md_module' => $this->getName(),
@@ -412,6 +503,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $this->messages;
        }
 
+       public function getGroup() {
+               return $this->group;
+       }
+
        public function getDependencies() {
                return $this->dependencies;
        }
@@ -439,6 +534,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
                        return $this->modifiedTime[$context->getHash()];
                }
+               wfProfileIn( __METHOD__ );
                
                // Sort of nasty way we can get a flat list of files depended on by all styles
                $styles = array();
@@ -464,19 +560,11 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $this->getFileDependencies( $context->getSkin() )
                );
                
+               wfProfileIn( __METHOD__.'-filemtime' );
                $filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__, 'remapFilename' ), $files ) ) );
-
-               // Get the mtime of the message blob
-               // TODO: This timestamp is queried a lot and queried separately for each module. Maybe it should be put in memcached?
-               $dbr = wfGetDb( DB_SLAVE );
-               $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
-                               'mr_resource' => $this->getName(),
-                               'mr_lang' => $context->getLanguage()
-                       ), __METHOD__
-               );
-               $msgBlobMtime = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
-
-               $this->modifiedTime[$context->getHash()] = max( $filesMtime, $msgBlobMtime );
+               wfProfileOut( __METHOD__.'-filemtime' );
+               $this->modifiedTime[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
+               wfProfileOut( __METHOD__ );
                return $this->modifiedTime[$context->getHash()];
        }
 
@@ -564,43 +652,6 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $retval;
        }
 
-       /**
-        * Get the files this module depends on indirectly for a given skin.
-        * Currently these are only image files referenced by the module's CSS.
-        *
-        * @param $skin String: skin name
-        * @return array of files
-        */
-       protected function getFileDependencies( $skin ) {
-               // Try in-object cache first
-               if ( isset( $this->fileDeps[$skin] ) ) {
-                       return $this->fileDeps[$skin];
-               }
-
-               // Now try memcached
-               global $wgMemc;
-
-               $key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $skin );
-               $deps = $wgMemc->get( $key );
-
-               if ( !$deps ) {
-                       $dbr = wfGetDb( DB_SLAVE );
-                       $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
-                                       'md_module' => $this->getName(),
-                                       'md_skin' => $skin,
-                               ), __METHOD__
-                       );
-                       if ( !$deps ) {
-                               $deps = '[]'; // Empty array so we can do negative caching
-                       }
-                       $wgMemc->set( $key, $deps );
-               }
-
-               $this->fileDeps = FormatJson::decode( $deps, true );
-
-               return $this->fileDeps;
-       }
-
        /**
         * Get the contents of a set of files and concatenate them, with
         * newlines in between. Each file is used only once.
@@ -609,7 +660,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return String: concatenated contents of $files
         */
        protected static function concatScripts( $files ) {
-               return implode( "\n", array_map( 'file_get_contents', array_map( array( __CLASS__, 'remapFilename' ), array_unique( (array) $files ) ) ) );
+               return implode( "\n", 
+                       array_map( 
+                               'file_get_contents', 
+                               array_map( 
+                                       array( __CLASS__, 'remapFilename' ), 
+                                       array_unique( (array) $files ) ) ) );
        }
 
        protected static function organizeFilesByOption( $files, $option, $default ) {
@@ -644,7 +700,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $styles = self::organizeFilesByOption( $styles, 'media', 'all' );
                foreach ( $styles as $media => $files ) {
                        $styles[$media] =
-                               implode( "\n", array_map( array( __CLASS__, 'remapStyle' ), array_unique( (array) $files ) ) );
+                               implode( "\n", 
+                                       array_map( 
+                                               array( __CLASS__, 'remapStyle' ), 
+                                               array_unique( (array) $files ) ) );
                }
                return $styles;
        }
@@ -669,51 +728,110 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return string Remapped CSS
         */
        protected static function remapStyle( $file ) {
-               global $wgUseDataURLs;
-               return CSSMin::remap( file_get_contents( self::remapFilename( $file ) ), dirname( $file ), $wgUseDataURLs );
+               global $wgScriptPath;
+               return CSSMin::remap(
+                       file_get_contents( self::remapFilename( $file ) ),
+                       dirname( $file ),
+                       $wgScriptPath . '/' . dirname( $file ),
+                       true
+               );
        }
 }
 
+/**
+ * Abstraction for resource loader modules which pull from wiki pages
+ */
 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
        
        /* Protected Members */
        
        // In-object cache for modified time
-       protected $modifiedTime = null;
+       protected $modifiedTime = array();
        
        /* Abstract Protected Methods */
        
        abstract protected function getPages( ResourceLoaderContext $context );
        
-       /* Methods */
+       /* Protected Methods */
+       
+       protected function getContent( $page, $ns ) {
+               if ( $ns === NS_MEDIAWIKI ) {
+                       return wfMsgExt( $page, 'content' );
+               }
+               if ( $title = Title::newFromText( $page, $ns ) ) {
+                       if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) {
+                               return $revision->getRawText();
+                       }
+               }
+               return null;
+       }
        
+       /* Methods */
+
+       public function getScript( ResourceLoaderContext $context ) {
+               global $wgCanonicalNamespaceNames;
+               
+               $scripts = '';
+               foreach ( $this->getPages( $context ) as $page => $options ) {
+                       if ( $options['type'] === 'script' ) {
+                               if ( $script = $this->getContent( $page, $options['ns'] ) ) {
+                                       $ns = $wgCanonicalNamespaceNames[$options['ns']];
+                                       $scripts .= "/*$ns:$page */\n$script\n";
+                               }
+                       }
+               }
+               return $scripts;
+       }
+
+       public function getStyles( ResourceLoaderContext $context ) {
+               global $wgCanonicalNamespaceNames;
+               
+               $styles = array();
+               foreach ( $this->getPages( $context ) as $page => $options ) {
+                       if ( $options['type'] === 'style' ) {
+                               $media = isset( $options['media'] ) ? $options['media'] : 'all';
+                               if ( $style = $this->getContent( $page, $options['ns'] ) ) {
+                                       if ( !isset( $styles[$media] ) ) {
+                                               $styles[$media] = '';
+                                       }
+                                       $ns = $wgCanonicalNamespaceNames[$options['ns']];
+                                       $styles[$media] .= "/* $ns:$page */\n$style\n";
+                               }
+                       }
+               }
+               return $styles;
+       }
+
        public function getModifiedTime( ResourceLoaderContext $context ) {
-               if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
-                       return $this->modifiedTime[$context->getHash()];
+               $hash = $context->getHash();
+               if ( isset( $this->modifiedTime[$hash] ) ) {
+                       return $this->modifiedTime[$hash];
+               }
+
+               $titles = array();
+               foreach ( $this->getPages( $context ) as $page => $options ) {
+                       $titles[$options['ns']][$page] = true;
                }
-               $pages = $this->getPages( $context );
-               foreach ( $pages as $i => $page ) {
-                       $pages[$i] = Title::makeTitle( NS_MEDIAWIKI, $page );
-               }
-               // Do batch existence check
-               // TODO: This would work better if page_touched were loaded by this as well
-               $lb = new LinkBatch( $pages );
-               $lb->execute();
-               $this->modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
-               foreach ( $pages as $page ) {
-                       if ( $page->exists() ) {
-                               $this->modifiedTime = max( $this->modifiedTime, wfTimestamp( TS_UNIX, $page->getTouched() ) );
+
+               $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
+
+               if ( $titles ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
+                               $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
+                               __METHOD__ );
+
+                       if ( $latest ) {
+                               $modifiedTime = wfTimestamp( TS_UNIX, $latest );
                        }
                }
-               return $this->modifiedTime;
+
+               return $this->modifiedTime[$hash] = $modifiedTime;
        }
-       public function getMessages() { return array(); }
-       public function getLoaderScript() { return ''; }
-       public function getDependencies() { return array(); }
 }
 
 /**
- * Custom module for MediaWiki:Common.js and MediaWiki:Skinname.js
+ * Module for site customizations
  */
 class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
 
@@ -722,128 +840,326 @@ class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
        protected function getPages( ResourceLoaderContext $context ) {
                global $wgHandheldStyle;
                
-               // HACK: We duplicate the message names from generateUserJs() and generateUserCss here and weird things (i.e.
-               // mtime moving backwards) can happen when a MediaWiki:Something.js page is deleted
                $pages = array(
-                       'Common.js',
-                       'Common.css',
-                       ucfirst( $context->getSkin() ) . '.js',
-                       ucfirst( $context->getSkin() ) . '.css',
-                       'Print.css',
+                       'Common.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
+                       'Common.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
+                       ucfirst( $context->getSkin() ) . '.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
+                       ucfirst( $context->getSkin() ) . '.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
+                       'Print.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'print' ),
                );
                if ( $wgHandheldStyle ) {
-                       $pages[] = 'Handheld.css';
+                       $pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'handheld' );
                }
                return $pages;
        }
+       
+       /* Methods */
+       
+       public function getGroup() {
+               return 'site';
+       }
+}
+
+/**
+ * Module for user customizations
+ */
+class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
+
+       /* Protected Methods */
+
+       protected function getPages( ResourceLoaderContext $context ) {
+               global $wgAllowUserCss;
+               
+               if ( $context->getUser() && $wgAllowUserCss ) {
+                       $username = $context->getUser();
+                       return array(
+                               "$username/common.js" => array( 'ns' => NS_USER, 'type' => 'script' ),
+                               "$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER, 'type' => 'script' ),
+                               "$username/common.css" => array( 'ns' => NS_USER, 'type' => 'style' ),
+                               "$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER, 'type' => 'style' ),
+                       );
+               }
+               return array();
+       }
+       
+       /* Methods */
+       
+       public function getGroup() {
+               return 'user';
+       }
+}
+
+/**
+ * Module for user preference customizations
+ */
+class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
+
+       /* Protected Members */
+
+       protected $modifiedTime = array();
 
        /* Methods */
 
+       public function getModifiedTime( ResourceLoaderContext $context ) {
+               $hash = $context->getHash();
+               if ( isset( $this->modifiedTime[$hash] ) ) {
+                       return $this->modifiedTime[$hash];
+               }
+
+               global $wgUser;
+
+               if ( $context->getUser() === $wgUser->getName() ) {
+                       return $this->modifiedTime[$hash] = $wgUser->getTouched();
+               } else {
+                       return 1;
+               }
+       }
+
+       /**
+        * Fetch the context's user options, or if it doesn't match current user,
+        * the default options.
+        * 
+        * @param ResourceLoaderContext $context
+        * @return array
+        */
+       protected function contextUserOptions( ResourceLoaderContext $context ) {
+               global $wgUser;
+
+               // Verify identity -- this is a private module
+               if ( $context->getUser() === $wgUser->getName() ) {
+                       return $wgUser->getOptions();
+               } else {
+                       return User::getDefaultOptions();
+               }
+       }
+
        public function getScript( ResourceLoaderContext $context ) {
-               return Skin::newFromKey( $context->getSkin() )->generateUserJs();
+               $encOptions = FormatJson::encode( $this->contextUserOptions( $context ) );
+               return "mediaWiki.user.options.set( $encOptions );";
        }
 
        public function getStyles( ResourceLoaderContext $context ) {
-               global $wgHandheldStyle;
-               $styles = array(
-                       'all' => array( 'Common.css', $context->getSkin() . '.css' ),
-                       'print' => array( 'Print.css' ),
-               );
-               if ( $wgHandheldStyle ) {
-                       $sources['handheld'] = array( 'Handheld.css' );
-               }
-               foreach ( $styles as $media => $messages ) {
-                       foreach ( $messages as $i => $message ) {
-                               $style = wfMsgExt( $message, 'content' );
-                               if ( !wfEmptyMsg( $message, $style ) ) {
-                                       $styles[$media][$i] = $style;
-                               }
+               global $wgAllowUserCssPrefs;
+
+               if ( $wgAllowUserCssPrefs ) {
+                       $options = $this->contextUserOptions( $context );
+
+                       // Build CSS rules
+                       $rules = array();
+                       if ( $options['underline'] < 2 ) {
+                               $rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
                        }
+                       if ( $options['highlightbroken'] ) {
+                               $rules[] = "a.new, #quickbar a.new { color: #CC2200; }\n";
+                       } else {
+                               $rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
+                               $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #CC2200; }";
+                               $rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
+                       }
+                       if ( $options['justify'] ) {
+                               $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
+                       }
+                       if ( !$options['showtoc'] ) {
+                               $rules[] = "#toc { display: none; }\n";
+                       }
+                       if ( !$options['editsection'] ) {
+                               $rules[] = ".editsection { display: none; }\n";
+                       }
+                       if ( $options['editfont'] !== 'default' ) {
+                               $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
+                       }
+                       return array( 'all' => implode( "\n", $rules ) );
                }
-               foreach ( $styles as $media => $messages ) {
-                       $styles[$media] = implode( "\n", $messages );
-               }
-               return $styles;
+               return array();
+       }
+
+       public function getFlip( $context ) {
+               global $wgContLang;
+
+               return $wgContLang->getDir() !== $context->getDirection();
+       }
+
+       public function getGroup() {
+               return 'private';
        }
 }
 
 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
        /* Protected Members */
 
-       protected $modifiedTime = null;
+       protected $modifiedTime = array();
+
+       /* Protected Methods */
+       
+       protected function getConfig( $context ) {
+               global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension, 
+                       $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames, 
+                       $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion, 
+                       $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest, 
+                       $wgSitename, $wgFileExtensions;
+
+               // Pre-process information
+               $separatorTransTable = $wgContLang->separatorTransformTable();
+               $separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
+               $compactSeparatorTransTable = array(
+                       implode( "\t", array_keys( $separatorTransTable ) ),
+                       implode( "\t", $separatorTransTable ),
+               );
+               $digitTransTable = $wgContLang->digitTransformTable();
+               $digitTransTable = $digitTransTable ? $digitTransTable : array();
+               $compactDigitTransTable = array(
+                       implode( "\t", array_keys( $digitTransTable ) ),
+                       implode( "\t", $digitTransTable ),
+               );
+               $mainPage = Title::newMainPage();
+               
+               // Build list of variables
+               $vars = array(
+                       'wgLoadScript' => $wgLoadScript,
+                       'debug' => $context->getDebug(),
+                       'skin' => $context->getSkin(),
+                       'stylepath' => $wgStylePath,
+                       'wgUrlProtocols' => wfUrlProtocols(),
+                       'wgArticlePath' => $wgArticlePath,
+                       'wgScriptPath' => $wgScriptPath,
+                       'wgScriptExtension' => $wgScriptExtension,
+                       'wgScript' => $wgScript,
+                       'wgVariantArticlePath' => $wgVariantArticlePath,
+                       'wgActionPaths' => $wgActionPaths,
+                       'wgServer' => $wgServer,
+                       'wgUserLanguage' => $context->getLanguage(),
+                       'wgContentLanguage' => $wgContLang->getCode(),
+                       'wgBreakFrames' => $wgBreakFrames,
+                       'wgVersion' => $wgVersion,
+                       'wgEnableAPI' => $wgEnableAPI,
+                       'wgEnableWriteAPI' => $wgEnableWriteAPI,
+                       'wgSeparatorTransformTable' => $compactSeparatorTransTable,
+                       'wgDigitTransformTable' => $compactDigitTransTable,
+                       'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
+                       'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
+                       'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
+                       'wgSiteName' => $wgSitename,
+                       'wgFileExtensions' => $wgFileExtensions,
+                       'wgDBname' => $wgDBname,
+               );
+               if ( $wgContLang->hasVariants() ) {
+                       $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
+               }
+               if ( $wgUseAjax && $wgEnableMWSuggest ) {
+                       $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
+               }
+               
+               return $vars;
+       }
+       
+       /**
+        * Gets registration code for all modules
+        *
+        * @param $context ResourceLoaderContext object
+        * @return String: JavaScript code for registering all modules with the client loader
+        */
+       public static function getModuleRegistrations( ResourceLoaderContext $context ) {
+               wfProfileIn( __METHOD__ );
+               
+               $out = '';
+               $registrations = array();
+               foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
+                       // Support module loader scripts
+                       if ( ( $loader = $module->getLoaderScript() ) !== false ) {
+                               $deps = $module->getDependencies();
+                               $group = $module->getGroup();
+                               $version = wfTimestamp( TS_ISO_8601_BASIC, round( $module->getModifiedTime( $context ), -2 ) );
+                               $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
+                       }
+                       // Automatically register module
+                       else {
+                               // Modules without dependencies or a group pass two arguments (name, timestamp) to 
+                               // mediaWiki.loader.register()
+                               if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
+                                       $registrations[] = array( $name, $module->getModifiedTime( $context ) );
+                               }
+                               // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies) 
+                               // to mediaWiki.loader.register()
+                               else if ( $module->getGroup() === null ) {
+                                       $registrations[] = array(
+                                               $name, $module->getModifiedTime( $context ),  $module->getDependencies() );
+                               }
+                               // Modules with dependencies pass four arguments (name, timestamp, dependencies, group) 
+                               // to mediaWiki.loader.register()
+                               else {
+                                       $registrations[] = array(
+                                               $name, $module->getModifiedTime( $context ),  $module->getDependencies(), $module->getGroup() );
+                               }
+                       }
+               }
+               $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
+               
+               wfProfileOut( __METHOD__ );
+               return $out;
+       }
 
        /* Methods */
 
        public function getScript( ResourceLoaderContext $context ) {
-               global $IP;
-
-               $scripts = file_get_contents( "$IP/resources/startup.js" );
+               global $IP, $wgLoadScript;
 
+               $out = file_get_contents( "$IP/resources/startup.js" );
                if ( $context->getOnly() === 'scripts' ) {
-                       // Get all module registrations
-                       $registration = ResourceLoader::getModuleRegistrations( $context );
-                       // Build configuration
-                       $config = FormatJson::encode(
-                               array( 'server' => $context->getServer(), 'debug' => $context->getDebug() )
-                       );
-                       // Add a well-known start-up function
-                       $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
                        // Build load query for jquery and mediawiki modules
-                       $query = wfArrayToCGI(
-                               array(
-                                       'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
-                                       'only' => 'scripts',
-                                       'lang' => $context->getLanguage(),
-                                       'dir' => $context->getDirection(),
-                                       'skin' => $context->getSkin(),
-                                       'debug' => $context->getDebug(),
-                                       'version' => wfTimestamp( TS_ISO_8601, round( max(
-                                               ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
-                                               ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
-                                       ), -2 ) )
-                               )
+                       $query = array(
+                               'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
+                               'only' => 'scripts',
+                               'lang' => $context->getLanguage(),
+                               'skin' => $context->getSkin(),
+                               'debug' => $context->getDebug() ? 'true' : 'false',
+                               'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
+                                       $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
+                                       $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
+                               ), -2 ) )
                        );
-
-                       // Build HTML code for loading jquery and mediawiki modules
-                       $loadScript = Html::linkedScript( $context->getServer() . "?$query" );
-                       // Add code to add jquery and mediawiki loading code; only if the current client is compatible
-                       $scripts .= "if ( isCompatible() ) { document.write( '$loadScript' ); }";
-                       // Delete the compatible function - it's not needed anymore
-                       $scripts .= "delete window['isCompatible'];";
+                       // Ensure uniform query order
+                       ksort( $query );
+                       
+                       // Startup function
+                       $configuration = FormatJson::encode( $this->getConfig( $context ) );
+                       $registrations = self::getModuleRegistrations( $context );
+                       $out .= "var startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};";
+                       
+                       // Conditional script injection
+                       $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
+                       $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete isCompatible;";
                }
 
-               return $scripts;
+               return $out;
        }
 
        public function getModifiedTime( ResourceLoaderContext $context ) {
                global $IP;
 
-               if ( !is_null( $this->modifiedTime ) ) {
-                       return $this->modifiedTime;
+               $hash = $context->getHash();
+               if ( isset( $this->modifiedTime[$hash] ) ) {
+                       return $this->modifiedTime[$hash];
                }
+               $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
 
-               // HACK getHighestModifiedTime() calls this function, so protect against infinite recursion
-               $this->modifiedTime = filemtime( "$IP/resources/startup.js" );
-               $this->modifiedTime = ResourceLoader::getHighestModifiedTime( $context );
-               return $this->modifiedTime;
-       }
-
-       public function getClientMaxage() {
-               return 300; // 5 minutes
-       }
-
-       public function getServerMaxage() {
-               return 300; // 5 minutes
+               // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
+               // before making changes to this code!
+               $time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice
+               foreach ( $context->getResourceLoader()->getModules() as $module ) {
+                       $time = max( $time, $module->getModifiedTime( $context ) );
+               }
+               return $this->modifiedTime[$hash] = $time;
        }
 
-       public function getStyles( ResourceLoaderContext $context ) { return array(); }
-
        public function getFlip( $context ) {
                global $wgContLang;
 
                return $wgContLang->getDir() !== $context->getDirection();
        }
-       public function getMessages() { return array(); }
-       public function getLoaderScript() { return ''; }
-       public function getDependencies() { return array(); }
+       
+       /* Methods */
+       
+       public function getGroup() {
+               return 'startup';
+       }
 }