Gazelle/sections/artist/autocomplete.php

57 lines
1.7 KiB
PHP
Raw Normal View History

<?
header('Content-type: application/x-suggestions+json');
require('classes/ajax_start.php');
2013-05-05 08:00:31 +00:00
if (empty($_GET['name'])) {
die('["",[],[],[]]');
}
$FullName = rawurldecode($_GET['name']);
$MaxKeySize = 4;
if (strtolower(substr($FullName,0,4)) == 'the ') {
$MaxKeySize += 4;
}
$KeySize = min($MaxKeySize,max(1,strlen($FullName)));
$Letters = strtolower(substr($FullName,0,$KeySize));
$AutoSuggest = $Cache->get('autocomplete_artist_'.$KeySize.'_'.$Letters);
2013-05-05 08:00:31 +00:00
if (!is_array($AutoSuggest)) {
if (!isset($DB) || !is_object($DB)) {
2013-05-27 08:00:58 +00:00
require(SERVER_ROOT.'/classes/mysql.class.php'); //Require the database wrapper
$DB = NEW DB_MYSQL; //Load the database wrapper
}
2013-05-05 08:00:31 +00:00
$Limit = (($KeySize === $MaxKeySize) ? 250 : 10);
$DB->query("
SELECT
a.ArtistID,
a.Name,
SUM(t.Snatched) AS Snatches
2013-02-22 08:00:24 +00:00
FROM artists_group AS a
2013-05-05 08:00:31 +00:00
INNER JOIN torrents_artists AS ta ON ta.ArtistID=a.ArtistID
INNER JOIN torrents AS t ON t.GroupID=ta.GroupID
2012-06-02 08:00:16 +00:00
WHERE a.Name LIKE '".db_string(str_replace('\\','\\\\',$Letters),true)."%'
2013-02-22 08:00:24 +00:00
GROUP BY ta.ArtistID
ORDER BY Snatches DESC
LIMIT $Limit");
$AutoSuggest = $DB->to_array(false,MYSQLI_NUM,false);
2013-05-05 08:00:31 +00:00
$Cache->cache_value('autocomplete_artist_'.$KeySize.'_'.$Letters,$AutoSuggest,1800 + 7200 * ($MaxKeySize - $KeySize)); // Can't cache things for too long in case names are edited
}
$Matched = 0;
$Suggestions = array();
$Snatches = array();
2012-08-20 08:00:13 +00:00
$ArtistIDs = array();
foreach ($AutoSuggest as $Suggestion) {
list($ID,$Name, $Snatch) = $Suggestion;
if (stripos($Name,$FullName) === 0) {
$Suggestions[] = display_str($Name);
$Snatches[] = number_format($Snatch).' snatches';
2012-06-13 08:00:16 +00:00
$ArtistIDs[] = $ID;
if (++$Matched > 9) {
break;
}
}
}
2012-08-31 08:00:22 +00:00
echo json_encode(array(display_str($FullName),$Suggestions,$Snatches,$ArtistIDs));