hotness.php 3.17 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: Functions for dealing with question hotness in the database


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


28 29 30
/**
 * Increment the views counter for the post (if different IP from last view).
 * @param  int $postid The ID of the post
31
 * @return bool Whether views were actually incremented.
32 33 34 35 36 37 38
 */
function qa_db_increment_views($postid)
{
	$query = 'UPDATE ^posts SET views=views+1, lastviewip=UNHEX($) WHERE postid=# AND (lastviewip IS NULL OR lastviewip!=UNHEX($))';
	$ipHex = bin2hex(@inet_pton(qa_remote_ip_address()));

	qa_db_query_sub($query, $ipHex, $postid, $ipHex);
39 40

	return qa_db_affected_rows() > 0;
41 42 43
}


Scott committed
44
/**
Scott committed
45 46
 * Recalculate the hotness in the database for one or more posts.
 *
47 48
 * @param int $firstpostid First post to recalculate (or only post if $lastpostid is null).
 * @param int $lastpostid Last post in the range to recalculate.
Scott committed
49 50 51
 * @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
Scott committed
52
 */
53
function qa_db_hotness_update($firstpostid, $lastpostid = null, $viewincrement = false)
Scott committed
54 55 56
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

Scott committed
57 58
	if (!qa_should_update_counts()) {
		return;
Scott committed
59
	}
Scott committed
60 61 62 63

	if (!isset($lastpostid))
		$lastpostid = $firstpostid;

64
	$query = "UPDATE ^posts AS x, (SELECT parents.postid, MAX(parents.created) AS qcreated, COALESCE(MAX(children.created), MAX(parents.created)) as acreated, COUNT(children.postid) AS acount, MAX(parents.netvotes) AS netvotes, MAX(parents.views) AS 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=(" .
Scott committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		'((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));
Scott committed
85
}