Gazelle/classes/requests.class.php

237 lines
6.7 KiB
PHP
Raw Normal View History

2012-10-11 08:00:15 +00:00
<?
class Requests {
/**
* Update the sphinx requests delta table for a request.
*
* @param $RequestID
*/
public static function update_sphinx_requests($RequestID) {
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-06-06 08:01:03 +00:00
REPLACE INTO sphinx_requests_delta (
ID, UserID, TimeAdded, LastVote, CategoryID, Title,
2013-09-15 08:00:53 +00:00
Year, ReleaseType, CatalogueNumber, RecordLabel, BitrateList,
2013-06-06 08:01:03 +00:00
FormatList, MediaList, LogCue, FillerID, TorrentID,
TimeFilled, Visible, Votes, Bounty)
SELECT
ID, r.UserID, UNIX_TIMESTAMP(TimeAdded) AS TimeAdded,
2013-09-15 08:00:53 +00:00
UNIX_TIMESTAMP(LastVote) AS LastVote, CategoryID, Title,
Year, ReleaseType, CatalogueNumber, RecordLabel, BitrateList,
2013-06-06 08:01:03 +00:00
FormatList, MediaList, LogCue, FillerID, TorrentID,
UNIX_TIMESTAMP(TimeFilled) AS TimeFilled, Visible,
COUNT(rv.UserID) AS Votes, SUM(rv.Bounty) >> 10 AS Bounty
FROM requests AS r
2013-07-11 08:00:55 +00:00
LEFT JOIN requests_votes AS rv ON rv.RequestID = r.ID
2013-06-06 08:01:03 +00:00
WHERE ID = $RequestID
GROUP BY r.ID");
2013-08-28 23:08:41 +00:00
G::$DB->query("
2013-06-06 08:01:03 +00:00
UPDATE sphinx_requests_delta
SET ArtistList = (
SELECT GROUP_CONCAT(aa.Name SEPARATOR ' ')
FROM requests_artists AS ra
2013-07-11 08:00:55 +00:00
JOIN artists_alias AS aa ON aa.AliasID = ra.AliasID
2013-06-06 08:01:03 +00:00
WHERE ra.RequestID = $RequestID
GROUP BY NULL
)
WHERE ID = $RequestID");
2013-08-28 23:08:41 +00:00
G::$DB->set_query_id($QueryID);
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G::$Cache->delete_value("requests_$RequestID");
2012-10-11 08:00:15 +00:00
}
/**
2013-09-13 08:00:53 +00:00
* Function to get data from an array of $RequestIDs. Order of keys doesn't matter (let's keep it that way).
2012-10-11 08:00:15 +00:00
*
* @param array $RequestIDs
* @param boolean $Return if set to false, data won't be returned (ie. if we just want to prime the cache.)
* @return The array of requests.
* Format: array(RequestID => Associative array)
* To see what's exactly inside each associate array, peek inside the function. It won't bite.
*/
//
//In places where the output from this is merged with sphinx filters, it will be in a different order.
public static function get_requests($RequestIDs, $Return = true) {
2013-09-13 08:00:53 +00:00
$Found = $NotFound = array_fill_keys($RequestIDs, false);
2012-10-11 08:00:15 +00:00
// Try to fetch the requests from the cache first.
2013-10-04 08:00:56 +00:00
foreach ($RequestIDs as $i => $RequestID) {
if (!is_number($RequestID)) {
unset($RequestIDs[$i], $Found[$GroupID], $NotFound[$GroupID]);
continue;
}
2013-08-28 23:08:41 +00:00
$Data = G::$Cache->get_value("request_$RequestID");
2012-10-11 08:00:15 +00:00
if (!empty($Data)) {
unset($NotFound[$RequestID]);
$Found[$RequestID] = $Data;
}
}
2013-10-04 08:00:56 +00:00
// Make sure there's something in $RequestIDs, otherwise the SQL will break
if (count($RequestIDs) === 0) {
return array();
}
2013-09-13 08:00:53 +00:00
$IDs = implode(',', array_keys($NotFound));
2012-10-11 08:00:15 +00:00
/*
Don't change without ensuring you change everything else that uses get_requests()
*/
if (count($NotFound) > 0) {
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-06-06 08:01:03 +00:00
SELECT
2013-09-13 08:00:53 +00:00
ID,
UserID,
TimeAdded,
LastVote,
CategoryID,
Title,
Year,
Image,
Description,
CatalogueNumber,
RecordLabel,
ReleaseType,
BitrateList,
FormatList,
MediaList,
LogCue,
FillerID,
TorrentID,
TimeFilled,
GroupID,
OCLC
FROM requests
WHERE ID IN ($IDs)
2013-06-06 08:01:03 +00:00
ORDER BY ID");
2013-09-13 08:00:53 +00:00
$Requests = G::$DB->to_array(false, MYSQLI_ASSOC, true);
$Tags = self::get_tags(G::$DB->collect('ID', false));
2012-10-11 08:00:15 +00:00
foreach ($Requests as $Request) {
unset($NotFound[$Request['ID']]);
2013-09-13 08:00:53 +00:00
$Request['Tags'] = isset($Tags[$Request['ID']]) ? $Tags[$Request['ID']] : array();
2012-10-11 08:00:15 +00:00
$Found[$Request['ID']] = $Request;
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value('request_'.$Request['ID'], $Request, 0);
2012-10-11 08:00:15 +00:00
}
2013-09-13 08:00:53 +00:00
G::$DB->set_query_id($QueryID);
// Orphan requests. There shouldn't ever be any
if (count($NotFound) > 0) {
foreach (array_keys($NotFound) as $GroupID) {
unset($Found[$GroupID]);
}
}
2012-10-11 08:00:15 +00:00
}
if ($Return) { // If we're interested in the data, and not just caching it
2013-09-13 08:00:53 +00:00
return $Found;
2012-10-11 08:00:15 +00:00
}
}
2013-02-08 08:00:46 +00:00
2013-09-13 08:00:53 +00:00
/**
* Return a single request. Wrapper for get_requests
*
* @param int $RequestID
* @return request array or false if request doesn't exist. See get_requests for a description of the format
*/
public static function get_request($RequestID) {
$Request = self::get_requests(array($RequestID));
if (isset($Request[$RequestID])) {
return $Request[$RequestID];
}
return false;
}
2013-05-13 08:00:33 +00:00
public static function get_artists($RequestID) {
2013-08-28 23:08:41 +00:00
$Artists = G::$Cache->get_value("request_artists_$RequestID");
2013-05-13 08:00:33 +00:00
if (is_array($Artists)) {
$Results = $Artists;
} else {
$Results = array();
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-05-13 08:00:33 +00:00
SELECT
ra.ArtistID,
aa.Name,
ra.Importance
FROM requests_artists AS ra
JOIN artists_alias AS aa ON ra.AliasID = aa.AliasID
WHERE ra.RequestID = $RequestID
ORDER BY ra.Importance ASC, aa.Name ASC;");
2013-08-28 23:08:41 +00:00
$ArtistRaw = G::$DB->to_array();
G::$DB->set_query_id($QueryID);
2013-05-13 08:00:33 +00:00
foreach ($ArtistRaw as $ArtistRow) {
list($ArtistID, $ArtistName, $ArtistImportance) = $ArtistRow;
$Results[$ArtistImportance][] = array('id' => $ArtistID, 'name' => $ArtistName);
}
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("request_artists_$RequestID", $Results);
2013-05-13 08:00:33 +00:00
}
return $Results;
}
2013-09-13 08:00:53 +00:00
public static function get_tags($RequestIDs) {
if (empty($RequestIDs)) {
return array();
}
if (is_array($RequestIDs)) {
$RequestIDs = implode(',', $RequestIDs);
}
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-05-13 08:00:33 +00:00
SELECT
2013-09-13 08:00:53 +00:00
rt.RequestID,
2013-05-13 08:00:33 +00:00
rt.TagID,
t.Name
FROM requests_tags AS rt
2013-07-11 08:00:55 +00:00
JOIN tags AS t ON rt.TagID = t.ID
2013-09-13 08:00:53 +00:00
WHERE rt.RequestID IN ($RequestIDs)
2013-05-13 08:00:33 +00:00
ORDER BY rt.TagID ASC");
2013-09-13 08:00:53 +00:00
$Tags = G::$DB->to_array(false, MYSQLI_NUM, false);
2013-08-28 23:08:41 +00:00
G::$DB->set_query_id($QueryID);
2013-05-13 08:00:33 +00:00
$Results = array();
foreach ($Tags as $TagsRow) {
2013-09-13 08:00:53 +00:00
list($RequestID, $TagID, $TagName) = $TagsRow;
$Results[$RequestID][$TagID] = $TagName;
2013-05-13 08:00:33 +00:00
}
return $Results;
}
public static function get_votes_array($RequestID) {
2013-08-28 23:08:41 +00:00
$RequestVotes = G::$Cache->get_value("request_votes_$RequestID");
2013-05-13 08:00:33 +00:00
if (!is_array($RequestVotes)) {
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-05-13 08:00:33 +00:00
SELECT
rv.UserID,
rv.Bounty,
u.Username
2013-11-17 08:00:47 +00:00
FROM requests_votes AS rv
2013-07-11 08:00:55 +00:00
LEFT JOIN users_main AS u ON u.ID = rv.UserID
2013-05-13 08:00:33 +00:00
WHERE rv.RequestID = $RequestID
ORDER BY rv.Bounty DESC");
2013-08-28 23:08:41 +00:00
if (!G::$DB->has_results()) {
2013-09-13 08:00:53 +00:00
return array(
'TotalBounty' => 0,
'Voters' => array());
}
$Votes = G::$DB->to_array();
2013-05-13 08:00:33 +00:00
2013-09-13 08:00:53 +00:00
$RequestVotes = array();
$RequestVotes['TotalBounty'] = array_sum(G::$DB->collect('Bounty'));
2013-05-13 08:00:33 +00:00
2013-09-13 08:00:53 +00:00
foreach ($Votes as $Vote) {
list($UserID, $Bounty, $Username) = $Vote;
$VoteArray = array();
$VotesArray[] = array('UserID' => $UserID, 'Username' => $Username, 'Bounty' => $Bounty);
2013-05-13 08:00:33 +00:00
}
2013-09-13 08:00:53 +00:00
$RequestVotes['Voters'] = $VotesArray;
G::$Cache->cache_value("request_votes_$RequestID", $RequestVotes);
2013-08-28 23:08:41 +00:00
G::$DB->set_query_id($QueryID);
2013-05-13 08:00:33 +00:00
}
return $RequestVotes;
}
2012-10-11 08:00:15 +00:00
}
?>