]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
application: default to the "stable" branch for update checks 394/head
authorVirtualTam <virtualtam@flibidi.net>
Thu, 26 Nov 2015 23:10:43 +0000 (00:10 +0100)
committerVirtualTam <virtualtam@flibidi.net>
Thu, 26 Nov 2015 23:10:43 +0000 (00:10 +0100)
Relates to #372
Relates to #390

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
application/ApplicationUtils.php
index.php
tests/ApplicationUtilsTest.php
tpl/page.footer.html

index c7414b776e56fdef23e5aef76f358566e7d50e21..6d878110cfe984f7e5b6e71d8d0801a888e673eb 100644 (file)
@@ -5,7 +5,7 @@
 class ApplicationUtils
 {
     private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
-    private static $GIT_BRANCH = 'master';
+    private static $GIT_BRANCHES = array('master', 'stable');
     private static $VERSION_FILE = 'shaarli_version.php';
     private static $VERSION_START_TAG = '<?php /* ';
     private static $VERSION_END_TAG = ' */ ?>';
@@ -52,8 +52,12 @@ class ApplicationUtils
      *
      * @return mixed the new version code if available and greater, else 'false'
      */
-    public static function checkUpdate(
-        $currentVersion, $updateFile, $checkInterval, $enableCheck, $isLoggedIn)
+    public static function checkUpdate($currentVersion,
+                                       $updateFile,
+                                       $checkInterval,
+                                       $enableCheck,
+                                       $isLoggedIn,
+                                       $branch='stable')
     {
         if (! $isLoggedIn) {
             // Do not check versions for visitors
@@ -75,10 +79,16 @@ class ApplicationUtils
             return false;
         }
 
+        if (! in_array($branch, self::$GIT_BRANCHES)) {
+            throw new Exception(
+                'Invalid branch selected for updates: "' . $branch . '"'
+            );
+        }
+
         // Late Static Binding allows overriding within tests
         // See http://php.net/manual/en/language.oop5.late-static-bindings.php
         $latestVersion = static::getLatestGitVersionCode(
-            self::$GIT_URL . '/' . self::$GIT_BRANCH . '/' . self::$VERSION_FILE
+            self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE
         );
 
         if (! $latestVersion) {
index 90045d607437d73d07b029b47a64f2aa4b35b6f5..a10056396b2ecfdc40c466cc528a2d17dc1e6712 100644 (file)
--- a/index.php
+++ b/index.php
@@ -92,7 +92,8 @@ $GLOBALS['config']['ENABLE_THUMBNAILS'] = true;
 $GLOBALS['config']['ENABLE_LOCALCACHE'] = true;
 
 // Update check frequency for Shaarli. 86400 seconds=24 hours
-$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400 ;
+$GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable';
+$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400;
 
 
 /*
@@ -631,18 +632,23 @@ class pageBuilder
     private function initialize()
     {
         $this->tpl = new RainTPL;
-        $this->tpl->assign(
-            'newversion',
-            escape(
-                ApplicationUtils::checkUpdate(
-                    shaarli_version,
-                    $GLOBALS['config']['UPDATECHECK_FILENAME'],
-                    $GLOBALS['config']['UPDATECHECK_INTERVAL'],
-                    $GLOBALS['config']['ENABLE_UPDATECHECK'],
-                    isLoggedIn()
-                )
-            )
-        );
+
+        try {
+            $version = ApplicationUtils::checkUpdate(
+                shaarli_version,
+                $GLOBALS['config']['UPDATECHECK_FILENAME'],
+                $GLOBALS['config']['UPDATECHECK_INTERVAL'],
+                $GLOBALS['config']['ENABLE_UPDATECHECK'],
+                isLoggedIn(),
+                $GLOBALS['config']['UPDATECHECK_BRANCH']
+            );
+            $this->tpl->assign('newVersion', escape($version));
+
+        } catch (Exception $exc) {
+            logm($exc->getMessage());
+            $this->tpl->assign('versionError', escape($exc->getMessage()));
+        }
+
         $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
         $searchcrits = ''; // Search criteria
         if (!empty($_GET['searchtags'])) {
index 437c21fd14ba96fbffaed6c4157c66f4875075f0..6064357dcb50b70da959dc451271621392c449de 100644 (file)
@@ -75,7 +75,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     public function testGetLatestGitVersionCodeInvalidUrl()
     {
         $this->assertFalse(
-            ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 0)
+            ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1)
         );
     }
 
@@ -102,7 +102,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     /**
      * A newer version is available
      */
-    public function testCheckUpdateNewVersionNew()
+    public function testCheckUpdateNewVersionAvailable()
     {
         $newVersion = '1.8.3';
         FakeApplicationUtils::$VERSION_CODE = $newVersion;
@@ -134,6 +134,16 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($version);
     }
 
+    /**
+     * Test update checks - invalid Git branch
+     * @expectedException              Exception
+     * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
+     */
+    public function testCheckUpdateInvalidGitBranch()
+    {
+        ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
+    }
+
     /**
      * Shaarli is up-to-date
      */
index 6c29850ffe2ba3af06a031c13b4527223950880a..b20aae540adab9b9c6d7ca277ac342dc06ef3524 100644 (file)
@@ -4,8 +4,16 @@
         {$value}
     {/loop}
 </div>
-{if="$newversion"}
-    <div id="newversion"><span id="version_id">&#x25CF;</span> Shaarli {$newversion} is <a href="https://github.com/shaarli/Shaarli/releases">available</a>.</div>
+{if="$newVersion"}
+<div id="newversion">
+  <span id="version_id">&#x25CF;</span> Shaarli {$newVersion} is
+  <a href="https://github.com/shaarli/Shaarli/releases">available</a>.
+</div>
+{/if}
+{if="$versionError"}
+<div id="newversion">
+  Error: {$versionError}
+</div>
 {/if}
 {if="isLoggedIn()"}
 <script>function confirmDeleteLink() { var agree=confirm("Are you sure you want to delete this link ?"); if (agree) return true ; else return false ; }</script>