asktitle.php 2.79 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Server-side response to Ajax request based on ask a question title


	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
22 23 24 25
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'util/string.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
26 27


Scott committed
28
// Collect the information we need from the database
Scott committed
29

Scott committed
30 31 32
$intitle = qa_post_text('title');
$doaskcheck = qa_opt('do_ask_check_qs');
$doexampletags = qa_using_tags() && qa_opt('do_example_tags');
Scott committed
33

Scott committed
34 35
if ($doaskcheck || $doexampletags) {
	$countqs = max($doexampletags ? QA_DB_RETRIEVE_ASK_TAG_QS : 0, $doaskcheck ? qa_opt('page_size_ask_check_qs') : 0);
Scott committed
36

Scott committed
37 38 39 40
	$relatedquestions = qa_db_select_with_pending(
		qa_db_search_posts_selectspec(null, qa_string_to_words($intitle), null, null, null, null, 0, false, $countqs)
	);
}
Scott committed
41 42


Scott committed
43
// Collect example tags if appropriate
Scott committed
44

Scott committed
45 46 47 48 49 50
if ($doexampletags) {
	$tagweight = array();
	foreach ($relatedquestions as $question) {
		$tags = qa_tagstring_to_tags($question['tags']);
		foreach ($tags as $tag) {
			@$tagweight[$tag] += exp($question['score']);
Scott committed
51
		}
Scott committed
52
	}
Scott committed
53

Scott committed
54
	arsort($tagweight, SORT_NUMERIC);
Scott committed
55

Scott committed
56
	$exampletags = array();
Scott committed
57

Scott committed
58 59
	$minweight = exp(qa_match_to_min_score(qa_opt('match_example_tags')));
	$maxcount = qa_opt('page_size_ask_tags');
Scott committed
60

Scott committed
61 62 63
	foreach ($tagweight as $tag => $weight) {
		if ($weight < $minweight)
			break;
Scott committed
64

Scott committed
65 66 67
		$exampletags[] = $tag;
		if (count($exampletags) >= $maxcount)
			break;
Scott committed
68
	}
Scott committed
69 70 71
} else {
	$exampletags = array();
}
Scott committed
72 73


Scott committed
74
// Output the response header and example tags
Scott committed
75

Scott committed
76
echo "QA_AJAX_RESPONSE\n1\n";
Scott committed
77

Scott committed
78
echo strtr(qa_html(implode(',', $exampletags)), "\r\n", '  ') . "\n";
Scott committed
79 80


Scott committed
81
// Collect and output the list of related questions
Scott committed
82

Scott committed
83 84 85
if ($doaskcheck) {
	$minscore = qa_match_to_min_score(qa_opt('match_ask_check_qs'));
	$maxcount = qa_opt('page_size_ask_check_qs');
Scott committed
86

Scott committed
87 88
	$relatedquestions = array_slice($relatedquestions, 0, $maxcount);
	$limitedquestions = array();
Scott committed
89

Scott committed
90 91 92
	foreach ($relatedquestions as $question) {
		if ($question['score'] < $minscore)
			break;
Scott committed
93

Scott committed
94
		$limitedquestions[] = $question;
Scott committed
95 96
	}

Scott committed
97 98 99 100
	$themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-asktitle', null, null);
	$themeclass->initialize();
	$themeclass->q_ask_similar($limitedquestions, qa_lang_html('question/ask_same_q'));
}