Update docs for return and exception info
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 8d50dca..7231c0f 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 /**
+ * Resource loader module based on local JavaScript/CSS files.
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -33,47 +35,74 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected $remoteBasePath = '';
        /**
         * Array: List of paths to JavaScript files to always include
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $scripts = array();
        /**
         * Array: List of JavaScript files to include when using a specific language
-        * @example array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $languageScripts = array();
        /**
         * Array: List of JavaScript files to include when using a specific skin
-        * @example array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $skinScripts = array();
        /**
         * Array: List of paths to JavaScript files to include in debug mode
-        * @example array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $debugScripts = array();
        /**
         * Array: List of paths to JavaScript files to include in the startup module
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $loaderScripts = array();
        /**
         * Array: List of paths to CSS files to always include
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $styles = array();
        /**
         * Array: List of paths to CSS files to include when using specific skins
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $skinStyles = array();
        /**
         * Array: List of modules this module depends on
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $dependencies = array();
        /**
         * Array: List of message keys used by this module
-        * @example array( [message-key], [message-key], ... )
+        * @par Usage:
+        * @code
+        * array( [message-key], [message-key], ... )
+        * @endcode
         */
        protected $messages = array();
        /** String: Name of group to load this module in */
@@ -82,14 +111,22 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected $position = 'bottom';
        /** Boolean: Link to raw files in debug mode */
        protected $debugRaw = true;
+       /** Boolean: Whether mw.loader.state() call should be omitted */
+       protected $raw = false;
        /**
         * Array: Cache for mtime
-        * @example array( [hash] => [mtime], [hash] => [mtime], ... )
+        * @par Usage:
+        * @code
+        * array( [hash] => [mtime], [hash] => [mtime], ... )
+        * @endcode
         */
        protected $modifiedTime = array();
        /**
         * Array: Place where readStyleFile() tracks file dependencies
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $localFileRefs = array();
 
@@ -106,51 +143,57 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *     to $wgScriptPath
         *
         * Below is a description for the $options array:
+        * @throws MWException
+        * @par Construction options:
         * @code
-        *      array(
-        *              // Base path to prepend to all local paths in $options. Defaults to $IP
-        *              'localBasePath' => [base path],
-        *              // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
-        *              'remoteBasePath' => [base path],
-        *              // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
-        *              'remoteExtPath' => [base path],
-        *              // Scripts to always include
-        *              'scripts' => [file path string or array of file path strings],
-        *              // Scripts to include in specific language contexts
-        *              'languageScripts' => array(
-        *                      [language code] => [file path string or array of file path strings],
-        *              ),
-        *              // Scripts to include in specific skin contexts
-        *              'skinScripts' => array(
-        *                      [skin name] => [file path string or array of file path strings],
-        *              ),
-        *              // Scripts to include in debug contexts
-        *              'debugScripts' => [file path string or array of file path strings],
-        *              // Scripts to include in the startup module
-        *              'loaderScripts' => [file path string or array of file path strings],
-        *              // Modules which must be loaded before this module
-        *              'dependencies' => [modile name string or array of module name strings],
-        *              // Styles to always load
-        *              'styles' => [file path string or array of file path strings],
-        *              // Styles to include in specific skin contexts
-        *              'skinStyles' => array(
-        *                      [skin name] => [file path string or array of file path strings],
-        *              ),
-        *              // Messages to always load
-        *              'messages' => [array of message key strings],
-        *              // Group which this module should be loaded together with
-        *              'group' => [group name string],
-        *              // Position on the page to load this module at
-        *              'position' => ['bottom' (default) or 'top']
-        *      )
+        *     array(
+        *         // Base path to prepend to all local paths in $options. Defaults to $IP
+        *         'localBasePath' => [base path],
+        *         // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
+        *         'remoteBasePath' => [base path],
+        *         // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
+        *         'remoteExtPath' => [base path],
+        *         // Scripts to always include
+        *         'scripts' => [file path string or array of file path strings],
+        *         // Scripts to include in specific language contexts
+        *         'languageScripts' => array(
+        *             [language code] => [file path string or array of file path strings],
+        *         ),
+        *         // Scripts to include in specific skin contexts
+        *         'skinScripts' => array(
+        *             [skin name] => [file path string or array of file path strings],
+        *         ),
+        *         // Scripts to include in debug contexts
+        *         'debugScripts' => [file path string or array of file path strings],
+        *         // Scripts to include in the startup module
+        *         'loaderScripts' => [file path string or array of file path strings],
+        *         // Modules which must be loaded before this module
+        *         'dependencies' => [modile name string or array of module name strings],
+        *         // Styles to always load
+        *         'styles' => [file path string or array of file path strings],
+        *         // Styles to include in specific skin contexts
+        *         'skinStyles' => array(
+        *             [skin name] => [file path string or array of file path strings],
+        *         ),
+        *         // Messages to always load
+        *         'messages' => [array of message key strings],
+        *         // Group which this module should be loaded together with
+        *         'group' => [group name string],
+        *         // Position on the page to load this module at
+        *         'position' => ['bottom' (default) or 'top']
+        *     )
         * @endcode
         */
        public function __construct( $options = array(), $localBasePath = null,
                $remoteBasePath = null )
        {
-               global $IP, $wgScriptPath;
+               global $IP, $wgScriptPath, $wgResourceBasePath;
                $this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
-               $this->remoteBasePath = $remoteBasePath === null ? $wgScriptPath : $remoteBasePath;
+               if ( $remoteBasePath !== null ) {
+                       $this->remoteBasePath = $remoteBasePath;
+               } else {
+                       $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+               }
 
                if ( isset( $options['remoteExtPath'] ) ) {
                        global $wgExtensionAssetsPath;
@@ -200,6 +243,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        break;
                                // Single booleans
                                case 'debugRaw':
+                               case 'raw':
                                        $this->{$member} = (bool) $option;
                                        break;
                        }
@@ -217,16 +261,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         */
        public function getScript( ResourceLoaderContext $context ) {
                $files = $this->getScriptFiles( $context );
-               if ( $context->getDebug() && $this->debugRaw ) {
-                       $urls = array();
-                       foreach ( $this->getScriptFiles( $context ) as $file ) {
-                               $urls[] = $this->getRemotePath( $file );
-                       }
-                       return $urls;
-               }
                return $this->readScriptFiles( $files );
        }
 
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getScriptFiles( $context ) as $file ) {
+                       $urls[] = $this->getRemotePath( $file );
+               }
+               return $urls;
+       }
+
+       /**
+        * @return bool
+        */
+       public function supportsURLLoading() {
+               return $this->debugRaw;
+       }
+
        /**
         * Gets loader script.
         *
@@ -250,20 +306,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $this->getStyleFiles( $context ),
                        $this->getFlip( $context )
                );
-               if ( !$context->getOnly() && $context->getDebug() && $this->debugRaw ) {
-                       $urls = array();
-                       foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
-                               $urls[$mediaType] = array();
-                               foreach ( $list as $file ) {
-                                       $urls[$mediaType][] = $this->getRemotePath( $file );
-                               }
-                       }
-                       return $urls;
-               }
                // Collect referenced files
                $this->localFileRefs = array_unique( $this->localFileRefs );
                // If the list has been modified since last time we cached it, update the cache
-               if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) ) {
+               if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) && !wfReadOnly() ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->replace( 'module_deps',
                                array( array( 'md_module', 'md_skin' ) ), array(
@@ -276,6 +322,21 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $styles;
        }
 
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
+                       $urls[$mediaType] = array();
+                       foreach ( $list as $file ) {
+                               $urls[$mediaType][] = $this->getRemotePath( $file );
+                       }
+               }
+               return $urls;
+       }
+
        /**
         * Gets list of message keys used by this module.
         *
@@ -310,6 +371,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $this->dependencies;
        }
 
+       /**
+        * @return bool
+        */
+       public function isRaw() {
+               return $this->raw;
+       }
+
        /**
         * Get the last modified timestamp of this module.
         *
@@ -367,7 +435,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                }
 
                wfProfileIn( __METHOD__.'-filemtime' );
-               $filesMtime = max( array_map( 'filemtime', $files ) );
+               $filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
                wfProfileOut( __METHOD__.'-filemtime' );
                $this->modifiedTime[$context->getHash()] = max(
                        $filesMtime,
@@ -483,6 +551,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * Gets the contents of a list of JavaScript files.
         *
         * @param $scripts Array: List of file paths to scripts to read, remap and concetenate
+        * @throws MWException
         * @return String: Concatenated and remapped JavaScript data from $scripts
         */
        protected function readScriptFiles( array $scripts ) {
@@ -493,10 +562,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $js = '';
                foreach ( array_unique( $scripts ) as $fileName ) {
                        $localPath = $this->getLocalPath( $fileName );
-                       $contents = file_get_contents( $localPath );
-                       if ( $contents === false ) {
+                       if ( !file_exists( $localPath ) ) {
                                throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
                        }
+                       $contents = file_get_contents( $localPath );
                        if ( $wgResourceLoaderValidateStaticJS ) {
                                // Static files don't really need to be checked as often; unlike
                                // on-wiki module they shouldn't change unexpectedly without
@@ -542,17 +611,18 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *
         * This method can be used as a callback for array_map()
         *
-        * @param $path String: File path of script file to read
+        * @param $path String: File path of style file to read
         * @param $flip bool
         *
         * @return String: CSS data in script file
+        * @throws MWException if the file doesn't exist
         */
        protected function readStyleFile( $path, $flip ) {
                $localPath = $this->getLocalPath( $path );
-               $style = file_get_contents( $localPath );
-               if ( $style === false ) {
+               if ( !file_exists( $localPath ) ) {
                        throw new MWException( __METHOD__.": style file not found: \"$localPath\"" );
                }
+               $style = file_get_contents( $localPath );
                if ( $flip ) {
                        $style = CSSJanus::transform( $style, true, false );
                }
@@ -566,12 +636,30 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                // Get and register local file references
                $this->localFileRefs = array_merge(
                        $this->localFileRefs,
-                       CSSMin::getLocalFileReferences( $style, $dir ) );
+                       CSSMin::getLocalFileReferences( $style, $dir )
+               );
                return CSSMin::remap(
                        $style, $dir, $remoteDir, true
                );
        }
 
+       /**
+        * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
+        * but returns 1 instead.
+        * @param $filename string File name
+        * @return int UNIX timestamp, or 1 if the file doesn't exist
+        */
+       protected static function safeFilemtime( $filename ) {
+               if ( file_exists( $filename ) ) {
+                       return filemtime( $filename );
+               } else {
+                       // We only ever map this function on an array if we're gonna call max() after,
+                       // so return our standard minimum timestamps here. This is 1, not 0, because
+                       // wfTimestamp(0) == NOW
+                       return 1;
+               }
+       }
+
        /**
         * Get whether CSS for this module should be flipped
         * @param $context ResourceLoaderContext