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

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