qa-search-basic.php 4.18 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-search-basic.php
	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
23 24 25 26 27
class qa_search_basic
{
	public function index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid)
	{
		require_once QA_INCLUDE_DIR.'db/post-create.php';
Scott committed
28

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

Scott committed
31 32 33 34
		$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
35

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

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

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

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

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

Scott committed
48 49 50 51
		$contentwordidcounts=array();
		foreach ($contentcount as $word => $count)
			if (isset($wordtoid[$word]))
				$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 58
		$tagwordids=qa_array_filter_by_keys($wordtoid, $tagwords);
		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 63
		$wholetagids=qa_array_filter_by_keys($wordtoid, $wholetags);
		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 72
		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);
		qa_db_tagcount_update();
	}
Scott committed
73

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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