unified search autocomplete backend, browser performance seems to be a bit sucky

This commit is contained in:
friendica 2012-07-16 18:14:45 -07:00
parent d3d60c8ae5
commit 07c31d547f
2 changed files with 104 additions and 0 deletions

View file

@ -114,6 +114,25 @@ function network_init(&$a) {
$a->page['aside'] .= saved_searches($search);
$a->page['aside'] .= fileas_widget($a->get_baseurl(true) . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));
$base = $a->get_baseurl();
$a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/friendica.complete.js" ></script>';
$a->page['htmlhead'] .= <<< EOT
<script>$(document).ready(function() {
var a;
a = $("#search-text").autocomplete({
serviceUrl: '$base/search_ac',
minChars: 2,
width: 350,
});
});
</script>
EOT;
}
function saved_searches($search) {

85
mod/search_ac.php Normal file
View file

@ -0,0 +1,85 @@
<?php
function search_ac_init(&$a){
if(!local_user())
return "";
$start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
$count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
$search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
$search = $_REQUEST['query'];
}
$sql_extra = '';
$x = array();
$x['query'] = $search;
$x['photos'] = array();
$x['links'] = array();
$x['suggestions'] = array();
$x['data'] = array();
// Priority to people searches
if ($search) {
$people_sql_extra = protect_sprintf(" AND `name` LIKE '%". dbesc($search) . "%' ");
$tag_sql_extra = protect_sprintf(" AND term LIKE '%". dbesc($search) . "%' ");
}
$r = q("SELECT `id`, `name`, `micro`, `url` FROM `contact`
WHERE `uid` = %d AND `pending` = 0
$people_sql_extra
ORDER BY `name` ASC ",
intval(local_user())
);
if(count($r)) {
foreach($r as $g) {
$x['photos'][] = $g['micro'];
$x['links'][] = $g['url'];
$x['suggestions'][] = '@' . $g['name'];
$x['data'][] = intval($g['id']);
}
}
$r = q("SELECT `id`, `name`, `photo`, `url` FROM `gcontact` where 1
$people_sql_extra
ORDER BY `name` ASC "
);
if(count($r)) {
foreach($r as $g) {
$x['photos'][] = $g['photo'];
$x['links'][] = $g['url'];
$x['suggestions'][] = '@' . $g['name'];
$x['data'][] = intval($g['id']);
}
}
$r = q("select tid, term, url from term where type = %d $tag_sql_extra order by term asc",
intval(TERM_HASHTAG)
);
if(count($r)) {
foreach($r as $g) {
$x['photos'][] = $a->get_baseurl() . '/images/hashtag.png';
$x['links'][] = $g['url'];
$x['suggestions'][] = '#' . $g['term'];
$x['data'][] = intval($g['tid']);
}
}
header("content-type: application/json");
echo json_encode($x);
logger('search_ac: ' . print_r($x,true));
killme();
}