Merge pull request 'Use PHP 7.4 features' (#77) from wn/tt-rss:feature/php-7.4-stuff into master

Reviewed-on: https://dev.tt-rss.org/fox/tt-rss/pulls/77
This commit is contained in:
fox 2022-08-17 19:38:15 +03:00
commit 60658be5bc
26 changed files with 169 additions and 266 deletions

View File

@ -286,7 +286,7 @@ class API extends Handler {
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
$field = $set_to $additional_fields $field = $set_to $additional_fields
WHERE ref_id IN ($article_qmarks) AND owner_uid = ?"); WHERE ref_id IN ($article_qmarks) AND owner_uid = ?");
$sth->execute(array_merge($article_ids, [$_SESSION['uid']])); $sth->execute([...$article_ids, $_SESSION['uid']]);
$num_updated = $sth->rowCount(); $num_updated = $sth->rowCount();

View File

@ -177,7 +177,7 @@ class Article extends Handler_Protected {
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
score = ? WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); score = ? WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
$sth->execute(array_merge([$score], $ids, [$_SESSION['uid']])); $sth->execute([$score, ...$ids, $_SESSION['uid']]);
print json_encode(["id" => $ids, "score" => $score]); print json_encode(["id" => $ids, "score" => $score]);
} }
@ -507,7 +507,7 @@ class Article extends Handler_Protected {
WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
} }
$sth->execute(array_merge($ids, [$owner_uid])); $sth->execute([...$ids, $owner_uid]);
} }
/** /**

View File

@ -5,13 +5,13 @@ class Counters {
* @return array<int, array<string, int|string>> * @return array<int, array<string, int|string>>
*/ */
static function get_all(): array { static function get_all(): array {
return array_merge( return [
self::get_global(), ...self::get_global(),
self::get_virt(), ...self::get_virt(),
self::get_labels(), ...self::get_labels(),
self::get_feeds(), ...self::get_feeds(),
self::get_cats() ...self::get_cats(),
); ];
} }
/** /**
@ -20,13 +20,13 @@ class Counters {
* @return array<int, array<string, int|string>> * @return array<int, array<string, int|string>>
*/ */
static function get_conditional(array $feed_ids = null, array $label_ids = null): array { static function get_conditional(array $feed_ids = null, array $label_ids = null): array {
return array_merge( return [
self::get_global(), ...self::get_global(),
self::get_virt(), ...self::get_virt(),
self::get_labels($label_ids), ...self::get_labels($label_ids),
self::get_feeds($feed_ids), ...self::get_feeds($feed_ids),
self::get_cats(is_array($feed_ids) ? Feeds::_cats_of($feed_ids, $_SESSION["uid"], true) : null) ...self::get_cats(is_array($feed_ids) ? Feeds::_cats_of($feed_ids, $_SESSION["uid"], true) : null)
); ];
} }
/** /**
@ -93,11 +93,7 @@ class Counters {
ue.feed_id = f.id AND ue.feed_id = f.id AND
ue.owner_uid = ?"); ue.owner_uid = ?");
$sth->execute(array_merge( $sth->execute([$_SESSION['uid'], ...$cat_ids, $_SESSION['uid']]);
[$_SESSION['uid']],
$cat_ids,
[$_SESSION['uid']]
));
} else { } else {
$sth = $pdo->prepare("SELECT fc.id, $sth = $pdo->prepare("SELECT fc.id,
@ -170,7 +166,7 @@ class Counters {
WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks) WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks)
GROUP BY f.id"); GROUP BY f.id");
$sth->execute(array_merge([$_SESSION['uid']], $feed_ids)); $sth->execute([$_SESSION['uid'], ...$feed_ids]);
} else { } else {
$sth = $pdo->prepare("SELECT f.id, $sth = $pdo->prepare("SELECT f.id,
f.title, f.title,
@ -319,7 +315,7 @@ class Counters {
LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = ? LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = ?
WHERE ttrss_labels2.owner_uid = ? AND ttrss_labels2.id IN ($label_ids_qmarks) WHERE ttrss_labels2.owner_uid = ? AND ttrss_labels2.id IN ($label_ids_qmarks)
GROUP BY ttrss_labels2.id, ttrss_labels2.caption"); GROUP BY ttrss_labels2.id, ttrss_labels2.caption");
$sth->execute(array_merge([$_SESSION["uid"], $_SESSION["uid"]], $label_ids)); $sth->execute([$_SESSION["uid"], $_SESSION["uid"], ...$label_ids]);
} else { } else {
$sth = $pdo->prepare("SELECT id, $sth = $pdo->prepare("SELECT id,
caption, caption,

View File

@ -1,33 +1,15 @@
<?php <?php
class Db_Migrations { class Db_Migrations {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ private string $base_filename = "schema.sql";
/** @var string */ private string $base_path;
private $base_filename = "schema.sql"; private string $migrations_path;
private string $migrations_table;
/** @var string */ private bool $base_is_latest;
private $base_path; private PDO $pdo;
private int $cached_version = 0;
/** @var string */ private int $cached_max_version = 0;
private $migrations_path; private int $max_version_override;
/** @var string */
private $migrations_table;
/** @var bool */
private $base_is_latest;
/** @var PDO */
private $pdo;
/** @var int */
private $cached_version = 0;
/** @var int */
private $cached_max_version = 0;
/** @var int */
private $max_version_override;
function __construct() { function __construct() {
$this->pdo = Db::pdo(); $this->pdo = Db::pdo();
@ -207,14 +189,11 @@ class Db_Migrations {
$filename = "{$this->base_path}/{$this->base_filename}"; $filename = "{$this->base_path}/{$this->base_filename}";
if (file_exists($filename)) { if (file_exists($filename)) {
$lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)), $lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)),
function ($line) { fn($line) => strlen(trim($line)) > 0 && strpos($line, "--") !== 0);
return strlen(trim($line)) > 0 && strpos($line, "--") !== 0;
});
return array_filter(explode(";", implode("", $lines)), function ($line) { return array_filter(explode(";", implode("", $lines)),
return strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]); fn($line) => strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]));
});
} else { } else {
user_error("Requested schema file ${filename} not found.", E_USER_ERROR); user_error("Requested schema file ${filename} not found.", E_USER_ERROR);

View File

@ -12,44 +12,31 @@ class Debug {
Debug::LOG_EXTENDED, Debug::LOG_EXTENDED,
]; ];
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+
/** /**
* @deprecated * @deprecated
* @var int
*/ */
public static $LOG_DISABLED = self::LOG_DISABLED; public static int $LOG_DISABLED = self::LOG_DISABLED;
/** /**
* @deprecated * @deprecated
* @var int
*/ */
public static $LOG_NORMAL = self::LOG_NORMAL; public static int $LOG_NORMAL = self::LOG_NORMAL;
/** /**
* @deprecated * @deprecated
* @var int
*/ */
public static $LOG_VERBOSE = self::LOG_VERBOSE; public static int $LOG_VERBOSE = self::LOG_VERBOSE;
/** /**
* @deprecated * @deprecated
* @var int
*/ */
public static $LOG_EXTENDED = self::LOG_EXTENDED; public static int $LOG_EXTENDED = self::LOG_EXTENDED;
/** @var bool */ private static bool $enabled = false;
private static $enabled = false; private static bool $quiet = false;
private static ?string $logfile = null;
/** @var bool */ private static int $loglevel = self::LOG_NORMAL;
private static $quiet = false;
/** @var string|null */
private static $logfile = null;
/**
* @var int Debug::LOG_*
*/
private static $loglevel = self::LOG_NORMAL;
public static function set_logfile(string $logfile): void { public static function set_logfile(string $logfile): void {
self::$logfile = $logfile; self::$logfile = $logfile;
@ -70,7 +57,7 @@ class Debug {
/** /**
* @param Debug::LOG_* $level * @param Debug::LOG_* $level
*/ */
public static function set_loglevel($level): void { public static function set_loglevel(int $level): void {
self::$loglevel = $level; self::$loglevel = $level;
} }

View File

@ -163,9 +163,7 @@ class Digest
$article_labels_formatted = ""; $article_labels_formatted = "";
if (is_array($article_labels) && count($article_labels) > 0) { if (is_array($article_labels) && count($article_labels) > 0) {
$article_labels_formatted = implode(", ", array_map(function($a) { $article_labels_formatted = implode(", ", array_map(fn($a) => $a[1], $article_labels));
return $a[1];
}, $article_labels));
} }
$tpl->setVariable('FEED_TITLE', $line["feed_title"]); $tpl->setVariable('FEED_TITLE', $line["feed_title"]);

View File

@ -1,15 +1,13 @@
<?php <?php
class DiskCache { class DiskCache {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ private string $dir;
/** @var string */
private $dir;
/** /**
* https://stackoverflow.com/a/53662733 * https://stackoverflow.com/a/53662733
* *
* @var array<string, string> * @var array<string, string>
*/ */
private $mimeMap = [ private array $mimeMap = [
'video/3gpp2' => '3g2', 'video/3gpp2' => '3g2',
'video/3gp' => '3gp', 'video/3gp' => '3gp',
'video/3gpp' => '3gp', 'video/3gpp' => '3gp',

View File

@ -201,7 +201,7 @@ class FeedItem_Atom extends FeedItem_Common {
} }
} }
$encs = array_merge($encs, parent::get_enclosures()); array_push($encs, ...parent::get_enclosures());
return $encs; return $encs;
} }

View File

@ -189,7 +189,7 @@ abstract class FeedItem_Common extends FeedItem {
$tmp = []; $tmp = [];
foreach ($cats as $rawcat) { foreach ($cats as $rawcat) {
$tmp = array_merge($tmp, explode(",", $rawcat)); array_push($tmp, ...explode(",", $rawcat));
} }
$tmp = array_map(function($srccat) { $tmp = array_map(function($srccat) {

View File

@ -153,7 +153,7 @@ class FeedItem_RSS extends FeedItem_Common {
array_push($encs, $enc); array_push($encs, $enc);
} }
$encs = array_merge($encs, parent::get_enclosures()); array_push($encs, ...parent::get_enclosures());
return $encs; return $encs;
} }

View File

@ -43,10 +43,8 @@ class FeedParser {
foreach (libxml_get_errors() as $error) { foreach (libxml_get_errors() as $error) {
if ($error->level == LIBXML_ERR_FATAL) { if ($error->level == LIBXML_ERR_FATAL) {
// currently only the first error is reported // currently only the first error is reported
if (!isset($this->error)) { $this->error ??= Errors::format_libxml_error($error);
$this->error = $this->format_error($error); $this->libxml_errors[] = Errors::format_libxml_error($error);
}
$this->libxml_errors[] = $this->format_error($error);
} }
} }
} }
@ -87,9 +85,7 @@ class FeedParser {
$this->type = $this::FEED_ATOM; $this->type = $this::FEED_ATOM;
break; break;
default: default:
if (!isset($this->error)) { $this->error ??= "Unknown/unsupported feed type";
$this->error = "Unknown/unsupported feed type";
}
return; return;
} }
} }
@ -186,9 +182,7 @@ class FeedParser {
if ($this->link) $this->link = trim($this->link); if ($this->link) $this->link = trim($this->link);
} else { } else {
if (!isset($this->error)) { $this->error ??= "Unknown/unsupported feed type";
$this->error = "Unknown/unsupported feed type";
}
return; return;
} }
} }

View File

@ -1938,8 +1938,8 @@ class Feeds extends Handler_Protected {
$sth->execute([$cat, $owner_uid]); $sth->execute([$cat, $owner_uid]);
while ($line = $sth->fetch()) { while ($line = $sth->fetch()) {
array_push($rv, (int)$line["parent_cat"]); $cat = (int) $line["parent_cat"];
$rv = array_merge($rv, self::_get_parent_cats($line["parent_cat"], $owner_uid)); array_push($rv, $cat, ...self::_get_parent_cats($cat, $owner_uid));
} }
return $rv; return $rv;
@ -1958,8 +1958,7 @@ class Feeds extends Handler_Protected {
$sth->execute([$cat, $owner_uid]); $sth->execute([$cat, $owner_uid]);
while ($line = $sth->fetch()) { while ($line = $sth->fetch()) {
array_push($rv, $line["id"]); array_push($rv, $line["id"], ...self::_get_child_cats($line["id"], $owner_uid));
$rv = array_merge($rv, self::_get_child_cats($line["id"], $owner_uid));
} }
return $rv; return $rv;
@ -1980,16 +1979,18 @@ class Feeds extends Handler_Protected {
$sth = $pdo->prepare("SELECT DISTINCT cat_id, fc.parent_cat FROM ttrss_feeds f LEFT JOIN ttrss_feed_categories fc $sth = $pdo->prepare("SELECT DISTINCT cat_id, fc.parent_cat FROM ttrss_feeds f LEFT JOIN ttrss_feed_categories fc
ON (fc.id = f.cat_id) ON (fc.id = f.cat_id)
WHERE f.owner_uid = ? AND f.id IN ($feeds_qmarks)"); WHERE f.owner_uid = ? AND f.id IN ($feeds_qmarks)");
$sth->execute(array_merge([$owner_uid], $feeds)); $sth->execute([$owner_uid, ...$feeds]);
$rv = []; $rv = [];
if ($row = $sth->fetch()) { if ($row = $sth->fetch()) {
$cat_id = (int) $row["cat_id"];
$rv[] = $cat_id;
array_push($rv, (int)$row["cat_id"]); array_push($rv, (int)$row["cat_id"]);
if ($with_parents && $row["parent_cat"]) if ($with_parents && $row["parent_cat"]) {
$rv = array_merge($rv, array_push($rv, ...self::_get_parent_cats($cat_id, $owner_uid));
self::_get_parent_cats($row["cat_id"], $owner_uid)); }
} }
$rv = array_unique($rv); $rv = array_unique($rv);

View File

@ -1,11 +1,9 @@
<?php <?php
class Handler implements IHandler { class Handler implements IHandler {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ protected PDO $pdo;
/** @var PDO */
protected $pdo;
/** @var array<int|string, mixed> */ /** @var array<int|string, mixed> */
protected $args; protected array $args;
/** /**
* @param array<int|string, mixed> $args * @param array<int|string, mixed> $args

View File

@ -417,8 +417,7 @@ class Handler_Public extends Handler {
if (session_status() != PHP_SESSION_ACTIVE) if (session_status() != PHP_SESSION_ACTIVE)
session_start(); session_start();
if (!isset($_SESSION["login_error_msg"])) $_SESSION["login_error_msg"] ??= __("Incorrect username or password");
$_SESSION["login_error_msg"] = __("Incorrect username or password");
} }
$return = clean($_REQUEST['return']); $return = clean($_REQUEST['return']);

View File

@ -1,8 +1,6 @@
<?php <?php
class Mailer { class Mailer {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ private string $last_error = "";
/** @var string */
private $last_error = "";
/** /**
* @param array<string, mixed> $params * @param array<string, mixed> $params
@ -47,7 +45,7 @@ class Mailer {
$headers = [ "From: $from_combined", "Content-Type: text/plain; charset=UTF-8" ]; $headers = [ "From: $from_combined", "Content-Type: text/plain; charset=UTF-8" ];
$rc = mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); $rc = mail($to_combined, $subject, $message, implode("\r\n", [...$headers, ...$additional_headers]));
if (!$rc) { if (!$rc) {
$this->set_error(error_get_last()['message'] ?? T_sprintf("Unknown error while sending mail. Hooks tried: %d.", $hooks_tried)); $this->set_error(error_get_last()['message'] ?? T_sprintf("Unknown error while sending mail. Hooks tried: %d.", $hooks_tried));

View File

@ -1,49 +1,42 @@
<?php <?php
class PluginHost { class PluginHost {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ private ?PDO $pdo = null;
/** @var PDO|null */
private $pdo = null;
/** /**
* separate handle for plugin data so transaction while saving wouldn't clash with possible main * separate handle for plugin data so transaction while saving wouldn't clash with possible main
* tt-rss code transactions; only initialized when first needed * tt-rss code transactions; only initialized when first needed
*
* @var PDO|null
*/ */
private $pdo_data = null; private ?PDO $pdo_data = null;
/** @var array<string, array<int, array<int, Plugin>>> hook types -> priority levels -> Plugins */ /** @var array<string, array<int, array<int, Plugin>>> hook types -> priority levels -> Plugins */
private $hooks = []; private array $hooks = [];
/** @var array<string, Plugin> */ /** @var array<string, Plugin> */
private $plugins = []; private array $plugins = [];
/** @var array<string, array<string, Plugin>> handler type -> method type -> Plugin */ /** @var array<string, array<string, Plugin>> handler type -> method type -> Plugin */
private $handlers = []; private array $handlers = [];
/** @var array<string, array{'description': string, 'suffix': string, 'arghelp': string, 'class': Plugin}> command type -> details array */ /** @var array<string, array{'description': string, 'suffix': string, 'arghelp': string, 'class': Plugin}> command type -> details array */
private $commands = []; private array $commands = [];
/** @var array<string, array<string, mixed>> plugin name -> (potential profile array) -> key -> value */ /** @var array<string, array<string, mixed>> plugin name -> (potential profile array) -> key -> value */
private $storage = []; private array $storage = [];
/** @var array<int, array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}>> */ /** @var array<int, array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}>> */
private $feeds = []; private array $feeds = [];
/** @var array<string, Plugin> API method name, Plugin sender */ /** @var array<string, Plugin> API method name, Plugin sender */
private $api_methods = []; private array $api_methods = [];
/** @var array<string, array<int, array{'action': string, 'description': string, 'sender': Plugin}>> */ /** @var array<string, array<int, array{'action': string, 'description': string, 'sender': Plugin}>> */
private $plugin_actions = []; private array $plugin_actions = [];
/** @var int|null */ private ?int $owner_uid = null;
private $owner_uid = null;
/** @var bool */ private bool $data_loaded = false;
private $data_loaded = false;
/** @var PluginHost|null */ private static ?PluginHost $instance = null;
private static $instance = null;
const API_VERSION = 2; const API_VERSION = 2;
const PUBLIC_METHOD_DELIMITER = "--"; const PUBLIC_METHOD_DELIMITER = "--";
@ -412,7 +405,7 @@ class PluginHost {
$tmp = []; $tmp = [];
foreach (array_keys($this->hooks[$type]) as $prio) { foreach (array_keys($this->hooks[$type]) as $prio) {
$tmp = array_merge($tmp, $this->hooks[$type][$prio]); array_push($tmp, ...$this->hooks[$type][$prio]);
} }
return $tmp; return $tmp;
@ -425,7 +418,7 @@ class PluginHost {
*/ */
function load_all(int $kind, int $owner_uid = null, bool $skip_init = false): void { function load_all(int $kind, int $owner_uid = null, bool $skip_init = false): void {
$plugins = array_merge(glob("plugins/*"), glob("plugins.local/*")); $plugins = [...(glob("plugins/*") ?: []), ...(glob("plugins.local/*") ?: [])];
$plugins = array_filter($plugins, "is_dir"); $plugins = array_filter($plugins, "is_dir");
$plugins = array_map("basename", $plugins); $plugins = array_map("basename", $plugins);
@ -542,10 +535,7 @@ class PluginHost {
$method = strtolower($method); $method = strtolower($method);
if ($this->is_system($sender)) { if ($this->is_system($sender)) {
if (!isset($this->handlers[$handler])) { $this->handlers[$handler] ??= [];
$this->handlers[$handler] = [];
}
$this->handlers[$handler][$method] = $sender; $this->handlers[$handler][$method] = $sender;
} }
} }
@ -648,8 +638,7 @@ class PluginHost {
owner_uid= ? AND name = ?"); owner_uid= ? AND name = ?");
$sth->execute([$this->owner_uid, $plugin]); $sth->execute([$this->owner_uid, $plugin]);
if (!isset($this->storage[$plugin])) $this->storage[$plugin] ??= [];
$this->storage[$plugin] = [];
$content = serialize($this->storage[$plugin]); $content = serialize($this->storage[$plugin]);
@ -680,14 +669,8 @@ class PluginHost {
if ($profile_id) { if ($profile_id) {
$idx = get_class($sender); $idx = get_class($sender);
if (!isset($this->storage[$idx])) { $this->storage[$idx] ??= [];
$this->storage[$idx] = []; $this->storage[$idx][$profile_id] ??= [];
}
if (!isset($this->storage[$idx][$profile_id])) {
$this->storage[$idx][$profile_id] = [];
}
$this->storage[$idx][$profile_id][$name] = $value; $this->storage[$idx][$profile_id][$name] = $value;
$this->save_data(get_class($sender)); $this->save_data(get_class($sender));
@ -702,9 +685,7 @@ class PluginHost {
function set(Plugin $sender, string $name, $value): void { function set(Plugin $sender, string $name, $value): void {
$idx = get_class($sender); $idx = get_class($sender);
if (!isset($this->storage[$idx])) $this->storage[$idx] ??= [];
$this->storage[$idx] = [];
$this->storage[$idx][$name] = $value; $this->storage[$idx][$name] = $value;
$this->save_data(get_class($sender)); $this->save_data(get_class($sender));
@ -716,8 +697,7 @@ class PluginHost {
function set_array(Plugin $sender, array $params): void { function set_array(Plugin $sender, array $params): void {
$idx = get_class($sender); $idx = get_class($sender);
if (!isset($this->storage[$idx])) $this->storage[$idx] ??= [];
$this->storage[$idx] = [];
foreach ($params as $name => $value) foreach ($params as $name => $value)
$this->storage[$idx][$name] = $value; $this->storage[$idx][$name] = $value;
@ -855,11 +835,13 @@ class PluginHost {
function add_filter_action(Plugin $sender, string $action_name, string $action_desc): void { function add_filter_action(Plugin $sender, string $action_name, string $action_desc): void {
$sender_class = get_class($sender); $sender_class = get_class($sender);
if (!isset($this->plugin_actions[$sender_class])) $this->plugin_actions[$sender_class] ??= [];
$this->plugin_actions[$sender_class] = [];
array_push($this->plugin_actions[$sender_class], $this->plugin_actions[$sender_class][] = [
array("action" => $action_name, "description" => $action_desc, "sender" => $sender)); "action" => $action_name,
"description" => $action_desc,
"sender" => $sender,
];
} }
/** /**

View File

@ -172,7 +172,7 @@ class Pref_Feeds extends Handler_Protected {
if ($enable_cats) { if ($enable_cats) {
array_push($root['items'], $cat); array_push($root['items'], $cat);
} else { } else {
$root['items'] = array_merge($root['items'], $cat['items']); array_push($root['items'], ...$cat['items']);
} }
$sth = $this->pdo->prepare("SELECT * FROM $sth = $this->pdo->prepare("SELECT * FROM
@ -202,7 +202,7 @@ class Pref_Feeds extends Handler_Protected {
if ($enable_cats) { if ($enable_cats) {
array_push($root['items'], $cat); array_push($root['items'], $cat);
} else { } else {
$root['items'] = array_merge($root['items'], $cat['items']); array_push($root['items'], ...$cat['items']);
} }
} }
} }
@ -848,7 +848,7 @@ class Pref_Feeds extends Handler_Protected {
if ($qpart) { if ($qpart) {
$sth = $this->pdo->prepare("UPDATE ttrss_feeds SET $qpart WHERE id IN ($feed_ids_qmarks) $sth = $this->pdo->prepare("UPDATE ttrss_feeds SET $qpart WHERE id IN ($feed_ids_qmarks)
AND owner_uid = ?"); AND owner_uid = ?");
$sth->execute(array_merge($feed_ids, [$_SESSION['uid']])); $sth->execute([...$feed_ids, $_SESSION['uid']]);
} }
} }

View File

@ -538,7 +538,7 @@ class Pref_Filters extends Handler_Protected {
$sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks) $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)
AND owner_uid = ?"); AND owner_uid = ?");
$sth->execute(array_merge($ids, [$_SESSION['uid']])); $sth->execute([...$ids, $_SESSION['uid']]);
} }
private function _save_rules_and_actions(int $filter_id): void { private function _save_rules_and_actions(int $filter_id): void {
@ -781,11 +781,11 @@ class Pref_Filters extends Handler_Protected {
$sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules $sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules
SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
$sth->execute(array_merge([$base_id], $ids)); $sth->execute([$base_id, ...$ids]);
$sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions
SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
$sth->execute(array_merge([$base_id], $ids)); $sth->execute([$base_id, ...$ids]);
$sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)"); $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
$sth->execute($ids); $sth->execute($ids);

View File

@ -2,18 +2,17 @@
use chillerlan\QRCode; use chillerlan\QRCode;
class Pref_Prefs extends Handler_Protected { class Pref_Prefs extends Handler_Protected {
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+
/** @var array<Prefs::*, array<int, string>> */ /** @var array<Prefs::*, array<int, string>> */
private $pref_help = []; private array $pref_help = [];
/** @var array<string, array<int, string>> pref items are Prefs::*|Pref_Prefs::BLOCK_SEPARATOR (PHPStan was complaining) */ /** @var array<string, array<int, string>> pref items are Prefs::*|Pref_Prefs::BLOCK_SEPARATOR (PHPStan was complaining) */
private $pref_item_map = []; private array $pref_item_map = [];
/** @var array<string, string> */ /** @var array<string, string> */
private $pref_help_bottom = []; private array $pref_help_bottom = [];
/** @var array<int, string> */ /** @var array<int, string> */
private $pref_blacklist = []; private array $pref_blacklist = [];
private const BLOCK_SEPARATOR = 'BLOCK_SEPARATOR'; private const BLOCK_SEPARATOR = 'BLOCK_SEPARATOR';
@ -26,7 +25,6 @@ class Pref_Prefs extends Handler_Protected {
const PI_ERR_PLUGIN_NOT_FOUND = "PI_ERR_PLUGIN_NOT_FOUND"; const PI_ERR_PLUGIN_NOT_FOUND = "PI_ERR_PLUGIN_NOT_FOUND";
const PI_ERR_NO_WORKDIR = "PI_ERR_NO_WORKDIR"; const PI_ERR_NO_WORKDIR = "PI_ERR_NO_WORKDIR";
/** @param string $method */
function csrf_ignore(string $method) : bool { function csrf_ignore(string $method) : bool {
$csrf_ignored = array("index", "updateself", "otpqrcode"); $csrf_ignored = array("index", "updateself", "otpqrcode");
@ -187,7 +185,7 @@ class Pref_Prefs extends Handler_Protected {
$boolean_prefs = explode(",", clean($_POST["boolean_prefs"])); $boolean_prefs = explode(",", clean($_POST["boolean_prefs"]));
foreach ($boolean_prefs as $pref) { foreach ($boolean_prefs as $pref) {
if (!isset($_POST[$pref])) $_POST[$pref] = 'false'; $_POST[$pref] ??= 'false';
} }
$need_reload = false; $need_reload = false;
@ -633,10 +631,11 @@ class Pref_Prefs extends Handler_Protected {
} else if ($pref_name == Prefs::USER_CSS_THEME) { } else if ($pref_name == Prefs::USER_CSS_THEME) {
$theme_files = array_map("basename", $theme_files = array_map("basename", [
array_merge(glob("themes/*.php"), ...glob("themes/*.php") ?: [],
glob("themes/*.css"), ...glob("themes/*.css") ?: [],
glob("themes.local/*.css"))); ...glob("themes.local/*.css") ?: [],
]);
asort($theme_files); asort($theme_files);
@ -869,18 +868,19 @@ class Pref_Prefs extends Handler_Protected {
$feed_handler_whitelist = [ "Af_Comics" ]; $feed_handler_whitelist = [ "Af_Comics" ];
$feed_handlers = array_merge( $feed_handlers = [
PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_FETCHED), ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_FETCHED),
PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_PARSED), ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_PARSED),
PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FETCH_FEED)); ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FETCH_FEED),
];
$feed_handlers = array_filter($feed_handlers, function($plugin) use ($feed_handler_whitelist) { $feed_handlers = array_filter($feed_handlers,
return in_array(get_class($plugin), $feed_handler_whitelist) === false; }); fn($plugin) => in_array(get_class($plugin), $feed_handler_whitelist) === false);
if (count($feed_handlers) > 0) { if (count($feed_handlers) > 0) {
print_error( print_error(
T_sprintf("The following plugins use per-feed content hooks. This may cause excessive data usage and origin server load resulting in a ban of your instance: <b>%s</b>" , T_sprintf("The following plugins use per-feed content hooks. This may cause excessive data usage and origin server load resulting in a ban of your instance: <b>%s</b>" ,
implode(", ", array_map(function($plugin) { return get_class($plugin); }, $feed_handlers)) implode(", ", array_map(fn($plugin) => get_class($plugin), $feed_handlers))
) . " (<a href='https://tt-rss.org/wiki/FeedHandlerPlugins' target='_blank'>".__("More info...")."</a>)" ) . " (<a href='https://tt-rss.org/wiki/FeedHandlerPlugins' target='_blank'>".__("More info...")."</a>)"
); );
} }
@ -1069,9 +1069,7 @@ class Pref_Prefs extends Handler_Protected {
} }
} }
$rv = array_values(array_filter($rv, function ($item) { $rv = array_values(array_filter($rv, fn($item) => $item["rv"]["need_update"]));
return $item["rv"]["need_update"];
}));
return $rv; return $rv;
} }

View File

@ -77,7 +77,7 @@ class RPC extends Handler_Protected {
$sth = $this->pdo->prepare("DELETE FROM ttrss_user_entries $sth = $this->pdo->prepare("DELETE FROM ttrss_user_entries
WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
$sth->execute(array_merge($ids, [$_SESSION['uid']])); $sth->execute([...$ids, $_SESSION['uid']]);
Article::_purge_orphans(); Article::_purge_orphans();
@ -364,7 +364,7 @@ class RPC extends Handler_Protected {
WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
} }
$sth->execute(array_merge($ids, [$_SESSION['uid']])); $sth->execute([...$ids, $_SESSION['uid']]);
} }
/** /**
@ -388,7 +388,7 @@ class RPC extends Handler_Protected {
WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
} }
$sth->execute(array_merge($ids, [$_SESSION['uid']])); $sth->execute([...$ids, $_SESSION['uid']]);
} }
function log(): void { function log(): void {
@ -753,12 +753,11 @@ class RPC extends Handler_Protected {
function hotkeyHelp(): void { function hotkeyHelp(): void {
$info = self::get_hotkeys_info(); $info = self::get_hotkeys_info();
$imap = self::get_hotkeys_map(); $imap = self::get_hotkeys_map();
$omap = array(); $omap = [];
foreach ($imap[1] as $sequence => $action) { foreach ($imap[1] as $sequence => $action) {
if (!isset($omap[$action])) $omap[$action] = array(); $omap[$action] ??= [];
$omap[$action][] = $sequence;
array_push($omap[$action], $sequence);
} }
?> ?>

View File

@ -39,7 +39,7 @@ class RSSUtils {
// check icon files once every Config::get(Config::CACHE_MAX_DAYS) days // check icon files once every Config::get(Config::CACHE_MAX_DAYS) days
$icon_files = array_filter(glob(Config::get(Config::ICONS_DIR) . "/*.ico"), $icon_files = array_filter(glob(Config::get(Config::ICONS_DIR) . "/*.ico"),
function($f) { return filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS); }); fn(string $f) => filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS));
foreach ($icon_files as $icon) { foreach ($icon_files as $icon) {
$feed_id = basename($icon, ".ico"); $feed_id = basename($icon, ".ico");
@ -733,7 +733,7 @@ class RSSUtils {
$article_labels = Article::_get_labels($base_entry_id, $feed_obj->owner_uid); $article_labels = Article::_get_labels($base_entry_id, $feed_obj->owner_uid);
$existing_tags = Article::_get_tags($base_entry_id, $feed_obj->owner_uid); $existing_tags = Article::_get_tags($base_entry_id, $feed_obj->owner_uid);
$entry_tags = array_unique(array_merge($entry_tags, $existing_tags)); $entry_tags = array_unique([...$entry_tags, ...$existing_tags]);
} else { } else {
$base_entry_id = false; $base_entry_id = false;
$entry_stored_hash = ""; $entry_stored_hash = "";
@ -865,7 +865,7 @@ class RSSUtils {
$pluginhost->run_hooks(PluginHost::HOOK_FILTER_TRIGGERED, $pluginhost->run_hooks(PluginHost::HOOK_FILTER_TRIGGERED,
$feed, $feed_obj->owner_uid, $article, $matched_filters, $matched_rules, $article_filters); $feed, $feed_obj->owner_uid, $article, $matched_filters, $matched_rules, $article_filters);
$matched_filter_ids = array_map(function($f) { return $f['id']; }, $matched_filters); $matched_filter_ids = array_map(fn(array $f) => $f['id'], $matched_filters);
if (count($matched_filter_ids) > 0) { if (count($matched_filter_ids) > 0) {
$filter_objs = ORM::for_table('ttrss_filters2') $filter_objs = ORM::for_table('ttrss_filters2')
@ -1190,8 +1190,7 @@ class RSSUtils {
// check for manual tags (we have to do it here since they're loaded from filters) // check for manual tags (we have to do it here since they're loaded from filters)
foreach ($article_filters as $f) { foreach ($article_filters as $f) {
if ($f["type"] == "tag") { if ($f["type"] == "tag") {
$entry_tags = array_merge($entry_tags, $entry_tags = [...$entry_tags, ...FeedItem_Common::normalize_categories(explode(",", $f["param"]))];
FeedItem_Common::normalize_categories(explode(",", $f["param"])));
} }
} }
@ -1796,12 +1795,10 @@ class RSSUtils {
owner_uid = ? AND enabled = true ORDER BY order_id, title"); owner_uid = ? AND enabled = true ORDER BY order_id, title");
$sth->execute([$owner_uid]); $sth->execute([$owner_uid]);
$check_cats = array_merge( $check_cats = [...Feeds::_get_parent_cats($cat_id, $owner_uid), $cat_id];
Feeds::_get_parent_cats($cat_id, $owner_uid),
[$cat_id]);
$check_cats_str = join(",", $check_cats); $check_cats_str = join(",", $check_cats);
$check_cats_fullids = array_map(function($a) { return "CAT:$a"; }, $check_cats); $check_cats_fullids = array_map(fn(int $a) => "CAT:$a", $check_cats);
while ($line = $sth->fetch()) { while ($line = $sth->fetch()) {
$filter_id = $line["id"]; $filter_id = $line["id"];

View File

@ -10,31 +10,14 @@ class UrlHelper {
"application/x-bittorrent" => [ "magnet" ], "application/x-bittorrent" => [ "magnet" ],
]; ];
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ static string $fetch_last_error;
/** @var string */ static int $fetch_last_error_code;
static $fetch_last_error; static string $fetch_last_error_content;
static string $fetch_last_content_type;
/** @var int */ static string $fetch_last_modified;
static $fetch_last_error_code; static string $fetch_effective_url;
static string $fetch_effective_ip_addr;
/** @var string */ static bool $fetch_curl_used;
static $fetch_last_error_content;
/** @var string */
static $fetch_last_content_type;
/** @var string */
static $fetch_last_modified;
/** @var string */
static $fetch_effective_url;
/** @var string */
static $fetch_effective_ip_addr;
/** @var bool */
static $fetch_curl_used;
/** /**
* @param array<string, string|int> $parts * @param array<string, string|int> $parts
@ -207,33 +190,27 @@ class UrlHelper {
if ($nest > 10) if ($nest > 10)
return false; return false;
if (version_compare(PHP_VERSION, '7.1.0', '>=')) { $context_options = array(
$context_options = array( 'http' => array(
'http' => array( 'header' => array(
'header' => array( 'Connection: close'
'Connection: close' ),
), 'method' => 'HEAD',
'method' => 'HEAD', 'timeout' => $timeout,
'timeout' => $timeout, 'protocol_version'=> 1.1)
'protocol_version'=> 1.1) );
);
if (Config::get(Config::HTTP_PROXY)) { if (Config::get(Config::HTTP_PROXY)) {
$context_options['http']['request_fulluri'] = true; $context_options['http']['request_fulluri'] = true;
$context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY); $context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY);
}
$context = stream_context_create($context_options);
// PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0
// @phpstan-ignore-next-line
$headers = get_headers($url, 0, $context);
} else {
// PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0
// @phpstan-ignore-next-line
$headers = get_headers($url, 0);
} }
$context = stream_context_create($context_options);
// PHP 8 changed the second param from int to bool, but we still support PHP >= 7.4.0
// @phpstan-ignore-next-line
$headers = get_headers($url, 0, $context);
if (is_array($headers)) { if (is_array($headers)) {
$headers = array_reverse($headers); // last one is the correct one $headers = array_reverse($headers); // last one is the correct one

View File

@ -108,8 +108,7 @@
$valid_langs[$lang] = $t; $valid_langs[$lang] = $t;
$lang = substr($lang, 0, 2); $lang = substr($lang, 0, 2);
if (!isset($valid_langs[$lang])) $valid_langs[$lang] ??= $t;
$valid_langs[$lang] = $t;
} }
// break up string into pieces (languages and q factors) // break up string into pieces (languages and q factors)

View File

@ -19,7 +19,10 @@ class Af_Comics extends Plugin {
require_once __DIR__ . "/filter_base.php"; require_once __DIR__ . "/filter_base.php";
$filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php")); $filters = [
...(glob(__DIR__ . "/filters.local/*.php") ?: []),
...(glob(__DIR__ . "/filters/*.php") ?: []),
];
$names = []; $names = [];
foreach ($filters as $file) { foreach ($filters as $file) {

View File

@ -376,11 +376,11 @@ class Af_RedditImgur extends Plugin {
} }
if ($post_is_nsfw && count($apply_nsfw_tags) > 0) { if ($post_is_nsfw && count($apply_nsfw_tags) > 0) {
$article["tags"] = array_merge($article["tags"], $apply_nsfw_tags); array_push($article["tags"], ...$apply_nsfw_tags);
} }
if (count($link_flairs) > 0) { if (count($link_flairs) > 0) {
$article["tags"] = array_merge($article["tags"], FeedItem_Common::normalize_categories($link_flairs)); array_push($article["tags"], ...FeedItem_Common::normalize_categories($link_flairs));
} }
$article["num_comments"] = $num_comments; $article["num_comments"] = $num_comments;
@ -903,7 +903,7 @@ class Af_RedditImgur extends Plugin {
// do not try to embed posts linking back to other reddit posts // do not try to embed posts linking back to other reddit posts
// readability.php requires PHP 5.6 // readability.php requires PHP 5.6
if ($url && strpos($url, "reddit.com") === false && version_compare(PHP_VERSION, '5.6.0', '>=')) { if ($url && strpos($url, "reddit.com") === false) {
/* link may lead to a huge video file or whatever, we need to check content type before trying to /* link may lead to a huge video file or whatever, we need to check content type before trying to
parse it which p much requires curl */ parse it which p much requires curl */
@ -937,7 +937,7 @@ class Af_RedditImgur extends Plugin {
$src_domain = parse_url($src, PHP_URL_HOST); $src_domain = parse_url($src, PHP_URL_HOST);
if ($src_domain) if ($src_domain)
foreach (array_merge($this->domain_blacklist, $also_blacklist) as $domain) { foreach ([...$this->domain_blacklist, ...$also_blacklist] as $domain) {
if (strstr($src_domain, $domain) !== false) { if (strstr($src_domain, $domain) !== false) {
return true; return true;
} }

View File

@ -84,10 +84,10 @@ class Cache_Starred_Images extends Plugin {
Debug::log("expiring {$this->cache->get_dir()} and {$this->cache_status->get_dir()}..."); Debug::log("expiring {$this->cache->get_dir()} and {$this->cache_status->get_dir()}...");
$files = array_merge( $files = [
glob($this->cache->get_dir() . "/*-*"), ...(glob($this->cache->get_dir() . "/*-*") ?: []),
glob($this->cache_status->get_dir() . "/*.status") ...(glob($this->cache_status->get_dir() . "/*.status") ?: []),
); ];
asort($files); asort($files);