table_home_feed.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: table_home_feed.php 28335 2012-02-28 04:37:47Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_home_feed extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'home_feed';
  15. $this->_pk = 'feedid';
  16. parent::__construct();
  17. }
  18. public function optimize_table() {
  19. return DB::query("OPTIMIZE TABLE %t", array($this->_table), true);
  20. }
  21. public function fetch($id, $idtype = '', $uid = '', $feedid = '') {
  22. $wherearr = array();
  23. if($feedid) {
  24. $wherearr[] = DB::field('feedid', $feedid);
  25. }
  26. if($id) {
  27. $wherearr[] = DB::field('id', $id);
  28. $wherearr[] = DB::field('idtype', $idtype);
  29. }
  30. if($uid) {
  31. $wherearr[] = DB::field('uid', $uid);
  32. }
  33. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  34. if(empty($wheresql)) {
  35. return null;
  36. }
  37. return DB::fetch_first('SELECT * FROM '.DB::table($this->_table).' '.$wheresql);
  38. }
  39. public function fetch_all_by_uid_dateline($uids, $findex = true, $start = 0, $limit = 5) {
  40. if(!($uids = dintval($uids, true))) {
  41. return null;
  42. }
  43. return DB::fetch_all('SELECT * FROM %t '.(($findex) ? 'USE INDEX(dateline)' : '').' WHERE uid IN (%n) ORDER BY dateline desc %i', array($this->_table, $uids, DB::limit($start, $limit)));
  44. }
  45. public function fetch_all_by_hot($hotstarttime) {
  46. return DB::fetch_all('SELECT * FROM %t USE INDEX(hot) WHERE dateline>=%d ORDER BY hot DESC LIMIT 0,10', array($this->_table, $hotstarttime));
  47. }
  48. public function update($id, $data, $idtype = '', $uid = '', $feedid = '') {
  49. $condition = array();
  50. if($feedid) {
  51. $condition[] = DB::field('feedid', $feedid);
  52. }
  53. if($id) {
  54. $condition[] = DB::field('id', $id);
  55. $condition[] = DB::field('idtype', $idtype);
  56. }
  57. if($uid) {
  58. $condition[] = DB::field('uid', $uid);
  59. }
  60. if(empty($data) || !is_array($data) || !count($condition)) {
  61. return null;
  62. }
  63. DB::update($this->_table, $data, implode(' AND ', $condition));
  64. }
  65. public function update_hot_by_id($id, $idtype, $uid, $inchot) {
  66. DB::query('UPDATE %t SET hot = hot+\'%d\' WHERE id = %d AND idtype = %s AND uid = %d', array($this->_table, $inchot, $id, $idtype, $uid));
  67. }
  68. public function update_hot_by_feedid($feedid, $inchot) {
  69. DB::query('UPDATE %t SET hot = hot+\'%d\' WHERE feedid = %d', array($this->_table, $inchot, $feedid));
  70. }
  71. public function delete_by_dateline($dateline, $hot = 0) {
  72. if(!is_numeric($dateline) || !is_numeric($hot)) {
  73. return false;
  74. }
  75. $condition = array();
  76. $condition[] = DB::field('dateline', $dateline, '<');
  77. $condition[] = DB::field('hot', $hot);
  78. DB::delete($this->_table, implode(' AND ', $condition));
  79. }
  80. public function delete_by_id_idtype($ids, $idtype) {
  81. if(!$ids || !$idtype) {
  82. return null;
  83. }
  84. $condition = array();
  85. $condition[] = DB::field('id', $ids);
  86. $condition[] = DB::field('idtype', $idtype);
  87. DB::delete($this->_table, implode(' AND ', $condition));
  88. }
  89. public function delete_by_uid_idtype($uid, $idtype) {
  90. if(!$uid || !$idtype) {
  91. return null;
  92. }
  93. $condition = array();
  94. $condition[] = DB::field('uid', $uid);
  95. $condition[] = DB::field('idtype', $idtype);
  96. DB::delete($this->_table, implode(' AND ', $condition));
  97. }
  98. public function delete_by_icon($icon) {
  99. if(!$icon) {
  100. return null;
  101. }
  102. DB::delete($this->_table, DB::field('icon', $icon));
  103. }
  104. public function delete($feedid, $uid = '') {
  105. $condition = array();
  106. if($feedid) {
  107. $condition[] = DB::field('feedid', $feedid);
  108. }
  109. if($uid) {
  110. $condition[] = DB::field('uid', $uid);
  111. }
  112. if(!count($condition)) {
  113. return null;
  114. }
  115. DB::delete($this->_table, implode(' AND ', $condition));
  116. }
  117. public function delete_by_uid($uids) {
  118. if(!$uids) {
  119. return null;
  120. }
  121. DB::delete($this->_table, DB::field('uid', $uids).' OR ('.DB::field('id', $uids).' AND idtype=\'uid\')');
  122. }
  123. public function fetch_uid_by_username($users) {
  124. if(!$users) {
  125. return null;
  126. }
  127. return DB::fetch_all('SELECT uid FROM %t WHERE username IN (%n)', array($this->_table, $users), 'uid');
  128. }
  129. public function fetch_icon_by_icon($icon) {
  130. return DB::fetch_first('SELECT icon FROM %t WHERE icon=%s', array($this->_table, $icon));
  131. }
  132. public function fetch_feedid_by_hashdata($uid, $hash_data) {
  133. return DB::fetch_first('SELECT feedid FROM %t WHERE uid=%d AND hash_data=%s LIMIT 0,1', array($this->_table, $uid, $hash_data));
  134. }
  135. public function fetch_feedid_by_feedid($feedid) {
  136. if(!$feedid) {
  137. return null;
  138. }
  139. return DB::fetch_all('SELECT feedid FROM %t WHERE feedid IN (%n)', array($this->_table, $feedid), 'feedid');
  140. }
  141. public function fetch_uid_by_uid($uid) {
  142. if(!$uid) {
  143. return null;
  144. }
  145. return DB::fetch_all('SELECT uid FROM %t WHERE uid IN (%n)', array($this->_table, $uid), 'uid');
  146. }
  147. public function fetch_all_by_search($fetchtype, $uids, $icon, $starttime, $endtime, $feedids, $hot1, $hot2, $start = 0, $limit = 0, $findex = '', $appid = '') {
  148. $parameter = array($this->_table);
  149. $wherearr = array();
  150. if(is_array($uids) && count($uids)) {
  151. $parameter[] = $uids;
  152. $wherearr[] = 'uid IN(%n)';
  153. }
  154. if($appid) {
  155. $parameter[] = $appid;
  156. $wherearr[] = 'appid=%d';
  157. }
  158. if($icon) {
  159. $parameter[] = $icon;
  160. $wherearr[] = 'icon=%s';
  161. }
  162. if($starttime) {
  163. $parameter[] = is_numeric($starttime) ? $starttime : strtotime($starttime);
  164. $wherearr[] = 'dateline>%d';
  165. }
  166. if($endtime) {
  167. $parameter[] = is_numeric($endtime) ? $endtime : strtotime($endtime);
  168. $wherearr[] = 'dateline<%d';
  169. }
  170. if(is_array($feedids) && count($feedids)) {
  171. $parameter[] = $feedids;
  172. $wherearr[] = 'feedid IN(%n)';
  173. }
  174. if($hot1) {
  175. $parameter[] = $hot1;
  176. $wherearr[] = 'hot>=%d';
  177. }
  178. if($hot2) {
  179. $parameter[] = $hot2;
  180. $wherearr[] = 'hot<=%d';
  181. }
  182. if($fetchtype == 3) {
  183. $selectfield = "count(*)";
  184. } elseif ($fetchtype == 2) {
  185. $selectfield = "feedid";
  186. } else {
  187. $selectfield = "*";
  188. $parameter[] = DB::limit($start, $limit);
  189. $ordersql = ' ORDER BY dateline DESC %i';
  190. }
  191. if($findex) {
  192. $findex = 'USE INDEX(dateline)';
  193. }
  194. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  195. if($fetchtype == 3) {
  196. return DB::result_first("SELECT $selectfield FROM %t $wheresql", $parameter);
  197. } else {
  198. return DB::fetch_all("SELECT $selectfield FROM %t {$findex} $wheresql $ordersql", $parameter);
  199. }
  200. }
  201. }
  202. ?>