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 | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/include/api.php b/include/api.php index fe5cc9168..c275dbf39 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']; } - - $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); - - foreach($result as $records) { - $comments[$records['parent']] = $records['comments']; + foreach ($data as $item) { + $ids[] = $item['id']; } - foreach($data as $idx => $item){ + $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 ($itemsData as $item) { + $comments[$item['parent']] = $item['comments']; + } + + foreach ($data as $idx => $item) { $id = $item['id']; $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0; }