BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autocomplete.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. #declare(strict_types=1);
  3. if (empty($_GET['query'])) {
  4. error(400);
  5. }
  6. header('Content-Type: application/json; charset=utf-8');
  7. $FullName = rawurldecode($_GET['query']);
  8. $MaxKeySize = 4;
  9. if (strtolower(substr($FullName, 0, 4)) === 'the ') {
  10. $MaxKeySize += 4;
  11. }
  12. $KeySize = min($MaxKeySize, max(1, strlen($FullName)));
  13. $Letters = strtolower(substr($FullName, 0, $KeySize));
  14. $AutoSuggest = $Cache->get('autocomplete_artist_'.$KeySize.'_'.$Letters);
  15. if (!$AutoSuggest) {
  16. $Limit = (($KeySize === $MaxKeySize) ? 250 : 10);
  17. $DB->query("
  18. SELECT
  19. a.ArtistID,
  20. a.Name
  21. FROM artists_group AS a
  22. INNER JOIN torrents_artists AS ta ON ta.ArtistID=a.ArtistID
  23. INNER JOIN torrents AS t ON t.GroupID=ta.GroupID
  24. WHERE a.Name LIKE '".db_string(str_replace('\\', '\\\\', $Letters), true)."%'
  25. GROUP BY ta.ArtistID
  26. ORDER BY t.Snatched DESC
  27. LIMIT $Limit");
  28. $AutoSuggest = $DB->to_array(false, MYSQLI_NUM, false);
  29. $Cache->cache_value('autocomplete_artist_'.$KeySize.'_'.$Letters, $AutoSuggest, 1800 + 7200 * ($MaxKeySize - $KeySize)); // Can't cache things for too long in case names are edited
  30. }
  31. $Matched = 0;
  32. $ArtistIDs = [];
  33. $Response = array(
  34. 'query' => $FullName,
  35. 'suggestions' => []
  36. );
  37. foreach ($AutoSuggest as $Suggestion) {
  38. list($ID, $Name) = $Suggestion;
  39. if (stripos($Name, $FullName) === 0) {
  40. $Response['suggestions'][] = array('value' => $Name, 'data' => $ID);
  41. if (++$Matched > 9) {
  42. break;
  43. }
  44. }
  45. }
  46. echo json_encode($Response);