qa-search-basic.php 4.24 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
class qa_search_basic
{
	public function index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid)
	{
Scott committed
27
		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
		$titlewordids = qa_array_filter_by_keys($wordtoid, $titlewords);
Scott committed
44
		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
		$contentwordidcounts = array();
		foreach ($contentcount as $word => $count) {
Scott committed
50
			if (isset($wordtoid[$word]))
Scott committed
51 52
				$contentwordidcounts[$wordtoid[$word]] = $count;
		}
Scott committed
53

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

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

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

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

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

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

Scott committed
68 69 70 71 72 73
		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
74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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