From ea24f9755f3595a81e3045ea2e8169e60b68be3b Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Sat, 13 Aug 2011 21:25:02 +0000 Subject: [PATCH] (bug 26486) ResourceLoader modules with paths to nonexistent files cause PHP warnings/notices to be thrown. Worked around all of them using file_exists() checks, and introduced a safe wrapper around filemtime() that checks for file_exists(). The only warning I couldn't squash is "Warning: array_map(): An error occurred while invoking the map callback" but that's due to a bug in PHP, which I've reported at https://bugs.php.net/bug.php?id=55416 --- RELEASE-NOTES-1.19 | 2 ++ .../ResourceLoaderFileModule.php | 30 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES-1.19 b/RELEASE-NOTES-1.19 index 054d2058fe..ee11da89ac 100644 --- a/RELEASE-NOTES-1.19 +++ b/RELEASE-NOTES-1.19 @@ -48,6 +48,8 @@ production. * Do not convert text in the user interface language to another script. * (bug 26283) Previewing user JS/CSS pages doesn't load other user JS/CSS pages * (bug 11374) Improved diff readability for colorblind people. +* (bug 26486) ResourceLoader modules with paths to nonexistent files cause PHP + warnings/notices to be thrown === API changes in 1.19 === * (bug 19838) siprop=interwikimap can now use the interwiki cache. diff --git a/includes/resourceloader/ResourceLoaderFileModule.php b/includes/resourceloader/ResourceLoaderFileModule.php index 8d50dcaf3e..ca215d5e10 100644 --- a/includes/resourceloader/ResourceLoaderFileModule.php +++ b/includes/resourceloader/ResourceLoaderFileModule.php @@ -367,7 +367,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, @@ -493,10 +493,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 +542,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 ); } @@ -571,6 +572,23 @@ class ResourceLoaderFileModule extends ResourceLoaderModule { $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 -- 2.20.1