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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. unset($NotFound[$Request['ID']]);
  105. $Request['Tags'] = isset($Tags[$Request['ID']]) ? $Tags[$Request['ID']] : [];
  106. $Found[$Request['ID']] = $Request;
  107. G::$Cache->cache_value('request_'.$Request['ID'], $Request, 0);
  108. }
  109. G::$DB->set_query_id($QueryID);
  110. // Orphan requests. There shouldn't ever be any
  111. if (count($NotFound) > 0) {
  112. foreach (array_keys($NotFound) as $GroupID) {
  113. unset($Found[$GroupID]);
  114. }
  115. }
  116. }
  117. if ($Return) { // If we're interested in the data, and not just caching it
  118. return $Found;
  119. }
  120. }
  121. /**
  122. * Return a single request. Wrapper for get_requests
  123. *
  124. * @param int $RequestID
  125. * @return request array or false if request doesn't exist. See get_requests for a description of the format
  126. */
  127. public static function get_request($RequestID) {
  128. $Request = self::get_requests(array($RequestID));
  129. if (isset($Request[$RequestID])) {
  130. return $Request[$RequestID];
  131. }
  132. return false;
  133. }
  134. public static function get_artists($RequestID) {
  135. $Artists = G::$Cache->get_value("request_artists_$RequestID");
  136. if (is_array($Artists)) {
  137. $Results = $Artists;
  138. } else {
  139. $Results = [];
  140. $QueryID = G::$DB->get_query_id();
  141. G::$DB->query("
  142. SELECT
  143. ra.ArtistID,
  144. ag.Name
  145. FROM requests_artists AS ra
  146. JOIN artists_group AS ag ON ra.ArtistID = ag.ArtistID
  147. WHERE ra.RequestID = $RequestID
  148. ORDER BY ag.Name ASC;");
  149. $ArtistRaw = G::$DB->to_array();
  150. G::$DB->set_query_id($QueryID);
  151. foreach ($ArtistRaw as $ArtistRow) {
  152. list($ArtistID, $ArtistName) = $ArtistRow;
  153. $Results[] = array('id' => $ArtistID, 'name' => $ArtistName);
  154. }
  155. G::$Cache->cache_value("request_artists_$RequestID", $Results);
  156. }
  157. return $Results;
  158. }
  159. public static function get_tags($RequestIDs) {
  160. if (empty($RequestIDs)) {
  161. return [];
  162. }
  163. if (is_array($RequestIDs)) {
  164. $RequestIDs = implode(',', $RequestIDs);
  165. }
  166. $QueryID = G::$DB->get_query_id();
  167. G::$DB->query("
  168. SELECT
  169. rt.RequestID,
  170. rt.TagID,
  171. t.Name
  172. FROM requests_tags AS rt
  173. JOIN tags AS t ON rt.TagID = t.ID
  174. WHERE rt.RequestID IN ($RequestIDs)
  175. ORDER BY rt.TagID ASC");
  176. $Tags = G::$DB->to_array(false, MYSQLI_NUM, false);
  177. G::$DB->set_query_id($QueryID);
  178. $Results = [];
  179. foreach ($Tags as $TagsRow) {
  180. list($RequestID, $TagID, $TagName) = $TagsRow;
  181. $Results[$RequestID][$TagID] = $TagName;
  182. }
  183. return $Results;
  184. }
  185. public static function get_votes_array($RequestID) {
  186. $RequestVotes = G::$Cache->get_value("request_votes_$RequestID");
  187. if (!is_array($RequestVotes)) {
  188. $QueryID = G::$DB->get_query_id();
  189. G::$DB->query("
  190. SELECT
  191. rv.UserID,
  192. rv.Bounty,
  193. u.Username
  194. FROM requests_votes AS rv
  195. LEFT JOIN users_main AS u ON u.ID = rv.UserID
  196. WHERE rv.RequestID = $RequestID
  197. ORDER BY rv.Bounty DESC");
  198. if (!G::$DB->has_results()) {
  199. return array(
  200. 'TotalBounty' => 0,
  201. 'Voters' => []);
  202. }
  203. $Votes = G::$DB->to_array();
  204. $RequestVotes = [];
  205. $RequestVotes['TotalBounty'] = array_sum(G::$DB->collect('Bounty'));
  206. foreach ($Votes as $Vote) {
  207. list($UserID, $Bounty, $Username) = $Vote;
  208. $VoteArray = [];
  209. $VotesArray[] = array('UserID' => $UserID, 'Username' => $Username, 'Bounty' => $Bounty);
  210. }
  211. $RequestVotes['Voters'] = $VotesArray;
  212. G::$Cache->cache_value("request_votes_$RequestID", $RequestVotes);
  213. G::$DB->set_query_id($QueryID);
  214. }
  215. return $RequestVotes;
  216. }
  217. }