From 06ccba1c28f9eff7c335ad4d17aa84e58d0b9ad9 Mon Sep 17 00:00:00 2001 From: root <17hado.com@gmail.com> Date: Fri, 21 Dec 2018 17:42:39 +0000 Subject: [PATCH] code style & use DBA::p --- include/api.php | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/include/api.php b/include/api.php index fe5cc9168a..c275dbf391 100644 --- a/include/api.php +++ b/include/api.php @@ -1526,6 +1526,7 @@ function api_search($type) $searchTerm = trim(rawurldecode($_REQUEST['q'])); $data = []; + $data['status'] = []; $count = 15; $exclude_replies = !empty($_REQUEST['exclude_replies']); if (!empty($_REQUEST['rpp'])) { @@ -1551,14 +1552,24 @@ function api_search($type) } $terms = DBA::select('term', ['oid'], $condition, []); $itemIds = []; - while($term = DBA::fetch($terms)){ $itemIds[] = $term['oid']; } + while ( $term = DBA::fetch($terms) ) { + $itemIds[] = $term['oid']; + } DBA::close($terms); - $condition = [$exclude_replies ? "`id` = `parent` AND " : '']; - $condition[0] .= empty($itemIds) ? '' : ' `id` IN ('.implode(", ", $itemIds).')' ; + if (empty($itemIds)) { + return api_format_data("statuses", $type, $data); + } + + $tmpAr = ['`id` IN ('.implode(", ", $itemIds).')']; + if ($exclude_replies) { + $tmpAr[] = "`id` = `parent`"; + } + + $condition = [ implode(" AND ", $tmpAr) ]; } else { $condition = ["`id` > ? - ". ($exclude_replies ? " AND `id` = `parent` " : ' ')." + " . ( $exclude_replies ? " AND `id` = `parent` " : ' ' ) . " AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `body` LIKE CONCAT('%',?,'%')", $since_id, api_user(), $_REQUEST['q']]; @@ -1566,7 +1577,6 @@ function api_search($type) $condition[0] .= " AND `id` <= ?"; $condition[] = $max_id; } - } $statuses = Item::selectForUser(api_user(), [], $condition, $params); @@ -5994,22 +6004,28 @@ api_register_func('api/saved_searches/list', 'api_saved_searches_list', true); * * @return void */ -function bindComments(&$data){ - if(count($data) == 0) return; +function bindComments(&$data) +{ + if (count($data) == 0) { + return; + } $ids = []; $comments = []; - foreach($data as $item){ $ids[] = $item['id']; } + foreach ($data as $item) { + $ids[] = $item['id']; + } - $sql = "SELECT `parent`,COUNT(*) as comments FROM `item` - WHERE `parent` IN ( %s ) AND `deleted` = %d AND `gravity`= %d GROUP BY `parent`"; - $result = q($sql, implode(",", $ids), 0, GRAVITY_COMMENT); + $idStr = DBA::escape(implode(", ", $ids)); + $sql = "SELECT `parent`, COUNT(*) as comments FROM `item` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`"; + $items = DBA::p($sql, 0, GRAVITY_COMMENT); + $itemsData = DBA::toArray($items); - foreach($result as $records) { - $comments[$records['parent']] = $records['comments']; + foreach ($itemsData as $item) { + $comments[$item['parent']] = $item['comments']; } - foreach($data as $idx => $item){ + foreach ($data as $idx => $item) { $id = $item['id']; $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0; } -- 2.39.5