From 320503dd3911de93d059ebe1ba8b96004d8f6b03 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 1 Mar 2021 13:43:37 +0300 Subject: [PATCH] move version-related stuff to Config; fix conditional feed requests --- classes/api.php | 2 +- classes/config.php | 73 +++++++++++++++++++ classes/handler/public.php | 2 +- classes/pref/prefs.php | 27 +------ classes/rpc.php | 6 +- classes/rssutils.php | 8 +- classes/urlhelper.php | 11 --- include/functions.php | 67 ++--------------- .../af_comics/filters/af_comics_gocomics.php | 2 +- .../filters/af_comics_gocomics_farside.php | 2 +- prefs.php | 12 ++- 11 files changed, 97 insertions(+), 115 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 = ""; diff --git a/include/functions.php b/include/functions.php index 68a1a1809..275caac69 100644 --- a/include/functions.php +++ b/include/functions.php @@ -156,11 +156,16 @@ require_once 'controls.php'; require_once 'controls_compat.php'; - define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . get_version() . ' (http://tt-rss.org/)'); + define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . Config::get_version() . ' (http://tt-rss.org/)'); ini_set('user_agent', SELF_USER_AGENT); /* compat shims */ + /** function is @deprecated */ + function get_version() { + return Config::get_version(); + } + /** function is @deprecated */ function get_schema_version() { return Config::get_schema_version(); @@ -438,63 +443,3 @@ return $ts; } - /* for 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 */ - - function get_version(&$git_commit = false, &$git_timestamp = false, &$last_error = false) { - global $ttrss_version; - - if (is_array($ttrss_version) && isset($ttrss_version['version'])) { - $git_commit = $ttrss_version['commit']; - $git_timestamp = $ttrss_version['timestamp']; - $last_error = $ttrss_version['last_error'] ?? ""; - - return $ttrss_version['version']; - } else { - $ttrss_version = []; - } - - $ttrss_version['version'] = "UNKNOWN (Unsupported)"; - - date_default_timezone_set('UTC'); - $root_dir = dirname(__DIR__); - - if (PHP_OS === "Darwin") { - $ttrss_version['version'] = "UNKNOWN (Unsupported, Darwin)"; - } else if (file_exists("$root_dir/version_static.txt")) { - $ttrss_version['version'] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)"; - } else if (is_dir("$root_dir/.git")) { - $rc = 0; - $output = []; - - $cwd = getcwd(); - - chdir($root_dir); - exec('git --no-pager log --pretty="version: %ct %h" -n1 HEAD 2>&1', $output, $rc); - chdir($cwd); - - if (is_array($output) && count($output) > 0) { - list ($test, $timestamp, $commit) = explode(" ", $output[0], 3); - - if ($test == "version:") { - $git_commit = $commit; - $git_timestamp = $timestamp; - - $ttrss_version['version'] = strftime("%y.%m", (int)$timestamp) . "-$commit"; - $ttrss_version['commit'] = $commit; - $ttrss_version['timestamp'] = $timestamp; - } - } - - if (!isset($ttrss_version['commit'])) { - $last_error = "Unable to determine version (using $root_dir): RC=$rc; OUTPUT=" . implode("\n", $output); - - $ttrss_version["last_error"] = $last_error; - - user_error($last_error, E_USER_WARNING); - } - } - - return $ttrss_version['version']; - } diff --git a/plugins/af_comics/filters/af_comics_gocomics.php b/plugins/af_comics/filters/af_comics_gocomics.php index 6c5c7c0d3..71d387918 100644 --- a/plugins/af_comics/filters/af_comics_gocomics.php +++ b/plugins/af_comics/filters/af_comics_gocomics.php @@ -40,7 +40,7 @@ class Af_Comics_Gocomics extends Af_ComicFilter { $tpl->readTemplateFromFile('templates/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($url), true); $tpl->setVariable('SELF_URL', $site_url, true); diff --git a/plugins/af_comics/filters/af_comics_gocomics_farside.php b/plugins/af_comics/filters/af_comics_gocomics_farside.php index 89322209d..0399015ab 100644 --- a/plugins/af_comics/filters/af_comics_gocomics_farside.php +++ b/plugins/af_comics/filters/af_comics_gocomics_farside.php @@ -33,7 +33,7 @@ class Af_Comics_Gocomics_FarSide extends Af_ComicFilter { $tpl->readTemplateFromFile('templates/generated_feed.txt'); $tpl->setVariable('FEED_TITLE', "The Far Side", true); - $tpl->setVariable('VERSION', get_version(), true); + $tpl->setVariable('VERSION', Config::get_version(), true); $tpl->setVariable('FEED_URL', htmlspecialchars($url), true); $tpl->setVariable('SELF_URL', htmlspecialchars($url), true); diff --git a/prefs.php b/prefs.php index 52f564a76..dd3d3a4b0 100644 --- a/prefs.php +++ b/prefs.php @@ -151,14 +151,12 @@ PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TABS); ?> - + Tiny Tiny RSS + v + © 2005- + Andrew Dolgov +