function qa_db_hotness_update($firstpostid, $lastpostid = null, $viewincrement = false) { 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, COUNT(children.postid) AS 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>=# AND parents.postid<=# AND parents.type=\'Q\' GROUP BY postid) AS a SET x.hotness=(' . '((TO_DAYS(a.qcreated)-734138)*86400.0+TIME_TO_SEC(a.qcreated))*# + ' . '((TO_DAYS(a.acreated)-734138)*86400.0+TIME_TO_SEC(a.acreated))*# + ' . '(a.acount+0.0)*# + ' . '(a.netvotes+0.0)*# + ' . '(a.views+0.0+#)*#' . ')' . ($viewincrement ? ', x.views=x.views+1, x.lastviewip=INET_ATON($)' : '') . ' 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, $viewincrement ? 1 : 0, qa_opt('hot_weight_views') * 4000); if ($viewincrement) { $arguments[] = qa_remote_ip_address(); } qa_db_query_raw(qa_db_apply_sub($query, $arguments)); } }
function qa_db_queuedcount_update() { if (qa_should_update_counts()) { qa_db_query_sub("REPLACE ^options (title, content) SELECT 'cache_queuedcount', COUNT(*) FROM ^posts WHERE type IN ('Q_QUEUED', 'A_QUEUED', 'C_QUEUED')"); } }
function qa_db_post_recount_flags($postid) { if (qa_should_update_counts()) { qa_db_query_sub('UPDATE ^posts AS x, (SELECT COALESCE(SUM(IF(flag, 1, 0)),0) AS flagcount FROM ^uservotes WHERE postid=#) AS a SET x.flagcount=a.flagcount WHERE x.postid=#', $postid, $postid); } }
function qa_db_uapprovecount_update() { if (qa_should_update_counts() && !QA_FINAL_EXTERNAL_USERS) { qa_db_query_sub("REPLACE ^options (title, content) SELECT 'cache_uapprovecount', COUNT(*) FROM ^users WHERE level<# AND NOT (flags&#)", QA_USER_LEVEL_APPROVED, QA_USER_FLAGS_USER_BLOCKED); } }
function qa_db_unupaqcount_update() { if (qa_should_update_counts()) { qa_db_query_sub("REPLACE ^options (title, content) SELECT 'cache_unupaqcount', COUNT(*) FROM ^posts WHERE type='Q' AND amaxvote=0"); } }
function qa_db_userpointscount_update() { if (qa_should_update_counts()) { qa_db_query_sub("REPLACE ^options (title, content) SELECT 'cache_userpointscount', COUNT(*) FROM ^userpoints"); } }
function qa_db_queuedcount_update() { if (qa_should_update_counts()) { qa_db_query_sub("INSERT INTO ^options (title, content) " . "SELECT 'cache_queuedcount', COUNT(*) FROM ^posts " . "WHERE type IN ('Q_QUEUED', 'A_QUEUED', 'C_QUEUED') " . "ON DUPLICATE KEY UPDATE content = VALUES(content)"); } }
function qa_db_flaggedcount_update() { if (qa_should_update_counts()) { qa_db_query_sub("REPLACE ^options (title, content) SELECT 'cache_flaggedcount', COUNT(*) FROM ^posts WHERE flagcount>0 AND type IN ('Q', 'A', 'C')"); } }
function qa_db_user_recount_posts($userid) { if (qa_should_update_counts()) { qa_db_query_sub("UPDATE ^users AS x, (SELECT COUNT(*) AS wallposts FROM ^messages WHERE touserid=# AND type='PUBLIC') AS a SET x.wallposts=a.wallposts WHERE x.userid=#", $userid, $userid); } }
function qa_db_uapprovecount_update() { if (qa_should_update_counts() && !QA_FINAL_EXTERNAL_USERS) { qa_db_query_sub("INSERT INTO ^options (title, content) " . "SELECT 'cache_uapprovecount', COUNT(*) FROM ^users " . "WHERE level < # AND NOT (flags & #) " . "ON DUPLICATE KEY UPDATE content = VALUES(content)", QA_USER_LEVEL_APPROVED, QA_USER_FLAGS_USER_BLOCKED); } }
function qa_db_userpointscount_update() { if (qa_should_update_counts()) { qa_db_query_sub("INSERT INTO ^options (title, content) " . "SELECT 'cache_userpointscount', COUNT(*) FROM ^userpoints " . "ON DUPLICATE KEY UPDATE content = VALUES(content)"); } }