PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/api/manyou/Service/Server/Search.php

https://github.com/kuaileshike/upload
PHP | 1077 lines | 977 code | 94 blank | 6 comment | 206 complexity | b482dacad0f1197c086a61fee1e67fff 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: Search.php 31868 2012-10-18 03:38:22Z zhouxiaobo $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. Cloud::loadFile('Service_SearchHelper');
  12. class Cloud_Service_Server_Search extends Cloud_Service_Server_Restful {
  13. protected static $_instance;
  14. public static function getInstance() {
  15. if (!(self::$_instance instanceof self)) {
  16. self::$_instance = new self();
  17. }
  18. return self::$_instance;
  19. }
  20. public function onSearchGetUserGroupPermissions($userGroupIds) {
  21. if (!$userGroupIds) {
  22. return array();
  23. }
  24. $result = Cloud_Service_SearchHelper::getUserGroupPermissions($userGroupIds);
  25. return $result;
  26. }
  27. public function onSearchGetUpdatedPosts($num, $lastPostIds = array()) {
  28. if ($lastPostIds) {
  29. C::t('forum_postlog')->delete_by_pid($lastPostIds);
  30. }
  31. $result = array();
  32. $totalNum = C::t('forum_postlog')->count();
  33. if (!$totalNum) {
  34. return $result;
  35. }
  36. $result['totalNum'] = $totalNum;
  37. $pIds = $deletePosts = $updatePostIds = array();
  38. $unDeletePosts = array();
  39. $posts = array();
  40. foreach(C::t('forum_postlog')->fetch_all_order_by_dateline($num) as $post) {
  41. $pIds[] = $post['pid'];
  42. if ($post['action'] == 'delete') {
  43. $deletePosts[$post['pid']] = array(
  44. 'pId' => $post['pid'],
  45. 'action' => $post['action'],
  46. 'updated' => dgmdate($post['dateline'], 'Y-m-d H:i:s', 8),
  47. );
  48. } else {
  49. $unDeletePosts[$post['pid']] = array(
  50. 'pId' => $post['pid'],
  51. 'action' => $post['action'],
  52. 'updated' => dgmdate($post['dateline'], 'Y-m-d H:i:s', 8),
  53. );
  54. }
  55. }
  56. if($pIds) {
  57. if($unDeletePosts) {
  58. $gfIds = array(); // groupForumIds
  59. $posts = $this->_getPosts(array_keys($unDeletePosts));
  60. foreach($unDeletePosts as $pId => $updatePost) {
  61. if($posts[$pId]) {
  62. $unDeletePosts[$pId] = array_merge($updatePost, $posts[$pId]);
  63. } else {
  64. $unDeletePosts[$pId]['pId'] = 0;
  65. }
  66. if($posts[$pId]['isGroup']) {
  67. $gfIds[$posts[$pId]['fId']] = $posts[$pId]['fId'];
  68. }
  69. }
  70. }
  71. }
  72. $result['data'] = $deletePosts + $unDeletePosts;
  73. $result['ids']['post'] = $pIds;
  74. return $result;
  75. }
  76. public function onSearchRemovePostLogs($pIds) {
  77. if (!$pIds) {
  78. return false;
  79. }
  80. C::t('forum_postlog')->delete_by_pid($pIds);
  81. return true;
  82. }
  83. protected function _preGetPosts($tableid, $pIds) {
  84. $result = array();
  85. foreach(C::t('forum_post')->fetch_all_by_pid($tableid, $pIds) as $post) {
  86. $result[$post['pid']] = Cloud_Service_SearchHelper::convertPost($post);
  87. }
  88. return $result;
  89. }
  90. protected function _getPosts($pIds) {
  91. $posts = array();
  92. foreach(Cloud_Service_SearchHelper::getTables('post') as $tableid) {
  93. $_posts = $this->_preGetPosts($tableid, $pIds);
  94. if ($_posts) {
  95. if (!$posts) {
  96. $posts = $_posts;
  97. } else {
  98. $posts = $posts + $_posts;
  99. }
  100. if (count($posts) == count($pIds)) {
  101. break;
  102. }
  103. }
  104. }
  105. if ($posts) {
  106. foreach($posts as $pId => $post) {
  107. $tIds[$post['pId']] = $post['tId'];
  108. }
  109. if ($tIds) {
  110. $gfIds = $vtIds = $stIds = array(); // poll
  111. $threads = Cloud_Service_SearchHelper::getThreads($tIds);
  112. foreach($posts as $pId => $post) {
  113. $tId = $tIds[$pId];
  114. $posts[$pId]['isGroup'] = $threads[$tId]['isGroup'];
  115. if ($threads[$tId]['isGroup']) {
  116. $gfIds[$threads[$tId]['fId']] = $threads[$tId]['fId'];
  117. }
  118. if ($post['isThread']) {
  119. $stIds[$pId] = $tId;
  120. $posts[$pId]['threadInfo'] = $threads[$tId];
  121. }
  122. if ($threads[$tId]['specialType'] == 'poll') {
  123. $vtIds[$pId] = $tId;
  124. }
  125. }
  126. if($stIds) {
  127. $sorts = Cloud_Service_SearchHelper::getThreadSort($stIds);
  128. foreach($stIds as $pId => $tId) {
  129. $posts[$pId]['category'] = $sorts[$tId];
  130. }
  131. }
  132. if ($vtIds) {
  133. $polls = Cloud_Service_SearchHelper::getPollInfo($vtIds);
  134. foreach($vtIds as $pId => $tId) {
  135. $posts[$pId]['threadInfo']['pollInfo'] = $polls[$tId];
  136. }
  137. }
  138. $guestPerm = Cloud_Service_SearchHelper::getGuestPerm($gfIds);
  139. foreach($posts as $pId => $post) {
  140. if (in_array($post['fId'], $guestPerm['allowForumIds'])) {
  141. $posts[$pId]['isPublic'] = true;
  142. } else {
  143. $posts[$pId]['isPublic'] = false;
  144. }
  145. if ($post['isThread']) {
  146. $posts[$pId]['threadInfo']['isPublic'] = $posts[$pId]['isPublic'];
  147. }
  148. }
  149. }
  150. }
  151. return $posts;
  152. }
  153. public function onSearchGetPosts($pIds) {
  154. $authors = array();
  155. $posts = $this->_getPosts($pIds);
  156. if ($posts) {
  157. foreach($posts as $post) {
  158. $authors[$post['authorId']][] = $post['pId'];
  159. }
  160. $authorids = array_keys($authors);
  161. if ($authorids) {
  162. $banuids= $uids = array();
  163. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  164. $uids[$uid] = $uid;
  165. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  166. $banuids[] = $author['uid'];
  167. }
  168. }
  169. $deluids = array_diff($authorids, $uids);
  170. foreach($deluids as $deluid) {
  171. if (!$deluid) {
  172. continue;
  173. }
  174. foreach($authors[$deluid] as $pid) {
  175. $posts[$pid]['authorStatus'] = 'delete';
  176. }
  177. }
  178. foreach($banuids as $banuid) {
  179. foreach($authors[$banuid] as $pid) {
  180. $posts[$pid]['authorStatus'] = 'ban';
  181. }
  182. }
  183. }
  184. }
  185. return $posts;
  186. }
  187. protected function _getNewPosts($tableid, $num, $fromPostId = 0) {
  188. $result = array();
  189. if (dintval($num)) {
  190. foreach(C::t('forum_post')->fetch_all_new_post_by_pid($fromPostId, '', $num, $tableid) as $post) {
  191. $result['maxPid'] = $post['pid'];
  192. $result['data'][$post['pid']] = Cloud_Service_SearchHelper::convertPost($post);
  193. }
  194. }
  195. return $result;
  196. }
  197. public function onSearchGetNewPosts($num, $fromPostId = 0) {
  198. $res = $data = array();
  199. $maxPid = 0;
  200. foreach(Cloud_Service_SearchHelper::getTables('post') as $tableid) {
  201. $_posts = $this->_getNewPosts($tableid, $num, $fromPostId);
  202. if ($_posts['data']) {
  203. if (!$data) {
  204. $data = $_posts['data'];
  205. } else {
  206. $data = $data + $_posts['data'];
  207. }
  208. }
  209. if ($maxPid < $_posts['maxPid']) {
  210. $maxPid = $_posts['maxPid'];
  211. }
  212. }
  213. $_postNum = 0;
  214. if ($maxPid) {
  215. ksort($data);
  216. foreach($data as $k => $v) {
  217. $_postNum++;
  218. $res['data'][$k] = $v;
  219. $res['maxPid'] = $k;
  220. if ($_postNum == $num) {
  221. break;
  222. }
  223. }
  224. if (!$res['maxPid']) {
  225. $res['maxPid'] = $maxPid;
  226. }
  227. }
  228. if ($res['data']) {
  229. $tIds = $autors = array();
  230. foreach($res['data'] as $pId => $post) {
  231. $authors[$post['authorId']][] = $post['pId'];
  232. $tIds[$pId] = $post['tId'];
  233. }
  234. if ($tIds) {
  235. $threads = Cloud_Service_SearchHelper::getThreads($tIds);
  236. $stIds = array();
  237. foreach ($tIds as $pId => $tId) {
  238. $res['data'][$pId]['isGroup'] = $threads[$tId]['isGroup'];
  239. if ($res['data'][$pId]['isThread']) {
  240. $stIds[$pId] = $tId;
  241. $res['data'][$pId]['threadInfo'] = $threads[$tId];
  242. }
  243. }
  244. if($stIds) {
  245. $sorts = Cloud_Service_SearchHelper::getThreadSort($stIds);
  246. foreach($stIds as $pId => $tId) {
  247. $res['data'][$pId]['category'] = $sorts[$tId];
  248. }
  249. }
  250. }
  251. $authorids = array_keys($authors);
  252. if ($authorids) {
  253. $banuids= $uids = array();
  254. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  255. $uids[$uid] = $uid;
  256. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  257. $banuids[] = $author['uid'];
  258. }
  259. }
  260. $deluids = array_diff($authorids, $uids);
  261. foreach($deluids as $deluid) {
  262. if (!$deluid) {
  263. continue;
  264. }
  265. foreach($authors[$deluid] as $pid) {
  266. $res['data'][$pid]['authorStatus'] = 'delete';
  267. }
  268. }
  269. foreach($banuids as $banuid) {
  270. foreach($authors[$banuid] as $pid) {
  271. $res['data'][$pid]['authorStatus'] = 'ban';
  272. }
  273. }
  274. }
  275. }
  276. return $res;
  277. }
  278. public function onSearchGetAllPosts($num, $pId = 0, $orderType = 'ASC') {
  279. $res = $data = $_tableInfo = array();
  280. $maxPid = $minPid = 0;
  281. $orderType = strtoupper($orderType);
  282. foreach(Cloud_Service_SearchHelper::getTables('post') as $tableid) {
  283. $_posts = $this->_getAllPosts($tableid, $num, $pId, $orderType);
  284. if ($_posts['data']) {
  285. if (!$data) {
  286. $data = $_posts['data'];
  287. } else {
  288. $data = $data + $_posts['data'];
  289. }
  290. }
  291. if ($orderType == 'DESC') {
  292. if (!$minPid) {
  293. $minPid = $_posts['minPid'];
  294. }
  295. if ($minPid > $_posts['minPid']) {
  296. $minPid = $_posts['minPid'];
  297. }
  298. $_tableInfo['minPids'][] = array('current_index' => $i,
  299. 'minPid' => $_posts['minPid'],
  300. );
  301. } else {
  302. if ($maxPid < $_posts['maxPid']) {
  303. $maxPid = $_posts['maxPid'];
  304. }
  305. $_tableInfo['maxPids'][] = array('current_index' => $i,
  306. 'maxPid' => $_posts['maxPid'],
  307. );
  308. }
  309. }
  310. $_postNum = 0;
  311. if ($orderType == 'DESC') {
  312. if ($minPid) {
  313. krsort($data);
  314. foreach($data as $k => $v) {
  315. $_postNum++;
  316. $res['minPid'] = $k;
  317. $res['data'][$k] = $v;
  318. if ($_postNum == $num) {
  319. break;
  320. }
  321. }
  322. if (!$res['minPid']) {
  323. $res['minPid'] = $minPid;
  324. }
  325. }
  326. } else {
  327. if ($maxPid) {
  328. ksort($data);
  329. foreach($data as $k => $v) {
  330. $_postNum++;
  331. $res['data'][$k] = $v;
  332. $res['maxPid'] = $k;
  333. if ($_postNum == $num) {
  334. break;
  335. }
  336. }
  337. if (!$res['maxPid']) {
  338. $res['maxPid'] = $maxPid;
  339. }
  340. }
  341. }
  342. if ($res['data']) {
  343. $_tableInfo['tables'] = $tables;
  344. $tIds = $authors = $forums = array();
  345. foreach($res['data'] as $pId => $post) {
  346. $authors[$post['authorId']][] = $post['pId'];
  347. $tIds[$post['pId']] = $post['tId'];
  348. }
  349. if ($tIds) {
  350. $vtIds = $gfIds = $stIds = array();
  351. $threads = Cloud_Service_SearchHelper::getThreads($tIds);
  352. foreach($tIds as $_pId => $tId) {
  353. $res['data'][$_pId]['isGroup'] = $threads[$tId]['isGroup'];
  354. $myPost = $res['data'][$_pId];
  355. if ($myPost['isGroup']) {
  356. $gfIds[$myPost['fId']] = $myPost['fId'];
  357. }
  358. if ($myPost['isThread']) {
  359. $stIds[$_pId] = $tId;
  360. $res['data'][$_pId]['threadInfo'] = $threads[$tId];
  361. if ($threads[$tId]['specialType'] == 'poll') {
  362. $vtIds[$_pId] = $tId;
  363. }
  364. }
  365. }
  366. if($stIds) {
  367. $sorts = Cloud_Service_SearchHelper::getThreadSort($stIds);
  368. foreach($stIds as $pId => $tId) {
  369. $res['data'][$pId]['category'] = $sorts[$tId];
  370. }
  371. }
  372. if ($vtIds) {
  373. $polls = Cloud_Service_SearchHelper::getPollInfo($vtIds);
  374. foreach($vtIds as $pId => $tId) {
  375. $res['data'][$pId]['threadInfo']['pollInfo'] = $polls[$tId];
  376. }
  377. }
  378. $guestPerm = Cloud_Service_SearchHelper::getGuestPerm($gfIds);
  379. foreach($res['data'] as $key => $row) {
  380. if (in_array($row['fId'], $guestPerm['allowForumIds'])) {
  381. $res['data'][$key]['isPublic'] = true;
  382. } else {
  383. $res['data'][$key]['isPublic'] = false;
  384. }
  385. if ($row['isThread']) {
  386. $res['data'][$key]['threadInfo']['isPublic'] = $res['data'][$key]['isPublic'];
  387. }
  388. }
  389. }
  390. $authorids = array_keys($authors);
  391. if ($authorids) {
  392. $banuids= $uids = array();
  393. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  394. $uids[$uid] = $uid;
  395. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  396. $banuids[] = $author['uid'];
  397. }
  398. }
  399. $deluids = array_diff($authorids, $uids);
  400. foreach($deluids as $deluid) {
  401. if (!$deluid) {
  402. continue;
  403. }
  404. foreach($authors[$deluid] as $pid) {
  405. $res['data'][$pid]['authorStatus'] = 'delete';
  406. }
  407. }
  408. foreach($banuids as $banuid) {
  409. foreach($authors[$banuid] as $pid) {
  410. $res['data'][$pid]['authorStatus'] = 'ban';
  411. }
  412. }
  413. }
  414. }
  415. return $res;
  416. }
  417. protected function _getAllPosts($tableid, $num, $pId = 0, $orderType = 'ASC') {
  418. $result = array();
  419. if (dintval($num)) {
  420. if (strtoupper($orderType) == 'DESC') {
  421. $glue = '<';
  422. $key = 'minPid';
  423. } else {
  424. $orderType = 'ASC';
  425. $glue = '>';
  426. $key = 'maxPid';
  427. }
  428. $tIds = $authors = array();
  429. foreach(C::t('forum_post')->fetch_all_new_post_by_pid($pId, 0, $num, $tableid, $glue, $orderType) as $post) {
  430. $result[$key] = $post['pid'];
  431. $result['data'][$post['pid']] = Cloud_Service_SearchHelper::convertPost($post);
  432. }
  433. }
  434. return $result;
  435. }
  436. protected function _removeThreads($tIds, $isRecycle = false) {
  437. $tableThreads = array();
  438. foreach(Cloud_Service_SearchHelper::getTables('thread') as $tableid) {
  439. $_threads = Cloud_Service_SearchHelper::preGetThreads($tableid, $tIds);
  440. $tableThreads[$tableid] = $_threads;
  441. }
  442. foreach($tableThreads as $tableid => $threads) {
  443. $_tids = $_threadIds = array();
  444. foreach($threads as $thread) {
  445. $_tids[] = $thread['tId'];
  446. $postTable = $thread['postTableId'] ? $thread['postTableId'] : 0;
  447. $_threadIds[$postTable][] = $thread['tId'];
  448. }
  449. if($_tids) {
  450. if($isRecycle) {
  451. C::t('forum_thread')->update($_tIds, array('displayorder' => -1), false, false, $tableid);
  452. continue;
  453. }
  454. C::t('forum_thread')->delete_by_tid($_tids, false, $tableid);
  455. foreach($_threadIds as $postTable => $_tIds) {
  456. if ($_tIds) {
  457. C::t('forum_post')->delete_by_tid($postTable, $_tIds);
  458. }
  459. }
  460. }
  461. }
  462. return true;
  463. }
  464. public function onSearchRecyclePosts($pIds) {
  465. $posts = array();
  466. foreach(Cloud_Service_SearchHelper::getTables('post') as $tableid) {
  467. $_posts = $this->_preGetPosts($tableid, $pIds);
  468. $posts[$tableid] = $_posts;
  469. }
  470. foreach($posts as $id => $rows) {
  471. $tids = $pids = array();
  472. foreach($rows as $row) {
  473. if ($row['isThread']) {
  474. $tids[] = $row['tId'];
  475. } else {
  476. $pids[] = $row['pId'];
  477. }
  478. }
  479. if ($pids) {
  480. C::t('forum_post')->update($id, $pids, array('invisible' => -1));
  481. }
  482. if ($tids) {
  483. $this->_removeThreads($tids, true);
  484. }
  485. }
  486. return true;
  487. }
  488. public function onSearchGetUpdatedThreads($num, $lastThreadIds = array(), $lastForumIds = array(), $lastUserIds = array()) {
  489. $this->onSearchRemoveThreadLogs($lastThreadIds, $lastForumIds, $lastUserIds);
  490. $result = array();
  491. $totalNum = C::t('forum_threadlog')->count();
  492. if (!$totalNum) {
  493. return $result;
  494. }
  495. $result['totalNum'] = $totalNum;
  496. $tIds = $deleteThreads = $updateThreadIds = $otherLogs = $ids = array();
  497. $unDeleteThreads = array();
  498. $threads = array();
  499. $otherActions = array('mergeforum', 'banuser', 'unbanuser', 'deluser', 'delforum');
  500. foreach(C::t('forum_threadlog')->fetch_all_order_by_dateline($num) as $thread) {
  501. $tIds[] = $thread['tid'];
  502. if (in_array($thread['action'], array('delete', 'redelete'))) {
  503. $ids['thread'][] = $thread['tid'];
  504. $deleteThreads[$thread['tid']] = array('tId' => $thread['tid'],
  505. 'action' => $thread['action'],
  506. 'updated' => dgmdate($thread['dateline'], 'Y-m-d H:i:s', 8),
  507. );
  508. } elseif (in_array($thread['action'], array('banuser', 'unbanuser', 'deluser'))) {
  509. $ids['user'][] = $thread['uid'];
  510. $expiry = 0;
  511. if ($thread['expiry']) {
  512. $expiry = dgmdate($thread['expiry'], 'Y-m-d H:i:s', 8);
  513. }
  514. $otherLogs[] = array('uId' => $thread['uid'],
  515. 'isDeletePost' => $thread['otherid'],
  516. 'action' => $thread['action'],
  517. 'expiry' => $expiry,
  518. 'updated' => dgmdate($thread['dateline'], 'Y-m-d H:i:s', 8),
  519. );
  520. } elseif (in_array($thread['action'], array('mergeforum', 'delforum'))) {
  521. $ids['forum'][] = $thread['fid'];
  522. $otherLogs[] = array('fId' => $thread['fid'],
  523. 'otherId' => $thread['otherid'],
  524. 'action' => $thread['action'],
  525. 'updated' => dgmdate($thread['dateline'], 'Y-m-d H:i:s', 8),
  526. );
  527. } elseif (in_array($thread['action'], array('merge'))) {
  528. $ids['thread'][] = $thread['tid'];
  529. $otherLogs[] = array('tId' => $thread['tid'],
  530. 'fId' => $thread['fId'],
  531. 'otherId' => $thread['otherid'],
  532. 'action' => $thread['action'],
  533. 'updated' => dgmdate($thread['dateline'], 'Y-m-d H:i:s', 8),
  534. );
  535. } else {
  536. $ids['thread'][] = $thread['tid'];
  537. $unDeleteThreads[$thread['tid']] = array('tId' => $thread['tid'],
  538. 'action' => $thread['action'],
  539. 'otherId' => $thread['otherid'],
  540. 'updated' => dgmdate($thread['dateline'], 'Y-m-d H:i:s', 8),
  541. );
  542. }
  543. }
  544. if ($tIds) {
  545. if ($unDeleteThreads) {
  546. $vtIds = $gfIds = array(); // poll, isPublic
  547. $threads = Cloud_Service_SearchHelper::getThreads(array_keys($unDeleteThreads));
  548. foreach($unDeleteThreads as $tId => $updateThread) {
  549. $vtIds[] = $tId;
  550. if ($threads[$tId]) {
  551. $unDeleteThreads[$tId] = array_merge($threads[$tId], $updateThread);
  552. } else {
  553. $unDeleteThreads[$tId]['tId'] = 0;
  554. }
  555. if ($threads[$tId]['isGroup']) {
  556. $gfIds[$threads[$tId]['fId']] = $threads[$tId]['fId'];
  557. }
  558. }
  559. $polls = Cloud_Service_SearchHelper::getPollInfo($vtIds);
  560. foreach($polls as $tId => $poll) {
  561. $unDeleteThreads[$tId]['pollInfo'] = $poll;
  562. }
  563. $guestPerm = Cloud_Service_SearchHelper::getGuestPerm($gfIds);
  564. foreach($unDeleteThreads as $tId => $row) {
  565. if (in_array($row['fId'], $guestPerm['allowForumIds'])) {
  566. $unDeleteThreads[$tId]['isPublic'] = true;
  567. } else {
  568. $unDeleteThreads[$tId]['isPublic'] = false;
  569. }
  570. }
  571. }
  572. }
  573. $result['data'] = $deleteThreads + $unDeleteThreads + $otherLogs;
  574. $result['ids'] = $ids;
  575. return $result;
  576. }
  577. public function onSearchRemoveThreadLogs($lastThreadIds = array(), $lastForumIds = array(), $lastUserIds = array()) {
  578. if($lastThreadIds) {
  579. C::t('forum_threadlog')->delete_by_tid_fid_uid($lastThreadIds);
  580. }
  581. if($lastForumIds) {
  582. C::t('forum_threadlog')->delete_by_tid_fid_uid(0, $lastForumIds);
  583. }
  584. if($lastUserIds) {
  585. C::t('forum_threadlog')->delete_by_tid_fid_uid(0, array(), $lastUserIds);
  586. }
  587. return true;
  588. }
  589. protected function _getThread($tId) {
  590. $result = Cloud_Service_SearchHelper::getThreads(array($tId));
  591. return $result[$tId];
  592. }
  593. public function onSearchGetThreads($tIds) {
  594. $authors = $authorids = array();
  595. $result = Cloud_Service_SearchHelper::getThreads($tIds);
  596. if ($result) {
  597. $vtIds = $gfIds = array();
  598. foreach($result as $key => $thread) {
  599. $authors[$thread['authorId']][] = $thread['tId'];
  600. if ($thread['specialType'] == 'poll') {
  601. $vtIds[] = $thread['tId'];
  602. }
  603. if ($thread['isGroup'] ) {
  604. $gfIds[$thread['fId']] = $thread['fId'];
  605. }
  606. }
  607. $guestPerm = Cloud_Service_SearchHelper::getGuestPerm($gfIds);
  608. foreach($result as $key => $row) {
  609. if (in_array($row['fId'], $guestPerm['allowForumIds'])) {
  610. $result[$key]['isPublic'] = true;
  611. } else {
  612. $result[$key]['isPublic'] = false;
  613. }
  614. }
  615. }
  616. if ($vtIds) { // vote
  617. $polls = Cloud_Service_SearchHelper::getPollInfo($vtIds);
  618. foreach($polls as $tId => $poll) {
  619. $result[$tId]['pollInfo'] = $poll;
  620. }
  621. }
  622. $authorids = array_keys($authors);
  623. if ($authorids) {
  624. $banuids= $uids = array();
  625. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  626. $uids[$uid] = $uid;
  627. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  628. $banuids[] = $author['uid'];
  629. }
  630. }
  631. $deluids = array_diff($authorids, $uids);
  632. foreach($deluids as $deluid) {
  633. if (!$deluid) {
  634. continue;
  635. }
  636. foreach($authors[$deluid] as $tid) {
  637. $result[$tid]['authorStatus'] = 'delete';
  638. }
  639. }
  640. foreach($banuids as $banuid) {
  641. foreach($authors[$banuid] as $tid) {
  642. $result[$tid]['authorStatus'] = 'ban';
  643. }
  644. }
  645. }
  646. return $result;
  647. }
  648. protected function _getNewThreads($tableid, $num, $fromThreadId = 0) {
  649. $result = array();
  650. foreach(C::t('forum_thread')->fetch_all_new_thread_by_tid($fromThreadId, 0, $num, $tableid) as $thread) {
  651. $result['maxTid'] = $thread['tid'];
  652. $result['data'][$thread['tid']] = Cloud_Service_SearchHelper::convertThread($thread);
  653. }
  654. return $result;
  655. }
  656. public function onSearchGetNewThreads($num, $tId = 0) {
  657. $res = $data = $_tableInfo = array();
  658. $maxTid = 0;
  659. foreach(Cloud_Service_SearchHelper::getTables('thread') as $tableid) {
  660. $_threads = $this->_getNewThreads($tableid, $num, $tId);
  661. if ($_threads['data']) {
  662. if (!$data) {
  663. $data = $_threads['data'];
  664. } else {
  665. $data = $data + $_threads['data'];
  666. }
  667. }
  668. if ($maxTid < $_threads['maxTid']) {
  669. $maxTid = $_threads['maxTid'];
  670. }
  671. $_tableInfo['maxTids'][] = array('current_index' => $i,
  672. 'maxTid' => $_threads['maxTid'],
  673. );
  674. }
  675. $_threadNum = 0;
  676. if ($maxTid) {
  677. ksort($data);
  678. foreach($data as $k => $v) {
  679. $_threadNum++;
  680. $res['maxTid'] = $k;
  681. $res['data'][$k] = $v;
  682. if ($_threadNum == $num) {
  683. break;
  684. }
  685. }
  686. if (!$res['maxTid']) {
  687. $res['maxTid'] = $maxTid;
  688. }
  689. }
  690. if ($res['data']) {
  691. $_tableInfo['tables'] = $tables;
  692. $postThreadIds = $authors = array();
  693. foreach($res['data'] as $tId => $thread) {
  694. $authors[$thread['authorId']][] = $thread['tId'];
  695. $postThreadIds[$thread['postTableId']][] = $thread['tId'];
  696. }
  697. $threadPosts = Cloud_Service_SearchHelper::getThreadPosts($postThreadIds);
  698. foreach($res['data'] as $tId => $v) {
  699. $res['data'][$tId]['pId'] = $threadPosts[$tId]['pId'];
  700. }
  701. $authorids = array_keys($authors);
  702. if ($authorids) {
  703. $banuids= $uids = array();
  704. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  705. $uids[$uid] = $uid;
  706. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  707. $banuids[] = $author['uid'];
  708. }
  709. }
  710. $deluids = array_diff($authorids, $uids);
  711. foreach($deluids as $deluid) {
  712. if (!$deluid) {
  713. continue;
  714. }
  715. foreach($authors[$deluid] as $tid) {
  716. $res['data'][$tid]['authorStatus'] = 'delete';
  717. }
  718. }
  719. foreach($banuids as $banuid) {
  720. foreach($authors[$banuid] as $tid) {
  721. $res['data'][$tid]['authorStatus'] = 'ban';
  722. }
  723. }
  724. }
  725. }
  726. return $res;
  727. }
  728. protected function _getAllThreads($tableid, $num, $tid = 0, $orderType = 'ASC') {
  729. $result = array();
  730. $orderType = strtoupper($orderType) == 'DESC' ? 'DESC' : 'ASC';
  731. $glue = ($orderType == 'DESC') ? '<' : '>';
  732. $key = ($orderType == 'DESC') ? 'minTid' : 'maxTid';
  733. $tIds = $vtIds = array();
  734. foreach(C::t('forum_thread')->fetch_all_new_thread_by_tid($tid, 0, $num, $tableid, $glue, $orderType) as $thread) {
  735. $result[$key] = $thread['tid'];
  736. $result['data'][$thread['tid']] = Cloud_Service_SearchHelper::convertThread($thread);
  737. if ($result['data'][$thread['tid']]['specialType'] == 'poll') {
  738. $vtIds[] = $thread['tid'];
  739. }
  740. }
  741. if(!empty($vtIds)) {
  742. $polls = Cloud_Service_SearchHelper::getPollInfo($vtIds);
  743. foreach($polls as $tId => $poll) {
  744. $result['data'][$tId]['pollInfo'] = $poll;
  745. }
  746. }
  747. return $result;
  748. }
  749. public function onSearchGetAllThreads($num, $tId = 0, $orderType = 'ASC') {
  750. $orderType = strtoupper($orderType);
  751. $res = $data = $_tableInfo = array();
  752. $minTid = $maxTid = 0;
  753. foreach(Cloud_Service_SearchHelper::getTables('thread') as $tableid) {
  754. $_threads = $this->_getAllThreads($tableid, $num, $tId, $orderType);
  755. if ($_threads['data']) {
  756. if (!$data) {
  757. $data = $_threads['data'];
  758. } else {
  759. $data = $data + $_threads['data'];
  760. }
  761. }
  762. if ($orderType == 'DESC') {
  763. if (!$minTid) {
  764. $minTid = $_threads['minTid'];
  765. }
  766. if ($minTid > $_threads['minTid']) {
  767. $minTid = $_threads['minTid'];
  768. }
  769. $_tableInfo['minTids'][] = array('current_index' => $i,
  770. 'minTid' => $_threads['minTid'],
  771. );
  772. } else {
  773. if ($maxTid < $_threads['maxTid']) {
  774. $maxTid = $_threads['maxTid'];
  775. }
  776. $_tableInfo['maxTids'][] = array('current_index' => $i,
  777. 'maxTid' => $_threads['maxTid'],
  778. );
  779. }
  780. }
  781. $_threadNum = 0;
  782. if ($orderType == 'DESC') {
  783. if ($minTid) {
  784. krsort($data);
  785. foreach($data as $k => $v) {
  786. $_threadNum++;
  787. $res['minTid'] = $k;
  788. $res['data'][$k] = $v;
  789. if ($_threadNum == $num) {
  790. break;
  791. }
  792. }
  793. if (!$res['minTid']) {
  794. $res['minTid'] = $minTid;
  795. }
  796. }
  797. } else {
  798. if ($maxTid) {
  799. ksort($data);
  800. foreach($data as $k => $v) {
  801. $_threadNum++;
  802. $res['data'][$k] = $v;
  803. $res['maxTid'] = $k;
  804. if ($_threadNum == $num) {
  805. break;
  806. }
  807. }
  808. if (!$res['maxTid']) {
  809. $res['maxTid'] = $maxTid;
  810. }
  811. }
  812. }
  813. if ($res['data']) {
  814. $_tableInfo['tables'] = $tables;
  815. $_tIds = array();
  816. $authors = $gfIds = array();
  817. foreach($res['data'] as $tId => $thread) {
  818. $_tIds[$thread['postTableId']][] = $tId;
  819. $authors[$thread['authorId']][] = $thread['tId'];
  820. if ($thread['isGroup']) {
  821. $gfIds[$thread['fId']] = $thread['fId'];
  822. }
  823. }
  824. if ($_tIds) {
  825. $guestPerm = Cloud_Service_SearchHelper::getGuestPerm($gfIds); // GuestPerm
  826. $threadPosts = Cloud_Service_SearchHelper::getThreadPosts($_tIds);
  827. foreach($res['data'] as $tId => $v) {
  828. $res['data'][$tId]['pId'] = $threadPosts[$tId]['pId'];
  829. if (in_array($v['fId'], $guestPerm['allowForumIds'])) {
  830. $res['data'][$tId]['isPublic'] = true;
  831. } else {
  832. $res['data'][$tId]['isPublic'] = false;
  833. }
  834. }
  835. }
  836. $authorids = array_keys($authors);
  837. if ($authorids) {
  838. $banuids= $uids = array();
  839. foreach(C::t('common_member')->fetch_all($authorids) as $uid => $author) {
  840. $uids[$uid] = $uid;
  841. if ($author['groupid'] == 4 || $author['groupid'] == 5) {
  842. $banuids[] = $author['uid'];
  843. }
  844. }
  845. $deluids = array_diff($authorids, $uids);
  846. foreach($deluids as $deluid) {
  847. if (!$deluid) {
  848. continue;
  849. }
  850. foreach($authors[$deluid] as $tid) {
  851. $res['data'][$tid]['authorStatus'] = 'delete';
  852. }
  853. }
  854. foreach($banuids as $banuid) {
  855. foreach($authors[$banuid] as $tid) {
  856. $res['data'][$tid]['authorStatus'] = 'ban';
  857. }
  858. }
  859. }
  860. }
  861. return $res;
  862. }
  863. public function onSearchGetForums($fIds = array()) {
  864. return Cloud_Service_SearchHelper::getForums($fIds);
  865. }
  866. public function onSearchSetConfig($data) {
  867. global $_G;
  868. $searchData = $_G['setting']['my_search_data'];
  869. if (!is_array($searchData)) {
  870. $searchData = array();
  871. }
  872. $settings = array();
  873. foreach($data as $k => $v) {
  874. if (substr($k, 0, strlen('hotWordChangedFId_')) == 'hotWordChangedFId_') {
  875. $hotWordChangedFId = dintval(substr($k, strlen('hotWordChangedFId_')));
  876. C::t('common_syscache')->delete('search_recommend_words_' . $hotWordChangedFId);
  877. continue;
  878. }
  879. if ($k == 'showDiscuzSearch' && $v) {
  880. $status = $v == 1 ? 1 : 0;
  881. $searchSetting = C::t('common_setting')->fetch('search', true);
  882. $searchSetting['forum']['status'] = $status;
  883. $settings['search'] = $searchSetting;
  884. continue;
  885. }
  886. $searchData[$k] = $v;
  887. }
  888. $settings['my_search_data'] = $searchData;
  889. C::t('common_setting')->update_batch($settings);
  890. require_once DISCUZ_ROOT . './source/function/function_cache.php';
  891. updatecache('setting');
  892. return true;
  893. }
  894. public function onSearchGetConfig($keys) {
  895. global $_G;
  896. $maps = array(
  897. 'hotWords' => 'srchhotkeywords',
  898. 'maxThreadPostId' => 'NON-SETTING',
  899. 'rewrite' => 'rewrite',
  900. 'domain' => 'domain',
  901. 'mySearchData' => 'my_search_data',
  902. );
  903. $confs = array();
  904. foreach($keys as $key) {
  905. if ($fieldName = $maps[$key]) {
  906. if ($key == 'maxThreadPostId') {
  907. $confs[$key] = $this->_getMaxDataItem();
  908. continue;
  909. }
  910. if ($key == 'domain') {
  911. $conf = array();
  912. if ($_G['setting']['domain']) {
  913. if ($_G['setting']['domain']['list']) {
  914. foreach($_G['setting']['domain']['list'] as $k => $v) {
  915. $conf['subDomain'][$k]['id'] = $v['id'];
  916. $conf['subDomain'][$k]['type'] = $v['idtype'];
  917. }
  918. }
  919. $conf['moduleDomain'] = $_G['setting']['domain']['app'];
  920. }
  921. $confs[$key] = $conf;
  922. continue;
  923. }
  924. if ($key == 'rewrite') {
  925. $conf = array();
  926. if ($_G['setting']['rewritestatus'] && $_G['setting']['rewriterule']) {
  927. $conf['compatible'] = $_G['setting']['rewritecompatible'] ? true : false;
  928. foreach($_G['setting']['rewriterule'] as $mod => $rule) {
  929. $conf['modules'][$mod]['rule'] = $rule;
  930. if (in_array($mod, $_G['setting']['rewritestatus'])) {
  931. $conf['modules'][$mod]['status'] = true;
  932. } else {
  933. $conf['modules'][$mod]['status'] = false;
  934. }
  935. }
  936. }
  937. $confs[$key] = $conf;
  938. continue;
  939. }
  940. $confs[$key] = $_G['setting'][$fieldName];
  941. }
  942. }
  943. return $confs;
  944. }
  945. public function onSearchSetHotWords($data, $method = 'append', $limit = 0) {
  946. global $_G;
  947. $srchhotkeywords = array();
  948. if ($_G['setting']['srchhotkeywords']) {
  949. $srchhotkeywords = $_G['setting']['srchhotkeywords'];
  950. }
  951. $newHotWords = array();
  952. foreach($data as $k => $v) {
  953. $newHotWords[] = $v;
  954. }
  955. switch ($method) {
  956. case 'overwrite':
  957. $hotWords = $newHotWords;
  958. break;
  959. case 'prepend':
  960. $hotWords = array_merge($newHotWords, $srchhotkeywords);
  961. break;
  962. case 'append':
  963. $hotWords = array_merge($srchhotkeywords, $newHotWords);
  964. break;
  965. }
  966. if ($limit) {
  967. $hotWords = array_slice($hotWords, 0, $limit);
  968. }
  969. $hotWords = array_unique($hotWords);
  970. $hotWords = implode("\n", $hotWords);
  971. C::t('common_setting')->update('srchhotkeywords', $hotWords);
  972. require_once DISCUZ_ROOT . './source/function/function_cache.php';
  973. updatecache('setting');
  974. return true;
  975. }
  976. public function _getMaxDataItem() {
  977. $threadTableInfo = C::t('forum_thread')->gettablestatus();
  978. $maxTId = $threadTableInfo['Auto_increment'] - 1;
  979. $maxPId = C::t('forum_post_tableid')->fetch_max_id();
  980. $maxPId = intval($maxPId);
  981. return array('maxThreadId' => $maxTId, 'maxPostId' => $maxPId);
  982. }
  983. }