From 91f9d0e896a0c75b33739b10473be71bb4737a61 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 5 Feb 2020 04:05:15 +0100 Subject: [PATCH] daos: Call stmt functions indirectly Apparently PHP 5.6 does not support calling functions using `static::$stmt::fn()` so we need to save `static::$stmt` into a local variable. --- src/daos/mysql/Items.php | 58 ++++++++++++++++++++++---------------- src/daos/mysql/Sources.php | 24 ++++++++++------ src/daos/mysql/Tags.php | 7 +++-- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/daos/mysql/Items.php b/src/daos/mysql/Items.php index 4f1b014812..01c2f4e508 100644 --- a/src/daos/mysql/Items.php +++ b/src/daos/mysql/Items.php @@ -184,8 +184,9 @@ public function findAll($itemsInFeed, $sourceId) { * @return void */ public function updateLastSeen(array $itemIds) { + $stmt = static::$stmt; \F3::get('db')->exec('UPDATE ' . \F3::get('db_prefix') . 'items SET lastseen = CURRENT_TIMESTAMP - WHERE ' . static::$stmt::intRowMatches('id', $itemIds)); + WHERE ' . $stmt::intRowMatches('id', $itemIds)); } /** @@ -196,12 +197,13 @@ public function updateLastSeen(array $itemIds) { * @return void */ public function cleanup(DateTime $date = null) { + $stmt = static::$stmt; \F3::get('db')->exec('DELETE FROM ' . \F3::get('db_prefix') . 'items WHERE source NOT IN ( SELECT id FROM ' . \F3::get('db_prefix') . 'sources)'); if ($date !== null) { \F3::get('db')->exec('DELETE FROM ' . \F3::get('db_prefix') . 'items - WHERE ' . static::$stmt::isFalse('starred') . ' AND lastseen<:date', + WHERE ' . $stmt::isFalse('starred') . ' AND lastseen<:date', [':date' => $date->format('Y-m-d') . ' 00:00:00'] ); } @@ -215,18 +217,19 @@ public function cleanup(DateTime $date = null) { * @return mixed items as array */ public function get($options = []) { + $stmt = static::$stmt; $params = []; - $where = [static::$stmt::bool(true)]; + $where = [$stmt::bool(true)]; $order = 'DESC'; // only starred if (isset($options['type']) && $options['type'] === 'starred') { - $where[] = static::$stmt::isTrue('starred'); + $where[] = $stmt::isTrue('starred'); } // only unread elseif (isset($options['type']) && $options['type'] === 'unread') { - $where[] = static::$stmt::isTrue('unread'); + $where[] = $stmt::isTrue('unread'); if (\F3::get('unread_order') === 'asc') { $order = 'ASC'; } @@ -243,7 +246,7 @@ public function get($options = []) { if (isset($options['tag']) && strlen($options['tag']) > 0) { $params[':tag'] = $options['tag']; $where[] = 'items.source=sources.id'; - $where[] = static::$stmt::csvRowMatches('sources.tags', ':tag'); + $where[] = $stmt::csvRowMatches('sources.tags', ':tag'); } // source filter elseif (isset($options['source']) && strlen($options['source']) > 0) { @@ -266,7 +269,7 @@ public function get($options = []) { // with seek pagination. $options['offset'] = 0; - $offset_from_datetime_sql = static::$stmt::datetime($options['fromDatetime']); + $offset_from_datetime_sql = $stmt::datetime($options['fromDatetime']); $params[':offset_from_datetime'] = [ $offset_from_datetime_sql, \PDO::PARAM_STR ]; @@ -297,7 +300,7 @@ public function get($options = []) { && count($options['extraIds']) > 0 // limit the query to a sensible max && count($options['extraIds']) <= \F3::get('items_perpage')) { - $extra_ids_stmt = static::$stmt::intRowMatches('items.id', $options['extraIds']); + $extra_ids_stmt = $stmt::intRowMatches('items.id', $options['extraIds']); if ($extra_ids_stmt !== null) { $where_ids = $extra_ids_stmt; } @@ -349,7 +352,7 @@ public function get($options = []) { $query = "$select $where_sql $order_sql LIMIT " . $options['items'] . ' OFFSET ' . $options['offset']; } - return static::$stmt::ensureRowTypes(\F3::get('db')->exec($query, $params), [ + return $stmt::ensureRowTypes(\F3::get('db')->exec($query, $params), [ 'id' => \daos\PARAM_INT, 'unread' => \daos\PARAM_BOOL, 'starred' => \daos\PARAM_BOOL, @@ -379,12 +382,13 @@ public function hasMore() { * @return array of items */ public function sync($sinceId, DateTime $notBefore, DateTime $since, $howMany) { + $stmt = static::$stmt; $query = 'SELECT items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, author, sources.title as sourcetitle, sources.tags as tags FROM ' . \F3::get('db_prefix') . 'items AS items, ' . \F3::get('db_prefix') . 'sources AS sources WHERE items.source=sources.id - AND (' . static::$stmt::isTrue('unread') . - ' OR ' . static::$stmt::isTrue('starred') . + AND (' . $stmt::isTrue('unread') . + ' OR ' . $stmt::isTrue('starred') . ' OR datetime >= :notBefore ) AND (items.id > :sinceId OR @@ -398,7 +402,7 @@ public function sync($sinceId, DateTime $notBefore, DateTime $since, $howMany) { 'since' => [$since->format(\DateTime::ATOM), \PDO::PARAM_STR] ]; - return static::$stmt::ensureRowTypes(\F3::get('db')->exec($query, $params), [ + return $stmt::ensureRowTypes(\F3::get('db')->exec($query, $params), [ 'id' => \daos\PARAM_INT, 'unread' => \daos\PARAM_BOOL, 'starred' => \daos\PARAM_BOOL, @@ -412,11 +416,12 @@ public function sync($sinceId, DateTime $notBefore, DateTime $since, $howMany) { * @return int lowest id of interest */ public function lowestIdOfInterest() { - $lowest = static::$stmt::ensureRowTypes( + $stmt = static::$stmt; + $lowest = $stmt::ensureRowTypes( \F3::get('db')->exec( 'SELECT id FROM ' . \F3::get('db_prefix') . 'items AS items - WHERE ' . static::$stmt::isTrue('unread') . - ' OR ' . static::$stmt::isTrue('starred') . + WHERE ' . $stmt::isTrue('unread') . + ' OR ' . $stmt::isTrue('starred') . ' ORDER BY id LIMIT 1'), ['id' => \daos\PARAM_INT] ); @@ -433,7 +438,8 @@ public function lowestIdOfInterest() { * @return int last id in db */ public function lastId() { - $lastId = static::$stmt::ensureRowTypes( + $stmt = static::$stmt; + $lastId = $stmt::ensureRowTypes( \F3::get('db')->exec( 'SELECT id FROM ' . \F3::get('db_prefix') . 'items AS items ORDER BY id DESC LIMIT 1'), @@ -573,9 +579,10 @@ public function getLastIcon($sourceid) { * @return int amount of entries in database which are unread */ public function numberOfUnread() { + $stmt = static::$stmt; $res = \F3::get('db')->exec('SELECT count(*) AS amount FROM ' . \F3::get('db_prefix') . 'items - WHERE ' . static::$stmt::isTrue('unread')); + WHERE ' . $stmt::isTrue('unread')); return $res[0]['amount']; } @@ -586,12 +593,13 @@ public function numberOfUnread() { * @return array mount of total, unread, starred entries in database */ public function stats() { + $stmt = static::$stmt; $res = \F3::get('db')->exec('SELECT COUNT(*) AS total, - ' . static::$stmt::sumBool('unread') . ' AS unread, - ' . static::$stmt::sumBool('starred') . ' AS starred + ' . $stmt::sumBool('unread') . ' AS unread, + ' . $stmt::sumBool('starred') . ' AS starred FROM ' . \F3::get('db_prefix') . 'items;'); - $res = static::$stmt::ensureRowTypes($res, [ + $res = $stmt::ensureRowTypes($res, [ 'total' => \daos\PARAM_INT, 'unread' => \daos\PARAM_INT, 'starred' => \daos\PARAM_INT @@ -621,11 +629,12 @@ public function lastUpdate() { * @return array of unread, starred, etc. status of specified items */ public function statuses(DateTime $since) { + $stmt = static::$stmt; $res = \F3::get('db')->exec('SELECT id, unread, starred FROM ' . \F3::get('db_prefix') . 'items WHERE ' . \F3::get('db_prefix') . 'items.updatetime > :since;', [':since' => [$since->format(DateTime::ATOM), \PDO::PARAM_STR]]); - $res = static::$stmt::ensureRowTypes($res, [ + $res = $stmt::ensureRowTypes($res, [ 'id' => \daos\PARAM_INT, 'unread' => \daos\PARAM_BOOL, 'starred' => \daos\PARAM_BOOL @@ -642,6 +651,7 @@ public function statuses(DateTime $since) { * @return void */ public function bulkStatusUpdate(array $statuses) { + $stmt = static::$stmt; $sql = []; foreach ($statuses as $status) { if (array_key_exists('id', $status)) { @@ -655,12 +665,12 @@ public function bulkStatusUpdate(array $statuses) { if ($status[$sk] == 'true') { $statusUpdate = [ 'sk' => $sk, - 'sql' => static::$stmt::isTrue($sk) + 'sql' => $stmt::isTrue($sk) ]; } elseif ($status[$sk] == 'false') { $statusUpdate = [ 'sk' => $sk, - 'sql' => static::$stmt::isFalse($sk) + 'sql' => $stmt::isFalse($sk) ]; } } @@ -715,7 +725,7 @@ public function bulkStatusUpdate(array $statuses) { // statuses. \F3::get('db')->exec( 'UPDATE ' . \F3::get('db_prefix') . 'items - SET ' . static::$stmt::rowTouch('updatetime') . ' + SET ' . $stmt::rowTouch('updatetime') . ' WHERE id = :id', [':id' => [$id, \PDO::PARAM_INT]]); } } diff --git a/src/daos/mysql/Sources.php b/src/daos/mysql/Sources.php index ada542ccbf..86ada2d84d 100644 --- a/src/daos/mysql/Sources.php +++ b/src/daos/mysql/Sources.php @@ -25,9 +25,11 @@ class Sources { * @return int new id */ public function add($title, array $tags, $filter, $spout, array $params) { - return static::$stmt::insert('INSERT INTO ' . \F3::get('db_prefix') . 'sources (title, tags, filter, spout, params) VALUES (:title, :tags, :filter, :spout, :params)', [ + $stmt = static::$stmt; + + return $stmt::insert('INSERT INTO ' . \F3::get('db_prefix') . 'sources (title, tags, filter, spout, params) VALUES (:title, :tags, :filter, :spout, :params)', [ ':title' => trim($title), - ':tags' => static::$stmt::csvRow($tags), + ':tags' => $stmt::csvRow($tags), ':filter' => $filter, ':spout' => $spout, ':params' => htmlentities(json_encode($params)) @@ -47,9 +49,10 @@ public function add($title, array $tags, $filter, $spout, array $params) { * @return void */ public function edit($id, $title, array $tags, $filter, $spout, array $params) { + $stmt = static::$stmt; \F3::get('db')->exec('UPDATE ' . \F3::get('db_prefix') . 'sources SET title=:title, tags=:tags, filter=:filter, spout=:spout, params=:params WHERE id=:id', [ ':title' => trim($title), - ':tags' => static::$stmt::csvRow($tags), + ':tags' => $stmt::csvRow($tags), ':filter' => $filter, ':spout' => $spout, ':params' => htmlentities(json_encode($params)), @@ -140,10 +143,11 @@ public function getByLastUpdate() { * @return ?mixed specified source or all sources */ public function get($id = null) { + $stmt = static::$stmt; // select source by id if specified or return all sources if (isset($id)) { $ret = \F3::get('db')->exec('SELECT id, title, tags, spout, params, filter, error FROM ' . \F3::get('db_prefix') . 'sources WHERE id=:id', [':id' => $id]); - $ret = static::$stmt::ensureRowTypes($ret, ['id' => \daos\PARAM_INT]); + $ret = $stmt::ensureRowTypes($ret, ['id' => \daos\PARAM_INT]); if (isset($ret[0])) { $ret = $ret[0]; } else { @@ -151,7 +155,7 @@ public function get($id = null) { } } else { $ret = \F3::get('db')->exec('SELECT id, title, tags, spout, params, filter, error FROM ' . \F3::get('db_prefix') . 'sources ORDER BY error DESC, lower(title) ASC'); - $ret = static::$stmt::ensureRowTypes($ret, [ + $ret = $stmt::ensureRowTypes($ret, [ 'id' => \daos\PARAM_INT, 'tags' => \daos\PARAM_CSV ]); @@ -166,15 +170,16 @@ public function get($id = null) { * @return mixed all sources */ public function getWithUnread() { + $stmt = static::$stmt; $ret = \F3::get('db')->exec('SELECT sources.id, sources.title, COUNT(items.id) AS unread FROM ' . \F3::get('db_prefix') . 'sources AS sources LEFT OUTER JOIN ' . \F3::get('db_prefix') . 'items AS items - ON (items.source=sources.id AND ' . static::$stmt::isTrue('items.unread') . ') + ON (items.source=sources.id AND ' . $stmt::isTrue('items.unread') . ') GROUP BY sources.id, sources.title ORDER BY lower(sources.title) ASC'); - return static::$stmt::ensureRowTypes($ret, [ + return $stmt::ensureRowTypes($ret, [ 'id' => \daos\PARAM_INT, 'unread' => \daos\PARAM_INT ]); @@ -186,6 +191,7 @@ public function getWithUnread() { * @return mixed all sources */ public function getWithIcon() { + $stmt = static::$stmt; $ret = \F3::get('db')->exec('SELECT sources.id, sources.title, sources.tags, sources.spout, sources.params, sources.filter, sources.error, sources.lastentry, @@ -201,9 +207,9 @@ public function getWithIcon() { WHERE items.id=icons.maxid AND items.source=icons.source ) AS sourceicons ON sources.id=sourceicons.source - ORDER BY ' . static::$stmt::nullFirst('sources.error', 'DESC') . ', lower(sources.title)'); + ORDER BY ' . $stmt::nullFirst('sources.error', 'DESC') . ', lower(sources.title)'); - return static::$stmt::ensureRowTypes($ret, [ + return $stmt::ensureRowTypes($ret, [ 'id' => \daos\PARAM_INT, 'tags' => \daos\PARAM_CSV ]); diff --git a/src/daos/mysql/Tags.php b/src/daos/mysql/Tags.php index 83f31f0bc2..a685e06a89 100644 --- a/src/daos/mysql/Tags.php +++ b/src/daos/mysql/Tags.php @@ -81,16 +81,17 @@ public function get() { * @return array of all tags */ public function getWithUnread() { + $stmt = static::$stmt; $select = 'SELECT tag, color, COUNT(items.id) AS unread FROM ' . \F3::get('db_prefix') . 'tags AS tags, ' . \F3::get('db_prefix') . 'sources AS sources LEFT OUTER JOIN ' . \F3::get('db_prefix') . 'items AS items - ON (items.source=sources.id AND ' . static::$stmt::isTrue('items.unread') . ') - WHERE ' . static::$stmt::csvRowMatches('sources.tags', 'tags.tag') . ' + ON (items.source=sources.id AND ' . $stmt::isTrue('items.unread') . ') + WHERE ' . $stmt::csvRowMatches('sources.tags', 'tags.tag') . ' GROUP BY tags.tag, tags.color ORDER BY LOWER(tags.tag);'; - return static::$stmt::ensureRowTypes(\F3::get('db')->exec($select), ['unread' => \daos\PARAM_INT]); + return $stmt::ensureRowTypes(\F3::get('db')->exec($select), ['unread' => \daos\PARAM_INT]); } /**