code style & use DBA::p

This commit is contained in:
root 2018-12-21 17:42:39 +00:00 committed by Hypolite Petovan
parent 61afd5b3fb
commit 06ccba1c28

View file

@ -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;
}