summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-03-01 13:43:37 +0300
committerAndrew Dolgov <[email protected]>2021-03-01 13:43:37 +0300
commit320503dd3911de93d059ebe1ba8b96004d8f6b03 (patch)
treece44d5c6b8ed3a6cd54d2bb7dda47445f1b2f688 /classes
parent20a844085f42975a9e2d20a9cd489a0251d52ad5 (diff)
move version-related stuff to Config; fix conditional feed requests
Diffstat (limited to 'classes')
-rwxr-xr-xclasses/api.php2
-rw-r--r--classes/config.php73
-rwxr-xr-xclasses/handler/public.php2
-rw-r--r--classes/pref/prefs.php27
-rwxr-xr-xclasses/rpc.php6
-rwxr-xr-xclasses/rssutils.php8
-rw-r--r--classes/urlhelper.php11
7 files changed, 84 insertions, 45 deletions
diff --git a/classes/api.php b/classes/api.php
index 31672575a..991682191 100755
--- a/classes/api.php
+++ b/classes/api.php
@@ -49,7 +49,7 @@ class API extends Handler {
}
function getVersion() {
- $rv = array("version" => get_version());
+ $rv = array("version" => Config::get_version());
$this->_wrap(self::STATUS_OK, $rv);
}
diff --git a/classes/config.php b/classes/config.php
index cc8710f5b..0f20fa27c 100644
--- a/classes/config.php
+++ b/classes/config.php
@@ -108,6 +108,7 @@ class Config {
private $params = [];
private $schema_version = null;
+ private $version = [];
public static function get_instance() : Config {
if (self::$instance == null)
@@ -134,6 +135,78 @@ class Config {
}
}
+ /* package maintainers who don't use git: if version_static.txt exists in tt-rss root
+ directory, its contents are displayed instead of git commit-based version, this could be generated
+ based on source git tree commit used when creating the package */
+
+ static function get_version(bool $as_string = true) {
+ return self::get_instance()->_get_version($as_string);
+ }
+
+ private function _get_version(bool $as_string = true) {
+ $root_dir = dirname(__DIR__);
+
+ if (empty($this->version)) {
+ $this->version["status"] = -1;
+
+ if (PHP_OS === "Darwin") {
+ $ttrss_version["version"] = "UNKNOWN (Unsupported, Darwin)";
+ } else if (file_exists("$root_dir/version_static.txt")) {
+ $this->version["version"] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)";
+ } else if (is_dir("$root_dir/.git")) {
+ $this->version = self::get_version_from_git($root_dir);
+
+ if ($this->version["status"] != 0) {
+ user_error("Unable to determine version: " . $this->version["version"], E_USER_WARNING);
+
+ $this->version["version"] = "UNKNOWN (Unsupported, Git error)";
+ }
+ } else {
+ $this->version["version"] = "UNKNOWN (Unsupported)";
+ }
+ }
+
+ return $as_string ? $this->version["version"] : $this->version;
+ }
+
+ static function get_version_from_git(string $dir) {
+ $descriptorspec = [
+ 1 => ["pipe", "w"], // STDOUT
+ 2 => ["pipe", "w"], // STDERR
+ ];
+
+ $rv = [
+ "status" => -1,
+ "version" => "",
+ "commit" => "",
+ "timestamp" => 0,
+ ];
+
+ $proc = proc_open("git --no-pager log --pretty=\"%ct %h\" -n1 HEAD",
+ $descriptorspec, $pipes, $dir);
+
+ if (is_resource($proc)) {
+ $stdout = trim(stream_get_contents($pipes[1]));
+ $stderr = trim(stream_get_contents($pipes[2]));
+ $status = proc_close($proc);
+
+ $rv["status"] = $status;
+
+ if ($status == 0) {
+ list($timestamp, $commit) = explode(" ", $stdout);
+
+ $rv["version"] = strftime("%y.%m", (int)$timestamp) . "-$commit";
+ $rv["commit"] = $commit;
+ $rv["timestamp"] = $timestamp;
+
+ } else {
+ $rv["version"] = T_sprintf("Git error [RC=%d]: %s", $status, $stderr);
+ }
+ }
+
+ return $rv;
+ }
+
static function get_schema_version(bool $nocache = false) {
return self::get_instance()->_schema_version($nocache);
}
diff --git a/classes/handler/public.php b/classes/handler/public.php
index e4572382e..1d970b689 100755
--- a/classes/handler/public.php
+++ b/classes/handler/public.php
@@ -75,7 +75,7 @@ class Handler_Public extends Handler {
$tpl->readTemplateFromFile("generated_feed.txt");
$tpl->setVariable('FEED_TITLE', $feed_title, true);
- $tpl->setVariable('VERSION', get_version(), true);
+ $tpl->setVariable('VERSION', Config::get_version(), true);
$tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
$tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php
index 565ddaded..5fe4f1bbf 100644
--- a/classes/pref/prefs.php
+++ b/classes/pref/prefs.php
@@ -1099,29 +1099,6 @@ class Pref_Prefs extends Handler_Protected {
set_pref(Prefs::_ENABLED_PLUGINS, $plugins);
}
- function _get_version_from_git(string $dir) {
- $descriptorspec = [
- 1 => ["pipe", "w"], // STDOUT
- 2 => ["pipe", "w"], // STDERR
- ];
-
- $proc = proc_open("git --no-pager log --pretty=\"%ct %h\" -n1 HEAD",
- $descriptorspec, $pipes, $dir);
-
- if (is_resource($proc)) {
- $stdout = stream_get_contents($pipes[1]);
- $stderr = stream_get_contents($pipes[2]);
- $status = proc_close($proc);
-
- if ($status == 0) {
- list($timestamp, $commit) = explode(" ", $stdout);
- return trim(strftime("%y.%m", (int)$timestamp) . "-$commit");
- } else {
- return T_sprintf("Git error [RC=%d]: %s", $status, $stderr);
- }
- }
- }
-
function _get_plugin_version(Plugin $plugin) {
$about = $plugin->about();
@@ -1137,7 +1114,9 @@ class Pref_Prefs extends Handler_Protected {
}
if (is_dir("$plugin_dir/.git")) {
- return T_sprintf("v%s, by %s", $this->_get_version_from_git($plugin_dir), $about[2]);
+ $ver = Config::get_version_from_git($plugin_dir);
+
+ return $ver["status"] == 0 ? T_sprintf("v%s, by %s", $ver["version"], $about[2]) : $ver["version"];
}
}
}
diff --git a/classes/rpc.php b/classes/rpc.php
index 7f7b924eb..65612ec34 100755
--- a/classes/rpc.php
+++ b/classes/rpc.php
@@ -396,10 +396,10 @@ class RPC extends Handler_Protected {
function checkforupdates() {
$rv = ["changeset" => [], "plugins" => []];
- $git_timestamp = false;
- $git_commit = false;
+ $version = Config::get_version();
- get_version($git_commit, $git_timestamp);
+ $git_timestamp = $version["timestamp"] ?? false;
+ $git_commit = $version["commit"] ?? false;
if (Config::get(Config::CHECK_FOR_UPDATES) && $_SESSION["access_level"] >= 10 && $git_timestamp) {
$content = @UrlHelper::fetch(["url" => "https://tt-rss.org/version.json"]);
diff --git a/classes/rssutils.php b/classes/rssutils.php
index d9f97e602..73ddaa235 100755
--- a/classes/rssutils.php
+++ b/classes/rssutils.php
@@ -458,8 +458,6 @@ class RSSUtils {
Debug::log("local cache will not be used for this feed", Debug::$LOG_VERBOSE);
}
- global $fetch_last_modified;
-
// fetch feed from source
if (!$feed_data) {
Debug::log("last unconditional update request: $last_unconditional", Debug::$LOG_VERBOSE);
@@ -490,11 +488,11 @@ class RSSUtils {
Debug::log("fetch done.", Debug::$LOG_VERBOSE);
Debug::log("effective URL (after redirects): " . clean(UrlHelper::$fetch_effective_url) . " (IP: ".UrlHelper::$fetch_effective_ip_addr.")", Debug::$LOG_VERBOSE);
- Debug::log("source last modified: " . $fetch_last_modified, Debug::$LOG_VERBOSE);
+ Debug::log("source last modified: " . UrlHelper::$fetch_last_modified, Debug::$LOG_VERBOSE);
- if ($feed_data && $fetch_last_modified != $stored_last_modified) {
+ if ($feed_data && UrlHelper::$fetch_last_modified != $stored_last_modified) {
$sth = $pdo->prepare("UPDATE ttrss_feeds SET last_modified = ? WHERE id = ?");
- $sth->execute([substr($fetch_last_modified, 0, 245), $feed]);
+ $sth->execute([substr(UrlHelper::$fetch_last_modified, 0, 245), $feed]);
}
// cache vanilla feed data for re-use
diff --git a/classes/urlhelper.php b/classes/urlhelper.php
index 020210a53..389298078 100644
--- a/classes/urlhelper.php
+++ b/classes/urlhelper.php
@@ -167,17 +167,6 @@ class UrlHelper {
public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false,
4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) {
- /*
- global $fetch_last_error;
- global $fetch_last_error_code;
- global $fetch_last_error_content;
- global $fetch_last_content_type;
- global $fetch_last_modified;
- global $fetch_effective_url;
- global $fetch_effective_ip_addr;
- global $fetch_curl_used;
- */
-
self::$fetch_last_error = false;
self::$fetch_last_error_code = -1;
self::$fetch_last_error_content = "";