search.php 4.21 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-page-search.php
	Description: Controller for search page


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

Scott committed
28 29 30
require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR . 'app/options.php';
require_once QA_INCLUDE_DIR . 'app/search.php';
Scott committed
31 32


Scott committed
33
// Perform the search if appropriate
Scott committed
34

Scott committed
35
if (strlen(qa_get('q'))) {
Scott committed
36
	// Pull in input parameters
Scott committed
37 38 39
	$inquery = trim(qa_get('q'));
	$userid = qa_get_logged_in_userid();
	$start = qa_get_start();
Scott committed
40

Scott committed
41 42 43
	$display = qa_opt_if_loaded('page_size_search');
	$count = 2 * (isset($display) ? $display : QA_DB_RETRIEVE_QS_AS) + 1;
	// get enough results to be able to give some idea of how many pages of search results there are
Scott committed
44

Scott committed
45
	// Perform the search using appropriate module
Scott committed
46

Scott committed
47
	$results = qa_get_search_results($inquery, $start, $count, $userid, false, false);
Scott committed
48

Scott committed
49
	// Count and truncate results
Scott committed
50

Scott committed
51 52 53
	$pagesize = qa_opt('page_size_search');
	$gotcount = count($results);
	$results = array_slice($results, 0, $pagesize);
Scott committed
54

Scott committed
55
	// Retrieve extra information on users
Scott committed
56

Scott committed
57
	$fullquestions = array();
Scott committed
58

Scott committed
59 60 61 62
	foreach ($results as $result) {
		if (isset($result['question']))
			$fullquestions[] = $result['question'];
	}
Scott committed
63

Scott committed
64
	$usershtml = qa_userids_handles_html($fullquestions);
Scott committed
65

Scott committed
66
	// Report the search event
Scott committed
67

Scott committed
68 69 70 71 72
	qa_report_event('search', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
		'query' => $inquery,
		'start' => $start,
	));
}
Scott committed
73 74


Scott committed
75
// Prepare content for theme
Scott committed
76

Scott committed
77
$qa_content = qa_content_prepare(true);
Scott committed
78

Scott committed
79 80
if (strlen(qa_get('q'))) {
	$qa_content['search']['value'] = qa_html($inquery);
Scott committed
81

Scott committed
82 83 84 85
	if (count($results))
		$qa_content['title'] = qa_lang_html_sub('main/results_for_x', qa_html($inquery));
	else
		$qa_content['title'] = qa_lang_html_sub('main/no_results_for_x', qa_html($inquery));
Scott committed
86

Scott committed
87 88
	$qa_content['q_list']['form'] = array(
		'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
89

Scott committed
90 91 92 93
		'hidden' => array(
			'code' => qa_get_form_security_code('vote'),
		),
	);
Scott committed
94

Scott committed
95
	$qa_content['q_list']['qs'] = array();
Scott committed
96

Scott committed
97
	$qdefaults = qa_post_html_defaults('Q');
Scott committed
98

Scott committed
99 100 101 102 103 104 105 106
	foreach ($results as $result) {
		if (!isset($result['question'])) { // if we have any non-question results, display with less statistics
			$qdefaults['voteview'] = false;
			$qdefaults['answersview'] = false;
			$qdefaults['viewsview'] = false;
			break;
		}
	}
Scott committed
107

Scott committed
108 109 110 111 112 113 114 115 116 117 118
	foreach ($results as $result) {
		if (isset($result['question'])) {
			$fields = qa_post_html_fields($result['question'], $userid, qa_cookie_get(),
				$usershtml, null, qa_post_html_options($result['question'], $qdefaults));
		} elseif (isset($result['url'])) {
			$fields = array(
				'what' => qa_html($result['url']),
				'meta_order' => qa_lang_html('main/meta_order'),
			);
		} else {
			continue; // nothing to show here
Scott committed
119 120
		}

Scott committed
121 122
		if (isset($qdefaults['blockwordspreg']))
			$result['title'] = qa_block_words_replace($result['title'], $qdefaults['blockwordspreg']);
Scott committed
123

Scott committed
124 125
		$fields['title'] = qa_html($result['title']);
		$fields['url'] = qa_html($result['url']);
Scott committed
126

Scott committed
127 128
		$qa_content['q_list']['qs'][] = $fields;
	}
Scott committed
129

Scott committed
130 131
	$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $start + $gotcount,
		qa_opt('pages_prev_next'), array('q' => $inquery), $gotcount >= $count);
Scott committed
132

Scott committed
133 134 135 136 137 138
	if (qa_opt('feed_for_search')) {
		$qa_content['feed'] = array(
			'url' => qa_path_html(qa_feed_request('search/' . $inquery)),
			'label' => qa_lang_html_sub('main/results_for_x', qa_html($inquery)),
		);
	}
Scott committed
139

Scott committed
140 141
	if (empty($qa_content['page_links']))
		$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
Scott committed
142

Scott committed
143 144
} else
	$qa_content['error'] = qa_lang_html('main/search_explanation');
Scott committed
145 146


Scott committed
147
return $qa_content;