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.

requests.class.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. #declare(strict_types=1);
  3. class Requests
  4. {
  5. /**
  6. * Update the sphinx requests delta table for a request.
  7. *
  8. * @param $RequestID
  9. */
  10. public static function update_sphinx_requests($RequestID)
  11. {
  12. $QueryID = G::$DB->get_query_id();
  13. G::$DB->query("
  14. SELECT REPLACE(t.Name, '.', '_')
  15. FROM tags AS t
  16. JOIN requests_tags AS rt ON t.ID = rt.TagID
  17. WHERE rt.RequestID = $RequestID");
  18. $TagList = G::$DB->collect(0, false);
  19. $TagList = db_string(implode(' ', $TagList));
  20. G::$DB->query("
  21. REPLACE INTO sphinx_requests_delta (
  22. ID, UserID, TimeAdded, LastVote, CategoryID, Title, TagList,
  23. CatalogueNumber, FillerID, TorrentID,
  24. TimeFilled, Visible, Votes, Bounty)
  25. SELECT
  26. ID, r.UserID, UNIX_TIMESTAMP(TimeAdded) AS TimeAdded,
  27. UNIX_TIMESTAMP(LastVote) AS LastVote, CategoryID, Title, '$TagList',
  28. CatalogueNumber, FillerID, TorrentID,
  29. UNIX_TIMESTAMP(TimeFilled) AS TimeFilled, Visible,
  30. COUNT(rv.UserID) AS Votes, SUM(rv.Bounty) >> 10 AS Bounty
  31. FROM requests AS r
  32. LEFT JOIN requests_votes AS rv ON rv.RequestID = r.ID
  33. WHERE ID = $RequestID
  34. GROUP BY r.ID");
  35. G::$DB->query("
  36. UPDATE sphinx_requests_delta
  37. SET ArtistList = (
  38. SELECT GROUP_CONCAT(ag.Name SEPARATOR ' ')
  39. FROM requests_artists AS ra
  40. JOIN artists_group AS ag ON ag.ArtistID = ra.ArtistID
  41. WHERE ra.RequestID = $RequestID
  42. GROUP BY NULL
  43. )
  44. WHERE ID = $RequestID");
  45. G::$DB->set_query_id($QueryID);
  46. G::$Cache->delete_value("request_$RequestID");
  47. }
  48. /**
  49. * Function to get data from an array of $RequestIDs. Order of keys doesn't matter (let's keep it that way).
  50. *
  51. * @param array $RequestIDs
  52. * @param boolean $Return if set to false, data won't be returned (ie. if we just want to prime the cache.)
  53. * @return The array of requests.
  54. * Format: array(RequestID => Associative array)
  55. * To see what's exactly inside each associate array, peek inside the function. It won't bite.
  56. */
  57. // In places where the output from this is merged with sphinx filters, it will be in a different order.
  58. public static function get_requests($RequestIDs, $Return = true)
  59. {
  60. $Found = $NotFound = array_fill_keys($RequestIDs, false);
  61. // Try to fetch the requests from the cache first.
  62. foreach ($RequestIDs as $i => $RequestID) {
  63. if (!is_number($RequestID)) {
  64. unset($RequestIDs[$i], $Found[$GroupID], $NotFound[$GroupID]);
  65. continue;
  66. }
  67. $Data = G::$Cache->get_value("request_$RequestID");
  68. if (!empty($Data)) {
  69. unset($NotFound[$RequestID]);
  70. $Found[$RequestID] = $Data;
  71. }
  72. }
  73. // Make sure there's something in $RequestIDs, otherwise the SQL will break
  74. if (count($RequestIDs) === 0) {
  75. return [];
  76. }
  77. $IDs = implode(',', array_keys($NotFound));
  78. /*
  79. * Don't change without ensuring you change everything else that uses get_requests()
  80. */
  81. if (count($NotFound) > 0) {
  82. $QueryID = G::$DB->get_query_id();
  83. G::$DB->query("
  84. SELECT
  85. ID,
  86. UserID,
  87. TimeAdded,
  88. LastVote,
  89. CategoryID,
  90. Title,
  91. Title2,
  92. TitleJP,
  93. Image,
  94. Description,
  95. CatalogueNumber,
  96. FillerID,
  97. TorrentID,
  98. TimeFilled,
  99. GroupID
  100. FROM requests
  101. WHERE ID IN ($IDs)
  102. ORDER BY ID");
  103. $Requests = G::$DB->to_array(false, MYSQLI_ASSOC, true);
  104. $Tags = self::get_tags(G::$DB->collect('ID', false));
  105. foreach ($Requests as $Request) {
  106. $Request['AnonymousFill'] = false;
  107. if ($Request['FillerID']) {
  108. G::$DB->query("
  109. SELECT Anonymous
  110. FROM torrents
  111. WHERE ID = ".$Request['TorrentID']);
  112. list($Anonymous) = G::$DB->next_record();
  113. if ($Anonymous) {
  114. $Request['AnonymousFill'] = true;
  115. }
  116. }
  117. unset($NotFound[$Request['ID']]);
  118. $Request['Tags'] = isset($Tags[$Request['ID']]) ? $Tags[$Request['ID']] : [];
  119. $Found[$Request['ID']] = $Request;
  120. G::$Cache->cache_value('request_'.$Request['ID'], $Request, 0);
  121. }
  122. G::$DB->set_query_id($QueryID);
  123. // Orphan requests. There shouldn't ever be any
  124. if (count($NotFound) > 0) {
  125. foreach (array_keys($NotFound) as $GroupID) {
  126. unset($Found[$GroupID]);
  127. }
  128. }
  129. }
  130. if ($Return) { // If we're interested in the data, and not just caching it
  131. return $Found;
  132. }
  133. }
  134. /**
  135. * Return a single request. Wrapper for get_requests
  136. *
  137. * @param int $RequestID
  138. * @return request array or false if request doesn't exist. See get_requests for a description of the format
  139. */
  140. public static function get_request($RequestID)
  141. {
  142. $Request = self::get_requests(array($RequestID));
  143. if (isset($Request[$RequestID])) {
  144. return $Request[$RequestID];
  145. }
  146. return false;
  147. }
  148. public static function get_artists($RequestID)
  149. {
  150. $Artists = G::$Cache->get_value("request_artists_$RequestID");
  151. if (is_array($Artists)) {
  152. $Results = $Artists;
  153. } else {
  154. $Results = [];
  155. $QueryID = G::$DB->get_query_id();
  156. G::$DB->query("
  157. SELECT
  158. ra.ArtistID,
  159. ag.Name
  160. FROM requests_artists AS ra
  161. JOIN artists_group AS ag ON ra.ArtistID = ag.ArtistID
  162. WHERE ra.RequestID = $RequestID
  163. ORDER BY ag.Name ASC;");
  164. $ArtistRaw = G::$DB->to_array();
  165. G::$DB->set_query_id($QueryID);
  166. foreach ($ArtistRaw as $ArtistRow) {
  167. list($ArtistID, $ArtistName) = $ArtistRow;
  168. $Results[] = array('id' => $ArtistID, 'name' => $ArtistName);
  169. }
  170. G::$Cache->cache_value("request_artists_$RequestID", $Results);
  171. }
  172. return $Results;
  173. }
  174. public static function get_tags($RequestIDs)
  175. {
  176. if (empty($RequestIDs)) {
  177. return [];
  178. }
  179. if (is_array($RequestIDs)) {
  180. $RequestIDs = implode(',', $RequestIDs);
  181. }
  182. $QueryID = G::$DB->get_query_id();
  183. G::$DB->query("
  184. SELECT
  185. rt.RequestID,
  186. rt.TagID,
  187. t.Name
  188. FROM requests_tags AS rt
  189. JOIN tags AS t ON rt.TagID = t.ID
  190. WHERE rt.RequestID IN ($RequestIDs)
  191. ORDER BY rt.TagID ASC");
  192. $Tags = G::$DB->to_array(false, MYSQLI_NUM, false);
  193. G::$DB->set_query_id($QueryID);
  194. $Results = [];
  195. foreach ($Tags as $TagsRow) {
  196. list($RequestID, $TagID, $TagName) = $TagsRow;
  197. $Results[$RequestID][$TagID] = $TagName;
  198. }
  199. return $Results;
  200. }
  201. public static function get_votes_array($RequestID)
  202. {
  203. $RequestVotes = G::$Cache->get_value("request_votes_$RequestID");
  204. if (!is_array($RequestVotes)) {
  205. $QueryID = G::$DB->get_query_id();
  206. G::$DB->query("
  207. SELECT
  208. rv.UserID,
  209. rv.Bounty,
  210. u.Username
  211. FROM requests_votes AS rv
  212. LEFT JOIN users_main AS u ON u.ID = rv.UserID
  213. WHERE rv.RequestID = $RequestID
  214. ORDER BY rv.Bounty DESC");
  215. if (!G::$DB->has_results()) {
  216. return array(
  217. 'TotalBounty' => 0,
  218. 'Voters' => []);
  219. }
  220. $Votes = G::$DB->to_array();
  221. $RequestVotes = [];
  222. $RequestVotes['TotalBounty'] = array_sum(G::$DB->collect('Bounty'));
  223. foreach ($Votes as $Vote) {
  224. list($UserID, $Bounty, $Username) = $Vote;
  225. $VoteArray = [];
  226. $VotesArray[] = array('UserID' => $UserID, 'Username' => $Username, 'Bounty' => $Bounty);
  227. }
  228. $RequestVotes['Voters'] = $VotesArray;
  229. G::$Cache->cache_value("request_votes_$RequestID", $RequestVotes);
  230. G::$DB->set_query_id($QueryID);
  231. }
  232. return $RequestVotes;
  233. }
  234. }