qa-search-basic.php 4.66 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-search-basic.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	class qa_search_basic {
Scott Vivian committed
34

35
		public function index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid)
Gideon Greenspan committed
36 37
		{
			require_once QA_INCLUDE_DIR.'qa-db-post-create.php';
Scott Vivian committed
38

Gideon Greenspan committed
39
		//	Get words from each textual element
Scott Vivian committed
40

Gideon Greenspan committed
41 42 43 44
			$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 Vivian committed
45

Gideon Greenspan committed
46
		//	Map all words to their word IDs
Scott Vivian committed
47

Gideon Greenspan committed
48 49
			$words=array_unique(array_merge($titlewords, array_keys($contentcount), $tagwords, $wholetags));
			$wordtoid=qa_db_word_mapto_ids_add($words);
Scott Vivian committed
50

Gideon Greenspan committed
51
		//	Add to title words index
Scott Vivian committed
52

Gideon Greenspan committed
53 54
			$titlewordids=qa_array_filter_by_keys($wordtoid, $titlewords);
			qa_db_titlewords_add_post_wordids($postid, $titlewordids);
Scott Vivian committed
55

Gideon Greenspan committed
56
		//	Add to content words index (including word counts)
Scott Vivian committed
57

Gideon Greenspan committed
58 59 60 61
			$contentwordidcounts=array();
			foreach ($contentcount as $word => $count)
				if (isset($wordtoid[$word]))
					$contentwordidcounts[$wordtoid[$word]]=$count;
Scott Vivian committed
62

Gideon Greenspan committed
63
			qa_db_contentwords_add_post_wordidcounts($postid, $type, $questionid, $contentwordidcounts);
Scott Vivian committed
64

Gideon Greenspan committed
65
		//	Add to tag words index
Scott Vivian committed
66

Gideon Greenspan committed
67 68
			$tagwordids=qa_array_filter_by_keys($wordtoid, $tagwords);
			qa_db_tagwords_add_post_wordids($postid, $tagwordids);
Scott Vivian committed
69

Gideon Greenspan committed
70
		//	Add to whole tags index
Scott Vivian committed
71

Gideon Greenspan committed
72 73
			$wholetagids=qa_array_filter_by_keys($wordtoid, $wholetags);
			qa_db_posttags_add_post_wordids($postid, $wholetagids);
Scott Vivian committed
74

Gideon Greenspan committed
75
		//	Update counts cached in database (will be skipped if qa_suspend_update_counts() was called
Scott Vivian committed
76

Gideon Greenspan committed
77 78 79 80 81 82
			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 Vivian committed
83

Gideon Greenspan committed
84

85
		public function unindex_post($postid)
Gideon Greenspan committed
86 87 88 89 90 91
		{
			require_once QA_INCLUDE_DIR.'qa-db-post-update.php';

			$titlewordids=qa_db_titlewords_get_post_wordids($postid);
			qa_db_titlewords_delete_post($postid);
			qa_db_word_titlecount_update($titlewordids);
Scott Vivian committed
92

Gideon Greenspan committed
93 94 95
			$contentwordids=qa_db_contentwords_get_post_wordids($postid);
			qa_db_contentwords_delete_post($postid);
			qa_db_word_contentcount_update($contentwordids);
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99
			$tagwordids=qa_db_tagwords_get_post_wordids($postid);
			qa_db_tagwords_delete_post($postid);
			qa_db_word_tagwordcount_update($tagwordids);
Scott Vivian committed
100

Gideon Greenspan committed
101 102 103 104 105
			$wholetagids=qa_db_posttags_get_post_wordids($postid);
			qa_db_posttags_delete_post($postid);
			qa_db_word_tagcount_update($wholetagids);
		}

Scott Vivian committed
106

107
		public function move_post($postid, $categoryid)
Gideon Greenspan committed
108 109 110
		{
			// for now, the built-in search engine ignores categories
		}
Scott Vivian committed
111 112


113
		public function index_page($pageid, $request, $title, $content, $format, $text)
Gideon Greenspan committed
114 115 116
		{
			// for now, the built-in search engine ignores custom pages
		}
Scott Vivian committed
117 118


119
		public function unindex_page($pageid)
Gideon Greenspan committed
120 121 122
		{
			// for now, the built-in search engine ignores custom pages
		}
Scott Vivian committed
123 124


125
		public function process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent)
Gideon Greenspan committed
126 127 128 129 130
		{
			require_once QA_INCLUDE_DIR.'qa-db-selects.php';
			require_once QA_INCLUDE_DIR.'qa-util-string.php';

			$words=qa_string_to_words($query);
Scott Vivian committed
131

Gideon Greenspan committed
132 133 134
			$questions=qa_db_select_with_pending(
				qa_db_search_posts_selectspec($userid, $words, $words, $words, $words, trim($query), $start, $fullcontent, $count)
			);
Scott Vivian committed
135

Gideon Greenspan committed
136
			$results=array();
Scott Vivian committed
137

Gideon Greenspan committed
138 139 140 141 142 143 144 145 146
			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,
				);
			}
Scott Vivian committed
147

Gideon Greenspan committed
148 149
			return $results;
		}
Scott Vivian committed
150

Gideon Greenspan committed
151
	}
Scott Vivian committed
152

Gideon Greenspan committed
153 154 155 156

/*
	Omit PHP closing tag to help avoid accidental output
*/