Oppaitime'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 7.6KB

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