#6275 - q support hashtag search

This commit is contained in:
root 2018-12-17 04:41:55 +00:00 committed by Hypolite Petovan
parent 939364f9a0
commit 8de263df8b
3 changed files with 18 additions and 34 deletions

View file

@ -751,7 +751,6 @@ Friendica doesn't allow showing followers of other users.
#### Parameters #### Parameters
* q: search query * q: search query
* friendica_tag: search hashtag (optional; query condition only accept one of the two [ q | friendica_tag ] )
* page: the page number (starting at 1) to return * page: the page number (starting at 1) to return
* rpp: the number of statuses to return per page * rpp: the number of statuses to return per page
* count: alias for the rpp parameter * count: alias for the rpp parameter

View file

@ -1521,7 +1521,9 @@ function api_search($type)
if (api_user() === false || $user_info === false) { throw new ForbiddenException(); } if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
if (empty($_REQUEST['q']) && empty($_REQUEST['friendica_tag'])) { throw new BadRequestException("q or friendica_tag parameter is required."); } if (empty($_REQUEST['q'])) { throw new BadRequestException("q parameter is required."); }
$searchTerm = trim(rawurldecode($_REQUEST['q']));
$data = []; $data = [];
$count = 15; $count = 15;
@ -1536,25 +1538,12 @@ function api_search($type)
$page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0); $page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0);
$start = $page * $count; $start = $page * $count;
$params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
if (preg_match('/^\#([^#]+)/', $searchTerm, $matches) === 1 && isset($matches[1])) {
if (!empty($_REQUEST['q'])) { $searchTerm = $matches[1];
$condition = [ $condition = ["`oid` > ?
"`id` > ?
AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `body` LIKE CONCAT('%',?,'%')",
$since_id, api_user(), $_REQUEST['q']];
if ($max_id > 0) {
$condition[0] .= " AND `id` <= ?";
$condition[] = $max_id;
}
} elseif (!empty($_REQUEST['friendica_tag'])) {
$condition = [
"`oid` > ?
AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `otype` = ? AND `type` = ? AND `term` = ?", AND `otype` = ? AND `type` = ? AND `term` = ?",
$since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG, $since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG, $searchTerm];
Strings::escapeTags(trim(rawurldecode($_REQUEST['friendica_tag'])))
];
if ($max_id > 0) { if ($max_id > 0) {
$condition[0] .= " AND `oid` <= ?"; $condition[0] .= " AND `oid` <= ?";
$condition[] = $max_id; $condition[] = $max_id;
@ -1564,6 +1553,16 @@ function api_search($type)
while($term = DBA::fetch($terms)){ $itemIds[] = $term['oid']; } while($term = DBA::fetch($terms)){ $itemIds[] = $term['oid']; }
DBA::close($terms); DBA::close($terms);
$condition = ['id' => empty($itemIds) ? [0] : $itemIds ]; $condition = ['id' => empty($itemIds) ? [0] : $itemIds ];
} else {
$condition = ["`id` > ?
AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `body` LIKE CONCAT('%',?,'%')",
$since_id, api_user(), $_REQUEST['q']];
if ($max_id > 0) {
$condition[0] .= " AND `id` <= ?";
$condition[] = $max_id;
}
} }
$statuses = Item::selectForUser(api_user(), [], $condition, $params); $statuses = Item::selectForUser(api_user(), [], $condition, $params);

View file

@ -1390,20 +1390,6 @@ class ApiTest extends DatabaseTest
} }
} }
/**
* Test the api_search() function with friendica_tag
* @return void
*/
public function testApiSearchWithFriendicaTag()
{
$_REQUEST['friendica_tag'] = 'friendica';
$result = api_search('json');
foreach ($result['status'] as $status) {
$this->assertStatus($status);
$this->assertContains('#friendica', $status['text'], null, true);
}
}
/** /**
* Test the api_search() function with an rpp parameter. * Test the api_search() function with an rpp parameter.
* @return void * @return void