PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/source/class/table/table_forum_thread.php

https://github.com/kuaileshike/upload
PHP | 1229 lines | 1149 code | 74 blank | 6 comment | 274 complexity | b5e3edaa11124662817092d58fd277e3 MD5 | raw file
  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_forum_thread.php 32004 2012-10-30 09:02:57Z zhengqingpeng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_forum_thread extends discuz_table
  12. {
  13. private $_posttableid = array();
  14. private $_urlparam = array();
  15. public function __construct() {
  16. $this->_table = 'forum_thread';
  17. $this->_pk = 'tid';
  18. $this->_pre_cache_key = 'forum_thread_';
  19. parent::__construct();
  20. }
  21. public function fetch($tid, $tableid = 0) {
  22. $tid = intval($tid);
  23. $data = array();
  24. if($tid && ($data = $this->fetch_cache($tid)) === false) {
  25. $parameter = array($this->get_table_name($tableid), $tid);
  26. $data = DB::fetch_first("SELECT * FROM %t WHERE tid=%d", $parameter);
  27. if(!empty($data)) $this->store_cache($tid, $data, $this->_cache_ttl);
  28. }
  29. return $data;
  30. }
  31. public function fetch_by_tid_displayorder($tid, $displayorder = null, $glue = '>=', $authorid = null, $tableid = 0) {
  32. $data = $this->fetch($tid, $tableid);
  33. if(!empty($data)) {
  34. if(($displayorder !== null && !($this->compare_number($data['displayorder'], $displayorder, $glue))) || ($authorid !== null && $data['authorid'] != $authorid)) {
  35. $data = array();
  36. }
  37. }
  38. return $data;
  39. }
  40. public function fetch_by_fid_displayorder($fid, $displayorder = 0, $glue = '>=', $order = 'lastpost', $sort = 'DESC') {
  41. $fid = intval($fid);
  42. if(!empty($fid)) {
  43. $parameter = array($this->get_table_name(), $fid, $displayorder);
  44. $glue = helper_util::check_glue($glue);
  45. $ordersql = !empty($order) ? ' ORDER BY '.DB::order($order, $sort) : '';
  46. return DB::fetch_first("SELECT * FROM %t WHERE fid=%d AND displayorder{$glue}%d $ordersql ".DB::limit(0, 1), $parameter);
  47. }
  48. return array();
  49. }
  50. public function fetch_next_tid_by_fid_lastpost($fid, $lastpost, $glue = '>', $sort = 'DESC', $tableid = 0) {
  51. $glue = helper_util::check_glue($glue);
  52. return DB::result_first("SELECT tid FROM %t WHERE fid=%d AND displayorder>=0 AND closed=0 AND lastpost{$glue}%d ORDER BY ".DB::order('lastpost', $sort).DB::limit(1), array($this->get_table_name($tableid), $fid, $lastpost));
  53. }
  54. public function fetch_by_tid_fid_displayorder($tid, $fid, $displayorder = null, $tableid = 0, $glue = '>=') {
  55. if($tid) {
  56. $data = $this->fetch($tid, $tableid);
  57. if(!empty($data)) {
  58. if(($data['fid'] != $fid) || ($displayorder !== null && !($this->compare_number($data['displayorder'], $displayorder, $glue)))) {
  59. $data = array();
  60. }
  61. }
  62. return $data;
  63. }
  64. return array();
  65. }
  66. public function fetch_thread_table_ids() {
  67. $threadtableids = array('0' => 0);
  68. $db = DB::object();
  69. $query = $db->query("SHOW TABLES LIKE '".str_replace('_', '\_', DB::table('forum_thread').'_%')."'");
  70. while($table = $db->fetch_array($query, MYSQL_NUM)) {
  71. $tablename = $table[0];
  72. $tableid = intval(substr($tablename, strrpos($tablename, '_') + 1));
  73. if(empty($tableid)) {
  74. continue;
  75. }
  76. $threadtableids[$tableid] = $tableid;
  77. }
  78. return $threadtableids;
  79. }
  80. public function fetch_all_by_digest_displayorder($digest, $digestglue = '=', $displayorder = 0, $glue = '>=', $start = 0, $limit = 0, $tableid = 0) {
  81. $parameter = array($this->get_table_name($tableid), $digest, $displayorder);
  82. $digestglue = helper_util::check_glue($digestglue);
  83. $glue = helper_util::check_glue($glue);
  84. return DB::fetch_all("SELECT * FROM %t WHERE digest{$digestglue}%d AND displayorder{$glue}%d".DB::limit($start, $limit), $parameter, $this->_pk);
  85. }
  86. public function fetch_all_by_fid_typeid_displayorder($fid, $typeid = null, $displayorder = null, $glue = '=', $start = 0, $limit = 0) {
  87. $parameter = array($this->get_table_name(), $fid);
  88. $wherearr = array();
  89. $wherearr[] = is_array($fid) ? 'fid IN(%n)' : 'fid=%d';
  90. if($typeid) {
  91. $parameter[] = $typeid;
  92. $wherearr[] = "typeid=%d";
  93. }
  94. if($displayorder !== null) {
  95. $parameter[] = $displayorder;
  96. $glue = helper_util::check_glue($glue);
  97. $wherearr[] = "displayorder{$glue}%d";
  98. }
  99. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  100. return DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY lastpost DESC ".DB::limit($start, $limit), $parameter, $this->_pk);
  101. }
  102. public function fetch_all_by_fid_lastpost($fid, $lstart = 0, $lend = 0, $tableid = 0) {
  103. $parameter = array($this->get_table_name($tableid), $fid);
  104. $wherearr = array();
  105. $wherearr[] = is_array($fid) ? 'fid IN(%n)' : 'fid=%d';
  106. $wherearr[] = 'displayorder=0';
  107. if($lstart) {
  108. $wherearr[] = 'lastpost>%d';
  109. $parameter[] = $lstart;
  110. }
  111. if($lend) {
  112. $wherearr[] = 'lastpost<%d';
  113. $parameter[] = $lend;
  114. }
  115. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  116. return DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY lastpost DESC ".DB::limit(0, 100), $parameter, $this->_pk);
  117. }
  118. public function fetch_all_by_authorid_displayorder($authorid, $displayorder = null, $dglue = '=', $closed = null, $subject = '', $start = 0, $limit = 0, $replies = null, $fid = null, $rglue = '>=', $tableid = 0) {
  119. $parameter = array($this->get_table_name($tableid));
  120. $wherearr = array();
  121. if(!empty($authorid)) {
  122. $authorid = dintval($authorid, true);
  123. $parameter[] = $authorid;
  124. $wherearr[] = is_array($authorid) && $authorid ? 'authorid IN(%n)' : 'authorid=%d';
  125. }
  126. if($fid !== null) {
  127. $fid = dintval($fid, true);
  128. $parameter[] = $fid;
  129. $wherearr[] = is_array($fid) && $fid ? 'fid IN(%n)' : 'fid=%d';
  130. }
  131. if(getglobal('setting/followforumid')) {
  132. $parameter[] = getglobal('setting/followforumid');
  133. $wherearr[] = 'fid<>%d';
  134. }
  135. if($displayorder !== null) {
  136. $parameter[] = $displayorder;
  137. $dglue = helper_util::check_glue($dglue);
  138. $wherearr[] = "displayorder{$dglue}%d";
  139. }
  140. if($closed !== null) {
  141. $parameter[] = $closed;
  142. $wherearr[] = "closed=%d";
  143. }
  144. if($replies !== null) {
  145. $parameter[] = $replies;
  146. $rglue = helper_util::check_glue($rglue);
  147. $wherearr[] = "replies{$rglue}%d";
  148. }
  149. if(!empty($subject)) {
  150. $parameter[] = '%'.$subject.'%';
  151. $wherearr[] = "subject LIKE %s";
  152. }
  153. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  154. return DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY dateline DESC ".DB::limit($start, $limit), $parameter, $this->_pk);
  155. }
  156. public function fetch_all_by_tid($tids, $start = 0, $limit = 0, $tableid = 0) {
  157. $data = array();
  158. if(($data = $this->fetch_cache($tids)) === false || count($tids) != count($data)) {
  159. if(is_array($data) && !empty($data)) {
  160. $tids = array_diff($tids, array_keys($data));
  161. }
  162. if($data === false) $data = array();
  163. if(!empty($tids)) {
  164. $parameter = array($this->get_table_name($tableid), $tids);
  165. $query = DB::query("SELECT * FROM %t WHERE tid IN(%n)".DB::limit($start, $limit), $parameter);
  166. while($value = DB::fetch($query)) {
  167. $data[$value['tid']] = $value;
  168. $this->store_cache($value['tid'], $value, $this->_cache_ttl);
  169. }
  170. }
  171. }
  172. return $data;
  173. }
  174. public function fetch_all_by_tid_displayorder($tids, $displayorder = null, $glue = '>=', $fids = array(), $closed = null) {
  175. $data = array();
  176. if(!empty($tids)) {
  177. $data = $this->fetch_all_by_tid((array)$tids);
  178. $fids = $fids && !is_array($fids) ? array($fids) : $fids;
  179. foreach($data as $tid => $value) {
  180. if($displayorder !== null && !(helper_util::compute($value['displayorder'], $displayorder, $glue))) {
  181. unset($data[$tid]);
  182. } elseif(!empty($fids) && !in_array($value['fid'], $fids)) {
  183. unset($data[$tid]);
  184. } elseif($closed !== null && $value['closed'] != $closed) {
  185. unset($data[$tid]);
  186. }
  187. }
  188. }
  189. return $data;
  190. }
  191. public function fetch_all_by_tid_fid_displayorder($tids, $fids = null, $displayorder = null, $order = 'dateline', $start = 0, $limit = 0, $glue = '>=', $sort = 'DESC', $tableid = 0) {
  192. $parameter = array($this->get_table_name($tableid));
  193. $wherearr = array();
  194. if(!empty($tids)) {
  195. $tids = dintval($tids, true);
  196. $parameter[] = $tids;
  197. $wherearr[] = is_array($tids) && $tids ? 'tid IN(%n)' : 'tid=%d';
  198. }
  199. if(!empty($fids)) {
  200. $fids = dintval($fids, true);
  201. $parameter[] = $fids;
  202. $wherearr[] = is_array($fids) && $fids ? 'fid IN(%n)' : 'fid=%d';
  203. }
  204. if($displayorder !== null) {
  205. $parameter[] = $displayorder;
  206. $glue = helper_util::check_glue($glue);
  207. $wherearr[] = "displayorder{$glue}%d";
  208. }
  209. if($order) {
  210. $order = 'ORDER BY '.DB::order($order, $sort);
  211. }
  212. if(!empty($wherearr)) {
  213. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  214. return DB::fetch_all("SELECT * FROM %t $wheresql $order ".DB::limit($start, $limit), $parameter, $this->_pk);
  215. } else {
  216. return array();
  217. }
  218. }
  219. public function fetch_all_by_tid_or_fid($fid, $tids = array()) {
  220. $parameter = array($this->get_table_name(), $fid);
  221. $forumstickytids = '';
  222. if(!empty($tids)) {
  223. $tids = dintval($tids, true);
  224. $parameter[] = $tids;
  225. $forumstickytids = ' OR '.(is_array($tids) && $tids ? 'tid IN(%n)' : 'tid=%d');
  226. }
  227. return DB::fetch_all("SELECT * FROM %t WHERE fid=%d AND displayorder=1 $forumstickytids ORDER BY lastpost DESC", $parameter);
  228. }
  229. public function fetch_all_by_displayorder($displayorder = 0, $glue = '>=', $start = 0, $limit = 0, $tableid = 0) {
  230. $glue = helper_util::check_glue($glue);
  231. $displayorder = dintval($displayorder);
  232. return DB::fetch_all('SELECT * FROM %t WHERE %i '.DB::limit($start, $limit), array($this->get_table_name($tableid), DB::field('displayorder', $displayorder, $glue)));
  233. }
  234. public function fetch_all_by_authorid($authorid, $start = 0, $limit = 0, $tableid = 0) {
  235. $authorid = dintval($authorid, true);
  236. return DB::fetch_all("SELECT * FROM %t WHERE %i ORDER BY dateline DESC ".DB::limit($start, $limit), array($this->get_table_name($tableid), DB::field('authorid', $authorid)), $this->_pk);
  237. }
  238. public function fetch_all_by_dateline($starttime, $start = 0, $limit = 0, $order = 'dateline', $sort = 'DESC') {
  239. if($starttime) {
  240. $orderby = '';
  241. if(!empty($order)) {
  242. $orderby = "ORDER BY ".DB::order($order, $sort);
  243. }
  244. $parameter = array($this->get_table_name(), $starttime);
  245. return DB::fetch_all("SELECT * FROM %t WHERE dateline>=%d AND displayorder>'-1' $orderby ".DB::limit($start, $limit), $parameter, $this->_pk);
  246. }
  247. return array();
  248. }
  249. public function fetch_all_by_fid_displayorder($fids, $displayorder = null, $dateline = null, $recommends = null, $start = 0, $limit = 0, $order = 'dateline', $sort = 'DESC', $dglue = '>=') {
  250. $parameter = array($this->get_table_name());
  251. $wherearr = array();
  252. $fids = dintval($fids, true);
  253. $parameter[] = $fids;
  254. $wherearr[] = is_array($fids) && $fids ? 'fid IN(%n)' : 'fid=%d';
  255. if($displayorder !== null) {
  256. $parameter[] = $displayorder;
  257. $dglue = helper_util::check_glue($dglue);
  258. $wherearr[] = "displayorder{$dglue}%d";
  259. }
  260. if($dateline !== null) {
  261. $parameter[] = $dateline;
  262. $wherearr[] = "dateline>=%d";
  263. }
  264. if($recommends !== null) {
  265. $parameter[] = $recommends;
  266. $wherearr[] = "recommends>%d";
  267. }
  268. $ordersql = !empty($order) ? ' ORDER BY '.DB::order($order, $sort) : '';
  269. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  270. return DB::fetch_all("SELECT * FROM %t $wheresql $ordersql ".DB::limit($start, $limit), $parameter, $this->_pk);
  271. }
  272. public function fetch_all_new_thread_by_tid($tid = 0, $start = 0, $limit = 0, $tableid = 0, $glue = '>', $sort = 'ASC') {
  273. $glue = helper_util::check_glue($glue);
  274. return DB::fetch_all("SELECT * FROM %t WHERE tid{$glue}%d ORDER BY ".DB::order('tid', $sort).DB::limit($start, $limit), array($this->get_table_name($tableid), $tid), $this->_pk);
  275. }
  276. public function fetch_all_group_thread_by_fid_displayorder($fids, $displayorder = null, $dateline = null, $lastpost = null, $digest = null, $order = 'dateline', $start = 0, $limit = 0, $dglue = '>=') {
  277. $fids = dintval($fids, true);
  278. $parameter = array($this->get_table_name(), $fids);
  279. $wherearr = array();
  280. $wherearr[] = is_array($fids) && $fids ? 'fid IN(%n)' : 'fid=%d';
  281. if($displayorder !== null) {
  282. $parameter[] = $displayorder;
  283. $dglue = helper_util::check_glue($dglue);
  284. $wherearr[] = "displayorder{$dglue}%d";
  285. }
  286. if($dateline !== null) {
  287. $parameter[] = $dateline;
  288. $wherearr[] = "dateline>=%d";
  289. }
  290. if($lastpost !== null) {
  291. $parameter[] = $lastpost;
  292. $wherearr[] = "lastpost>=%d";
  293. }
  294. if($digest !== null) {
  295. $parameter[] = $digest;
  296. $wherearr[] = "$digest>%d";
  297. }
  298. $ordersql = !empty($order) ? 'ORDER BY'.DB::order($order, 'DESC') : '';
  299. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  300. return DB::fetch_all("SELECT * FROM %t $wheresql $ordersql ".DB::limit($start, $limit), $parameter, $this->_pk);
  301. }
  302. public function fetch_all_by_fid_authorid_displayorder($fids, $authorid, $displayorder = null, $lastpost = 0, $start = 0, $limit = 0) {
  303. $parameter = array($this->get_table_name());
  304. $wherearr = array();
  305. if($authorid) {
  306. $authorid = dintval($authorid, true);
  307. $parameter[] = $authorid;
  308. $wherearr[] = is_array($authorid) ? 'authorid IN(%n)' : 'authorid=%d';
  309. }
  310. $fids = dintval($fids, true);
  311. $parameter[] = $fids;
  312. $wherearr[] = is_array($fids) ? 'fid IN(%n)' : 'fid=%d';
  313. if($displayorder !== null) {
  314. $parameter[] = $displayorder;
  315. $wherearr[] = "displayorder=%d";
  316. }
  317. if($lastpost) {
  318. $parameter[] = $lastpost;
  319. $wherearr[] = "lastpost>%d";
  320. }
  321. $wheresql = ' WHERE '.implode(' AND ', $wherearr);
  322. return DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY lastpost DESC ".DB::limit($start, $limit), $parameter, $this->_pk);
  323. }
  324. public function fetch_all_by_tid_fid($tids, $fids = array(), $isgroup = -1, $author = '', $subject = '', $start = 0, $limit = 0) {
  325. $data = array();
  326. $condition = $this->make_query_condition($tids, $fids, $isgroup, $author, $subject);
  327. $query = DB::query("SELECT * FROM %t $condition[0]".DB::limit($start, $limit), $condition[1]);
  328. while($value = DB::fetch($query)) {
  329. $data[$value['tid']] = $value;
  330. $this->_posttableid[$value['posttableid']][] = $value['tid'];
  331. }
  332. return $data;
  333. }
  334. public function fetch_all_by_fid($fids, $start = 0, $limit = 0, $tableid = 0) {
  335. $fids = dintval($fids, true);
  336. if($fids) {
  337. return DB::fetch_all("SELECT * FROM %t WHERE fid IN(%n) ".DB::limit($start, $limit), array($this->get_table_name($tableid), (array)$fids));
  338. }
  339. return array();
  340. }
  341. public function fetch_all_by_replies($number, $start = 0, $limit = 0, $glue = '>', $tableid = 0) {
  342. $number = dintval($number);
  343. if($number) {
  344. $glue = helper_util::check_glue($glue);
  345. return DB::fetch_all("SELECT * FROM %t WHERE replies{$glue}%d ".DB::limit($start, $limit), array($this->get_table_name($tableid), $number));
  346. }
  347. return array();
  348. }
  349. public function fetch_all_rank_thread($dateline, $notfid, $order = 'dateline', $start = 0, $limit = 0) {
  350. $parameter = array($this->get_table_name());
  351. $data = $fids = $wherearr = array();
  352. if($dateline) {
  353. $parameter[] = $dateline;
  354. $wherearr[] = 'dateline>%d';
  355. }
  356. $wherearr[] = 'displayorder>=0';
  357. if($notfid) {
  358. $parameter[] = $notfid;
  359. $wherearr[] = 'fid NOT IN(%n)';
  360. }
  361. $wheresql = ' WHERE '.implode(' AND ', $wherearr);
  362. $ordersql = !empty($order) ? ' ORDER BY '.DB::order($order, 'DESC') : '';
  363. $query = DB::query("SELECT tid, fid, author, authorid, subject, dateline, views, replies, favtimes, sharetimes, heats FROM %t $wheresql $ordersql ".DB::limit($start, $limit), $parameter);
  364. while($value = DB::fetch($query)) {
  365. $data[$value['tid']] = $value;
  366. $fids[$value['fid']][$value['tid']] = $value['tid'];
  367. }
  368. if(!empty($fids)) {
  369. foreach(C::t('forum_forum')->fetch_all_name_by_fid(array_keys($fids)) as $value) {
  370. foreach($fids[$value['fid']] as $tid) {
  371. $data[$tid]['forum'] = $value['name'];
  372. }
  373. }
  374. }
  375. return $data;
  376. }
  377. public function fetch_all_rank_poll($dateline, $notfid, $order = 'dateline', $start = 0, $limit = 0) {
  378. $parameter = array($this->get_table_name(), 'forum_poll');
  379. $wherearr = array('t.special=1');
  380. if($dateline) {
  381. $parameter[] = $dateline;
  382. $wherearr[] = 't.dateline>%d';
  383. }
  384. $wherearr[] = 't.displayorder>=0';
  385. if($notfid) {
  386. $parameter[] = $notfid;
  387. $wherearr[] = 't.fid NOT IN(%n)';
  388. }
  389. $wheresql = ' WHERE '.implode(' AND ', $wherearr);
  390. $ordersql = !empty($order) ? ' ORDER BY '.DB::order($order, 'DESC') : '';
  391. return DB::fetch_all("SELECT t.tid, t.fid, t.author, t.authorid, t.subject, t.dateline, t.favtimes, t.sharetimes, t.heats, p.pollpreview, p.voters FROM %t t LEFT JOIN %t p ON p.tid=t.tid $wheresql $ordersql ".DB::limit($start, $limit), $parameter, $this->_pk);
  392. }
  393. public function fetch_all_rank_activity($dateline, $notfid, $order = 'dateline', $start = 0, $limit = 0) {
  394. $parameter = array($this->get_table_name(), 'forum_activity');
  395. $wherearr = array('t.special=4', 't.isgroup=0', 't.closed=0');
  396. if($dateline) {
  397. $parameter[] = $dateline;
  398. $wherearr[] = 't.dateline>%d';
  399. }
  400. $wherearr[] = 't.displayorder>=0';
  401. if($notfid) {
  402. $parameter[] = $notfid;
  403. $wherearr[] = 't.fid NOT IN(%n)';
  404. }
  405. $wheresql = ' WHERE '.implode(' AND ', $wherearr);
  406. $ordersql = !empty($order) ? ' ORDER BY '.DB::order($order, 'DESC') : '';
  407. return DB::fetch_all("SELECT t.tid, t.subject, t.views, t.author, t.authorid, t.replies, t.heats, t.sharetimes, t.favtimes, act.aid, act.starttimefrom, act.starttimeto, act.place, act.class, act.applynumber, act.expiration FROM %t t LEFT JOIN %t act ON act.tid=t.tid $wheresql $ordersql ".DB::limit($start, $limit), $parameter, $this->_pk);
  408. }
  409. public function fetch_all_by_recyclebine($fid = 0, $isgroup = 0, $author = array(), $username = array(), $pstarttime = 0, $pendtime = 0, $mstarttime = 0, $mendtime = 0, $keywords = '', $start = 0, $limit = 0) {
  410. $sql = $this->recyclebine_where($fid, $isgroup, $author, $username, $pstarttime, $pendtime, $mstarttime, $mendtime, $keywords);
  411. return DB::fetch_all('SELECT f.name AS forumname, f.allowsmilies, f.allowhtml, f.allowbbcode, f.allowimgcode,
  412. t.tid, t.fid, t.authorid, t.author, t.subject, t.views, t.replies, t.dateline, t.posttableid,
  413. tm.uid AS moduid, tm.username AS modusername, tm.dateline AS moddateline, tm.action AS modaction, tm.reason
  414. FROM '.DB::table('forum_thread').' t LEFT JOIN '.DB::table('forum_threadmod').' tm ON tm.tid=t.tid
  415. LEFT JOIN '.DB::table('forum_forum').' f ON f.fid=t.fid '.$sql[0].' ORDER BY t.dateline DESC '.DB::limit($start, $limit), $sql[1]);
  416. }
  417. public function fetch_all_moderate($fid = 0, $displayorder = null, $isgroup = null, $dateline = null, $author = null, $subject = null) {
  418. $parameter = $this->make_query_condition(null, $fid, $isgroup, $author, $subject, $displayorder, $dateline);
  419. return DB::fetch_all('SELECT * FROM %t '.$parameter[0], $parameter[1], $this->_pk);
  420. }
  421. public function fetch_all_movedthread($start = 0, $limit = 0) {
  422. return DB::fetch_all('SELECT t1.tid, t2.tid AS threadexists, f.status, t1.isgroup FROM %t t1
  423. LEFT JOIN %t t2 ON t2.tid=t1.closed AND t2.displayorder>=0 LEFT JOIN %t f ON f.fid=t1.fid
  424. WHERE t1.closed>1'.DB::limit($start, $limit), array($this->get_table_name(), $this->get_table_name(), 'forum_forum'));
  425. }
  426. public function fetch_all_by_fid_cover_lastpost($fid, $cover = null, $starttime = 0, $endtime = 0, $start = 0, $limit = 0) {
  427. $parameter = array($this->get_table_name(), $fid);
  428. $wherearr = array('fid=%d', 'displayorder>=0');
  429. if($cover !== null) {
  430. $wherearr[] = 'cover=%d';
  431. $parameter[] = $cover;
  432. }
  433. if($starttime) {
  434. $wherearr[] = 'lastpost>%d';
  435. $parameter[] = $starttime;
  436. }
  437. if($endtime) {
  438. $wherearr[] = 'lastpost<%d';
  439. $parameter[] = $endtime;
  440. }
  441. $wheresql = ' WHERE '.implode(' AND ', $wherearr);
  442. return DB::fetch_all('SELECT * FROM %t '.$wheresql.DB::limit($start, $limit), $parameter, $this->_pk);
  443. }
  444. public function fetch_all_by_posttableid_displayorder($tableid = 0, $posttableid = 0, $displayorder = 0) {
  445. return DB::fetch_all('SELECT * FROM %t WHERE posttableid=%d AND displayorder>=%d ORDER BY lastpost'.DB::limit(1000), array($this->get_table_name($tableid), $posttableid, $displayorder), $this->_pk);
  446. }
  447. public function fetch_all_search($conditions, $tableid = 0, $start = 0, $limit = 0, $order = '', $sort = 'DESC', $forceindex='') {
  448. $ordersql = '';
  449. if(!empty($order)) {
  450. $ordersql = " ORDER BY $order $sort ";
  451. }
  452. $data = array();
  453. $tlkey = !empty($conditions['inforum']) && !is_array($conditions['inforum']) ? $conditions['inforum'] : '';
  454. $firstpage = false;
  455. $defult = count($conditions) < 5 ? true : false;
  456. if(count($conditions) < 5) {
  457. foreach(array_keys($conditions) as $key) {
  458. if(!in_array($key, array('inforum', 'sticky', 'displayorder', 'intids'))) {
  459. $defult = false;
  460. break;
  461. }
  462. }
  463. }
  464. if(!defined('IN_MOBILE') && $defult && $conditions['sticky'] == 4 && $start == 0 && $limit && strtolower(preg_replace("/\s?/ies", '', $order)) == 'displayorderdesc,lastpostdesc' && empty($sort)) {
  465. foreach($conditions['displayorder'] as $id) {
  466. if($id < 2) {
  467. $firstpage = true;
  468. if($id < 0) {
  469. $firstpage = false;
  470. break;
  471. }
  472. }
  473. }
  474. if($firstpage && !empty($tlkey) && ($ttl = getglobal('setting/memory/forum_thread_forumdisplay')) !== null && ($data = $this->fetch_cache($tlkey, 'forumdisplay_')) !== false) {
  475. $delusers = $this->fetch_cache('deleteuids', '');
  476. if(!empty($delusers)) {
  477. foreach($data as $tid => $value) {
  478. if(isset($delusers[$value['authorid']])) {
  479. $data = array();
  480. }
  481. }
  482. }
  483. if($data) {
  484. return $data;
  485. }
  486. }
  487. }
  488. $data = DB::fetch_all("SELECT * FROM ".DB::table($this->get_table_name($tableid))." $forceindex".$this->search_condition($conditions)." $ordersql ".DB::limit($start, $limit));
  489. if($firstpage && !empty($tlkey) && ($ttl = getglobal('setting/memory/forum_thread_forumdisplay')) !== null) {
  490. $this->store_cache($tlkey, $data, $ttl, 'forumdisplay_');
  491. }
  492. return $data;
  493. }
  494. public function fetch_all_by_special($special, $authorid = 0, $replies = 0, $displayorder = null, $subject = '', $join = 0, $start = 0, $limit = 0, $order = 'dateline', $sort = 'DESC') {
  495. $condition = $this->make_special_condition($special, $authorid, $replies, $displayorder, $subject, $join, 0);
  496. $ordersql = !empty($order) ? ' ORDER BY t.'.DB::order($order, $sort) : '';
  497. return DB::fetch_all("SELECT t.* FROM %t t $condition[jointable] ".$condition['where'].$ordersql.DB::limit($start, $limit), $condition['parameter'], $this->_pk);
  498. }
  499. public function fetch_all_heats() {
  500. $heatdateline = getglobal('timestamp') - 86400 * getglobal('setting/indexhot/days');
  501. $addtablesql = $addsql = '';
  502. if(!helper_access::check_module('group')) {
  503. $addtablesql = " LEFT JOIN ".DB::table('forum_forum')." f ON f.fid = t.fid ";
  504. $addsql = " AND f.status IN ('0', '1') ";
  505. }
  506. return DB::fetch_all("SELECT t.tid,t.posttableid,t.views,t.dateline,t.replies,t.author,t.authorid,t.subject,t.price
  507. FROM ".DB::table('forum_thread')." t $addtablesql
  508. WHERE t.dateline>'$heatdateline' AND t.heats>'0' AND t.displayorder>='0' $addsql ORDER BY t.heats DESC LIMIT ".(getglobal('setting/indexhot/limit') * 2));
  509. }
  510. private function make_query_condition($tids, $fids = array(), $isgroup = -1, $author = '', $subject = '', $displayorder = null, $dateline = null) {
  511. $parameter = array($this->get_table_name());
  512. $wherearr = array();
  513. if(!empty($tids)) {
  514. $tids = dintval($tids, true);
  515. $parameter[] = $tids;
  516. $wherearr[] = is_array($tids) ? 'tid IN(%n)' : 'tid=%d';
  517. }
  518. if(!empty($fids)) {
  519. $fids = dintval($fids, true);
  520. $parameter[] = $fids;
  521. $wherearr[] = is_array($fids) ? 'fid IN(%n)' : 'fid=%d';
  522. }
  523. if(in_array($isgroup, array(0, 1))) {
  524. $parameter[] = $isgroup;
  525. $wherearr[] = "isgroup=%d";
  526. }
  527. if(!empty($author)) {
  528. $parameter[] = $author;
  529. $wherearr[] = "author=%s";
  530. }
  531. if($displayorder !== null) {
  532. $parameter[] = $displayorder;
  533. $wherearr[] = 'displayorder=%d';
  534. }
  535. if($dateline !== null) {
  536. $parameter[] = getglobal('timestamp') - $dateline;
  537. $wherearr[] = 'dateline>=%d';
  538. }
  539. if(!empty($subject)) {
  540. $parameter[] = '%'.$subject.'%';
  541. $wherearr[] = "subject LIKE %s";
  542. }
  543. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  544. return array($wheresql, $parameter);
  545. }
  546. public function count_by_special($special, $authorid = 0, $replies = 0, $displayorder = null, $subject = '', $join = 0) {
  547. $condition = $this->make_special_condition($special, $authorid, $replies, $displayorder, $subject, $join, 0);
  548. return DB::result_first("SELECT COUNT(*) FROM %t t $condition[jointable] ".$condition['where'], $condition['parameter']);
  549. }
  550. private function make_special_condition($special, $authorid = 0, $replies = 0, $displayorder = null, $subject = '', $join = 0, $tableid = 0) {
  551. $wherearr = $condition = array();
  552. $parameter = array($this->get_table_name($tableid));
  553. if($authorid && !$join) {
  554. $authorid = dintval($authorid, true);
  555. $parameter[] = $authorid;
  556. $wherearr[] = is_array($authorid) && $authorid ? 't.authorid IN(%n)' : 't.authorid=%d';
  557. }
  558. $parameter[] = $special;
  559. $wherearr[] = 't.special=%d';
  560. if($replies) {
  561. $parameter[] = $replies;
  562. $wherearr[] = 't.replies>=%d';
  563. }
  564. if($displayorder !== null) {
  565. $parameter[] = $displayorder;
  566. $wherearr[] = 't.displayorder>=%d';
  567. }
  568. if(!empty($subject)) {
  569. $parameter[] = '%'.$subject.'%';
  570. $wherearr[] = "subject LIKE %s";
  571. }
  572. if($join) {
  573. if($special == 1) {
  574. $parameter[] = $authorid;
  575. $wherearr[] = 'p.uid=%d';
  576. $wherearr[] = 'p.tid = t.tid';
  577. $condition['jointable'] = ', '.DB::table('forum_pollvoter').' p ';
  578. } elseif($special == 5) {
  579. $parameter[] = $authorid;
  580. $wherearr[] = 'p.authorid=%d';
  581. $wherearr[] = 'p.first=0';
  582. $wherearr[] = 'p.tid = t.tid';
  583. $posttable = getposttable();
  584. $condition['jointable'] = ', '.DB::table($posttable).' p ';
  585. }
  586. }
  587. $condition['parameter'] = $parameter;
  588. $condition['where'] = ' WHERE '.implode(' AND ', $wherearr);
  589. return $condition;
  590. }
  591. public function count_search($conditions, $tableid = 0, $prefix = false) {
  592. $prefix = $prefix ? '' : 't';
  593. return DB::result_first("SELECT COUNT(*) FROM %t $prefix %i", array($this->get_table_name($tableid), $this->search_condition($conditions, $prefix)));
  594. }
  595. public function search_condition($conditions, $prefix = false) {
  596. $this->_urlparam = $wherearr = array();
  597. if($prefix) {
  598. $prefix = 't.';
  599. }
  600. if($conditions['sourcetableid'] != '') {
  601. $this->_urlparam[] = "sourcetableid={$conditions['sourcetableid']}";
  602. }
  603. if($conditions['inforum'] != '' && $conditions['inforum'] != 'all') {
  604. $wherearr[] = $prefix.DB::field('fid', $conditions['inforum']);
  605. $this->_urlparam[] = "inforum={$conditions['inforum']}";
  606. }
  607. if($conditions['intids']) {
  608. $wherearr[] = $prefix.DB::field('tid', $conditions['intids']);
  609. $this->_urlparam[] = "intids={$conditions['intids']}";
  610. }
  611. if($conditions['tidmin'] != '') {
  612. $wherearr[] = $prefix.DB::field('tid', $conditions['tidmin'], '>=');
  613. $this->_urlparam[] = "tidmin={$conditions['tidmin']}";
  614. }
  615. if($conditions['tidmax'] != '') {
  616. $wherearr[] = $prefix.DB::field('tid', $conditions['tidmax'], '<=');
  617. $this->_urlparam[] = "tidmax={$conditions['tidmax']}";
  618. }
  619. if(isset($conditions['sticky'])) {
  620. if($conditions['sticky'] == 1) {
  621. $wherearr[] = $prefix.DB::field('displayorder', 0, '>');
  622. $this->_urlparam[] = "sticky=1";
  623. } elseif($conditions['sticky'] == 2) {
  624. $wherearr[] = $prefix.DB::field('displayorder', 0);
  625. $this->_urlparam[] = "sticky=2";
  626. } elseif($conditions['sticky'] == 3) {
  627. $wherearr[] = $prefix.DB::field('displayorder', -1);
  628. $this->_urlparam[] = "sticky=3";
  629. } elseif($conditions['sticky'] == 4) {
  630. $wherearr[] = $prefix.DB::field('displayorder', $conditions['displayorder']);
  631. $this->_urlparam[] = "sticky=4";
  632. } else {
  633. $wherearr[] = $prefix.DB::field('displayorder', 0, '>=');
  634. $this->_urlparam[] = "sticky=0";
  635. }
  636. }
  637. if($conditions['noreplydays']) {
  638. $conditions['noreplydays'] = intval($conditions['noreplydays']);
  639. $lastpost = getglobal('timestamp') - $conditions['noreplydays'] * 86400;
  640. $wherearr[] = $prefix.DB::field('lastpost', $lastpost, '<');
  641. $this->_urlparam[] = "noreplydays={$conditions['noreplydays']}";
  642. }
  643. if($conditions['lastpostless']) {
  644. $wherearr[] = $prefix.DB::field('lastpost', $conditions['lastpostless'], '<=');
  645. $this->_urlparam[] = "lastpostless={$conditions['lastpostless']}";
  646. }
  647. if($conditions['lastpostmore']) {
  648. $wherearr[] = $prefix.DB::field('lastpost', $conditions['lastpostmore'], '>=');
  649. $this->_urlparam[] = "lastpostmore={$conditions['lastpostmore']}";
  650. }
  651. if($conditions['intype'] != '' && $conditions['intype'] != 'all') {
  652. $wherearr[] = $prefix.DB::field('typeid', $conditions['intype']);
  653. $this->_urlparam[] = "intype={$conditions['intype']}";
  654. }
  655. if($conditions['insort'] != '' && $conditions['insort'] != 'all') {
  656. $wherearr[] = $prefix.DB::field('sortid', $conditions['insort']);
  657. $this->_urlparam[] = "insort={$conditions['insort']}";
  658. }
  659. if(isset($conditions['viewsless']) && $conditions['viewsless'] !== '') {
  660. $wherearr[] = $prefix.DB::field('views', $conditions['viewsless'], '<=');
  661. $this->_urlparam[] = "viewsless={$conditions['viewsless']}";
  662. }
  663. if(isset($conditions['viewsmore']) && $conditions['viewsmore'] !== '') {
  664. $wherearr[] = $prefix.DB::field('views', $conditions['viewsmore'], '>=');
  665. $this->_urlparam[] = "viewsmore={$conditions['viewsmore']}";
  666. }
  667. if(isset($conditions['repliesless']) && $conditions['repliesless'] !== '') {
  668. $wherearr[] = $prefix.DB::field('replies', $conditions['repliesless'], '<=');
  669. $this->_urlparam[] = "repliesless={$conditions['repliesless']}";
  670. }
  671. if(isset($conditions['repliesmore']) && $conditions['repliesmore'] !== '') {
  672. $wherearr[] = $prefix.DB::field('replies', $conditions['repliesmore'], '>=');
  673. $this->_urlparam[] = "repliesmore={$conditions['repliesmore']}";
  674. }
  675. if(isset($conditions['readpermmore']) && $conditions['readpermmore'] !== '') {
  676. $wherearr[] = $prefix.DB::field('readperm', $conditions['readpermmore'], '>');
  677. $this->_urlparam[] = "readpermmore={$conditions['readpermmore']}";
  678. }
  679. if(isset($conditions['pricesless']) && $conditions['pricesless'] !== '') {
  680. $wherearr[] = $prefix.DB::field('price', $conditions['pricesless'], '<');
  681. $this->_urlparam[] = "pricemore={$conditions['pricesless']}";
  682. }
  683. if(isset($conditions['pricemore']) && $conditions['pricemore'] !== '') {
  684. $wherearr[] = $prefix.DB::field('price', $conditions['pricemore'], '>');
  685. $this->_urlparam[] = "pricemore={$conditions['pricemore']}";
  686. }
  687. if($conditions['beforedays'] != '') {
  688. $dateline = getglobal('timestamp') - $conditions['beforedays']*86400;
  689. $wherearr[] = $prefix.DB::field('dateline', $dateline, '<');
  690. $this->_urlparam[] = "beforedays={$conditions['beforedays']}";
  691. }
  692. if($conditions['starttime'] != '') {
  693. $starttime = strtotime($conditions['starttime']);
  694. $wherearr[] = $prefix.DB::field('dateline', $starttime, '>');
  695. $this->_urlparam[] = "starttime={$conditions['starttime']}";
  696. }
  697. if($conditions['endtime'] != '') {
  698. $endtime = strtotime($conditions['endtime']);
  699. $wherearr[] = $prefix.DB::field('dateline', $endtime, '<=');
  700. $this->_urlparam[] = "endtime={$conditions['endtime']}";
  701. }
  702. $conditions['users'] = trim($conditions['users']);
  703. if(!empty($conditions['users'])) {
  704. $wherearr[] = $prefix.DB::field('author', explode(' ', trim($conditions['users'])));
  705. $this->_urlparam[] = "users={$conditions['users']}";
  706. }
  707. if($conditions['digest'] == 1) {
  708. $wherearr[] = $prefix.DB::field('digest', 0, '>');
  709. $this->_urlparam[] = "digest=1";
  710. } elseif($conditions['digest'] == 2) {
  711. $wherearr[] = $prefix.DB::field('digest', 0);
  712. $this->_urlparam[] = "digest=2";
  713. } elseif(is_array($conditions['digest'])) {
  714. $wherearr[] = $prefix.DB::field('digest', $conditions['digest']);
  715. $this->_urlparam[] = "digest=".implode(',', $conditions['digest']);
  716. }
  717. if($conditions['recommends']) {
  718. $wherearr[] = $prefix.DB::field('recommends', $conditions['recommends'], '>');
  719. $this->_urlparam[] = "recommends=".$conditions['recommends'];
  720. }
  721. if($conditions['authorid']) {
  722. $wherearr[] = $prefix.DB::field('authorid', $conditions['authorid']);
  723. $this->_urlparam[] = "authorid=".$conditions['authorid'];
  724. }
  725. if($conditions['attach'] == 1) {
  726. $wherearr[] = $prefix.DB::field('attachment', 0, '>');
  727. $this->_urlparam[] = "attach=1";
  728. } elseif($conditions['attach'] == 2) {
  729. $wherearr[] = $prefix.DB::field('attachment', 0);
  730. $this->_urlparam[] = "attach=2";
  731. }
  732. if($conditions['rate'] == 1) {
  733. $wherearr[] = $prefix.DB::field('rate', 0, '>');
  734. $this->_urlparam[] = "rate=1";
  735. } elseif($conditions['rate'] == 2) {
  736. $wherearr[] = $prefix.DB::field('rate', 0);
  737. $this->_urlparam[] = "rate=2";
  738. }
  739. if($conditions['highlight'] == 1) {
  740. $wherearr[] = $prefix.DB::field('highlight', 0, '>');
  741. $this->_urlparam[] = "highlight=1";
  742. } elseif($conditions['highlight'] == 2) {
  743. $wherearr[] = $prefix.DB::field('highlight', 0);
  744. $this->_urlparam[] = "highlight=2";
  745. }
  746. if(!empty($conditions['special'])) {
  747. $this->_urlparam[] = "special={$conditions['special']}";
  748. if($conditions['specialthread'] == 1) {
  749. $wherearr[] = $prefix.DB::field('special', $conditions['special']);
  750. $this->_urlparam[] = "specialthread=1";
  751. } elseif($conditions['specialthread'] == 2) {
  752. $wherearr[] = $prefix.DB::field('special', $conditions['special'], 'notin');
  753. $this->_urlparam[] = "specialthread=2";
  754. }
  755. }
  756. if(isset($conditions['isgroup']) && in_array($conditions['isgroup'], array(0, 1))) {
  757. $wherearr[] = $prefix.DB::field('isgroup', $conditions['isgroup']);
  758. }
  759. if(trim($conditions['keywords'])) {
  760. $sqlkeywords = '';
  761. $or = '';
  762. $keywords = explode(',', str_replace(' ', '', $conditions['keywords']));
  763. for($i = 0; $i < count($keywords); $i++) {
  764. $sqlkeywords .= " $or ".$prefix.DB::field('subject', '%'.$keywords[$i].'%', 'like');
  765. $or = 'OR';
  766. }
  767. $wherearr[] = "($sqlkeywords)";
  768. $this->_urlparam[] = "keywords={$conditions['keywords']}";
  769. }
  770. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  771. return $wheresql;
  772. }
  773. public function get_posttableid() {
  774. return $this->_posttableid;
  775. }
  776. public function get_url_param() {
  777. return $this->_urlparam;
  778. }
  779. public function update_displayorder_by_tid_displayorder($tids, $olddisplayorder, $newdisplayorder) {
  780. $tids = dintval((array)$tids, true);
  781. if($tids) {
  782. return DB::query('UPDATE %t SET displayorder=%d WHERE tid IN (%n) AND displayorder=%d', array($this->get_table_name(), $newdisplayorder, $tids, $olddisplayorder));
  783. }
  784. return 0;
  785. }
  786. public function update($tid, $data, $unbuffered = false, $low_priority = false, $tableid = 0, $realdata = false) {
  787. $tid = dintval($tid, true);
  788. if($data && is_array($data) && $tid) {
  789. if(!$realdata) {
  790. $num = DB::update($this->get_table_name($tableid), $data, DB::field('tid', $tid), $unbuffered, $low_priority);
  791. $this->update_batch_cache((array)$tid, $data);
  792. } else {
  793. $num = DB::query('UPDATE '.DB::table($this->get_table_name($tableid))." SET ".implode(',', $data)." WHERE ".DB::field('tid', $tid), 'UNBUFFERED');
  794. $this->clear_cache($tid);
  795. }
  796. return $num;
  797. }
  798. return !$unbuffered ? 0 : false;
  799. }
  800. public function update_by_fid($fid, $data, $tableid = 0) {
  801. $fid = dintval($fid, true);
  802. if($data && is_array($data) && $fid) {
  803. return DB::update($this->get_table_name($tableid), $data, DB::field('fid', $fid));
  804. }
  805. return array();
  806. }
  807. public function update_by_tid_displayorder($tid, $displayorder, $data, $fid = 0, $tableid = 0) {
  808. $condition = array();
  809. $tid = dintval($tid, true);
  810. $condition[] = DB::field('tid', $tid);
  811. if($fid) {
  812. $fid = dintval($fid, true);
  813. $condition[] = DB::field('fid', $fid);
  814. }
  815. $condition[] = DB::field('displayorder', $displayorder);
  816. if($data && is_array($data) && $tid) {
  817. return DB::update($this->get_table_name($tableid), $data, implode(' AND ', $condition));
  818. }
  819. return 0;
  820. }
  821. public function update_by_closed($tids, $data, $tableid = 0) {
  822. $tids = dintval($tids, true);
  823. if(!empty($data) && is_array($data)) {
  824. $num = DB::update($this->get_table_name($tableid), $data, DB::field('closed', $tids), true);
  825. if($num) {
  826. foreach((array)$tids as $tid) {
  827. $this->update_cache($tid, $data, $this->_cache_ttl);
  828. }
  829. }
  830. return $num;
  831. }
  832. return 0;
  833. }
  834. public function update_status_by_tid($tids, $value, $glue = '|') {
  835. $tids = dintval($tids, true);
  836. if($tids) {
  837. $this->clear_cache((array)$tids);
  838. $glue = helper_util::check_glue($glue);
  839. return DB::query("UPDATE %t SET status=status{$glue}%s WHERE tid IN(%n)", array($this->get_table_name(), $value, (array)$tids));
  840. }
  841. return 0;
  842. }
  843. public function update_sortid_by_sortid($sortid, $oldsortid) {
  844. $sortid = dintval($sortid);
  845. $oldsortid = dintval($oldsortid, true);
  846. if($oldsortid) {
  847. return DB::query("UPDATE %t SET sortid=%d WHERE sortid IN (%n)", array($this->get_table_name(), $sortid, $oldsortid));
  848. }
  849. return 0;
  850. }
  851. public function increase($tids, $fieldarr, $low_priority = false, $tableid = 0, $getsetarr = false) {
  852. $tids = dintval((array)$tids, true);
  853. $sql = array();
  854. $num = 0;
  855. $allowkey = array('views', 'replies', 'recommends', 'recommend_add', 'recommend_sub', 'favtimes', 'sharetimes', 'moderated', 'heats', 'lastposter', 'lastpost');
  856. foreach($fieldarr as $key => $value) {
  857. if(in_array($key, $allowkey)) {
  858. if(is_array($value)) {
  859. $sql[] = DB::field($key, $value[0]);
  860. } else {
  861. $value = dintval($value);
  862. $sql[] = "`$key`=`$key`+'$value'";
  863. }
  864. } else {
  865. unset($fieldarr[$key]);
  866. }
  867. }
  868. if($getsetarr) {
  869. return $sql;
  870. }
  871. if(!empty($sql)){
  872. $cmd = "UPDATE " . ($low_priority ? 'LOW_PRIORITY ' : '');
  873. $num = DB::query($cmd.DB::table($this->get_table_name($tableid))." SET ".implode(',', $sql)." WHERE tid IN (".dimplode($tids).")", 'UNBUFFERED');
  874. $this->increase_cache($tids, $fieldarr);
  875. }
  876. return $num;
  877. }
  878. public function insert($data, $return_insert_id = false, $replace = false, $silent = false) {
  879. if($data && is_array($data)) {
  880. $this->clear_cache($data['fid'], 'forumdisplay_');
  881. return DB::insert($this->_table, $data, $return_insert_id, $replace, $silent);
  882. }
  883. return 0;
  884. }
  885. public function insert_thread_copy_by_tid($tids, $origin = 0, $target = 0) {
  886. $tids = dintval($tids, true);
  887. if($tids) {
  888. $wheresql = is_array($tids) && $tids ? 'tid IN(%n)' : 'tid=%d';
  889. DB::query("INSERT INTO %t SELECT * FROM %t WHERE $wheresql", array($this->get_table_name($origin), $this->get_table_name($target), $tids));
  890. }
  891. }
  892. public function count_by_authorid($authorid, $tableid = 0) {
  893. return DB::result_first("SELECT COUNT(*) FROM %t WHERE authorid=%d", array($this->get_table_name($tableid), $authorid));
  894. }
  895. public function count_by_fid($fid, $tableid = 0) {
  896. return DB::result_first("SELECT COUNT(*) FROM %t WHERE fid=%d", array($this->get_table_name($tableid), $fid));
  897. }
  898. public function count_by_displayorder($displayorder) {
  899. return DB::result_first("SELECT COUNT(*) FROM %t WHERE displayorder=%d", array($this->get_table_name(), $displayorder));
  900. }
  901. public function count_by_replies($number, $glue = '>') {
  902. $glue = helper_util::check_glue($glue);
  903. return DB::result_first("SELECT COUNT(*) FROM %t WHERE replies{$glue}%d", array($this->get_table_name(), $number));
  904. }
  905. public function count_by_fid_typeid_displayorder($fid, $typeid = null, $displayorder = null, $glue = '=') {
  906. $parameter = array($this->get_table_name(), $fid);
  907. $wherearr = array();
  908. $fid = dintval($fid, true);
  909. $wherearr[] = is_array($fid) ? 'fid IN(%n)' : 'fid=%d';
  910. if($typeid) {
  911. $parameter[] = $typeid;
  912. $wherearr[] = "typeid=%d";
  913. }
  914. if($displayorder !== null) {
  915. $parameter[] = $displayorder;
  916. $glue = helper_util::check_glue($glue);
  917. $wherearr[] = "displayorder{$glue}%d";
  918. }
  919. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  920. return DB::result_first("SELECT COUNT(*) FROM %t $wheresql", $parameter);
  921. }
  922. public function count_posts_by_fid($fid, $forcetableid = null) {
  923. $data = array('threads' => 0, 'posts' => 0);
  924. loadcache('threadtableids');
  925. $threadtableids = array(0);
  926. $tableids = getglobal('cache/threadtableids');
  927. if(!empty($tableids)) {
  928. if($forcetableid === null || ($forcetableid > 0 && !in_array($forcetableid, $tableids))) {
  929. $threadtableids = array_merge($threadtableids, $tableids);
  930. } else {
  931. $threadtableids = array(intval($forcetableid));
  932. }
  933. }
  934. $threadtableids = array_unique($threadtableids);
  935. foreach($threadtableids as $tableid) {
  936. $value = DB::fetch_first('SELECT COUNT(*) AS threads, SUM(replies)+COUNT(*) AS posts FROM %t WHERE fid=%d AND displayorder>=0', array($this->get_table_name($tableid), $fid));
  937. $data['threads'] += intval($value['threads']);
  938. $data['posts'] += intval($value['posts']);
  939. }
  940. return $data;
  941. }
  942. public function count_by_fid_displayorder_authorid($fid, $displayorder, $authorid, $tableid=0) {
  943. return DB::result_first("SELECT COUNT(*) FROM %t WHERE fid=%d AND displayorder=%d AND authorid=%d", array($this->get_table_name($tableid), $fid, $displayorder, $authorid));
  944. }
  945. public function count_all_thread() {
  946. $count = 0;
  947. $settings = C::t('common_setting')->fetch_all('threadtableids', true);
  948. if(empty($settings['threadtableids']) || !is_array($settings['threadtableids'])) {
  949. $settings['threadtableids'] = array(0);
  950. }
  951. foreach($settings['threadtableids'] as $tableid) {
  952. $count += $this->count_by_tableid($tableid);
  953. }
  954. return $count;
  955. }
  956. public function count_by_posttableid_displayorder($tableid = 0, $posttableid = 0, $displayorder = 0) {
  957. return DB::result_first('SELECT COUNT(*) FROM %t WHERE posttableid=%d AND displayorder=%d', array($this->get_table_name($tableid), $posttableid, $displayorder));
  958. }
  959. public function count_by_tableid($tableid) {
  960. return DB::result_first("SELECT COUNT(*) FROM %t", array($this->get_table_name($tableid)));
  961. }
  962. public function count_by_tid_fid($tids, $fids = array(), $isgroup = -1, $author = '', $subject = '') {
  963. $condition = $this->make_query_condition($tids, $fids, $isgroup, $author, $subject);
  964. return DB::result_first("SELECT COUNT(*) FROM %t $condition[0]", $condition[1]);
  965. }
  966. public function count_group_thread_by_fid($fid) {
  967. return DB::fetch_all('SELECT COUNT(*) AS num, authorid FROM %t WHERE fid=%d GROUP BY authorid', array($this->get_table_name(), $fid));
  968. }
  969. public function count_group_by_fid($tableid = 0) {
  970. return DB::fetch_all('SELECT fid, COUNT(*) AS threads, SUM(replies)+COUNT(*) AS posts FROM %t GROUP BY fid', array($this->get_table_name($tableid)));
  971. }
  972. public function count_special_group_by_special() {
  973. return DB::fetch_all('SELECT special, count(*) AS spcount FROM %t GROUP BY special', array($this->get_table_name()));
  974. }
  975. public function count_by_recyclebine($fid = 0, $isgroup = 0, $author = array(), $username = array(), $pstarttime = 0, $pendtime = 0, $mstarttime = 0, $mendtime = 0, $keywords = '') {
  976. $sql = $this->recyclebine_where($fid, $isgroup, $author, $username, $pstarttime, $pendtime, $mstarttime, $mendtime, $keywords);
  977. return DB::result_first('SELECT COUNT(*) FROM '.DB::table('forum_thread').' t LEFT JOIN '.DB::table('forum_threadmod').' tm ON tm.tid=t.tid '.$sql[0], $sql[1]);
  978. }
  979. public function delete_by_tid($tids, $unbuffered = false, $tableid = 0, $limit = 0) {
  980. $tids = dintval($tids, true);
  981. if($tids) {
  982. $this->clear_cache($tids);
  983. return DB::delete($this->get_table_name($tableid), DB::field('tid', $tids), $limit, $unbuffered);
  984. }
  985. return !$unbuffered ? 0 : false;
  986. }
  987. public function delete_by_fid($fid, $unbuffered = false, $tableid = 0, $limit = 0) {
  988. $fid = dintval($fid, true);
  989. if($fid) {
  990. foreach((array)$fid as $delfid) {
  991. $this->clear_cache($delfid, 'forumdisplay_');
  992. }
  993. return DB::delete($this->get_table_name($tableid), DB::field('fid', $fid), $limit, $unbuffered);
  994. }
  995. return 0;
  996. }
  997. public function get_table_name($tableid = 0){
  998. $tableid = intval($tableid);
  999. return $tableid ? "forum_thread_$tableid" : 'forum_thread';
  1000. }
  1001. public function fetch_all_for_guide($type, $limittid, $tids = array(), $heatslimit = 3, $dateline = 0) {
  1002. switch ($type) {
  1003. case 'hot' :
  1004. $addsql = ' AND heats>'.intval($heatslimit);
  1005. break;
  1006. case 'digest' :
  1007. $addsql = ' AND digest>0';
  1008. break;
  1009. default :
  1010. $addsql = '';
  1011. }
  1012. if(getglobal('setting/followforumid')) {
  1013. $addsql .= ' AND '.DB::field('fid', getglobal('setting/followforumid'), '<>');
  1014. }
  1015. $tidsql = '';
  1016. if($tids) {
  1017. $tids = dintval($tids, true);
  1018. $tidsql = DB::field('tid', $tids);
  1019. } else {
  1020. $tidsql = 'tid>'.intval($limittid);
  1021. if($dateline) {
  1022. $addsql .= ' AND dateline > '.intval($dateline);
  1023. }
  1024. $addsql .= ' AND displayorder>=0 ORDER BY lastpost DESC LIMIT 600';
  1025. }
  1026. return DB::fetch_all("SELECT * FROM ".DB::table('forum_thread')." WHERE ".$tidsql.$addsql);
  1027. }
  1028. public function fetch_max_tid() {
  1029. return DB::result_first("SELECT MAX(tid) as maxtid FROM ".DB::table('forum_thread'));
  1030. }
  1031. public function move_thread_by_tid($tids, $source, $target) {
  1032. $source = intval($source);
  1033. $target = intval($target);
  1034. if($source != $target) {
  1035. DB::query('REPLACE INTO %t SELECT * FROM %t WHERE tid IN (%n)', array($this->get_table_name($target), $this->get_table_name($source), $tids));
  1036. return DB::delete($this->get_table_name($source), DB::field('tid', $tids));
  1037. } else {
  1038. return false;
  1039. }
  1040. }
  1041. function gettablestatus($tableid = 0) {
  1042. $table_info = DB::fetch_first("SHOW TABLE STATUS LIKE '".str_replace('_', '\_', DB::table($this->get_table_name($tableid)))."'");
  1043. $table_info['Data_length'] = $table_info['Data_length'] / 1024 / 1024;
  1044. $nums = intval(log($table_info['Data_length']) / log(10));
  1045. $digits = 0;
  1046. if($nums <= 3) {
  1047. $digits = 3 - $nums;
  1048. }
  1049. $table_info['Data_length'] = number_format($table_info['Data_length'], $digits).' MB';
  1050. $table_info['Index_length'] = $table_info['Index_length'] / 1024 / 1024;
  1051. $nums = intval(log($table_info['Index_length']) / log(10));
  1052. $digits = 0;
  1053. if($nums <= 3) {
  1054. $digits = 3 - $nums;
  1055. }
  1056. $table_info['Index_length'] = number_format($table_info['Index_length'], $digits).' MB';
  1057. return $table_info;
  1058. }
  1059. private function recyclebine_where($fid = 0, $isgroup = 0, $authors = array(), $username = array(), $pstarttime = 0, $pendtime = 0, $mstarttime = 0, $mendtime = 0, $keywords = '') {
  1060. $parameter = array();
  1061. $wherearr = array('t.displayorder=-1', 'tm.action=\'DEL\'');
  1062. if($fid) {
  1063. $fid = dintval($fid, true);
  1064. $parameter[] = $fid;
  1065. $wherearr[] = is_array($fid) ? 't.fid IN(%n)' : 't.fid=%d';
  1066. }
  1067. if($isgroup) {
  1068. $wherearr[] = 't.isgroup=1';
  1069. }
  1070. if(!empty($authors)) {
  1071. $parameter[] = $authors;
  1072. $wherearr[] = is_array($authors) && $authors ? 't.author IN(%n)' : 't.author=%s';
  1073. }
  1074. if(!empty($username)) {
  1075. $parameter[] = $username;
  1076. $wherearr[] = is_array($username) && $username ? 'tm.username IN(%n)' : 'tm.username=%s';
  1077. }
  1078. if($pstarttime) {
  1079. $parameter[] = $pstarttime;
  1080. $wherearr[] = 't.dateline>=%d';
  1081. }
  1082. if($pendtime) {
  1083. $parameter[] = $pendtime;
  1084. $wherearr[] = 't.dateline<%d';
  1085. }
  1086. if($mstarttime) {
  1087. $parameter[] = $mstarttime;
  1088. $wherearr[] = 'tm.dateline>=%d';
  1089. }
  1090. if($mendtime) {
  1091. $parameter[] = $mendtime;
  1092. $wherearr[] = 'tm.dateline<%d';
  1093. }
  1094. if($keywords) {
  1095. $keysql = array();
  1096. foreach(explode(',', str_replace(' ', '', $keywords)) as $keyword) {
  1097. $parameter[] = '%'.$keyword.'%';
  1098. $keysql[] = "t.subject LIKE %s";
  1099. }
  1100. if($keysql) {
  1101. $wherearr[] = '('.implode(' OR ', $keysql).')';
  1102. }
  1103. }
  1104. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  1105. return array($wheresql, $parameter);
  1106. }
  1107. public function create_table($maxtableid) {
  1108. if($maxtableid) {
  1109. DB::query('SET SQL_QUOTE_SHOW_CREATE=0', 'SILENT');
  1110. $db = &DB::object();
  1111. $query = DB::query("SHOW CREATE TABLE %t", array($this->get_table_name()));
  1112. $create = $db->fetch_row($query);
  1113. $createsql = $create[1];
  1114. $createsql = str_replace(DB::table($this->get_table_name()), DB::table($this->get_table_name($maxtableid)), $createsql);
  1115. DB::query($createsql);
  1116. return true;
  1117. } else {
  1118. return false;
  1119. }
  1120. }
  1121. public function drop_table($tableid) {
  1122. $tableid = intval($tableid);
  1123. if($tableid) {
  1124. DB::query("DROP TABLE %t", array($this->get_table_name($tableid)), true);
  1125. return true;
  1126. } else {
  1127. return false;
  1128. }
  1129. }
  1130. private function compare_number($firstnum, $secondnum, $glue = '>=') {
  1131. switch($glue) {
  1132. case '==':
  1133. case '=':
  1134. return $firstnum == $secondnum;
  1135. break;
  1136. case '>':
  1137. return $firstnum > $secondnum;
  1138. break;
  1139. case '<':
  1140. return $firstnum < $secondnum;
  1141. break;
  1142. case '<>':
  1143. return $firstnum <> $secondnum;
  1144. break;
  1145. case '<=':
  1146. return $firstnum <= $secondnum;
  1147. break;
  1148. case '>=':
  1149. return $firstnum >= $secondnum;
  1150. break;
  1151. }
  1152. return false;
  1153. }
  1154. }
  1155. ?>