search.php 4.97 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-app-search.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.

	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 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
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}


/**
 * 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.
 * @param $query
 * @param $start
 * @param $count
 * @param $userid
 * @param $absoluteurls
 * @param $fullcontent
 * @return
 */
function qa_get_search_results($query, $start, $count, $userid, $absoluteurls, $fullcontent)
{
	// Identify which search module should be used

	$searchmodules = qa_load_modules_with('search', 'process_search');

	if (!count($searchmodules))
		qa_fatal_error('No search engine is available');

	$module = reset($searchmodules); // use first one by default

	if (count($searchmodules) > 1) {
		$tryname = qa_opt('search_module'); // use chosen one if it's available

		if (isset($searchmodules[$tryname]))
			$module = $searchmodules[$tryname];
Scott committed
58 59
	}

Scott committed
60
	// Get the results
Scott committed
61

Scott committed
62
	$results = $module->process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent);
Scott committed
63

Scott committed
64
	// Work out what additional information (if any) we need to retrieve for the results
Scott committed
65

Scott committed
66 67 68 69
	$keypostidgetfull = array();
	$keypostidgettype = array();
	$keypostidgetquestion = array();
	$keypageidgetpage = array();
Scott committed
70

Scott committed
71 72 73
	foreach ($results as $result) {
		if (isset($result['question_postid']) && !isset($result['question']))
			$keypostidgetfull[$result['question_postid']] = true;
Scott committed
74

Scott committed
75 76 77
		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
Scott committed
78

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

Scott committed
83 84 85
		if (isset($result['page_pageid']) && !isset($result['page']))
			$keypageidgetpage[$result['page_pageid']] = true;
	}
Scott committed
86

Scott committed
87
	// Perform the appropriate database queries
Scott committed
88

Scott committed
89 90 91 92 93 94
	list($postidfull, $postidtype, $postidquestion, $pageidpage) = qa_db_select_with_pending(
		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 committed
95

Scott committed
96
	// Supplement the results as appropriate
Scott committed
97

Scott committed
98 99 100 101
	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']];
Scott committed
102

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

Scott committed
107 108
				if (!isset($result['match_type']))
					$result['match_type'] = @$result['question']['obasetype'];
Scott committed
109

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

Scott committed
114 115
		if (isset($result['question']) && !isset($result['question_postid']))
			$result['question_postid'] = $result['question']['postid'];
Scott committed
116

Scott committed
117 118
		if (isset($result['page_pageid']) && !isset($result['page']))
			$result['page'] = @$pageidpage[$result['page_pageid']];
Scott committed
119

Scott committed
120 121 122 123 124
		if (!isset($result['title'])) {
			if (isset($result['question']))
				$result['title'] = $result['question']['title'];
			elseif (isset($result['page']))
				$result['title'] = $result['page']['heading'];
Scott committed
125 126
		}

Scott committed
127 128 129 130 131 132 133
		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 committed
134

Scott committed
135
		$results[$key] = $result;
Scott committed
136 137
	}

Scott committed
138
	// Return the results
Scott committed
139

Scott committed
140 141
	return $results;
}