qa-search-basic.php 4.17 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Basic module for indexing and searching Q2A posts


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22 23 24 25
class qa_search_basic
{
	public function index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid)
	{
Scott committed
26
		require_once QA_INCLUDE_DIR . 'db/post-create.php';
Scott committed
27

Scott committed
28
		// Get words from each textual element
Scott committed
29

Scott committed
30 31 32 33
		$titlewords = array_unique(qa_string_to_words($title));
		$contentcount = array_count_values(qa_string_to_words($text));
		$tagwords = array_unique(qa_string_to_words($tagstring));
		$wholetags = array_unique(qa_tagstring_to_tags($tagstring));
Scott committed
34

Scott committed
35
		// Map all words to their word IDs
Scott committed
36

Scott committed
37 38
		$words = array_unique(array_merge($titlewords, array_keys($contentcount), $tagwords, $wholetags));
		$wordtoid = qa_db_word_mapto_ids_add($words);
Scott committed
39

Scott committed
40
		// Add to title words index
Scott committed
41

Scott committed
42
		$titlewordids = qa_array_filter_by_keys($wordtoid, $titlewords);
Scott committed
43
		qa_db_titlewords_add_post_wordids($postid, $titlewordids);
Scott committed
44

Scott committed
45
		// Add to content words index (including word counts)
Scott committed
46

Scott committed
47 48
		$contentwordidcounts = array();
		foreach ($contentcount as $word => $count) {
Scott committed
49
			if (isset($wordtoid[$word]))
Scott committed
50 51
				$contentwordidcounts[$wordtoid[$word]] = $count;
		}
Scott committed
52

Scott committed
53
		qa_db_contentwords_add_post_wordidcounts($postid, $type, $questionid, $contentwordidcounts);
Scott committed
54

Scott committed
55
		// Add to tag words index
Scott committed
56

Scott committed
57
		$tagwordids = qa_array_filter_by_keys($wordtoid, $tagwords);
Scott committed
58
		qa_db_tagwords_add_post_wordids($postid, $tagwordids);
Scott committed
59

Scott committed
60
		// Add to whole tags index
Scott committed
61

Scott committed
62
		$wholetagids = qa_array_filter_by_keys($wordtoid, $wholetags);
Scott committed
63
		qa_db_posttags_add_post_wordids($postid, $wholetagids);
Scott committed
64

Scott committed
65
		// Update counts cached in database (will be skipped if qa_suspend_update_counts() was called
Scott committed
66

Scott committed
67 68 69 70 71
		qa_db_word_titlecount_update($titlewordids);
		qa_db_word_contentcount_update(array_keys($contentwordidcounts));
		qa_db_word_tagwordcount_update($tagwordids);
		qa_db_word_tagcount_update($wholetagids);
	}
Scott committed
72

Scott committed
73 74
	public function unindex_post($postid)
	{
Scott committed
75
		require_once QA_INCLUDE_DIR . 'db/post-update.php';
Scott committed
76

Scott committed
77
		$titlewordids = qa_db_titlewords_get_post_wordids($postid);
Scott committed
78 79
		qa_db_titlewords_delete_post($postid);
		qa_db_word_titlecount_update($titlewordids);
Scott committed
80

Scott committed
81
		$contentwordids = qa_db_contentwords_get_post_wordids($postid);
Scott committed
82 83
		qa_db_contentwords_delete_post($postid);
		qa_db_word_contentcount_update($contentwordids);
Scott committed
84

Scott committed
85
		$tagwordids = qa_db_tagwords_get_post_wordids($postid);
Scott committed
86 87
		qa_db_tagwords_delete_post($postid);
		qa_db_word_tagwordcount_update($tagwordids);
Scott committed
88

Scott committed
89
		$wholetagids = qa_db_posttags_get_post_wordids($postid);
Scott committed
90 91 92
		qa_db_posttags_delete_post($postid);
		qa_db_word_tagcount_update($wholetagids);
	}
Scott committed
93

Scott committed
94 95 96 97
	public function move_post($postid, $categoryid)
	{
		// for now, the built-in search engine ignores categories
	}
Scott committed
98

Scott committed
99 100 101 102
	public function index_page($pageid, $request, $title, $content, $format, $text)
	{
		// for now, the built-in search engine ignores custom pages
	}
Scott committed
103

Scott committed
104 105 106 107
	public function unindex_page($pageid)
	{
		// for now, the built-in search engine ignores custom pages
	}
Scott committed
108

Scott committed
109 110
	public function process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent)
	{
Scott committed
111 112
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
113

Scott committed
114
		$words = qa_string_to_words($query);
Scott committed
115

Scott committed
116
		$questions = qa_db_select_with_pending(
Scott committed
117 118
			qa_db_search_posts_selectspec($userid, $words, $words, $words, $words, trim($query), $start, $fullcontent, $count)
		);
Scott committed
119

Scott committed
120
		$results = array();
Scott committed
121

Scott committed
122 123
		foreach ($questions as $question) {
			qa_search_set_max_match($question, $type, $postid); // to link straight to best part
Scott committed
124

Scott committed
125
			$results[] = array(
Scott committed
126 127 128
				'question' => $question,
				'match_type' => $type,
				'match_postid' => $postid,
Scott committed
129 130 131
			);
		}

Scott committed
132
		return $results;
Scott committed
133
	}
Scott committed
134
}