[MediaWiki-commits] [Gerrit] mediawiki...MediaWikiFarm[master]: Improved reading of config files

2017-09-10 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/377055 )

Change subject: Improved reading of config files
..


Improved reading of config files

1. The cached files are always prefered over the origin files, even for the
   files in .php or .ser format.
2. An additional safeguard is in place for PHP files in case of parsing
   errors; this will only work for PHP 7+, but is compatible with PHP 5.2.
3. Avoid edge cases when the origin file and the cached file have the same
   timestamp: possibly the cached file has an old version and there is no
   mean to know this fact (I guess I/O are sometimes written in batch with
   the same timestamp, explaining this fact).

Change-Id: Ia76d2ad46f9f3124da8e681c73de275d0da579ef
---
M .gitignore
M src/Utils.php
M tests/phpunit/InstallationIndependantTest.php
M tests/phpunit/MediaWikiFarmTestCase.php
M tests/phpunit/MultiversionInstallationTest.php
5 files changed, 39 insertions(+), 13 deletions(-)

Approvals:
  Seb35: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/.gitignore b/.gitignore
index 07aae69..050dc52 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,6 +14,7 @@
 # Tests
 /phpunitHTTP404.php
 /tests/perfs/results
+/tests/phpunit/data/config/badsyntax.php
 /tests/phpunit/data/config/badsyntax.json
 /tests/phpunit/data/config/deployments.php
 /tests/phpunit/data/config/empty.json
diff --git a/src/Utils.php b/src/Utils.php
index 59125c8..46a1ea9 100644
--- a/src/Utils.php
+++ b/src/Utils.php
@@ -46,10 +46,20 @@
$format = null;
}
 
-   # Format PHP
-   if( $format == '.php' ) {
+   # Cached version - avoid the case where the timestamps are the 
same, the two files could have non-coherent versions
+   if( $cachedFile && is_string( $format ) && is_file( $cachedFile 
) && filemtime( $cachedFile ) > filemtime( $prefixedFile ) ) {
 
-   $array = include $prefixedFile;
+   return self::readFile( $filename . '.php', $cacheDir, 
$log, $cacheDir . '/config', false );
+   }
+
+   # Format PHP
+   elseif( $format == '.php' ) {
+
+   try {
+   $array = @include $prefixedFile;
+   } catch( Throwable $e ) {
+   $array = null;
+   }
}
 
# Format 'serialisation'
@@ -63,12 +73,6 @@
else {
$array = unserialize( $content );
}
-   }
-
-   # Cached version
-   elseif( $cachedFile && is_string( $format ) && is_file( 
$cachedFile ) && filemtime( $cachedFile ) >= filemtime( $prefixedFile ) ) {
-
-   return self::readFile( $filename . '.php', $cacheDir, 
$log, $cacheDir . '/config', false );
}
 
# Format YAML
@@ -136,7 +140,8 @@
# Regular return for arrays
if( is_array( $array ) ) {
 
-   if( $cachedFile && $directory != $cacheDir . '/config' 
&& ( !is_file( $cachedFile ) || ( filemtime( $cachedFile ) < filemtime( 
$prefixedFile ) ) ) ) {
+   # Cache this version - avoid the case where the 
timestamps are the same, the two files could have non-coherent versions
+   if( $cachedFile && $directory != $cacheDir . '/config' 
&& ( !is_file( $cachedFile ) || ( filemtime( $cachedFile ) <= filemtime( 
$prefixedFile ) ) ) ) {
self::cacheFile( $array, $filename . '.php', 
$cacheDir . '/config' );
}
 
diff --git a/tests/phpunit/InstallationIndependantTest.php 
b/tests/phpunit/InstallationIndependantTest.php
index 250fd61..ea1b88a 100644
--- a/tests/phpunit/InstallationIndependantTest.php
+++ b/tests/phpunit/InstallationIndependantTest.php
@@ -242,6 +242,21 @@
}
 
/**
+* Test reading a badly-formatted PHP file.
+*
+* @requires PHP 7.0
+* @covers MediaWikiFarm::readFile
+* @covers MediaWikiFarmUtils::readFile
+* @uses MediaWikiFarm::__construct
+* @uses MediaWikiFarm::selectFarm
+*/
+   function testBadSyntaxFileReadingPHP() {
+
+   $result = $this->farm->readFile( 'badsyntax.php', 
self::$wgMediaWikiFarmConfigDir );
+   $this->assertFalse( $result );
+   }
+
+   /**
 * Test reading a badly-formatted YAML file.
 *
 * @covers MediaWikiFarm::readFile
@@ -342,7 +357,9 @@
 
$farm = new MediaWikiFarm( 
'a.testfarm-monoversion.example.org', null, self::$wgMediaWikiFarmConfigDir, 
null, self::$wgMediaWikiFarmCacheDir );
 
+   # Simulate an old origin file, so that the cached version (with 
current 

[MediaWiki-commits] [Gerrit] mediawiki...MediaWikiFarm[master]: Improved reading of config files

2017-09-10 Thread Seb35 (Code Review)
Seb35 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/377055 )

Change subject: Improved reading of config files
..

Improved reading of config files

1. The cached files are always prefered over the origin files, even for the
   files in .php or .ser format.
2. An additional safeguard is in place for PHP files in case of parsing
   errors; this will only work for PHP 7+, but is compatible with PHP 5.2.
3. Avoid edge cases when the origin file and the cached file have the same
   timestamp: possibly the cached file has an old version and there is no
   mean to know this fact (I guess I/O are sometimes written in batch with
   the same timestamp, explaining this fact).

Change-Id: Ia76d2ad46f9f3124da8e681c73de275d0da579ef
---
M .gitignore
M src/Utils.php
M tests/phpunit/InstallationIndependantTest.php
M tests/phpunit/MediaWikiFarmTestCase.php
M tests/phpunit/MultiversionInstallationTest.php
5 files changed, 39 insertions(+), 13 deletions(-)


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

diff --git a/.gitignore b/.gitignore
index 07aae69..050dc52 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,6 +14,7 @@
 # Tests
 /phpunitHTTP404.php
 /tests/perfs/results
+/tests/phpunit/data/config/badsyntax.php
 /tests/phpunit/data/config/badsyntax.json
 /tests/phpunit/data/config/deployments.php
 /tests/phpunit/data/config/empty.json
diff --git a/src/Utils.php b/src/Utils.php
index 59125c8..46a1ea9 100644
--- a/src/Utils.php
+++ b/src/Utils.php
@@ -46,10 +46,20 @@
$format = null;
}
 
-   # Format PHP
-   if( $format == '.php' ) {
+   # Cached version - avoid the case where the timestamps are the 
same, the two files could have non-coherent versions
+   if( $cachedFile && is_string( $format ) && is_file( $cachedFile 
) && filemtime( $cachedFile ) > filemtime( $prefixedFile ) ) {
 
-   $array = include $prefixedFile;
+   return self::readFile( $filename . '.php', $cacheDir, 
$log, $cacheDir . '/config', false );
+   }
+
+   # Format PHP
+   elseif( $format == '.php' ) {
+
+   try {
+   $array = @include $prefixedFile;
+   } catch( Throwable $e ) {
+   $array = null;
+   }
}
 
# Format 'serialisation'
@@ -63,12 +73,6 @@
else {
$array = unserialize( $content );
}
-   }
-
-   # Cached version
-   elseif( $cachedFile && is_string( $format ) && is_file( 
$cachedFile ) && filemtime( $cachedFile ) >= filemtime( $prefixedFile ) ) {
-
-   return self::readFile( $filename . '.php', $cacheDir, 
$log, $cacheDir . '/config', false );
}
 
# Format YAML
@@ -136,7 +140,8 @@
# Regular return for arrays
if( is_array( $array ) ) {
 
-   if( $cachedFile && $directory != $cacheDir . '/config' 
&& ( !is_file( $cachedFile ) || ( filemtime( $cachedFile ) < filemtime( 
$prefixedFile ) ) ) ) {
+   # Cache this version - avoid the case where the 
timestamps are the same, the two files could have non-coherent versions
+   if( $cachedFile && $directory != $cacheDir . '/config' 
&& ( !is_file( $cachedFile ) || ( filemtime( $cachedFile ) <= filemtime( 
$prefixedFile ) ) ) ) {
self::cacheFile( $array, $filename . '.php', 
$cacheDir . '/config' );
}
 
diff --git a/tests/phpunit/InstallationIndependantTest.php 
b/tests/phpunit/InstallationIndependantTest.php
index 250fd61..ea1b88a 100644
--- a/tests/phpunit/InstallationIndependantTest.php
+++ b/tests/phpunit/InstallationIndependantTest.php
@@ -242,6 +242,21 @@
}
 
/**
+* Test reading a badly-formatted PHP file.
+*
+* @requires PHP 7.0
+* @covers MediaWikiFarm::readFile
+* @covers MediaWikiFarmUtils::readFile
+* @uses MediaWikiFarm::__construct
+* @uses MediaWikiFarm::selectFarm
+*/
+   function testBadSyntaxFileReadingPHP() {
+
+   $result = $this->farm->readFile( 'badsyntax.php', 
self::$wgMediaWikiFarmConfigDir );
+   $this->assertFalse( $result );
+   }
+
+   /**
 * Test reading a badly-formatted YAML file.
 *
 * @covers MediaWikiFarm::readFile
@@ -342,7 +357,9 @@
 
$farm = new MediaWikiFarm( 
'a.testfarm-monoversion.example.org', null, self::$wgMediaWikiFarmConfigDir, 
null, self::$wgMediaWikiFarmCacheDir );
 
+   # Simulate an old origin file, so that the cached version