diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-03-21 20:08:40 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-03-21 20:08:40 +0100 |
commit | b786c8836f0576d4feb1543471950c5d24bc7939 (patch) | |
tree | 4f81a6fce786083b9555787ecac84f1f9063990b | |
parent | bbc6b844c1749e82968c11e277f551e767576d86 (diff) | |
download | Shaarli-b786c8836f0576d4feb1543471950c5d24bc7939.tar.gz Shaarli-b786c8836f0576d4feb1543471950c5d24bc7939.tar.zst Shaarli-b786c8836f0576d4feb1543471950c5d24bc7939.zip |
Set Shaarli's version only in shaarli_version.php file
-rw-r--r-- | application/ApplicationUtils.php | 32 | ||||
-rw-r--r-- | index.php | 7 | ||||
-rw-r--r-- | tests/ApplicationUtilsTest.php | 36 |
3 files changed, 62 insertions, 13 deletions
diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index 94c49040..85dcbeeb 100644 --- a/application/ApplicationUtils.php +++ b/application/ApplicationUtils.php | |||
@@ -4,9 +4,13 @@ | |||
4 | */ | 4 | */ |
5 | class ApplicationUtils | 5 | class ApplicationUtils |
6 | { | 6 | { |
7 | /** | ||
8 | * @var string File containing the current version | ||
9 | */ | ||
10 | public static $VERSION_FILE = 'shaarli_version.php'; | ||
11 | |||
7 | private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli'; | 12 | private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli'; |
8 | private static $GIT_BRANCHES = array('latest', 'stable'); | 13 | private static $GIT_BRANCHES = array('latest', 'stable'); |
9 | private static $VERSION_FILE = 'shaarli_version.php'; | ||
10 | private static $VERSION_START_TAG = '<?php /* '; | 14 | private static $VERSION_START_TAG = '<?php /* '; |
11 | private static $VERSION_END_TAG = ' */ ?>'; | 15 | private static $VERSION_END_TAG = ' */ ?>'; |
12 | 16 | ||
@@ -29,6 +33,30 @@ class ApplicationUtils | |||
29 | return false; | 33 | return false; |
30 | } | 34 | } |
31 | 35 | ||
36 | return $data; | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Retrieve the version from a remote URL or a file. | ||
41 | * | ||
42 | * @param string $remote URL or file to fetch. | ||
43 | * @param int $timeout For URLs fetching. | ||
44 | * | ||
45 | * @return bool|string The version or false if it couldn't be retrieved. | ||
46 | */ | ||
47 | public static function getVersion($remote, $timeout = 2) | ||
48 | { | ||
49 | if (startsWith($remote, 'http')) { | ||
50 | if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) { | ||
51 | return false; | ||
52 | } | ||
53 | } else { | ||
54 | if (! is_file($remote)) { | ||
55 | return false; | ||
56 | } | ||
57 | $data = file_get_contents($remote); | ||
58 | } | ||
59 | |||
32 | return str_replace( | 60 | return str_replace( |
33 | array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL), | 61 | array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL), |
34 | array('', '', ''), | 62 | array('', '', ''), |
@@ -90,7 +118,7 @@ class ApplicationUtils | |||
90 | 118 | ||
91 | // Late Static Binding allows overriding within tests | 119 | // Late Static Binding allows overriding within tests |
92 | // See http://php.net/manual/en/language.oop5.late-static-bindings.php | 120 | // See http://php.net/manual/en/language.oop5.late-static-bindings.php |
93 | $latestVersion = static::getLatestGitVersionCode( | 121 | $latestVersion = static::getVersion( |
94 | self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE | 122 | self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE |
95 | ); | 123 | ); |
96 | 124 | ||
@@ -1,8 +1,6 @@ | |||
1 | <?php | 1 | <?php |
2 | /** | 2 | /** |
3 | * Shaarli dev - Shaare your links... | 3 | * Shaarli - The personal, minimalist, super-fast, database free, bookmarking service. |
4 | * | ||
5 | * The personal, minimalist, super-fast, database free, bookmarking service. | ||
6 | * | 4 | * |
7 | * Friendly fork by the Shaarli community: | 5 | * Friendly fork by the Shaarli community: |
8 | * - https://github.com/shaarli/Shaarli | 6 | * - https://github.com/shaarli/Shaarli |
@@ -25,7 +23,6 @@ if (date_default_timezone_get() == '') { | |||
25 | /* | 23 | /* |
26 | * PHP configuration | 24 | * PHP configuration |
27 | */ | 25 | */ |
28 | define('shaarli_version', 'dev'); | ||
29 | 26 | ||
30 | // http://server.com/x/shaarli --> /shaarli/ | 27 | // http://server.com/x/shaarli --> /shaarli/ |
31 | define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0))); | 28 | define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0))); |
@@ -90,6 +87,8 @@ try { | |||
90 | exit; | 87 | exit; |
91 | } | 88 | } |
92 | 89 | ||
90 | define('shaarli_version', ApplicationUtils::getVersion(__DIR__ .'/'. ApplicationUtils::$VERSION_FILE)); | ||
91 | |||
93 | // Force cookie path (but do not change lifetime) | 92 | // Force cookie path (but do not change lifetime) |
94 | $cookie = session_get_cookie_params(); | 93 | $cookie = session_get_cookie_params(); |
95 | $cookiedir = ''; | 94 | $cookiedir = ''; |
diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index ef4f46a8..ebdc365c 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php | |||
@@ -17,7 +17,7 @@ class FakeApplicationUtils extends ApplicationUtils | |||
17 | /** | 17 | /** |
18 | * Toggle HTTP requests, allow overriding the version code | 18 | * Toggle HTTP requests, allow overriding the version code |
19 | */ | 19 | */ |
20 | public static function getLatestGitVersionCode($url, $timeout=0) | 20 | public static function getVersion($url, $timeout=0) |
21 | { | 21 | { |
22 | return self::$VERSION_CODE; | 22 | return self::$VERSION_CODE; |
23 | } | 23 | } |
@@ -45,17 +45,27 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase | |||
45 | } | 45 | } |
46 | 46 | ||
47 | /** | 47 | /** |
48 | * Remove test version file if it exists | ||
49 | */ | ||
50 | public function tearDown() | ||
51 | { | ||
52 | if (is_file('sandbox/version.php')) { | ||
53 | unlink('sandbox/version.php'); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | /** | ||
48 | * Retrieve the latest version code available on Git | 58 | * Retrieve the latest version code available on Git |
49 | * | 59 | * |
50 | * Expected format: Semantic Versioning - major.minor.patch | 60 | * Expected format: Semantic Versioning - major.minor.patch |
51 | */ | 61 | */ |
52 | public function testGetLatestGitVersionCode() | 62 | public function testGetVersionCode() |
53 | { | 63 | { |
54 | $testTimeout = 10; | 64 | $testTimeout = 10; |
55 | 65 | ||
56 | $this->assertEquals( | 66 | $this->assertEquals( |
57 | '0.5.4', | 67 | '0.5.4', |
58 | ApplicationUtils::getLatestGitVersionCode( | 68 | ApplicationUtils::getVersion( |
59 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | 69 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' |
60 | .'v0.5.4/shaarli_version.php', | 70 | .'v0.5.4/shaarli_version.php', |
61 | $testTimeout | 71 | $testTimeout |
@@ -63,7 +73,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase | |||
63 | ); | 73 | ); |
64 | $this->assertRegExp( | 74 | $this->assertRegExp( |
65 | self::$versionPattern, | 75 | self::$versionPattern, |
66 | ApplicationUtils::getLatestGitVersionCode( | 76 | ApplicationUtils::getVersion( |
67 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | 77 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' |
68 | .'master/shaarli_version.php', | 78 | .'master/shaarli_version.php', |
69 | $testTimeout | 79 | $testTimeout |
@@ -72,14 +82,26 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase | |||
72 | } | 82 | } |
73 | 83 | ||
74 | /** | 84 | /** |
75 | * Attempt to retrieve the latest version from an invalid URL | 85 | * Attempt to retrieve the latest version from an invalid File |
86 | */ | ||
87 | public function testGetVersionCodeFromFile() | ||
88 | { | ||
89 | file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL); | ||
90 | $this->assertEquals( | ||
91 | '1.2.3', | ||
92 | ApplicationUtils::getVersion('sandbox/version.php', 1) | ||
93 | ); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Attempt to retrieve the latest version from an invalid File | ||
76 | */ | 98 | */ |
77 | public function testGetLatestGitVersionCodeInvalidUrl() | 99 | public function testGetVersionCodeInvalidFile() |
78 | { | 100 | { |
79 | $oldlog = ini_get('error_log'); | 101 | $oldlog = ini_get('error_log'); |
80 | ini_set('error_log', '/dev/null'); | 102 | ini_set('error_log', '/dev/null'); |
81 | $this->assertFalse( | 103 | $this->assertFalse( |
82 | ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1) | 104 | ApplicationUtils::getVersion('idontexist', 1) |
83 | ); | 105 | ); |
84 | ini_set('error_log', $oldlog); | 106 | ini_set('error_log', $oldlog); |
85 | } | 107 | } |