Commit 694c2eaf by Scott

Refactor hotness code

parent 788f260c
......@@ -368,18 +368,18 @@ function qa_output_content($qa_content)
*/
function qa_do_content_stats($qa_content)
{
if (isset($qa_content['inc_views_postid'])) {
require_once QA_INCLUDE_DIR . 'db/hotness.php';
if (!isset($qa_content['inc_views_postid'])) {
return false;
}
qa_db_increment_views($qa_content['inc_views_postid']);
if (qa_opt('recalc_hotness_q_view')) {
qa_db_hotness_update($qa_content['inc_views_postid']);
}
require_once QA_INCLUDE_DIR . 'db/hotness.php';
return true;
qa_db_increment_views($qa_content['inc_views_postid']);
if (qa_opt('recalc_hotness_q_view')) {
qa_db_hotness_update($qa_content['inc_views_postid']);
}
return false;
return true;
}
......
......@@ -40,42 +40,44 @@ function qa_db_increment_views($postid)
/**
* Recalculate the hotness in the database for posts $firstpostid to $lastpostid (if specified)
* If $viewincrement is true, also increment the views counter for the post (if different IP from last view),
* and include that in the hotness calculation
* @param $firstpostid
* @param $lastpostid
* @param bool $viewincrement
* @return mixed
* Recalculate the hotness in the database for one or more posts.
*
* @param $firstpostid First post to recalculate (or only post if $lastpostid is null).
* @param $lastpostid Last post in the range to recalculate.
* @param bool $viewincrement Deprecated - view counter is now incremented separately. Previously, would increment the post's
* views and include that in the hotness calculation.
* @return void
*/
function qa_db_hotness_update($firstpostid, $lastpostid = null, $viewincrement = false)
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
if (qa_should_update_counts()) {
if (!isset($lastpostid))
$lastpostid = $firstpostid;
$query = "UPDATE ^posts AS x, (SELECT parents.postid, parents.created AS qcreated, COALESCE(MAX(children.created), parents.created) as acreated, children.acount, parents.netvotes, parents.views FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type='A' WHERE parents.postid BETWEEN # AND # GROUP BY postid) AS a SET x.hotness=(" .
'((TO_DAYS(a.qcreated)-734138)*86400.0+TIME_TO_SEC(a.qcreated))*# + ' . // zero-point is Jan 1, 2010
'((TO_DAYS(a.acreated)-734138)*86400.0+TIME_TO_SEC(a.acreated))*# + ' .
'(a.acount+0.0)*# + ' .
'(a.netvotes+0.0)*# + ' .
'(a.views+0.0)*#' .
') WHERE x.postid=a.postid';
// Additional multiples based on empirical analysis of activity on Q2A meta site to give approx equal influence for all factors
$arguments = array(
$firstpostid,
$lastpostid,
qa_opt('hot_weight_q_age'),
qa_opt('hot_weight_a_age'),
qa_opt('hot_weight_answers') * 160000,
qa_opt('hot_weight_votes') * 160000,
qa_opt('hot_weight_views') * 4000,
);
qa_db_query_raw(qa_db_apply_sub($query, $arguments));
if (!qa_should_update_counts()) {
return;
}
if (!isset($lastpostid))
$lastpostid = $firstpostid;
$query = "UPDATE ^posts AS x, (SELECT parents.postid, parents.created AS qcreated, COALESCE(MAX(children.created), parents.created) as acreated, children.acount, parents.netvotes, parents.views FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type='A' WHERE parents.postid BETWEEN # AND # GROUP BY postid) AS a SET x.hotness=(" .
'((TO_DAYS(a.qcreated)-734138)*86400.0+TIME_TO_SEC(a.qcreated))*# + ' . // zero-point is Jan 1, 2010
'((TO_DAYS(a.acreated)-734138)*86400.0+TIME_TO_SEC(a.acreated))*# + ' .
'(a.acount+0.0)*# + ' .
'(a.netvotes+0.0)*# + ' .
'(a.views+0.0)*#' .
') WHERE x.postid=a.postid';
// Additional multiples based on empirical analysis of activity on Q2A meta site to give approx equal influence for all factors
$arguments = array(
$firstpostid,
$lastpostid,
qa_opt('hot_weight_q_age'),
qa_opt('hot_weight_a_age'),
qa_opt('hot_weight_answers') * 160000,
qa_opt('hot_weight_votes') * 160000,
qa_opt('hot_weight_views') * 4000,
);
qa_db_query_raw(qa_db_apply_sub($query, $arguments));
}
......@@ -203,7 +203,7 @@ return array(
'recalc_categories_note' => ' - for post categories and category counts',
'recalc_categories_recounting' => 'Recounting questions for ^1 of ^2 categories...',
'recalc_categories_updated' => 'Recalculated for ^1 of ^2 posts...',
'recalc_hotness_q_view_note' => 'Slightly more efficient if disabled, but hotness values will become out of date if views are given higher importance in settings',
'recalc_hotness_q_view_note' => 'May slightly improve page speed if disabled, but hotness values will become out of date if views are included in hotness settings',
'recalc_points' => 'Recalculate user points',
'recalc_points_complete' => 'All user points were successfully recalculated.',
'recalc_points_note' => ' - for user ranking and points displays',
......
......@@ -429,27 +429,30 @@ if ($question['basetype'] == 'Q') {
? qa_lang_html_sub_split('question/1_answer_title', '1', '1')
: qa_lang_html_sub_split('question/x_answers_title', $countfortitle);
if ($microdata)
if ($microdata) {
$split['data'] = '<span itemprop="answerCount">' . $split['data'] . '</span>';
}
$qa_content['a_list']['title'] = $split['prefix'] . $split['data'] . $split['suffix'];
} else
$qa_content['a_list']['title_tags'] .= ' style="display:none;" ';
}
if (!$formrequested)
if (!$formrequested) {
$qa_content['page_links'] = qa_html_page_links(qa_request(), $pagestart, $pagesize, $countforpages, qa_opt('pages_prev_next'), array(), false, 'a_list_title');
}
// Some generally useful stuff
if (qa_using_categories() && count($categories))
if (qa_using_categories() && count($categories)) {
$qa_content['navigation']['cat'] = qa_category_navigation($categories, $question['categoryid']);
}
if (isset($jumptoanchor))
if (isset($jumptoanchor)) {
$qa_content['script_onloads'][] = array(
'qa_scroll_page_to($("#"+' . qa_js($jumptoanchor) . ').offset().top);'
);
}
// Determine whether this request should be counted for page view statistics.
......
......@@ -810,6 +810,7 @@ function qa_suspend_update_counts($suspend = true)
/**
* Returns whether counts should currently be updated (i.e. if count updating has not been suspended).
* @return bool
*/
function qa_should_update_counts()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment