Skip to content

Commit

Permalink
daos: Call stmt functions indirectly
Browse files Browse the repository at this point in the history
Apparently PHP 5.6 does not support calling functions using `static::$stmt::fn()` so we need to save `static::$stmt` into a local variable.
  • Loading branch information
jtojnar committed Feb 5, 2020
1 parent fda5478 commit 91f9d0e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
58 changes: 34 additions & 24 deletions src/daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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']
);
}
Expand All @@ -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';
}
Expand All @@ -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) {
Expand All @@ -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
];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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]
);
Expand All @@ -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'),
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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)
];
}
}
Expand Down Expand Up @@ -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]]);
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/daos/mysql/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)),
Expand Down Expand Up @@ -140,18 +143,19 @@ 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 {
$ret = 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
]);
Expand All @@ -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
]);
Expand All @@ -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,
Expand All @@ -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
]);
Expand Down
7 changes: 4 additions & 3 deletions src/daos/mysql/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/**
Expand Down

0 comments on commit 91f9d0e

Please sign in to comment.