protected function _render(array $widget, $positionCode, array $params, XenForo_Template_Abstract $template)
 {
     $threads = array();
     if (isset($params['thread']) and !empty($params['thread']['thread_id'])) {
         // $thread is found in the template params, try to fetch the tags
         $tags = Tinhte_XenTag_Helper::unserialize($params['thread'][Tinhte_XenTag_Constants::FIELD_THREAD_TAGS]);
     }
     if (empty($tags) and isset($params['page']) and !empty($params['page']['node_id'])) {
         // fetch page's tags
         $tags = Tinhte_XenTag_Helper::unserialize($params['page'][Tinhte_XenTag_Constants::FIELD_PAGE_TAGS]);
     }
     if (empty($tags) and isset($params['forum']) and !empty($params['forum']['node_id'])) {
         // fetch forum's tags
         $tags = Tinhte_XenTag_Helper::unserialize($params['forum'][Tinhte_XenTag_Constants::FIELD_FORUM_TAGS]);
     }
     if (empty($tags) and isset($params['resource']) and !empty($params['resource']['resource_id'])) {
         // fetch resource's tags
         $tags = Tinhte_XenTag_Helper::unserialize($params['resource'][Tinhte_XenTag_Constants::FIELD_RESOURCE_TAGS]);
     }
     if (!empty($tags)) {
         $core = WidgetFramework_Core::getInstance();
         /* @var $searchModel XenForo_Model_Search */
         $searchModel = $core->getModelFromCache('XenForo_Model_Search');
         $tagConstraint = implode(' ', array_keys($tags));
         $constraints = array(Tinhte_XenTag_Constants::SEARCH_CONSTRAINT_TAGS => $tagConstraint);
         $searcher = new XenForo_Search_Searcher($searchModel);
         $searchQuery = '';
         $order = 'date';
         $typeHandler = $searchModel->getSearchDataHandler('thread');
         $results = $searcher->searchType($typeHandler, $searchQuery, $constraints, $order, false, $widget['options']['limit'] * 10);
         if (!empty($results)) {
             $threadIds = array();
             foreach ($results as $result) {
                 if ($result[0] === 'thread') {
                     $threadIds[] = $result[1];
                 }
             }
             $threadIds = array_unique($threadIds);
             $forumIds = $this->_helperGetForumIdsFromOption(array(), $params, empty($widget['options']['as_guest']) ? false : true);
             $conditions = array('node_id' => $forumIds, Tinhte_XenTag_XenForo_Model_Thread::CONDITIONS_THREAD_ID => $threadIds, 'deleted' => false, 'moderated' => false);
             $fetchOptions = array('limit' => $widget['options']['limit'], 'join' => XenForo_Model_Thread::FETCH_AVATAR, 'order' => 'post_date', 'orderDirection' => 'desc');
             /* @var $threadModel XenForo_Model_Thread */
             $threadModel = $core->getModelFromCache('XenForo_Model_Thread');
             $threads = $threadModel->getThreads($conditions, $fetchOptions);
         }
     }
     $template->setParam('threads', $threads);
     return $template->render();
 }
Esempio n. 2
0
 public function prioritizeResults(array &$results, XenForo_Search_Searcher $searcher, $searchQuery, array $constraints = array(), $order = 'relevance')
 {
     // prioritize contents by doing a second search for prioritized contents
     // this may cause performance issue but shouldn't make a big impact
     // admin can easily disable the feature in AdminCP
     // it's required to do another search because the original search has its own
     // max results and may not include all prioritized results
     $prioritizedContents = $this->getPrioritizedContents();
     if (empty($prioritizedContents)) {
         // nothing to do
         return;
     }
     $typeHandler = XenForo_Search_DataHandler_Abstract::create('Tinhte_XenTag_Search_DataHandler_General');
     $typeHandler->setSearchContentTypes(array_keys($prioritizedContents));
     $prioritizedResults = $searcher->searchType($typeHandler, $searchQuery, $constraints, $order);
     if (empty($prioritizedResults)) {
         // no prioritized results could be found, do nothing
         return;
     }
     // drop all prioritized results from general results
     foreach (array_keys($results) as $resultKey) {
         if (isset($prioritizedContents[$results[$resultKey][0]])) {
             unset($results[$resultKey]);
         }
     }
     // append priorizied results
     asort($prioritizedContents);
     $prioritizedResultsKeys = array_reverse(array_keys($prioritizedResults));
     foreach ($prioritizedContents as $contentType => $displayOrder) {
         foreach ($prioritizedResultsKeys as $prioritizedResultKey) {
             if ($prioritizedResults[$prioritizedResultKey][0] == $contentType) {
                 array_unshift($results, $prioritizedResults[$prioritizedResultKey]);
             }
         }
     }
     // normalized array indeces
     $results = array_values($results);
 }
Esempio n. 3
0
 /**
  * Reruns the given search with higher maximum.
  * If errors occur, a response exception will be thrown.
  *
  * @param array $search Search info (does not need search_id, constraints,
  * results, or warnings)
  * @param array $constraints Array of search constraints
  *
  * @return array New search
  */
 public function runExtendedSearch(array $search, array $constraints)
 {
     if (!XenForo_Visitor::getInstance()->canSearch()) {
         throw $this->getNoPermissionResponseException();
     }
     $visitorUserId = XenForo_Visitor::getUserId();
     $searchModel = $this->_getSearchModel();
     $typeHandler = null;
     if ($search['search_type']) {
         $typeHandler = $searchModel->getSearchDataHandler($search['search_type']);
     }
     $searcher = new XenForo_Search_Searcher($searchModel);
     $maxResults = XenForo_Application::get('options')->th_searchAndExport_maximumSearchResults;
     if ($typeHandler) {
         $results = $searcher->searchType($typeHandler, $search['search_query'], $constraints, $search['search_order'], $search['search_grouping'], $maxResults);
     } else {
         $search['search_type'] = '';
         $results = $searcher->searchGeneral($search['search_query'], $constraints, $search['search_order'], $maxResults);
     }
     if (!$results) {
         throw $this->responseException($this->getNoSearchResultsResponse($searcher));
     }
     $constraints['extended'] = true;
     return $searchModel->insertSearch($results, $search['search_type'], $search['search_query'], $constraints, $search['search_order'], $search['search_grouping'], array(), $searcher->getWarnings(), $visitorUserId);
 }
Esempio n. 4
0
 public function _doSearch($contentType = '', array $constraints = array())
 {
     if (!XenForo_Visitor::getInstance()->canSearch()) {
         throw $this->getNoPermissionResponseException();
     }
     $input = array('order' => 'relevance', 'group_discussion' => false);
     $input['keywords'] = $this->_input->filterSingle('q', XenForo_Input::STRING);
     $input['keywords'] = XenForo_Helper_String::censorString($input['keywords'], null, '');
     // don't allow searching of censored stuff
     if (empty($input['keywords'])) {
         throw $this->responseException($this->responseError(new XenForo_Phrase('bdapi_slash_search_requires_q'), 400));
     }
     $maxResults = XenForo_Application::getOptions()->get('maximumSearchResults');
     switch ($contentType) {
         case 'thread':
         case 'post':
             // only these two legacy content types support `limit` param while searching
             // others use `limit` to control how many pieces of data are returned
             $limit = $this->_input->filterSingle('limit', XenForo_Input::UINT);
             if ($limit > 0) {
                 $maxResults = min($maxResults, $limit);
             }
     }
     $forumId = $this->_input->filterSingle('forum_id', XenForo_Input::UINT);
     if (!empty($forumId)) {
         $childNodeIds = array_keys($this->_getNodeModel()->getChildNodesForNodeIds(array($forumId)));
         $nodeIds = array_unique(array_merge(array($forumId), $childNodeIds));
         $constraints['node'] = implode(' ', $nodeIds);
         if (!$constraints['node']) {
             unset($constraints['node']);
         }
     }
     $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
     if (!empty($userId)) {
         $constraints['user'] = array($userId);
     }
     $searchModel = $this->_getSearchModel();
     $searcher = new XenForo_Search_Searcher($searchModel);
     if (!empty($contentType)) {
         // content type searching
         $typeHandler = $searchModel->getSearchDataHandler($contentType);
         if (empty($typeHandler)) {
             throw new XenForo_Exception(sprintf('No search data handler found for content type %s', $contentType));
         }
         $results = $searcher->searchType($typeHandler, $input['keywords'], $constraints, $input['order'], $input['group_discussion'], $maxResults);
     } else {
         // general searching
         $results = $searcher->searchGeneral($input['keywords'], $constraints, $input['order'], $maxResults);
     }
     $search = $searchModel->insertSearch($results, $contentType, $input['keywords'], $constraints, $input['order'], $input['group_discussion']);
     return $search;
 }
Esempio n. 5
0
 /**
  * Reruns the given search. If errors occur, a response exception will be thrown.
  *
  * @param array $search Search info (does not need search_id, constraints, results, or warnings)
  * @param array $constraints Array of search constraints
  *
  * @return array New search
  */
 public function rerunSearch(array $search, array $constraints)
 {
     if (!XenForo_Visitor::getInstance()->canSearch()) {
         throw $this->getNoPermissionResponseException();
     }
     $visitorUserId = XenForo_Visitor::getUserId();
     $searchModel = $this->_getSearchModel();
     $existingSearch = $searchModel->getExistingSearch($search['search_type'], $search['search_query'], $constraints, $search['search_order'], $search['search_grouping'], $visitorUserId);
     if ($existingSearch) {
         return $existingSearch;
     }
     $typeHandler = null;
     if ($search['search_type']) {
         $typeHandler = $searchModel->getSearchDataHandler($search['search_type']);
     }
     $searcher = new XenForo_Search_Searcher($searchModel);
     $userResults = array();
     if ($typeHandler) {
         $results = $searcher->searchType($typeHandler, $search['search_query'], $constraints, $search['search_order'], $search['search_grouping']);
         $userResults = array();
     } else {
         $search['search_type'] = '';
         $results = $searcher->searchGeneral($search['search_query'], $constraints, $search['search_order']);
         $userResults = $this->_getUserSearch($search['search_query']);
     }
     if (!$results && !$userResults) {
         throw $this->responseException($this->getNoSearchResultsResponse($searcher));
     }
     return $searchModel->insertSearch($results, $search['search_type'], $search['search_query'], $constraints, $search['search_order'], $search['search_grouping'], $userResults, $searcher->getWarnings(), $visitorUserId);
 }
Esempio n. 6
0
 public function actionSearch()
 {
     $visitor = XenForo_Visitor::getInstance()->toArray();
     $visitor_userid = XenForo_Visitor::getUserId();
     $search_model = $this->_getSearchModel();
     $node_model = $this->_getNodeModel();
     $user_model = $this->getModelFromCache('XenForo_Model_User');
     $vals = $this->_input->filter(array('userid' => XenForo_Input::UINT, 'showposts' => XenForo_Input::UINT, 'searchdate' => XenForo_Input::UINT, 'query' => XenForo_Input::STRING, 'searchuser' => XenForo_Input::STRING, 'sortby' => XenForo_Input::STRING, 'starteronly' => XenForo_Input::UINT, 'titleonly' => XenForo_Input::UINT));
     // Munge FR search constrants into XenForo search constraints
     $xf_input = array();
     if ($vals['sortby'] == 'replycount') {
         $xf_input['order'] = 'replies';
     } else {
         $xf_input['order'] = 'date';
     }
     if ($vals['showposts'] == 1) {
         $xf_input['group_discussion'] = 0;
     } else {
         $xf_input['group_discussion'] = 1;
     }
     if ($vals['starteronly']) {
         $xf_input['user_content'] = 'thread';
     }
     if ($vals['searchdate']) {
         $xf_input['date'] = date('Y-m-d', XenForo_Application::$time - $vals['searchdate'] * 86400);
     }
     $exclude = XenForo_Application::get('options')->forumrunnerExcludeForums;
     if (!$exclude) {
         $exclude = array();
     }
     $forums = $node_model->getViewableNodeList(null, true);
     foreach ($exclude as $remove) {
         fr_remove_node_and_children($forums, $remove);
     }
     $xf_input['nodes'] = array_keys($forums);
     $query = $vals['query'];
     $vals['query'] = XenForo_Helper_String::censorString($vals['query'], null, '');
     if ($vals['userid']) {
         // Look up Username
         $user = $user_model->getUserById($vals['userid']);
         if ($user) {
             $vals['searchuser'] = $user['username'];
         }
     }
     $xf_input += array('users' => $vals['searchuser'], 'keywords' => $vals['query'], 'title_only' => $vals['titleonly'] ? 1 : 0);
     $cons = $search_model->getGeneralConstraintsFromInput($xf_input, $errors);
     if ($errors) {
         return $this->sendError($errors[0]);
     }
     if ($xf_input['keywords'] == '' && empty($cons['user'])) {
         return $this->sendError(new XenForo_Phrase('please_specify_search_query_or_name_of_member'));
     }
     $post_handler = $search_model->getSearchDataHandler('post');
     $search = $search_model->getExistingSearch('post', $xf_input['keywords'], $cons, $xf_input['order'], $xf_input['group_discussion'], $visitor_userid);
     if (!$search) {
         $searcher = new XenForo_Search_Searcher($search_model);
         if ($post_handler) {
             $results = $searcher->searchType($post_handler, $xf_input['keywords'], $cons, $xf_input['order'], $xf_input['group_discussion']);
         } else {
             $results = $searcher->searchGeneral($xf_input['keywords'], $cons, $xf_input['order']);
         }
         if (!$results) {
             $errors = $searcher->getErrors();
             if ($errors) {
                 $errors = array_values($errors);
                 return $this->sendError($errors[0]);
             }
             return $this->sendError(new XenForo_Phrase('no_results_found'));
         }
         $search = $search_model->insertSearch($results, 'post', $query, $cons, $xf_input['order'], $xf_input['group_discussion'], $searcher->getWarnings(), $visitor_userid);
     }
     return $this->processSearch($search);
 }
Esempio n. 7
0
 protected function _doSearch($tagText)
 {
     $visitorUserId = XenForo_Visitor::getUserId();
     /* @var $searchModel XenForo_Model_Search */
     $searchModel = $this->getModelFromCache('XenForo_Model_Search');
     /* @var $tagSearchModel Tinhte_XenTag_Model_Search */
     $tagSearchModel = $this->getModelFromCache('Tinhte_XenTag_Model_Search');
     $input = array('type' => Tinhte_XenTag_Constants::SEARCH_TYPE_TAG, 'keywords' => '', Tinhte_XenTag_Constants::SEARCH_INPUT_TAGS => $tagText, 'order' => 'date', 'group_discussion' => 0);
     $constraints = $searchModel->getGeneralConstraintsFromInput($input, $errors);
     if ($errors) {
         return $this->responseError($errors);
     }
     $forceRefresh = $this->_input->filterSingle('force_refresh', XenForo_Input::UINT) > 0;
     if ($forceRefresh) {
         if (XenForo_Visitor::getInstance()->get('isTrusted')) {
             // good, this is a trusted user (admin or mod)
         } else {
             // do not accept force refresh request from not-trusted users
             $forceRefresh = false;
         }
     }
     if ($forceRefresh == false) {
         // force to use cache to have a nice and clean url
         $search = $searchModel->getExistingSearch($input['type'], $input['keywords'], $constraints, $input['order'], $input['group_discussion'], $visitorUserId, Tinhte_XenTag_Option::get('searchForceUseCache'));
     } else {
         // skip getting existing results, this will cause a real search to be made
         $search = false;
     }
     if (empty($search)) {
         $searcher = new XenForo_Search_Searcher($searchModel);
         $typeHandler = XenForo_Search_DataHandler_Abstract::create('Tinhte_XenTag_Search_DataHandler_General');
         $results = $searcher->searchType($typeHandler, $input['keywords'], $constraints, $input['order']);
         if (empty($results)) {
             return $this->_getNoResultsResponse($tagText);
         } else {
             $tagSearchModel->prioritizeResults($results, $searcher, $input['keywords'], $constraints, $input['order']);
         }
         $warnings = $searcher->getErrors() + $searcher->getWarnings();
         $search = $searchModel->insertSearch($results, $input['type'], $input['keywords'], $constraints, $input['order'], $input['group_discussion'], array(), $warnings, $visitorUserId);
     }
     return $search;
 }