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 8.4KB

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