From: ArthurHoaro Date: Wed, 22 Mar 2017 17:51:20 +0000 (+0100) Subject: Merge pull request #798 from ArthurHoaro/feature/composer-lock X-Git-Tag: v0.9.0~27 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=dcc85ea619ee728361c62b2b7a7cc8f37081dfbf;hp=1c070fa8128982cd58a37a21cd6a1bef3318b5ff;p=github%2Fshaarli%2FShaarli.git Merge pull request #798 from ArthurHoaro/feature/composer-lock Include composer.lock in git files --- diff --git a/README.md b/README.md index db1b901f..cfdefc66 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,18 @@ _Do you want to share the links you discover?_ _Shaarli is a minimalist delicious clone that you can install on your own server._ _It is designed to be personal (single-user), fast and handy._ -[![](https://img.shields.io/travis/shaarli/Shaarli.svg?label=master)](https://travis-ci.org/shaarli/Shaarli) +[![](https://img.shields.io/badge/stable-v0.7.1-blue.svg)](https://github.com/shaarli/Shaarli/releases/tag/v0.7.1) [![](https://img.shields.io/travis/shaarli/Shaarli/stable.svg?label=stable)](https://travis-ci.org/shaarli/Shaarli) -[![](https://img.shields.io/github/release/shaarli/shaarli.svg)](https://github.com/shaarli/Shaarli/releases/latest/) -[![Docker repository](https://img.shields.io/docker/pulls/shaarli/shaarli.svg)](https://hub.docker.com/r/shaarli/shaarli/) +• +[![](https://img.shields.io/badge/latest-v0.8.4-blue.svg)](https://github.com/shaarli/Shaarli/releases/tag/v0.8.4) +[![](https://img.shields.io/travis/shaarli/Shaarli/latest.svg?label=latest)](https://travis-ci.org/shaarli/Shaarli) +• +[![](https://img.shields.io/badge/master-v0.9.x-blue.svg)](https://github.com/shaarli/Shaarli) +[![](https://img.shields.io/travis/shaarli/Shaarli.svg?label=master)](https://travis-ci.org/shaarli/Shaarli) [![Join the chat at https://gitter.im/shaarli/Shaarli](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/shaarli/Shaarli) [![Bountysource](https://www.bountysource.com/badge/team?team_id=19583&style=bounties_received)](https://www.bountysource.com/teams/shaarli/issues) +[![Docker repository](https://img.shields.io/docker/pulls/shaarli/shaarli.svg)](https://hub.docker.com/r/shaarli/shaarli/) ## Quickstart - [Wiki/documentation](https://github.com/shaarli/Shaarli/wiki) diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index a0f482b0..85dcbeeb 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('master', 'stable'); - private static $VERSION_FILE = 'shaarli_version.php'; + private static $GIT_BRANCHES = array('latest', 'stable'); 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('', '', ''), @@ -65,13 +93,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; } @@ -93,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/application/PageBuilder.php b/application/PageBuilder.php index 544aba7c..b133dee8 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -75,7 +75,8 @@ class PageBuilder } $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); - $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', false)); + $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); + $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); $this->tpl->assign('token', getToken($this->conf)); // To be removed with a proper theme configuration. diff --git a/application/Updater.php b/application/Updater.php index fd7e2073..0fb68c5a 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -378,6 +378,66 @@ class Updater $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL')); $this->conf->write($this->isLoggedIn); + + return true; + } + + /** + * Use ATOM feed as default. + */ + public function updateMethodAtomDefault() + { + if (!$this->conf->exists('feed.show_atom') || $this->conf->get('feed.show_atom') === true) { + return true; + } + + $this->conf->set('feed.show_atom', true); + $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; } } diff --git a/application/config/ConfigJson.php b/application/config/ConfigJson.php index 30908d90..9ef2ef56 100644 --- a/application/config/ConfigJson.php +++ b/application/config/ConfigJson.php @@ -21,8 +21,14 @@ class ConfigJson implements ConfigIO $data = str_replace(self::getPhpSuffix(), '', $data); $data = json_decode($data, true); if ($data === null) { - $error = json_last_error(); - throw new \Exception('An error occurred while parsing JSON file: error code #'. $error); + $errorCode = json_last_error(); + $error = 'An error occurred while parsing JSON configuration file ('. $filepath .'): error code #'; + $error .= $errorCode. '
➜ ' . json_last_error_msg() .''; + if ($errorCode === JSON_ERROR_SYNTAX) { + $error .= '
Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as '; + $error .= 'jsonlint.com.'; + } + throw new \Exception($error); } return $data; } diff --git a/application/config/ConfigManager.php b/application/config/ConfigManager.php index f2097410..7bfbfc72 100644 --- a/application/config/ConfigManager.php +++ b/application/config/ConfigManager.php @@ -81,7 +81,11 @@ class ConfigManager */ protected function load() { - $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); + try { + $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); + } catch (\Exception $e) { + die($e->getMessage()); + } $this->setDefaultValues(); } @@ -317,7 +321,7 @@ class ConfigManager $this->setEmpty('updates.check_updates_interval', 86400); $this->setEmpty('feed.rss_permalinks', true); - $this->setEmpty('feed.show_atom', false); + $this->setEmpty('feed.show_atom', true); $this->setEmpty('privacy.default_private_links', false); $this->setEmpty('privacy.hide_public_links', false); diff --git a/index.php b/index.php index cc7f3ca3..cf85197a 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/shaarli_version.php b/shaarli_version.php index 3f79a4c6..9167b43e 100644 --- a/shaarli_version.php +++ b/shaarli_version.php @@ -1 +1 @@ - + diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index ad86e21c..ebdc365c 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 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 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); } @@ -332,4 +354,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) + ); + } } diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index b522d616..11b6444a 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -615,4 +615,49 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertTrue($updater->updateMethodPiwikUrl()); $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL')); } + + /** + * Test updateMethodAtomDefault with show_atom set to false + * => update to true. + */ + public function testUpdateMethodAtomDefault() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('feed.show_atom', false); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertTrue($this->conf->get('feed.show_atom')); + } + /** + * Test updateMethodAtomDefault with show_atom not set. + * => nothing to do + */ + public function testUpdateMethodAtomDefaultNoExist() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + } + /** + * Test updateMethodAtomDefault with show_atom set to true. + * => nothing to do + */ + public function testUpdateMethodAtomDefaultAlreadyTrue() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('feed.show_atom', true); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + } } diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 3527f83d..d237bc80 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php @@ -40,7 +40,7 @@ class ConfigJsonTest extends \PHPUnit_Framework_TestCase * Read a non existent config file -> empty array. * * @expectedException \Exception - * @expectedExceptionMessage An error occurred while parsing JSON file: error code #4 + * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/ */ public function testReadInvalidJson() { diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html index 03ca6843..9388ef79 100644 --- a/tpl/default/page.header.html +++ b/tpl/default/page.header.html @@ -48,7 +48,7 @@ {/loop}
  • - {'RSS Feed'|t} + {'RSS Feed'|t}
  • {if="isLoggedIn()"}
  • @@ -70,7 +70,7 @@
  • - +