]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Set Shaarli's version only in shaarli_version.php file 816/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 21 Mar 2017 19:08:40 +0000 (20:08 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 21 Mar 2017 19:08:40 +0000 (20:08 +0100)
application/ApplicationUtils.php
index.php
tests/ApplicationUtilsTest.php

index 94c490409742e053e4dc53116390362bae011f34..85dcbeebdb164858680ff68b9fbc1048340d05f1 100644 (file)
@@ -4,9 +4,13 @@
  */
 class ApplicationUtils
 {
+    /**
+     * @var string File containing the current version
+     */
+    public static $VERSION_FILE = 'shaarli_version.php';
+
     private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
     private static $GIT_BRANCHES = array('latest', 'stable');
-    private static $VERSION_FILE = 'shaarli_version.php';
     private static $VERSION_START_TAG = '<?php /* ';
     private static $VERSION_END_TAG = ' */ ?>';
 
@@ -29,6 +33,30 @@ class ApplicationUtils
             return false;
         }
 
+        return $data;
+    }
+
+    /**
+     * Retrieve the version from a remote URL or a file.
+     *
+     * @param string $remote  URL or file to fetch.
+     * @param int    $timeout For URLs fetching.
+     *
+     * @return bool|string The version or false if it couldn't be retrieved.
+     */
+    public static function getVersion($remote, $timeout = 2)
+    {
+        if (startsWith($remote, 'http')) {
+            if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
+                return false;
+            }
+        } else {
+            if (! is_file($remote)) {
+                return false;
+            }
+            $data = file_get_contents($remote);
+        }
+
         return str_replace(
             array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL),
             array('', '', ''),
@@ -90,7 +118,7 @@ class ApplicationUtils
 
         // Late Static Binding allows overriding within tests
         // See http://php.net/manual/en/language.oop5.late-static-bindings.php
-        $latestVersion = static::getLatestGitVersionCode(
+        $latestVersion = static::getVersion(
             self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE
         );
 
index bf35f1d43a47653232d899fde0940a682e1099c6..cf85197a375cb384e5dfb2b1820d6697a85e4441 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,8 +1,6 @@
 <?php
 /**
- * Shaarli dev - Shaare your links...
- *
- * The personal, minimalist, super-fast, database free, bookmarking service.
+ * Shaarli - The personal, minimalist, super-fast, database free, bookmarking service.
  *
  * Friendly fork by the Shaarli community:
  *  - https://github.com/shaarli/Shaarli
@@ -25,7 +23,6 @@ if (date_default_timezone_get() == '') {
 /*
  * PHP configuration
  */
-define('shaarli_version', 'dev');
 
 // http://server.com/x/shaarli --> /shaarli/
 define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0)));
@@ -90,6 +87,8 @@ try {
     exit;
 }
 
+define('shaarli_version', ApplicationUtils::getVersion(__DIR__ .'/'. ApplicationUtils::$VERSION_FILE));
+
 // Force cookie path (but do not change lifetime)
 $cookie = session_get_cookie_params();
 $cookiedir = '';
index ef4f46a8ea23eb4099b337110c71855bb71c7872..ebdc365c37e6fb2a174890d7ced1dd73f49c56db 100644 (file)
@@ -17,7 +17,7 @@ class FakeApplicationUtils extends ApplicationUtils
     /**
      * Toggle HTTP requests, allow overriding the version code
      */
-    public static function getLatestGitVersionCode($url, $timeout=0)
+    public static function getVersion($url, $timeout=0)
     {
         return self::$VERSION_CODE;
     }
@@ -44,18 +44,28 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * Remove test version file if it exists
+     */
+    public function tearDown()
+    {
+        if (is_file('sandbox/version.php')) {
+            unlink('sandbox/version.php');
+        }
+    }
+
     /**
      * Retrieve the latest version code available on Git
      *
      * Expected format: Semantic Versioning - major.minor.patch
      */
-    public function testGetLatestGitVersionCode()
+    public function testGetVersionCode()
     {
         $testTimeout = 10;
 
         $this->assertEquals(
             '0.5.4',
-            ApplicationUtils::getLatestGitVersionCode(
+            ApplicationUtils::getVersion(
                 'https://raw.githubusercontent.com/shaarli/Shaarli/'
                .'v0.5.4/shaarli_version.php',
                 $testTimeout
@@ -63,7 +73,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
         );
         $this->assertRegExp(
             self::$versionPattern,
-            ApplicationUtils::getLatestGitVersionCode(
+            ApplicationUtils::getVersion(
                 'https://raw.githubusercontent.com/shaarli/Shaarli/'
                .'master/shaarli_version.php',
                 $testTimeout
@@ -72,14 +82,26 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     }
 
     /**
-     * Attempt to retrieve the latest version from an invalid URL
+     * Attempt to retrieve the latest version from an invalid File
+     */
+    public function testGetVersionCodeFromFile()
+    {
+        file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL);
+        $this->assertEquals(
+            '1.2.3',
+            ApplicationUtils::getVersion('sandbox/version.php', 1)
+        );
+    }
+
+    /**
+     * Attempt to retrieve the latest version from an invalid File
      */
-    public function testGetLatestGitVersionCodeInvalidUrl()
+    public function testGetVersionCodeInvalidFile()
     {
         $oldlog = ini_get('error_log');
         ini_set('error_log', '/dev/null');
         $this->assertFalse(
-            ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1)
+            ApplicationUtils::getVersion('idontexist', 1)
         );
         ini_set('error_log', $oldlog);
     }