From b897c81f8cbf117828fb710f0827f124025f9a89 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 12 Mar 2017 15:02:06 +0100 Subject: [PATCH 1/3] Use 'dev' version on the master branch Allowed check branches are now `latest` and `stable`. --- application/ApplicationUtils.php | 13 +++++-------- index.php | 4 ++-- shaarli_version.php | 2 +- tests/ApplicationUtilsTest.php | 11 +++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index a0f482b..94c4904 100644 --- a/application/ApplicationUtils.php +++ b/application/ApplicationUtils.php @@ -5,7 +5,7 @@ class ApplicationUtils { private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli'; - private static $GIT_BRANCHES = array('master', 'stable'); + private static $GIT_BRANCHES = array('latest', 'stable'); private static $VERSION_FILE = 'shaarli_version.php'; private static $VERSION_START_TAG = ''; @@ -65,13 +65,10 @@ class ApplicationUtils $isLoggedIn, $branch='stable') { - if (! $isLoggedIn) { - // Do not check versions for visitors - return false; - } - - if (empty($enableCheck)) { - // Do not check if the user doesn't want to + // Do not check versions for visitors + // Do not check if the user doesn't want to + // Do not check with dev version + if (! $isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') { return false; } diff --git a/index.php b/index.php index cc7f3ca..bf35f1d 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ /shaarli/ define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0))); diff --git a/shaarli_version.php b/shaarli_version.php index 3f79a4c..9167b43 100644 --- a/shaarli_version.php +++ b/shaarli_version.php @@ -1 +1 @@ - + diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index ad86e21..ef4f46a 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -332,4 +332,15 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase ApplicationUtils::checkResourcePermissions($conf) ); } + + /** + * Check update with 'dev' as curent version (master branch). + * It should always return false. + */ + public function testCheckUpdateDev() + { + $this->assertFalse( + ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) + ); + } } From bbc6b844c1749e82968c11e277f551e767576d86 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 12 Mar 2017 15:28:23 +0100 Subject: [PATCH 2/3] Add an updateMethod to match the current remote branch for updates --- application/Updater.php | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/application/Updater.php b/application/Updater.php index fd7e207..1bc5be0 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -380,6 +380,50 @@ class Updater $this->conf->write($this->isLoggedIn); return true; } + + /** + * Update updates.check_updates_branch setting. + * + * If the current major version digit matches the latest branch + * major version digit, we set the branch to `latest`, + * otherwise we'll check updates on the `stable` branch. + * + * No update required for the dev version. + * + * Note: due to hardcoded URL and lack of dependency injection, this is not unit testable. + * + * FIXME! This needs to be removed when we switch to first digit major version + * instead of the second one since the versionning process will change. + */ + public function updateMethodCheckUpdateRemoteBranch() + { + if (shaarli_version === 'dev' || $this->conf->get('updates.check_updates_branch') === 'latest') { + return true; + } + + // Get latest branch major version digit + $latestVersion = ApplicationUtils::getLatestGitVersionCode( + 'https://raw.githubusercontent.com/shaarli/Shaarli/latest/shaarli_version.php', + 5 + ); + if (preg_match('/(\d+)\.\d+$/', $latestVersion, $matches) === false) { + return false; + } + $latestMajor = $matches[1]; + + // Get current major version digit + preg_match('/(\d+)\.\d+$/', shaarli_version, $matches); + $currentMajor = $matches[1]; + + if ($currentMajor === $latestMajor) { + $branch = 'latest'; + } else { + $branch = 'stable'; + } + $this->conf->set('updates.check_updates_branch', $branch); + $this->conf->write($this->isLoggedIn); + return true; + } } /** From b786c8836f0576d4feb1543471950c5d24bc7939 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 21 Mar 2017 20:08:40 +0100 Subject: [PATCH 3/3] Set Shaarli's version only in shaarli_version.php file --- application/ApplicationUtils.php | 32 ++++++++++++++++++++++++++-- index.php | 7 +++---- tests/ApplicationUtilsTest.php | 36 +++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index 94c4904..85dcbee 100644 --- a/application/ApplicationUtils.php +++ b/application/ApplicationUtils.php @@ -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 = ''; @@ -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 ); diff --git a/index.php b/index.php index bf35f1d..cf85197 100644 --- a/index.php +++ b/index.php @@ -1,8 +1,6 @@ /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 = ''; diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index ef4f46a..ebdc365 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -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 testGetLatestGitVersionCodeInvalidUrl() + public function testGetVersionCodeFromFile() + { + file_put_contents('sandbox/version.php', ''. 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 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); }