function relevanssi_recognize_phrases($q)
{
    global $wpdb;
    $phrases = relevanssi_extract_phrases($q);
    if (count($phrases) > 0) {
        $phrase_matches = array();
        foreach ($phrases as $phrase) {
            $phrase = esc_sql($phrase);
            "on" == get_option("relevanssi_index_excerpt") ? $excerpt = " OR post_excerpt LIKE '%{$phrase}%'" : ($excerpt = "");
            $query = "SELECT ID FROM {$wpdb->posts} \n\t\t\t\tWHERE (post_content LIKE '%{$phrase}%' OR post_title LIKE '%{$phrase}%' {$excerpt})\n\t\t\t\tAND post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit')";
            $docs = $wpdb->get_results($query);
            if (is_array($docs)) {
                foreach ($docs as $doc) {
                    if (!isset($phrase_matches[$phrase])) {
                        $phrase_matches[$phrase] = array();
                    }
                    $phrase_matches[$phrase][] = $doc->ID;
                }
            }
            $query = "SELECT ID FROM {$wpdb->posts} as p, {$wpdb->term_relationships} as r, {$wpdb->term_taxonomy} as s, {$wpdb->terms} as t\n\t\t\t\tWHERE r.term_taxonomy_id = s.term_taxonomy_id AND s.term_id = t.term_id AND p.ID = r.object_id\n\t\t\t\tAND t.name LIKE '%{$phrase}%' AND p.post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit')";
            $docs = $wpdb->get_results($query);
            if (is_array($docs)) {
                foreach ($docs as $doc) {
                    if (!isset($phrase_matches[$phrase])) {
                        $phrase_matches[$phrase] = array();
                    }
                    $phrase_matches[$phrase][] = $doc->ID;
                }
            }
            $query = "SELECT ID\n              FROM {$wpdb->posts} AS p, {$wpdb->postmeta} AS m\n              WHERE p.ID = m.post_id\n              AND m.meta_value LIKE '%{$phrase}%'\n              AND p.post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit')";
            $docs = $wpdb->get_results($query);
            if (is_array($docs)) {
                foreach ($docs as $doc) {
                    if (!isset($phrase_matches[$phrase])) {
                        $phrase_matches[$phrase] = array();
                    }
                    $phrase_matches[$phrase][] = $doc->ID;
                }
            }
        }
        if (count($phrase_matches) < 1) {
            $phrases = "-1";
        } else {
            // Complicated mess, but necessary...
            $i = 0;
            $phms = array();
            foreach ($phrase_matches as $phm) {
                $phms[$i++] = $phm;
            }
            $phrases = $phms[0];
            if ($i > 1) {
                for ($i = 1; $i < count($phms); $i++) {
                    $phrases = array_intersect($phrases, $phms[$i]);
                }
            }
            if (count($phrases) < 1) {
                $phrases = "-1";
            } else {
                $phrases = implode(",", $phrases);
            }
        }
    } else {
        $phrases = false;
    }
    return $phrases;
}
예제 #2
0
function relevanssi_recognize_phrases($q)
{
    global $wpdb;
    $phrases = relevanssi_extract_phrases($q);
    $all_queries = array();
    if (count($phrases) > 0) {
        foreach ($phrases as $phrase) {
            $queries = array();
            $phrase = esc_sql($phrase);
            "on" == get_option("relevanssi_index_excerpt") ? $excerpt = " OR post_excerpt LIKE '%{$phrase}%'" : ($excerpt = "");
            $query = "(SELECT ID FROM {$wpdb->posts} \n\t\t\t\tWHERE (post_content LIKE '%{$phrase}%' OR post_title LIKE '%{$phrase}%' {$excerpt})\n\t\t\t\tAND post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit'))";
            $queries[] = $query;
            $query = "(SELECT ID FROM {$wpdb->posts} as p, {$wpdb->term_relationships} as r, {$wpdb->term_taxonomy} as s, {$wpdb->terms} as t\n\t\t\t\tWHERE r.term_taxonomy_id = s.term_taxonomy_id AND s.term_id = t.term_id AND p.ID = r.object_id\n\t\t\t\tAND t.name LIKE '%{$phrase}%' AND p.post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit'))";
            $queries[] = $query;
            $query = "(SELECT ID\n              FROM {$wpdb->posts} AS p, {$wpdb->postmeta} AS m\n              WHERE p.ID = m.post_id\n              AND m.meta_value LIKE '%{$phrase}%'\n              AND p.post_status IN ('publish', 'draft', 'private', 'pending', 'future', 'inherit'))";
            $queries[] = $query;
            $queries = implode(' OR relevanssi.doc IN ', $queries);
            $queries = "AND (relevanssi.doc IN {$queries})";
            $all_queries[] = $queries;
        }
    } else {
        $phrases = "";
    }
    $all_queries = implode(" ", $all_queries);
    return $all_queries;
}
function relevanssi_highlight_terms($excerpt, $query)
{
    $type = get_option("relevanssi_highlight");
    if ("none" == $type) {
        return $excerpt;
    }
    switch ($type) {
        case "mark":
            // thanks to Jeff Byrnes
            $start_emp = "<mark>";
            $end_emp = "</mark>";
            break;
        case "strong":
            $start_emp = "<strong>";
            $end_emp = "</strong>";
            break;
        case "em":
            $start_emp = "<em>";
            $end_emp = "</em>";
            break;
        case "col":
            $col = get_option("relevanssi_txt_col");
            if (!$col) {
                $col = "#ff0000";
            }
            $start_emp = "<span style='color: {$col}'>";
            $end_emp = "</span>";
            break;
        case "bgcol":
            $col = get_option("relevanssi_bg_col");
            if (!$col) {
                $col = "#ff0000";
            }
            $start_emp = "<span style='background-color: {$col}'>";
            $end_emp = "</span>";
            break;
        case "css":
            $css = get_option("relevanssi_css");
            if (!$css) {
                $css = "color: #ff0000";
            }
            $start_emp = "<span style='{$css}'>";
            $end_emp = "</span>";
            break;
        case "class":
            $css = get_option("relevanssi_class");
            if (!$css) {
                $css = "relevanssi-query-term";
            }
            $start_emp = "<span class='{$css}'>";
            $end_emp = "</span>";
            break;
        default:
            return $excerpt;
    }
    $start_emp_token = "**[";
    $end_emp_token = "]**";
    if (function_exists('mb_internal_encoding')) {
        mb_internal_encoding("UTF-8");
    }
    $terms = array_keys(relevanssi_tokenize($query, $remove_stopwords = true, $min_word_length = -1));
    if (is_array($query)) {
        $query = implode(' ', $query);
    }
    // just in case
    $phrases = relevanssi_extract_phrases(stripslashes($query));
    $non_phrase_terms = array();
    foreach ($phrases as $phrase) {
        $phrase_terms = array_keys(relevanssi_tokenize($phrase, $remove_stopwords = false));
        foreach ($terms as $term) {
            if (!in_array($term, $phrase_terms)) {
                $non_phrase_terms[] = $term;
            }
        }
        $terms = $non_phrase_terms;
        $terms[] = $phrase;
    }
    uksort($terms, 'relevanssi_strlen_sort');
    get_option('relevanssi_word_boundaries', 'on') == 'on' ? $word_boundaries = true : ($word_boundaries = false);
    foreach ($terms as $term) {
        $pr_term = relevanssi_replace_punctuation(preg_quote($term, '/'));
        if ($word_boundaries) {
            $excerpt = preg_replace("/(\\b{$pr_term}|{$pr_term}\\b)(?!(^&+)?(;))/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt);
        } else {
            $excerpt = preg_replace("/({$pr_term})(?!(^&+)?(;))/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt);
        }
        $preg_start = preg_quote($start_emp_token);
        $preg_end = preg_quote($end_emp_token);
        if (preg_match_all('/<.*>/U', $excerpt, $matches) > 0) {
            // Remove highlights from inside HTML tags
            foreach ($matches as $match) {
                $new_match = str_replace($start_emp_token, '', $match);
                $new_match = str_replace($end_emp_token, '', $new_match);
                $excerpt = str_replace($match, $new_match, $excerpt);
            }
        }
        if (preg_match_all('/<(style|script|object|embed)>.*<\\/(style|script|object|embed)>/U', $excerpt, $matches) > 0) {
            // Remove highlights in style, object, embed and script tags
            foreach ($matches as $match) {
                $new_match = str_replace($start_emp_token, '', $match);
                $new_match = str_replace($end_emp_token, '', $new_match);
                $excerpt = str_replace($match, $new_match, $excerpt);
            }
        }
    }
    $excerpt = relevanssi_remove_nested_highlights($excerpt, $start_emp_token, $end_emp_token);
    /*
    	$excerpt = htmlentities($excerpt, ENT_QUOTES, 'UTF-8');
    	// return the HTML entities that were stripped before
    */
    $excerpt = str_replace($start_emp_token, $start_emp, $excerpt);
    $excerpt = str_replace($end_emp_token, $end_emp, $excerpt);
    $excerpt = str_replace($end_emp . $start_emp, "", $excerpt);
    if (function_exists('mb_ereg_replace')) {
        $pattern = $end_emp . '\\s*' . $start_emp;
        $excerpt = mb_ereg_replace($pattern, " ", $excerpt);
    }
    return $excerpt;
}
예제 #4
0
function relevanssi_highlight_terms($excerpt, $query) {
	$type = get_option("relevanssi_highlight");
	if ("none" == $type) {
		return $excerpt;
	}
	
	switch ($type) {
		case "mark":						// thanks to Jeff Byrnes
			$start_emp = "<mark>";
			$end_emp = "</mark>";
			break;
		case "strong":
			$start_emp = "<strong>";
			$end_emp = "</strong>";
			break;
		case "em":
			$start_emp = "<em>";
			$end_emp = "</em>";
			break;
		case "col":
			$col = get_option("relevanssi_txt_col");
			if (!$col) $col = "#ff0000";
			$start_emp = "<span style='color: $col'>";
			$end_emp = "</span>";
			break;
		case "bgcol":
			$col = get_option("relevanssi_bg_col");
			if (!$col) $col = "#ff0000";
			$start_emp = "<span style='background-color: $col'>";
			$end_emp = "</span>";
			break;
		case "css":
			$css = get_option("relevanssi_css");
			if (!$css) $css = "color: #ff0000";
			$start_emp = "<span style='$css'>";
			$end_emp = "</span>";
			break;
		case "class":
			$css = get_option("relevanssi_class");
			if (!$css) $css = "relevanssi-query-term";
			$start_emp = "<span class='$css'>";
			$end_emp = "</span>";
			break;
		default:
			return $excerpt;
	}
	
	$start_emp_token = "*[/";
	$end_emp_token = "\]*";
	mb_internal_encoding("UTF-8");
	
	$terms = array_keys(relevanssi_tokenize($query, false));

	$phrases = relevanssi_extract_phrases(stripslashes($query));
	
	$non_phrase_terms = array();
	foreach ($phrases as $phrase) {
		$phrase_terms = array_keys(relevanssi_tokenize($phrase, false));
		foreach ($terms as $term) {
			if (!in_array($term, $phrase_terms)) {
				$non_phrase_terms[] = $term;
			}
		}
		$terms = $non_phrase_terms;
		$terms[] = $phrase;
	}

	usort($terms, 'relevanssi_strlen_sort');

	foreach ($terms as $term) {
		$excerpt = preg_replace("/(\b$term|$term\b)(?!([^<]+)?>)/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt);
		// thanks to http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/
	}

	$excerpt = relevanssi_remove_nested_highlights($excerpt, $start_emp_token, $end_emp_token);

	$excerpt = str_replace($start_emp_token, $start_emp, $excerpt);
	$excerpt = str_replace($end_emp_token, $end_emp, $excerpt);
	$excerpt = str_replace($end_emp . $start_emp, "", $excerpt);
	if (function_exists('mb_ereg_replace')) {
		$pattern = $end_emp . '\s*' . $start_emp;
		$excerpt = mb_ereg_replace($pattern, " ", $excerpt);
	}

	return $excerpt;
}