forked from TOMMMMMMMMMC/GreatPosterWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.class.php
252 lines (234 loc) · 8.45 KB
/
requests.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?
class Requests {
/**
* Update the sphinx requests delta table for a request.
*
* @param $RequestID
*/
public static function update_sphinx_requests($RequestID) {
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT REPLACE(t.Name, '.', '_')
FROM tags AS t
JOIN requests_tags AS rt ON t.ID = rt.TagID
WHERE rt.RequestID = $RequestID");
$TagList = G::$DB->collect(0, false);
$TagList = db_string(implode(' ', $TagList));
G::$DB->query("
REPLACE INTO sphinx_requests_delta (
ID, UserID, TimeAdded, LastVote, CategoryID, Title, TagList,
Year, ReleaseType, CatalogueNumber, RecordLabel, BitrateList,
FormatList, MediaList, LogCue, FillerID, TorrentID,
TimeFilled, Visible, Votes, Bounty)
SELECT
ID, r.UserID, UNIX_TIMESTAMP(TimeAdded) AS TimeAdded,
UNIX_TIMESTAMP(LastVote) AS LastVote, CategoryID, Title, '$TagList',
Year, ReleaseType, CatalogueNumber, RecordLabel, BitrateList,
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
LEFT JOIN requests_votes AS rv ON rv.RequestID = r.ID
WHERE ID = $RequestID
GROUP BY r.ID");
G::$DB->query("
UPDATE sphinx_requests_delta
SET ArtistList = (
SELECT GROUP_CONCAT(aa.Name SEPARATOR ' ')
FROM requests_artists AS ra
JOIN artists_alias AS aa ON aa.AliasID = ra.AliasID
WHERE ra.RequestID = $RequestID
GROUP BY NULL
)
WHERE ID = $RequestID");
G::$DB->set_query_id($QueryID);
G::$Cache->delete_value("request_$RequestID");
}
/**
* Function to get data from an array of $RequestIDs. Order of keys doesn't matter (let's keep it that way).
*
* @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) {
$Found = $NotFound = array_fill_keys($RequestIDs, false);
// Try to fetch the requests from the cache first.
foreach ($RequestIDs as $i => $RequestID) {
if (!is_number($RequestID)) {
unset($RequestIDs[$i], $Found[$GroupID], $NotFound[$GroupID]);
continue;
}
$Data = G::$Cache->get_value("request_$RequestID");
if (!empty($Data)) {
unset($NotFound[$RequestID]);
$Found[$RequestID] = $Data;
}
}
// Make sure there's something in $RequestIDs, otherwise the SQL will break
if (count($RequestIDs) === 0) {
return array();
}
$IDs = implode(',', array_keys($NotFound));
/*
Don't change without ensuring you change everything else that uses get_requests()
*/
if (count($NotFound) > 0) {
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT
ID,
UserID,
TimeAdded,
LastVote,
CategoryID,
Title,
Title as Name,
Subtitle as SubName,
Subtitle,
Year,
Image,
Description,
ReleaseType,
CodecList,
ResolutionList,
ContainerList,
SourceList,
IMDBID,
SourceTorrent,
PurchasableAt,
FillerID,
TorrentID,
TimeFilled,
GroupID,
OCLC
FROM requests
WHERE ID IN ($IDs)
ORDER BY ID");
$Requests = G::$DB->to_array(false, MYSQLI_ASSOC, true);
$Tags = self::get_tags(G::$DB->collect('ID', false));
foreach ($Requests as $Request) {
unset($NotFound[$Request['ID']]);
$Request['Tags'] = isset($Tags[$Request['ID']]) ? $Tags[$Request['ID']] : array();
$Found[$Request['ID']] = $Request;
G::$Cache->cache_value('request_' . $Request['ID'], $Request, 0);
}
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]);
}
}
}
if ($Return) { // If we're interested in the data, and not just caching it
return $Found;
}
}
/**
* 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;
}
public static function get_artists($RequestID) {
$Artists = G::$Cache->get_value("request_artists_$RequestID");
if (is_array($Artists)) {
$Results = $Artists;
} else {
$Results = array();
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT
ra.ArtistID,
aa.Name,
ra.Importance,
wa.ChineseName,
wa.IMDBID
FROM requests_artists AS ra
JOIN artists_alias AS aa ON ra.AliasID = aa.AliasID
JOIN artists_group AS ag ON ag.ArtistID = ra.ArtistID
JOIN wiki_artists AS wa ON wa.RevisionID = ag.RevisionID
WHERE ra.RequestID = $RequestID
ORDER BY ra.Importance ASC, aa.Name ASC;");
$ArtistRaw = G::$DB->to_array();
G::$DB->set_query_id($QueryID);
foreach ($ArtistRaw as $ArtistRow) {
list($ArtistID, $ArtistName, $ArtistImportance, $ArtistChineseName, $ArtistIMDBID) = $ArtistRow;
$Results[$ArtistImportance][] = array('id' => $ArtistID, 'name' => $ArtistName, 'imdbid' => $ArtistIMDBID, 'cname' => $ArtistChineseName);
}
G::$Cache->cache_value("request_artists_$RequestID", $Results);
}
return $Results;
}
public static function get_tags($RequestIDs) {
if (empty($RequestIDs)) {
return array();
}
if (is_array($RequestIDs)) {
$RequestIDs = implode(',', $RequestIDs);
}
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT
rt.RequestID,
rt.TagID,
t.Name
FROM requests_tags AS rt
JOIN tags AS t ON rt.TagID = t.ID
WHERE rt.RequestID IN ($RequestIDs)
ORDER BY rt.TagID ASC");
$Tags = G::$DB->to_array(false, MYSQLI_NUM, false);
G::$DB->set_query_id($QueryID);
$Results = array();
foreach ($Tags as $TagsRow) {
list($RequestID, $TagID, $TagName) = $TagsRow;
$Results[$RequestID][$TagID] = $TagName;
}
return $Results;
}
public static function get_votes_array($RequestID) {
$RequestVotes = G::$Cache->get_value("request_votes_$RequestID");
if (!is_array($RequestVotes)) {
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT
rv.UserID,
rv.Bounty,
u.Username
FROM requests_votes AS rv
LEFT JOIN users_main AS u ON u.ID = rv.UserID
WHERE rv.RequestID = $RequestID
ORDER BY rv.Bounty DESC");
if (!G::$DB->has_results()) {
return array(
'TotalBounty' => 0,
'Voters' => array()
);
}
$Votes = G::$DB->to_array();
$RequestVotes = array();
$RequestVotes['TotalBounty'] = array_sum(G::$DB->collect('Bounty'));
foreach ($Votes as $Vote) {
list($UserID, $Bounty, $Username) = $Vote;
$VoteArray = array();
$VotesArray[] = array('UserID' => $UserID, 'Username' => $Username, 'Bounty' => $Bounty);
}
$RequestVotes['Voters'] = $VotesArray;
G::$Cache->cache_value("request_votes_$RequestID", $RequestVotes);
G::$DB->set_query_id($QueryID);
}
return $RequestVotes;
}
}