Seb35 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/299397

Change subject: Added support for reading serialised files in readFile; added a 
function to cache a file in serialised or PHP format; used these two functions 
in getMediaWikiConfig
......................................................................

Added support for reading serialised files in readFile; added a function to 
cache a file in serialised or PHP format; used these two functions in 
getMediaWikiConfig

This prepares for a larger change where all files will be cached.
---
M src/MediaWikiFarm.php
1 file changed, 59 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MediaWikiFarm 
refs/changes/97/299397/1

diff --git a/src/MediaWikiFarm.php b/src/MediaWikiFarm.php
index ab04a50..a5c74d5 100644
--- a/src/MediaWikiFarm.php
+++ b/src/MediaWikiFarm.php
@@ -543,16 +543,9 @@
                
                $this->params['globals'] = false;
                
-               if( $cacheFile && @filemtime( $cacheFile ) >= $oldness && 
is_string( $cacheFile ) ) {   
-                       if( preg_match( '/\.php$/', $cacheFile ) )
-                                $this->params['globals'] = @include $cacheFile;
-                       
-                       else {
-                               $cache = @file_get_contents( $cacheFile );
-                               if( $cache !== false )
-                                       $this->params['globals'] = unserialize( 
$cache );
-                       }
-               }
+               if( $cacheFile && is_string( $cacheFile ) && is_file( 
$cacheFile) && @filemtime( $cacheFile ) >= $oldness )
+                       $this->params['globals'] = $this->readFile( $cacheFile 
);
+               
                else {
                        
                        $this->params['globals'] = array(
@@ -587,21 +580,8 @@
                        $globals['extensions']['MediaWikiFarm'] = array( 
'_loading' => 'wfLoadExtension' );
                        
                        # Save this configuration in a serialised file
-                       if( $cacheFile ) {
-                               @mkdir( dirname( $cacheFile ) );
-                               $tmpFile = tempnam( dirname( $cacheFile ), 
basename( $cacheFile ).'.tmp' );
-                               chmod( $tmpFile, 0744 );
-                               if( preg_match( '/\.php$/', $cacheFile ) ) {
-                                       if( $tmpFile && file_put_contents( 
$tmpFile, "<?php\n\n// WARNING: file automatically generated: do not 
modify.\n\nreturn ".var_export( $globals, true ).';' ) ) {
-                                               rename( $tmpFile, $cacheFile );
-                                       }
-                               }
-                               else {
-                                       if( $tmpFile && file_put_contents( 
$tmpFile, serialize( $globals ) ) ) {
-                                               rename( $tmpFile, $cacheFile );
-                                       }
-                               }
-                       }
+                       if( $cacheFile )
+                               $this->cacheFile( $globals, $cacheFile );
                }
        }
        
@@ -780,29 +760,46 @@
         * -------------- */
        
        /**
-        * Read a file either in PHP, YAML (if library available), JSON, or 
dblist, and returns the interpreted array.
+        * Read a file either in PHP, YAML (if library available), JSON, 
dblist, or serialised, and returns the interpreted array.
         * 
-        * The choice between the format depends on the extension: php, yml, 
yaml, json, dblist.
+        * The choice between the format depends on the extension: php, yml, 
yaml, json, dblist, serialised.
         * 
         * @param string $filename Name of the requested file.
+        * @param string $directory Parent directory.
         * @return array|false The interpreted array in case of success, else 
false.
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
         * @SuppressWarnings(PHPMD.StaticAccess)
         */
-       function readFile( $filename ) {
+       private function readFile( $filename, $directory = '' ) {
                
                # Check parameter
-               if( !is_string( $filename ) || !is_file( $filename ) )
+               if( !is_string( $filename ) )
                        return false;
                
                # Detect the format
                # Note the regex must be greedy to correctly select double 
extensions
                $format = preg_replace( '/^.*\.([a-z]+)$/', '$1', $filename );
                
+               # Check the file exists
+               $filename = $directory ? $directory . '/' . $filename : 
$filename;
+               if( !is_file( $filename ) )
+                       return false;
+               
                # Format PHP
                if( $format == 'php' )
                        
                        $array = @include $filename;
+               
+               # Format 'serialisation'
+               elseif( $format == 'ser' ) {
+                       
+                       $content = @file_get_contents( $filename );
+                       
+                       if( !$content )
+                               return array();
+                       
+                       $array = @unserialize( $content );
+               }
                
                # Format YAML
                elseif( $format == 'yml' || $format == 'yaml' ) {
@@ -824,7 +821,7 @@
                        
                        $array = json_decode( @file_get_contents( $filename ), 
true );
                
-               # Format dblist (simple list of strings separated by newlines)
+               # Format 'dblist' (simple list of strings separated by newlines)
                elseif( $format == 'dblist' ) {
                        
                        $content = @file_get_contents( $filename );
@@ -851,6 +848,38 @@
        }
        
        /**
+        * Create a cache file.
+        * 
+        * @param array $array Array of the data to be cached.
+        * @param string $filename Name of the cache file.
+        * @param string $format Format of the cache file, in ['ser', 'php'].
+        * @return void
+        */
+       private function cacheFile( $array, $filename, $format = 'ser' ) {
+               
+               if( !array_key_exists( 'cache', $this->params ) )
+                       return;
+               
+               # Create temporary file
+               @mkdir( dirname( $filename ) );
+               $tmpFile = tempnam( dirname( $filename ), $filename.'.tmp' );
+               chmod( $tmpFile, 0644 );
+               
+               if( !$tmpFile )
+                       return;
+               
+               if( $format == 'php' ) {
+                       if( file_put_contents( $tmpFile, "<?php\n\n// WARNING: 
file automatically generated: do not modify.\n\nreturn ".var_export( $array, 
true ).';' ) )
+                               rename( $tmpFile, $filename );
+               }
+               elseif( $format == 'ser' ) {
+                       if( file_put_contents( $tmpFile, serialize( $array ) ) 
) {
+                               rename( $tmpFile, $filename );
+                       }
+               }
+       }
+       
+       /**
         * Set a wiki property and replace placeholders (property name version).
         * 
         * @param string $name Name of the property.

-- 
To view, visit https://gerrit.wikimedia.org/r/299397
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9ee30fc7a14c5979c2f52d7f7176558471d0a3c7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MediaWikiFarm
Gerrit-Branch: master
Gerrit-Owner: Seb35 <seb35wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to