qa-search-basic.php 4.2 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 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
	public function unindex_post($postid)
	{
Scott committed
76
		require_once QA_INCLUDE_DIR . 'db/post-update.php';
Scott committed
77

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

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

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

Scott committed
90
		$wholetagids = qa_db_posttags_get_post_wordids($postid);
Scott committed
91 92 93
		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
	public function process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent)
	{
Scott committed
112 113
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		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
		$questions = qa_db_select_with_pending(
Scott committed
118 119
			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
			$results[] = array(
Scott committed
127 128 129
				'question' => $question,
				'match_type' => $type,
				'match_postid' => $postid,
Scott committed
130 131 132
			);
		}

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