Example #1
0
 /**
  * Kicks off the appropriate search(es)
  *
  * Initiates the search engine and returns HTML formatted
  * results. It also provides support to plugins using a
  * search API.
  *
  * @author Sami Barakat <s.m.barakat AT gmail DOT com>
  * @access public
  * @return string HTML output for search results
  *
  */
 function doSearch()
 {
     global $_CONF, $LANG01, $LANG09, $LANG31, $_TABLES, $_USER;
     $debug_info = '';
     $retval = '';
     $list_top = '';
     // Verify current user can perform requested search
     if (!$this->_isSearchAllowed()) {
         return $this->_getAccessDeniedMessage();
     }
     // Make sure there is a query string
     // Full text searches have a minimum word length of 3 by default
     if (empty($this->_query)) {
         if ((empty($this->_author) || $this->_author == 0) && (empty($this->_type) || $this->_type == 'all') && (empty($this->_topic) || $this->_topic == 'all') && (empty($this->_dateStart) || empty($this->_dateEnd))) {
             $retval = $this->showForm();
             $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div><p>' . $LANG09[41] . '</p>' . LB;
             return $retval;
         }
     } elseif (strlen($this->_query) < 3) {
         $retval = $this->showForm();
         $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div><p>' . $LANG09[41] . '</p>' . LB;
         return $retval;
     }
     // Build the URL strings
     $this->_searchURL = $_CONF['site_url'] . '/search.php?query=' . urlencode($this->_query) . (!empty($this->_keyType) ? '&amp;keyType=' . urlencode($this->_keyType) : '') . (!empty($this->_dateStart) ? '&amp;datestart=' . urlencode($this->_dateStart) : '') . (!empty($this->_dateEnd) ? '&amp;dateend=' . urlencode($this->_dateEnd) : '') . (!empty($this->_topic) ? '&amp;topic=' . urlencode($this->_topic) : '') . (!empty($this->_author) ? '&amp;author=' . urlencode($this->_author) : '') . (!empty($this->_searchDays) ? '&amp;st=' . urlencode($this->_searchDays) : '');
     $url = "{$this->_searchURL}&amp;type={$this->_type}&amp;mode=";
     $obj = new ListFactory($url . 'search', $_CONF['search_limits'], $_CONF['num_search_results']);
     $obj->setField('ID', 'id', false);
     $obj->setField('URL', 'url', false);
     $show_num = $_CONF['search_show_num'];
     $show_type = $_CONF['search_show_type'];
     $show_user = $_CONF['search_show_user'];
     $show_hits = $_CONF['search_show_hits'];
     $style = isset($_CONF['search_style']) ? $_CONF['search_style'] : 'google';
     if (!COM_isAnonUser()) {
         $userStyle = DB_getItem($_TABLES['userprefs'], 'search_result_format', 'uid=' . (int) $_USER['uid']);
         if ($userStyle != '') {
             $style = $userStyle;
         }
     }
     if ($style == 'table') {
         $obj->setStyle('table');
         //             Title        Name           Display     Sort   Format
         $obj->setField($LANG09[62], ROW_NUMBER, $show_num, false, '<b>%d.</b>');
         $obj->setField($LANG09[5], SQL_TITLE, $show_type, true, '<b>%s</b>');
         $obj->setField($LANG09[16], 'title', true, true);
         $obj->setField($LANG09[63], 'description', true, false);
         $obj->setField($LANG09[17], 'date', true, true);
         $obj->setField($LANG09[18], 'uid', $show_user, true);
         $obj->setField($LANG09[50], 'hits', $show_hits, true);
         $this->_wordlength = 7;
     } else {
         if ($style == 'google') {
             $obj->setStyle('inline');
             $obj->setField('', ROW_NUMBER, $show_num, false, '<span style="font-size:larger; font-weight:bold;">%d.</span>');
             $obj->setField($LANG09[16], 'title', true, true, '<span style="font-size:larger; font-weight:bold;">%s</span><br/>');
             $obj->setField('', 'description', true, false, '%s<br/>');
             $obj->setField('', '_html', true, false, '<span style="color:green;">');
             $obj->setField($LANG09[18], 'uid', $show_user, true, $LANG01[104] . ' %s ');
             $obj->setField($LANG09[17], 'date', true, true, $LANG01[36] . ' %s');
             $obj->setField($LANG09[5], SQL_TITLE, $show_type, true, ' - %s');
             $obj->setField($LANG09[50], 'hits', $show_hits, true, ' - %s ' . $LANG09[50]);
             $obj->setField('', '_html', true, false, '</span>');
             $this->_wordlength = 50;
         }
     }
     $obj->setDefaultSort('date');
     $obj->setRowFunction(array($this, 'searchFormatCallBack'));
     // Start search timer
     $searchtimer = new timerobject();
     $searchtimer->setPercision(4);
     $searchtimer->startTimer();
     // Have plugins do their searches
     $page = isset($_REQUEST['page']) ? COM_applyFilter($_REQUEST['page'], true) : 1;
     $result_plugins = PLG_doSearch($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
     $result_plugins_comment = PLG_doSearchComment($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
     $result_plugins = array_merge($result_plugins, $result_plugins_comment);
     // Add core searches
     if ($this->_type == 'all' || $this->_type == 'stories') {
         $result_plugins[] = $this->_searchStories();
     }
     if ($this->_type == 'all' || $this->_type == 'comments') {
         $result_plugins[] = $this->_searchComments();
     }
     // Loop through all plugins separating the new API from the old
     $new_api = 0;
     $old_api = 0;
     $num_results = 0;
     if (!isset($_CONF['search_use_fulltext'])) {
         $_CONF['search_use_fulltext'] = false;
     }
     foreach ($result_plugins as $result) {
         if (is_a($result, 'SearchCriteria')) {
             $debug_info .= $result->getName() . " using APIv2, ";
             $type = $result->getType();
             if ($type == 'sql') {
                 if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '') {
                     $debug_info .= "search using FULLTEXT\n";
                     $sql = $result->getFTSQL();
                 } else {
                     $debug_info .= "search using LIKE\n";
                     $sql = $result->getSQL();
                 }
                 $sql = $this->_convertsql($sql);
                 $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
                 $this->_url_rewrite[$result->getName()] = $result->UrlRewriteEnable() ? true : false;
             } else {
                 if ($type == 'text') {
                     $obj->setQueryText($result->getLabel(), $result->getName(), $this->_query, $result->getNumResults(), $result->getRank());
                 }
             }
             $new_api++;
         } else {
             if (is_a($result, 'Plugin') && $result->num_searchresults != 0) {
                 // Some backwards compatibility
                 $debug_info .= $result->plugin_name . " using APIv1, search using backwards compatibility\n";
                 // Find the column heading names that closely match what we are looking for
                 // There may be issues here on different languages, but this _should_ capture most of the data
                 $col_title = $this->_findColumn($result->searchheading, array($LANG09[16], $LANG31[4], 'Question'));
                 //Title,Subject
                 $col_desc = $this->_findColumn($result->searchheading, array($LANG09[63], 'Answer'));
                 $col_date = $this->_findColumn($result->searchheading, array($LANG09[17]));
                 //'Date','Date Added','Last Updated','Date & Time'
                 $col_user = $this->_findColumn($result->searchheading, array($LANG09[18], 'Submited by'));
                 $col_hits = $this->_findColumn($result->searchheading, array($LANG09[50], $LANG09[23], 'Downloads', 'Clicks'));
                 //'Hits','Views'
                 $col_url = $this->_findColumn($result->searchheading, array('URL'));
                 //'Hits','Views'
                 $label = str_replace($LANG09[59], '', $result->searchlabel);
                 if ($result->num_itemssearched > 0) {
                     $_page = isset($_REQUEST['page']) ? COM_applyFilter($_REQUEST['page'], true) : 1;
                     if (isset($_REQUEST['results'])) {
                         $_per_page = COM_applyFilter($_REQUEST['results'], true);
                     } else {
                         $_per_page = $obj->getPerPage();
                     }
                     $obj->addTotalRank(3);
                     $pp = round(3 / $obj->getTotalRank() * $_per_page);
                     $offset = ($_page - 1) * $pp;
                     $limit = $pp;
                     $obj->addToTotalFound($result->num_itemssearched);
                     $counter = 0;
                     // Extract the results
                     foreach ($result->searchresults as $old_row) {
                         if ($counter >= $offset && $counter <= $offset + $limit) {
                             if ($col_date != -1) {
                                 // Convert the date back to a timestamp
                                 $date = $old_row[$col_date];
                                 $date = substr($date, 0, strpos($date, '@'));
                                 if ($date == '') {
                                     $date = $old_row[$col_date];
                                 } else {
                                     $date = strtotime($date);
                                 }
                             }
                             $api_results = array(SQL_NAME => $result->plugin_name, SQL_TITLE => $label, 'title' => $col_title == -1 ? $_CONF['search_no_data'] : $old_row[$col_title], 'description' => $col_desc == -1 ? $_CONF['search_no_data'] : $old_row[$col_desc], 'date' => $col_date == -1 ? '&nbsp;' : $date, 'uid' => $col_user == -1 ? '' : $old_row[$col_user], 'hits' => $col_hits == -1 ? '0' : str_replace(',', '', $old_row[$col_hits]), 'url' => $old_row[$col_url]);
                             $obj->addResult($api_results);
                         }
                         $counter++;
                     }
                 }
                 $old_api++;
             }
         }
     }
     // Find out how many plugins are on the old/new system
     $debug_info .= "\nAPIv1: {$old_api}\nAPIv2: {$new_api}";
     // Execute the queries
     $results = $obj->ExecuteQueries();
     // Searches are done, stop timer
     $searchtime = $searchtimer->stopTimer();
     $escquery = htmlspecialchars($this->_query);
     if ($this->_keyType == 'any') {
         $searchQuery = str_replace(' ', "</b>' " . $LANG09[57] . " '<b>", $escquery);
         $searchQuery = "<b>'{$searchQuery}'</b>";
     } else {
         if ($this->_keyType == 'all') {
             $searchQuery = str_replace(' ', "</b>' " . $LANG09[56] . " '<b>", $escquery);
             $searchQuery = "<b>'{$searchQuery}'</b>";
         } else {
             $searchQuery = $LANG09[55] . " '<b>{$escquery}</b>'";
         }
     }
     // Clean the query string so that sprintf works as expected
     $searchQuery = str_replace("%", "%%", $searchQuery);
     $searchText = "{$LANG09[25]} {$searchQuery}. ";
     $retval .= $this->showForm();
     if (count($results) == 0) {
         $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div>';
         $retval .= $LANG09[74];
     } else {
         $retval .= $obj->getFormattedOutput($results, $LANG09[11], $list_top, '');
     }
     return $retval;
 }