1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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
*/
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';
// Get words from each textual element
$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));
// Map all words to their word IDs
$words = array_unique(array_merge($titlewords, array_keys($contentcount), $tagwords, $wholetags));
$wordtoid = qa_db_word_mapto_ids_add($words);
// Add to title words index
$titlewordids = qa_array_filter_by_keys($wordtoid, $titlewords);
qa_db_titlewords_add_post_wordids($postid, $titlewordids);
// Add to content words index (including word counts)
$contentwordidcounts = array();
foreach ($contentcount as $word => $count) {
if (isset($wordtoid[$word]))
$contentwordidcounts[$wordtoid[$word]] = $count;
}
qa_db_contentwords_add_post_wordidcounts($postid, $type, $questionid, $contentwordidcounts);
// Add to tag words index
$tagwordids = qa_array_filter_by_keys($wordtoid, $tagwords);
qa_db_tagwords_add_post_wordids($postid, $tagwordids);
// Add to whole tags index
$wholetagids = qa_array_filter_by_keys($wordtoid, $wholetags);
qa_db_posttags_add_post_wordids($postid, $wholetagids);
// Update counts cached in database (will be skipped if qa_suspend_update_counts() was called
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);
}
public function unindex_post($postid)
{
require_once QA_INCLUDE_DIR . 'db/post-update.php';
$titlewordids = qa_db_titlewords_get_post_wordids($postid);
qa_db_titlewords_delete_post($postid);
qa_db_word_titlecount_update($titlewordids);
$contentwordids = qa_db_contentwords_get_post_wordids($postid);
qa_db_contentwords_delete_post($postid);
qa_db_word_contentcount_update($contentwordids);
$tagwordids = qa_db_tagwords_get_post_wordids($postid);
qa_db_tagwords_delete_post($postid);
qa_db_word_tagwordcount_update($tagwordids);
$wholetagids = qa_db_posttags_get_post_wordids($postid);
qa_db_posttags_delete_post($postid);
qa_db_word_tagcount_update($wholetagids);
}
public function move_post($postid, $categoryid)
{
// for now, the built-in search engine ignores categories
}
public function index_page($pageid, $request, $title, $content, $format, $text)
{
// for now, the built-in search engine ignores custom pages
}
public function unindex_page($pageid)
{
// for now, the built-in search engine ignores custom pages
}
public function process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent)
{
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'util/string.php';
$words = qa_string_to_words($query);
$questions = qa_db_select_with_pending(
qa_db_search_posts_selectspec($userid, $words, $words, $words, $words, trim($query), $start, $fullcontent, $count)
);
$results = array();
foreach ($questions as $question) {
qa_search_set_max_match($question, $type, $postid); // to link straight to best part
$results[] = array(
'question' => $question,
'match_type' => $type,
'match_postid' => $postid,
);
}
return $results;
}
}