Minor resource loader changes:
[lhc/web/wiklou.git] / includes / ResourceLoaderModule.php
index 93813ba..f883c6f 100644 (file)
@@ -50,30 +50,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
         */
@@ -404,7 +380,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(),
@@ -454,6 +430,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();
@@ -478,20 +455,25 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $this->loaders,
                        $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;
-
+               wfProfileOut( __METHOD__.'-filemtime' );
+               // Only get the message timestamp if there are messages in the module
+               $msgBlobMtime = 0;
+               if ( count( $this->messages ) ) {
+                       // 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__ );
                return $this->modifiedTime[$context->getHash()];
        }
 
@@ -599,7 +581,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $deps = $wgMemc->get( $key );
 
                if ( !$deps ) {
-                       $dbr = wfGetDb( DB_SLAVE );
+                       $dbr = wfGetDB( DB_SLAVE );
                        $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
                                        'md_module' => $this->getName(),
                                        'md_skin' => $skin,
@@ -624,7 +606,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 ) {
@@ -659,7 +646,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;
        }
@@ -763,20 +753,25 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
                if ( isset( $this->modifiedTime[$hash] ) ) {
                        return $this->modifiedTime[$hash];
                }
+
                $titles = array();
                foreach ( $this->getPages( $context ) as $page => $options ) {
-                       $titles[] = Title::makeTitle( $options['ns'], $page );
+                       $titles[$options['ns']][$page] = true;
                }
-               // Do batch existence check
-               // TODO: This would work better if page_touched were loaded by this as well
-               $lb = new LinkBatch( $titles );
-               $lb->execute();
+
                $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
-               foreach ( $titles as $title ) {
-                       if ( $title->exists() ) {
-                               $modifiedTime = max( $modifiedTime, wfTimestamp( TS_UNIX, $title->getTouched() ) );
+
+               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, $modifiedTime );
                        }
                }
+
                return $this->modifiedTime[$hash] = $modifiedTime;
        }
 }
@@ -831,7 +826,7 @@ class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
 /**
  * Module for user preference customizations
  */
-class ResourceLoaderUserPreferencesModule extends ResourceLoaderModule {
+class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
 
        /* Protected Members */
 
@@ -844,39 +839,57 @@ class ResourceLoaderUserPreferencesModule extends ResourceLoaderModule {
                if ( isset( $this->modifiedTime[$hash] ) ) {
                        return $this->modifiedTime[$hash];
                }
-               if ( $context->getUser() && $user = User::newFromName( $context->getUser() ) ) {
+
+               global $wgUser;
+               $username = $context->getUser();
+               // Avoid extra db query by using $wgUser if possible
+               $user = $wgUser->getName() === $username ? $wgUser : User::newFromName( $username );
+
+               if ( $user ) {
                        return $this->modifiedTime[$hash] = $user->getTouched();
                } else {
                        return 0;
                }
        }
 
+       public function getScript( ResourceLoaderContext $context ) {
+               $user = User::newFromName( $context->getUser() );
+               if ( $user instanceof User ) {
+                       $options = FormatJson::encode( $user->getOptions() );
+               } else {
+                       $options = FormatJson::encode( User::getDefaultOptions() );
+               }
+               return "mediaWiki.user.options.set( $options );";
+       }
+
        public function getStyles( ResourceLoaderContext $context ) {
                global $wgAllowUserCssPrefs;
                if ( $wgAllowUserCssPrefs ) {
                        $user = User::newFromName( $context->getUser() );
+                       $options = $user instanceof User ? $user->getOptions() : User::getDefaultOptions();
+                       
                        $rules = array();
-                       if ( ( $underline = $user->getOption( 'underline' ) ) < 2 ) {
-                               $rules[] = "a { text-decoration: " . ( $underline ? 'underline' : 'none' ) . "; }";
+                       if ( $options['underline'] < 2 ) {
+                               $rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
                        }
-                       if ( $user->getOption( 'highlightbroken' ) ) {
+                       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 ( $user->getOption( 'justify' ) ) {
+                       if ( $options['justify'] ) {
                                $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
                        }
-                       if ( !$user->getOption( 'showtoc' ) ) {
+                       if ( !$options['showtoc'] ) {
                                $rules[] = "#toc { display: none; }\n";
                        }
-                       if ( !$user->getOption( 'editsection' ) ) {
+                       if ( !$options['editsection'] ) {
                                $rules[] = ".editsection { display: none; }\n";
                        }
-                       if ( ( $fontstyle = $user->getOption( 'editfont' ) ) !== 'default' ) {
-                               $rules[] = "textarea { font-family: $fontstyle; }\n";
+                       if ( $options['editfont'] !== 'default' ) {
+                               $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
                        }
                        return array( 'all' => implode( "\n", $rules ) );
                }
@@ -898,9 +911,11 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
        /* Protected Methods */
        
        protected function getConfig( $context ) {
-               global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension, $wgArticlePath, $wgScriptPath, $wgServer,
-                       $wgContLang, $wgBreakFrames, $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgAjaxWatch, $wgVersion,
-                       $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest, $wgSitename, $wgFileExtensions;
+               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();
@@ -959,7 +974,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
        /* Methods */
 
        public function getScript( ResourceLoaderContext $context ) {
-               global $IP, $wgStylePath, $wgLoadScript;
+               global $IP, $wgLoadScript;
 
                $scripts = file_get_contents( "$IP/resources/startup.js" );
 
@@ -969,7 +984,12 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                        // Build configuration
                        $config = FormatJson::encode( $this->getConfig( $context ) );
                        // Add a well-known start-up function
-                       $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
+                       $scripts .= <<<JAVASCRIPT
+window.startUp = function() {
+       $registration
+       mediaWiki.config.set( $config ); 
+};
+JAVASCRIPT;
                        // Build load query for jquery and mediawiki modules
                        $query = array(
                                'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
@@ -987,9 +1007,9 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                        // Build HTML code for loading jquery and mediawiki modules
                        $loadScript = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
                        // Add code to add jquery and mediawiki loading code; only if the current client is compatible
-                       $scripts .= "if ( isCompatible() ) { document.write( '$loadScript' ); }";
+                       $scripts .= "if ( isCompatible() ) { document.write( " . FormatJson::encode( $loadScript ) . "); }\n";
                        // Delete the compatible function - it's not needed anymore
-                       $scripts .= "delete window['isCompatible'];";
+                       $scripts .= "delete window['isCompatible'];\n";
                }
 
                return $scripts;
@@ -1009,14 +1029,6 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                return $this->modifiedTime[$hash];
        }
 
-       public function getClientMaxage() {
-               return 300; // 5 minutes
-       }
-
-       public function getServerMaxage() {
-               return 300; // 5 minutes
-       }
-
        public function getFlip( $context ) {
                global $wgContLang;