示例#1
0
/**
 * Search and score items
 *
 * @param string original search term
 * @param array all separated words from the search term
 * @param array all quoted parts from the search term
 * @param number max possible score
 */
function search_and_score_items($search_term, $keywords, $quoted_parts)
{
    global $DB, $Blog, $posttypes_perms;
    // Exclude from search: 'sidebar' type posts and from reserved type with ID 5000
    $filter_post_types = isset($posttypes_perms['sidebar']) ? $posttypes_perms['sidebar'] : array();
    $filter_post_types = array_merge($filter_post_types, array(5000));
    // Prepare filters:
    $search_ItemList = new ItemList2($Blog, $Blog->get_timestamp_min(), $Blog->get_timestamp_max(), '', 'ItemCache', 'search_item');
    $search_ItemList->set_filters(array('keywords' => $search_term, 'keyword_scope' => 'title,content,tags', 'phrase' => 'OR', 'types' => '-' . implode(',', $filter_post_types), 'orderby' => 'datemodified', 'order' => 'DESC', 'posts' => 1000));
    // Generate query from filters above and count results:
    $search_ItemList->query_init();
    // Make a custom search query:
    $search_query = 'SELECT DISTINCT post_ID, post_datemodified, post_title, post_content, user_login as creator_login, tag_name' . $search_ItemList->ItemQuery->get_from() . ' LEFT JOIN T_users ON post_creator_user_ID = user_ID' . $search_ItemList->ItemQuery->get_where() . $search_ItemList->ItemQuery->get_group_by() . $search_ItemList->ItemQuery->get_order_by() . $search_ItemList->ItemQuery->get_limit();
    // Run query:
    $query_result = $DB->get_results($search_query, OBJECT, 'Search items query');
    // Compute scores:
    $search_result = array();
    foreach ($query_result as $row) {
        $scores_map = array();
        $scores_map['title'] = score_text($row->post_title, $search_term, $keywords, $quoted_parts, 5);
        $scores_map['content'] = score_text($row->post_content, $search_term, $keywords, $quoted_parts);
        $scores_map['tags'] = score_tags($row->tag_name, $search_term, 4);
        if (!empty($search_term) && !empty($row->creator_login) && strpos($row->creator_login, $search_term) !== false) {
            $scores_map['creator_login'] = 5;
        }
        $scores_map['last_mod_date'] = score_date($row->post_datemodified);
        $final_score = $scores_map['title']['score'] + $scores_map['content']['score'] + $scores_map['tags']['score'] + (isset($scores_map['creator_login']) ? $scores_map['creator_login'] : 0) + $scores_map['last_mod_date'];
        $search_result[] = array('type' => 'item', 'score' => $final_score, 'ID' => $row->post_ID, 'scores_map' => $scores_map);
    }
    return $search_result;
}
示例#2
0
 /**
  * Display a button to view the Recent/New Topics
  */
 function display_button_recent_topics()
 {
     global $Blog;
     if (!is_logged_in() || !$Blog->get_setting('track_unread_content')) {
         // For not logged in users AND if the tracking of unread content is turned off for the collection
         $btn_class = 'btn-info';
         $btn_title = T_('Recent Topics');
     } else {
         // For logged in users:
         global $current_User, $DB, $localtimenow;
         // Initialize SQL query to get only the posts which are displayed by global $MainList on disp=posts:
         $ItemList2 = new ItemList2($Blog, $Blog->get_timestamp_min(), $Blog->get_timestamp_max(), NULL, 'ItemCache', 'recent_topics');
         $ItemList2->set_default_filters(array('unit' => 'all'));
         $ItemList2->query_init();
         // Get a count of the unread topics for current user:
         $unread_posts_SQL = new SQL();
         $unread_posts_SQL->SELECT('COUNT( post_ID )');
         $unread_posts_SQL->FROM('T_items__item');
         $unread_posts_SQL->FROM_add('LEFT JOIN T_users__postreadstatus ON post_ID = uprs_post_ID AND uprs_user_ID = ' . $DB->quote($current_User->ID));
         $unread_posts_SQL->FROM_add('INNER JOIN T_categories ON post_main_cat_ID = cat_ID');
         $unread_posts_SQL->WHERE($ItemList2->ItemQuery->get_where(''));
         $unread_posts_SQL->WHERE_and('post_last_touched_ts > ' . $DB->quote(date2mysql($localtimenow - 30 * 86400)));
         // In theory, it would be more safe to use this comparison:
         // $unread_posts_SQL->WHERE_and( 'uprs_post_ID IS NULL OR uprs_read_post_ts <= post_last_touched_ts' );
         // But until we have milli- or micro-second precision on timestamps, we decided it was a better trade-off to never see our own edits as unread. So we use:
         $unread_posts_SQL->WHERE_and('uprs_post_ID IS NULL OR uprs_read_post_ts < post_last_touched_ts');
         // Execute a query with to know if current user has new data to view:
         $unread_posts_count = $DB->get_var($unread_posts_SQL->get(), 0, NULL, 'Get a count of the unread topics for current user');
         if ($unread_posts_count > 0) {
             // If at least one new unread topic exists
             $btn_class = 'btn-warning';
             $btn_title = T_('New Topics') . ' <span class="badge">' . $unread_posts_count . '</span>';
         } else {
             // Current user already have read all topics
             $btn_class = 'btn-info';
             $btn_title = T_('Recent Topics');
         }
     }
     // Print out the button:
     echo '<a href="' . $Blog->get('recentpostsurl') . '" class="btn ' . $btn_class . ' pull-right btn_recent_topics">' . $btn_title . '</a>';
 }