qa-app-search.php 5.1 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-app-search.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Wrapper functions and utilities for search modules


	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
	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;
	}

Scott Vivian committed
32

Gideon Greenspan committed
33 34 35 36 37 38 39 40 41 42
	function qa_get_search_results($query, $start, $count, $userid, $absoluteurls, $fullcontent)
/*
	Returns $count search results for $query performed by $userid, starting at offset $start. Set $absoluteurls to true
	to get absolute URLs for the results and $fullcontent if the results should include full post content. This calls
	through to the chosen search module, and performs all the necessary post-processing to supplement the results for
	display online or in an RSS feed.
*/
	{

	//	Identify which search module should be used
Scott Vivian committed
43

Gideon Greenspan committed
44
		$searchmodules=qa_load_modules_with('search', 'process_search');
Scott Vivian committed
45

Gideon Greenspan committed
46 47
		if (!count($searchmodules))
			qa_fatal_error('No search engine is available');
Scott Vivian committed
48

Gideon Greenspan committed
49
		$module=reset($searchmodules); // use first one by default
Scott Vivian committed
50

Gideon Greenspan committed
51 52
		if (count($searchmodules)>1) {
			$tryname=qa_opt('search_module'); // use chosen one if it's available
Scott Vivian committed
53

Gideon Greenspan committed
54 55 56
			if (isset($searchmodules[$tryname]))
				$module=$searchmodules[$tryname];
		}
Scott Vivian committed
57

Gideon Greenspan committed
58
	//	Get the results
Scott Vivian committed
59

Gideon Greenspan committed
60
		$results=$module->process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent);
Scott Vivian committed
61

Gideon Greenspan committed
62
	//	Work out what additional information (if any) we need to retrieve for the results
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66 67
		$keypostidgetfull=array();
		$keypostidgettype=array();
		$keypostidgetquestion=array();
		$keypageidgetpage=array();
Scott Vivian committed
68

Gideon Greenspan committed
69 70 71
		foreach ($results as $result) {
			if (isset($result['question_postid']) && !isset($result['question']))
				$keypostidgetfull[$result['question_postid']]=true;
Scott Vivian committed
72

Gideon Greenspan committed
73 74 75 76 77 78 79
			if (isset($result['match_postid'])) {
				if (!( (isset($result['question_postid'])) || (isset($result['question'])) ))
					$keypostidgetquestion[$result['match_postid']]=true; // we can also get $result['match_type'] from this

				elseif (!isset($result['match_type']))
					$keypostidgettype[$result['match_postid']]=true;
			}
Scott Vivian committed
80

Gideon Greenspan committed
81 82 83
			if (isset($result['page_pageid']) && !isset($result['page']))
				$keypageidgetpage[$result['page_pageid']]=true;
		}
Scott Vivian committed
84

Gideon Greenspan committed
85
	//	Perform the appropriate database queries
Scott Vivian committed
86

Gideon Greenspan committed
87
		list($postidfull, $postidtype, $postidquestion, $pageidpage)=qa_db_select_with_pending(
Gideon Greenspan committed
88 89 90 91 92
			count($keypostidgetfull) ? qa_db_posts_selectspec($userid, array_keys($keypostidgetfull), $fullcontent) : null,
			count($keypostidgettype) ? qa_db_posts_basetype_selectspec(array_keys($keypostidgettype)) : null,
			count($keypostidgetquestion) ? qa_db_posts_to_qs_selectspec($userid, array_keys($keypostidgetquestion), $fullcontent) : null,
			count($keypageidgetpage) ? qa_db_pages_selectspec(null, array_keys($keypageidgetpage)) : null
		);
Scott Vivian committed
93

Gideon Greenspan committed
94
	//	Supplement the results as appropriate
Scott Vivian committed
95

Gideon Greenspan committed
96 97 98 99 100 101 102 103
		foreach ($results as $key => $result) {
			if (isset($result['question_postid']) && !isset($result['question']))
				if (@$postidfull[$result['question_postid']]['basetype']=='Q')
					$result['question']=@$postidfull[$result['question_postid']];

			if (isset($result['match_postid'])) {
				if (!( (isset($result['question_postid'])) || (isset($result['question'])) )) {
					$result['question']=@$postidquestion[$result['match_postid']];
Scott Vivian committed
104

Gideon Greenspan committed
105 106 107 108 109 110
					if (!isset($result['match_type']))
						$result['match_type']=@$result['question']['obasetype'];

				} elseif (!isset($result['match_type']))
					$result['match_type']=@$postidtype[$result['match_postid']];
			}
Scott Vivian committed
111

Gideon Greenspan committed
112 113
			if (isset($result['question']) && !isset($result['question_postid']))
				$result['question_postid']=$result['question']['postid'];
Scott Vivian committed
114

Gideon Greenspan committed
115
			if (isset($result['page_pageid']) && !isset($result['page']))
116
				$result['page']=@$pageidpage[$result['page_pageid']];
Scott Vivian committed
117

Gideon Greenspan committed
118 119 120 121 122 123
			if (!isset($result['title'])) {
				if (isset($result['question']))
					$result['title']=$result['question']['title'];
				elseif (isset($result['page']))
					$result['title']=$result['page']['heading'];
			}
Scott Vivian committed
124

Gideon Greenspan committed
125 126 127 128 129 130 131
			if (!isset($result['url'])) {
				if (isset($result['question']))
					$result['url']=qa_q_path($result['question']['postid'], $result['question']['title'],
						$absoluteurls, @$result['match_type'], @$result['match_postid']);
				elseif (isset($result['page']))
					$result['url']=qa_path($result['page']['tags'], null, qa_opt('site_url'));
			}
Scott Vivian committed
132

Gideon Greenspan committed
133 134
			$results[$key]=$result;
		}
Scott Vivian committed
135

Gideon Greenspan committed
136
	//	Return the results
Scott Vivian committed
137

Gideon Greenspan committed
138 139
		return $results;
	}
Scott Vivian committed
140

Gideon Greenspan committed
141 142 143 144

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