experimental distance search

This commit is contained in:
Mike Macgirvin 2022-10-02 00:41:06 -07:00
parent a569df38a5
commit cf0acdc1ba
2 changed files with 40 additions and 8 deletions

View file

@ -86,6 +86,15 @@ class Stream extends Controller
$distance = ((x($_REQUEST, 'distance')) ? $_REQUEST['distance'] : 0);
$distance_from = ((x($_REQUEST,'distance_from')) ? notags(trim($_REQUEST['distance_from'])) : '');
if ($distance_from) {
$tmp = preg_split('/[ ,\/]/', $distance_from);
$distance_lat = floatval($tmp[0]);
$distance_lon = floatval($tmp[1]);
if (! ($distance_lat || $distance_lon)) {
$distance = 0;
}
}
$c_order = get_pconfig(local_channel(), 'mod_stream', 'order', 0);
switch ($c_order) {
case 0:
@ -579,18 +588,30 @@ class Stream extends Controller
$items = fetch_post_tags($items, true);
} elseif ($this->updating) {
// Normal conversation view
if ($order === 'post') {
$ordering = 'created';
} elseif ($order === 'received') {
$ordering = 'changed';
} elseif ($distance) {
$ordering = 'distance asc, commented';
$sql_extra .= " and (lat != 0 or lon != 0) ";
} else {
$ordering = 'commented';
}
$base_query = ($distance)
? "SELECT item.parent AS item_id,
(6371 * acos(cos(radians(lat) )
* cos( radians( $distance_lat ) )
* cos( radians( $distance_lon ) - radians(lon) )
+ sin( radians(lat) )
* sin( radians( $distance_lat ) )
)) as distance from item"
: "SELECT item.parent AS item_id FROM item";
if ($this->loading) {
// Fetch a page full of parent items for this page
$r = q("SELECT item.parent AS item_id FROM item
$r = q("$base_query
left join abook on ( item.owner_xchan = abook.abook_xchan $abook_uids )
$net_query
WHERE true $uids $item_thread_top $item_normal
@ -599,10 +620,10 @@ class Stream extends Controller
$sql_extra3 $sql_extra $sql_options $sql_nets
$net_query2
ORDER BY $ordering DESC $pager_sql ");
} else {
// this is an update
$r = q("SELECT item.parent AS item_id FROM item
$r = q("$base_query
left join abook on ( item.owner_xchan = abook.abook_xchan $abook_uids )
$net_query
WHERE true $uids $item_normal_update $simple_update
@ -621,6 +642,17 @@ class Stream extends Controller
dbesc($parents_str)
);
if ($distance) {
foreach ($r as $parent) {
for($cnt = 0; $cnt < count($items); $cnt ++) {
if ($parent['item_id'] === $items[$cnt]['id']) {
$items[$cnt]['distance'] = $parent['distance'];
}
}
}
}
xchan_query($items, true);
$items = fetch_post_tags($items, true);
$items = conv_sort($items, $ordering);

View file

@ -1604,6 +1604,8 @@ function conv_sort($arr, $order)
if (stristr($order, 'created')) {
usort($parents, 'sort_thr_created');
} elseif (stristr($order, 'distance')) {
usort($parents, 'sort_thr_distance');
} elseif (stristr($order, 'commented')) {
usort($parents, 'sort_thr_commented');
} elseif (stristr($order, 'updated')) {
@ -1612,8 +1614,6 @@ function conv_sort($arr, $order)
usort($parents, 'sort_thr_received');
} elseif (stristr($order, 'ascending')) {
usort($parents, 'sort_thr_created_rev');
} elseif (stristr($order, 'distance')) {
usort($parents, 'sort_thr_distance');
}
if ($parents) {
foreach ($parents as $i => $_x) {
@ -1714,7 +1714,7 @@ function sort_thr_distance($a, $b)
if (!isset($b['distance'])) {
$b['distance'] = 999999999;
}
return strcmp($b['distance'], $a['distance']);
return floatval($a['distance']) <=> floatval($b['distance']);
}
function sort_thr_updated($a, $b)
@ -1746,7 +1746,7 @@ function format_location($item)
Hook::call('render_location', $locate);
$location = ((strlen($locate['html'])) ? $locate['html'] : render_location_default($locate));
}
return $location;
return $location . (!empty($item['distance']) ? t(' distance: ') . sprintf("%05.03f km",$item['distance']) : '');
}
function render_location_default($item)