2011-06-30 08:00:06 +00:00
< ?
2013-08-13 08:00:43 +00:00
if ( empty ( $_GET [ 'query' ])) {
error ( 0 );
}
2013-07-01 08:01:00 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2011-06-30 08:00:06 +00:00
2013-07-01 08:01:00 +00:00
$FullName = rawurldecode ( $_GET [ 'query' ]);
2011-07-13 08:00:06 +00:00
2011-06-30 08:00:06 +00:00
$MaxKeySize = 4 ;
2011-07-13 08:00:06 +00:00
if ( strtolower ( substr ( $FullName , 0 , 4 )) == 'the ' ) {
2011-06-30 08:00:06 +00:00
$MaxKeySize += 4 ;
}
2011-07-13 08:00:06 +00:00
$KeySize = min ( $MaxKeySize , max ( 1 , strlen ( $FullName )));
2011-06-30 08:00:06 +00:00
2011-07-13 08:00:06 +00:00
$Letters = strtolower ( substr ( $FullName , 0 , $KeySize ));
2011-06-30 08:00:06 +00:00
$AutoSuggest = $Cache -> get ( 'autocomplete_artist_' . $KeySize . '_' . $Letters );
2013-07-01 08:01:00 +00:00
if ( ! $AutoSuggest ) {
2013-05-05 08:00:31 +00:00
$Limit = (( $KeySize === $MaxKeySize ) ? 250 : 10 );
$DB -> query ( "
SELECT
a . ArtistID ,
2013-07-01 08:01:00 +00:00
a . Name
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
2013-07-01 08:01:00 +00:00
ORDER BY t . Snatched DESC
2011-06-30 08:00:06 +00:00
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
2011-06-30 08:00:06 +00:00
}
$Matched = 0 ;
2012-08-20 08:00:13 +00:00
$ArtistIDs = array ();
2013-08-13 08:00:43 +00:00
$Response = array (
'query' => $FullName ,
'suggestions' => array ()
);
2011-06-30 08:00:06 +00:00
foreach ( $AutoSuggest as $Suggestion ) {
2013-07-01 08:01:00 +00:00
list ( $ID , $Name ) = $Suggestion ;
if ( stripos ( $Name , $FullName ) === 0 ) {
2013-07-02 08:01:37 +00:00
$Response [ 'suggestions' ][] = array ( 'value' => $Name , 'data' => $ID );
2011-06-30 08:00:06 +00:00
if ( ++ $Matched > 9 ) {
break ;
}
}
}
2013-07-01 08:01:00 +00:00
echo json_encode ( $Response );