move version-related stuff to Config; fix conditional feed requests

This commit is contained in:
Andrew Dolgov 2021-03-01 13:43:37 +03:00
parent 20a844085f
commit 320503dd39
11 changed files with 97 additions and 115 deletions

View File

@ -49,7 +49,7 @@ class API extends Handler {
} }
function getVersion() { function getVersion() {
$rv = array("version" => get_version()); $rv = array("version" => Config::get_version());
$this->_wrap(self::STATUS_OK, $rv); $this->_wrap(self::STATUS_OK, $rv);
} }

View File

@ -108,6 +108,7 @@ class Config {
private $params = []; private $params = [];
private $schema_version = null; private $schema_version = null;
private $version = [];
public static function get_instance() : Config { public static function get_instance() : Config {
if (self::$instance == null) 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) { static function get_schema_version(bool $nocache = false) {
return self::get_instance()->_schema_version($nocache); return self::get_instance()->_schema_version($nocache);
} }

View File

@ -75,7 +75,7 @@ class Handler_Public extends Handler {
$tpl->readTemplateFromFile("generated_feed.txt"); $tpl->readTemplateFromFile("generated_feed.txt");
$tpl->setVariable('FEED_TITLE', $feed_title, true); $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('FEED_URL', htmlspecialchars($feed_self_url), true);
$tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true); $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);

View File

@ -1099,29 +1099,6 @@ class Pref_Prefs extends Handler_Protected {
set_pref(Prefs::_ENABLED_PLUGINS, $plugins); 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) { function _get_plugin_version(Plugin $plugin) {
$about = $plugin->about(); $about = $plugin->about();
@ -1137,7 +1114,9 @@ class Pref_Prefs extends Handler_Protected {
} }
if (is_dir("$plugin_dir/.git")) { 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"];
} }
} }
} }

View File

@ -396,10 +396,10 @@ class RPC extends Handler_Protected {
function checkforupdates() { function checkforupdates() {
$rv = ["changeset" => [], "plugins" => []]; $rv = ["changeset" => [], "plugins" => []];
$git_timestamp = false; $version = Config::get_version();
$git_commit = false;
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) { if (Config::get(Config::CHECK_FOR_UPDATES) && $_SESSION["access_level"] >= 10 && $git_timestamp) {
$content = @UrlHelper::fetch(["url" => "https://tt-rss.org/version.json"]); $content = @UrlHelper::fetch(["url" => "https://tt-rss.org/version.json"]);

View File

@ -458,8 +458,6 @@ class RSSUtils {
Debug::log("local cache will not be used for this feed", Debug::$LOG_VERBOSE); Debug::log("local cache will not be used for this feed", Debug::$LOG_VERBOSE);
} }
global $fetch_last_modified;
// fetch feed from source // fetch feed from source
if (!$feed_data) { if (!$feed_data) {
Debug::log("last unconditional update request: $last_unconditional", Debug::$LOG_VERBOSE); 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("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("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 = $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 // cache vanilla feed data for re-use

View File

@ -167,17 +167,6 @@ class UrlHelper {
public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false, 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*/) { 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 = false;
self::$fetch_last_error_code = -1; self::$fetch_last_error_code = -1;
self::$fetch_last_error_content = ""; self::$fetch_last_error_content = "";

View File

@ -156,11 +156,16 @@
require_once 'controls.php'; require_once 'controls.php';
require_once 'controls_compat.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); ini_set('user_agent', SELF_USER_AGENT);
/* compat shims */ /* compat shims */
/** function is @deprecated */
function get_version() {
return Config::get_version();
}
/** function is @deprecated */ /** function is @deprecated */
function get_schema_version() { function get_schema_version() {
return Config::get_schema_version(); return Config::get_schema_version();
@ -438,63 +443,3 @@
return $ts; 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'];
}

View File

@ -40,7 +40,7 @@ class Af_Comics_Gocomics extends Af_ComicFilter {
$tpl->readTemplateFromFile('templates/generated_feed.txt'); $tpl->readTemplateFromFile('templates/generated_feed.txt');
$tpl->setVariable('FEED_TITLE', $feed_title, true); $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('FEED_URL', htmlspecialchars($url), true);
$tpl->setVariable('SELF_URL', $site_url, true); $tpl->setVariable('SELF_URL', $site_url, true);

View File

@ -33,7 +33,7 @@ class Af_Comics_Gocomics_FarSide extends Af_ComicFilter {
$tpl->readTemplateFromFile('templates/generated_feed.txt'); $tpl->readTemplateFromFile('templates/generated_feed.txt');
$tpl->setVariable('FEED_TITLE', "The Far Side", true); $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('FEED_URL', htmlspecialchars($url), true);
$tpl->setVariable('SELF_URL', htmlspecialchars($url), true); $tpl->setVariable('SELF_URL', htmlspecialchars($url), true);

View File

@ -151,14 +151,12 @@
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TABS); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TABS);
?> ?>
</div> </div>
<?php $version = get_version($git_commit, $git_timestamp, $last_error); ?>
<div id="footer" dojoType="dijit.layout.ContentPane" region="bottom"> <div id="footer" dojoType="dijit.layout.ContentPane" region="bottom">
<a class="text-muted" target="_blank" href="https://tt-rss.org/">Tiny Tiny RSS</a> <a class="text-muted" target="_blank" href="https://tt-rss.org/">Tiny Tiny RSS</a>
<span title="<?= htmlspecialchars($last_error) ?>">v<?= $version ?></span> <span>v<?= Config::get_version() ?></span>
&copy; 2005-<?= date('Y') ?> &copy; 2005-<?= date('Y') ?>
<a class="text-muted" target="_blank" <a class="text-muted" target="_blank" href="https://fakecake.org/">Andrew Dolgov</a>
href="https://fakecake.org/">Andrew Dolgov</a> </div>
</div> <!-- footer -->
</div> </div>
</body> </body>