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