]> git.mxchange.org Git - friendica.git/commitdiff
Implement search API (fixes #929)
authorPierre Rudloff <contact@rudloff.pro>
Mon, 18 Dec 2017 01:59:11 +0000 (02:59 +0100)
committerPierre Rudloff <contact@rudloff.pro>
Mon, 18 Dec 2017 12:05:27 +0000 (13:05 +0100)
doc/api.md
include/api.php

index 159bc6991449d2060434a84795b2cd9a945a7ded..3c5cd0b7be2b4a019413495671320cd355b297e6 100644 (file)
@@ -449,6 +449,24 @@ It shows all direct answers (excluding the original post) to a given id.
 
 Friendica doesn't allow showing followers of other users.
 
+---
+### search (*; AUTH)
+#### Parameters
+* q: search query
+* page: the page number (starting at 1) to return
+* rpp: the number of statuses to return per page
+* count: alias for the rpp parameter
+* since_id: returns statuses with ids greater than the given id
+* max_id: returns statuses with ids lower or equal to the given id
+
+#### Unsupported parameters
+* geocode
+* lang
+* locale
+* result_type
+* until
+* include_entities
+
 ---
 ### users/search (*)
 #### Parameters
index 2aa8fc645cf1816a24dcbd5d9ce36c1405e15dd1..9cc82560d2e358e61c2c9965bf44323c7690534c 100644 (file)
@@ -1485,6 +1485,71 @@ function api_users_search($type)
 /// @TODO move to top of file or somewhere better
 api_register_func('api/users/search', 'api_users_search');
 
+/**
+ * Returns statuses that match a specified query.
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
+ *
+ * @param string $type Return format: json, xml, atom, rss
+ *
+ * @return array|string
+ * @throws UnauthorizedException
+ * @throws BadRequestException
+ */
+function api_search($type)
+{
+       $data = array();
+
+       if (x($_REQUEST, 'q')) {
+               if (x($_REQUEST, 'rpp')) {
+                       $count = $_REQUEST['rpp'];
+               } elseif (x($_REQUEST, 'count')) {
+                       $count = $_REQUEST['count'];
+               } else {
+                       $count = 15;
+               }
+
+               $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0);
+               $max_id = (x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0);
+               $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] - 1 : 0);
+
+               $start = $page * $count;
+
+               if ($max_id > 0) {
+                       $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id);
+               }
+
+               $r = q(
+                       "SELECT %s
+                       FROM `item` %s
+                       WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
+                       AND `item`.`body` REGEXP '%s'
+                       $sql_extra
+                       AND `item`.`id`>%d
+                       GROUP BY `item`.`uri`, `item`.`id`
+                       ORDER BY `item`.`id` DESC LIMIT %d ,%d ",
+                       item_fieldlists(),
+                       item_joins(),
+                       item_condition(),
+                       intval(local_user()),
+                       dbesc(protect_sprintf(preg_quote($_REQUEST['q']))),
+                       intval($since_id),
+                       intval($start),
+                       intval($count)
+               );
+
+               $data['status'] = api_format_items($r, api_get_user(get_app()));
+       } else {
+               throw new BadRequestException("q parameter is required.");
+       }
+
+       return api_format_data("statuses", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/search/tweets', 'api_search', true);
+api_register_func('api/search', 'api_search', true);
+
 /**
  *
  * http://developer.twitter.com/doc/get/statuses/home_timeline