From 5b57925d87f9cec699d9f39eb6d4d26b5cd8cc47 Mon Sep 17 00:00:00 2001 From: pupi1985 Date: Sat, 13 Aug 2022 02:04:38 -0300 Subject: [PATCH] Add ability to completely remove hotness calculation --- qa-include/app/options.php | 4 +++- qa-include/app/page.php | 6 ++++-- qa-include/app/post-create.php | 11 +++++++++-- qa-include/app/post-update.php | 7 ++++++- qa-include/app/posts.php | 10 ++++++++-- qa-include/app/q-list.php | 18 +++++++++++------- qa-include/app/votes.php | 2 +- qa-include/db/hotness.php | 5 +++++ qa-include/db/recalc.php | 10 ++++++++-- qa-include/lang/qa-lang-admin.php | 5 ++++- qa-include/lang/qa-lang-options.php | 6 +++++- qa-include/pages/admin/admin-default.php | 19 ++++++++++++++----- qa-include/pages/admin/admin-pages.php | 24 +++++++++++++++++------- 13 files changed, 95 insertions(+), 32 deletions(-) diff --git a/qa-include/app/options.php b/qa-include/app/options.php index 8e623253c..1407e75f2 100644 --- a/qa-include/app/options.php +++ b/qa-include/app/options.php @@ -203,6 +203,8 @@ function qa_default_option($name) { if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + $fixed_defaults = array( 'allow_anonymous_naming' => 1, 'allow_change_usernames' => 1, @@ -353,7 +355,7 @@ function qa_default_option($name) 'points_q_voted_max_loss' => 3, 'points_select_a' => 3, 'q_urls_title_length' => 50, - 'recalc_hotness_q_view' => 1, + 'recalc_hotness_frequency' => QA_HOTNESS_RECALC_ALWAYS, 'show_a_c_links' => 1, 'show_a_form_immediate' => 'if_no_as', 'show_c_reply_buttons' => 1, diff --git a/qa-include/app/page.php b/qa-include/app/page.php index 3d6fab214..bde070b4d 100644 --- a/qa-include/app/page.php +++ b/qa-include/app/page.php @@ -376,7 +376,7 @@ function qa_do_content_stats($qa_content) $viewsIncremented = qa_db_increment_views($qa_content['inc_views_postid']); - if ($viewsIncremented && qa_opt('recalc_hotness_q_view')) { + if ($viewsIncremented && (int)qa_opt('recalc_hotness_frequency') === QA_HOTNESS_RECALC_ALWAYS) { qa_db_hotness_update($qa_content['inc_views_postid']); } @@ -470,6 +470,8 @@ function qa_content_prepare($voting = false, $categoryids = array()) global $qa_template, $qa_page_error_html; + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + if (QA_DEBUG_PERFORMANCE) { global $qa_usage; $qa_usage->mark('control'); @@ -579,7 +581,7 @@ function qa_content_prepare($voting = false, $categoryids = array()) ); } - if (qa_opt('nav_hot')) { + if (qa_opt('nav_hot') && (int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { $qa_content['navigation']['main']['hot'] = array( 'url' => qa_path_html('hot'), 'label' => qa_lang_html('main/nav_hot'), diff --git a/qa-include/app/post-create.php b/qa-include/app/post-create.php index a69a23f01..a3d50d15d 100644 --- a/qa-include/app/post-create.php +++ b/qa-include/app/post-create.php @@ -69,6 +69,7 @@ function qa_question_create($followanswer, $userid, $handle, $cookieid, $title, $categoryid = null, $extravalue = null, $queued = false, $name = null) { require_once QA_INCLUDE_DIR . 'db/selects.php'; + require_once QA_INCLUDE_DIR . 'db/hotness.php'; $postid = qa_db_post_create($queued ? 'Q_QUEUED' : 'Q', @$followanswer['postid'], $userid, isset($userid) ? null : $cookieid, qa_remote_ip_address(), $title, $content, $format, $tagstring, qa_combine_notify_email($userid, $notify, $email), @@ -80,7 +81,9 @@ function qa_question_create($followanswer, $userid, $handle, $cookieid, $title, } qa_db_posts_calc_category_path($postid); - qa_db_hotness_update($postid); + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + qa_db_hotness_update($postid); + } if ($queued) { qa_db_queuedcount_update(); @@ -248,8 +251,12 @@ function qa_answer_create($userid, $handle, $cookieid, $content, $format, $text, */ function qa_update_q_counts_for_a($questionid) { + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + qa_db_post_acount_update($questionid); - qa_db_hotness_update($questionid); + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + qa_db_hotness_update($questionid); + } qa_db_acount_update(); qa_db_unaqcount_update(); qa_db_unupaqcount_update(); diff --git a/qa-include/app/post-update.php b/qa-include/app/post-update.php index 102dd43ca..ca2f36f50 100644 --- a/qa-include/app/post-update.php +++ b/qa-include/app/post-update.php @@ -386,7 +386,12 @@ function qa_question_set_status($oldquestion, $status, $userid, $handle, $cookie else { // ... otherwise we're approving original created post qa_db_post_set_created($oldquestion['postid'], null); - qa_db_hotness_update($oldquestion['postid']); + + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + qa_db_hotness_update($oldquestion['postid']); + } } } diff --git a/qa-include/app/posts.php b/qa-include/app/posts.php index d13c603f9..b4f5413d3 100644 --- a/qa-include/app/posts.php +++ b/qa-include/app/posts.php @@ -303,10 +303,16 @@ function qa_post_set_status($postid, $status, $byuserid = null) */ function qa_post_set_created($postid, $created) { - $oldpost = qa_post_get_full($postid); - qa_db_post_set_created($postid, $created); + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + + if ((int)qa_opt('recalc_hotness_frequency') === QA_HOTNESS_RECALC_NEVER) { + return; + } + + $oldpost = qa_post_get_full($postid); + switch ($oldpost['basetype']) { case 'Q': qa_db_hotness_update($postid); diff --git a/qa-include/app/q-list.php b/qa-include/app/q-list.php index f600042bb..7455fdb2a 100644 --- a/qa-include/app/q-list.php +++ b/qa-include/app/q-list.php @@ -159,6 +159,8 @@ function qa_q_list_page_content($questions, $pagesize, $start, $count, $sometitl */ function qa_qs_sub_navigation($sort, $categoryslugs) { + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + $request = 'questions'; if (isset($categoryslugs)) { @@ -167,17 +169,19 @@ function qa_qs_sub_navigation($sort, $categoryslugs) } } - $navigation = array( - 'recent' => array( - 'label' => qa_lang('main/nav_most_recent'), - 'url' => qa_path_html($request), - ), + $navigation['recent'] = array( + 'label' => qa_lang('main/nav_most_recent'), + 'url' => qa_path_html($request), + ); - 'hot' => array( + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + $navigation['hot'] = array( 'label' => qa_lang('main/nav_hot'), 'url' => qa_path_html($request, array('sort' => 'hot')), - ), + ); + } + $navigation += array( 'votes' => array( 'label' => qa_lang('main/nav_most_votes'), 'url' => qa_path_html($request, array('sort' => 'votes')), diff --git a/qa-include/app/votes.php b/qa-include/app/votes.php index 14f41c385..2617581ae 100644 --- a/qa-include/app/votes.php +++ b/qa-include/app/votes.php @@ -158,7 +158,7 @@ function qa_vote_set($post, $userid, $handle, $cookieid, $vote) qa_db_points_update_ifuser($post['userid'], array($prefix . 'voteds', 'upvoteds', 'downvoteds')); - if ($prefix === 'q') { + if ($prefix === 'q' && (int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { qa_db_hotness_update($post['postid']); } diff --git a/qa-include/db/hotness.php b/qa-include/db/hotness.php index 768e1d233..fc5a01983 100644 --- a/qa-include/db/hotness.php +++ b/qa-include/db/hotness.php @@ -25,6 +25,11 @@ } +define('QA_HOTNESS_RECALC_NEVER', 0); +define('QA_HOTNESS_RECALC_NO_Q_VIEW', 1); +define('QA_HOTNESS_RECALC_ALWAYS', 2); + + /** * Increment the views counter for the post (if different IP from last view). * @param int $postid The ID of the post diff --git a/qa-include/db/recalc.php b/qa-include/db/recalc.php index 351cd751b..891971de8 100644 --- a/qa-include/db/recalc.php +++ b/qa-include/db/recalc.php @@ -213,12 +213,16 @@ function qa_db_posts_get_for_recounting($startpostid, $count) */ function qa_db_posts_votes_recount($firstpostid, $lastpostid) { + require_once QA_INCLUDE_DIR . 'db/hotness.php'; + qa_db_query_sub( 'UPDATE ^posts AS x, (SELECT ^posts.postid, COALESCE(SUM(GREATEST(0,^uservotes.vote)),0) AS upvotes, -COALESCE(SUM(LEAST(0,^uservotes.vote)),0) AS downvotes, COALESCE(SUM(IF(^uservotes.flag, 1, 0)),0) AS flagcount FROM ^posts LEFT JOIN ^uservotes ON ^uservotes.postid=^posts.postid WHERE ^posts.postid>=# AND ^posts.postid<=# GROUP BY postid) AS a SET x.upvotes=a.upvotes, x.downvotes=a.downvotes, x.netvotes=a.upvotes-a.downvotes, x.flagcount=a.flagcount WHERE x.postid=a.postid', $firstpostid, $lastpostid ); - qa_db_hotness_update($firstpostid, $lastpostid); + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + qa_db_hotness_update($firstpostid, $lastpostid); + } } @@ -236,7 +240,9 @@ function qa_db_posts_answers_recount($firstpostid, $lastpostid) $firstpostid, $lastpostid ); - qa_db_hotness_update($firstpostid, $lastpostid); + if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + qa_db_hotness_update($firstpostid, $lastpostid); + } } diff --git a/qa-include/lang/qa-lang-admin.php b/qa-include/lang/qa-lang-admin.php index d1ea83f4a..2f3b28c9c 100644 --- a/qa-include/lang/qa-lang-admin.php +++ b/qa-include/lang/qa-lang-admin.php @@ -205,7 +205,10 @@ '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' => 'May slightly improve page speed if disabled, but hotness values will become out of date if views are included in hotness 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', // @deprecated + 'recalc_hotness_never_note' => 'Hotness is never recalculated or even used. Removes the hotness submenu in questions list. No performance impact.', + 'recalc_hotness_no_q_view_note' => 'Hotness is recalculated in several events (e.g. post creation) with the exception of question views. Medium performance impact.', + 'recalc_hotness_always_note' => 'Hotness is recalculated in all possible events, including question views. High performance impact.', 'recalc_points' => 'Recalculate user points', 'recalc_points_complete' => 'All user points were successfully recalculated.', 'recalc_points_note' => ' - for user ranking and points displays', diff --git a/qa-include/lang/qa-lang-options.php b/qa-include/lang/qa-lang-options.php index 0e11aee1e..2905ff615 100644 --- a/qa-include/lang/qa-lang-options.php +++ b/qa-include/lang/qa-lang-options.php @@ -238,7 +238,11 @@ 'points_vote_up_q' => 'Voting up a question:', 'q_urls_remove_accents' => 'Remove accents from question URLs:', 'q_urls_title_length' => 'Question title length in URLs:', - 'recalc_hotness_q_view' => 'Recalculate hotness on every question page view:', + 'recalc_hotness_always' => 'Always recalculate, including page views', + 'recalc_hotness_frequency' => 'Hotness recalculation frequency:', + 'recalc_hotness_never' => 'Never recalculate', + 'recalc_hotness_no_q_view' => 'Always recalculate, except for page views', + 'recalc_hotness_q_view' => 'Recalculate hotness on every question page view:', // @deprecated 'register_notify_admin' => 'Email me when a new user registers:', 'search_module' => 'Use search module:', 'show_a_form_immediate' => 'Show answer form immediately:', diff --git a/qa-include/pages/admin/admin-default.php b/qa-include/pages/admin/admin-default.php index b7ccc62c0..828d54bb5 100644 --- a/qa-include/pages/admin/admin-default.php +++ b/qa-include/pages/admin/admin-default.php @@ -25,6 +25,7 @@ } require_once QA_INCLUDE_DIR . 'db/admin.php'; +require_once QA_INCLUDE_DIR . 'db/hotness.php'; require_once QA_INCLUDE_DIR . 'db/maxima.php'; require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'app/options.php'; @@ -193,7 +194,6 @@ 'notify_admin_q_post' => 'checkbox', 'notify_users_default' => 'checkbox', 'q_urls_remove_accents' => 'checkbox', - 'recalc_hotness_q_view' => 'checkbox', 'register_notify_admin' => 'checkbox', 'show_c_reply_buttons' => 'checkbox', 'show_compact_numbers' => 'checkbox', @@ -388,7 +388,7 @@ case 'viewing': $subtitle = 'admin/viewing_title'; $showoptions = array( - 'q_urls_title_length', 'q_urls_remove_accents', 'do_count_q_views', 'show_view_counts', 'show_view_count_q_page', 'recalc_hotness_q_view', '', + 'q_urls_title_length', 'q_urls_remove_accents', 'do_count_q_views', 'show_view_counts', 'show_view_count_q_page', 'recalc_hotness_frequency', '', 'voting_on_qs', 'voting_on_q_page_only', 'voting_on_as', 'voting_on_cs', 'votes_separated', '', 'show_url_links', 'links_in_new_window', 'show_when_created', 'show_full_date_days' ); @@ -424,7 +424,6 @@ $checkboxtodisplay = array( 'show_view_counts' => 'option_do_count_q_views', 'show_view_count_q_page' => 'option_do_count_q_views', - 'recalc_hotness_q_view' => 'option_do_count_q_views', 'votes_separated' => 'option_voting_on_qs || option_voting_on_as', 'voting_on_q_page_only' => 'option_voting_on_qs', 'show_full_date_days' => 'option_show_when_created', @@ -1204,8 +1203,18 @@ function qa_optionfield_make_select(&$optionfield, $options, $value, $default) $optionfield['note'] = qa_lang_html('admin/characters'); break; - case 'recalc_hotness_q_view': - $optionfield['note'] = '?'; + case 'recalc_hotness_frequency': + $hotnessOptions = array( + QA_HOTNESS_RECALC_NEVER => 'recalc_hotness_never', + QA_HOTNESS_RECALC_NO_Q_VIEW => 'recalc_hotness_no_q_view', + QA_HOTNESS_RECALC_ALWAYS => 'recalc_hotness_always', + ); + + qa_optionfield_make_select($optionfield, array_map(function ($optionValue) { + return qa_lang_html('options/' . $optionValue); + }, $hotnessOptions), $value, QA_HOTNESS_RECALC_ALWAYS); + // recalc_hotness_always_note, recalc_hotness_no_q_view_note, recalc_hotness_never_note + $optionfield['note'] = sprintf('?', qa_lang_html(sprintf('admin/%s_note', isset($hotnessOptions[$value]) ? $hotnessOptions[$value] : reset($hotnessOptions)))); break; case 'min_num_q_tags': diff --git a/qa-include/pages/admin/admin-pages.php b/qa-include/pages/admin/admin-pages.php index af82bc96d..757b15d30 100644 --- a/qa-include/pages/admin/admin-pages.php +++ b/qa-include/pages/admin/admin-pages.php @@ -26,6 +26,7 @@ require_once QA_INCLUDE_DIR . 'app/admin.php'; require_once QA_INCLUDE_DIR . 'app/format.php'; +require_once QA_INCLUDE_DIR . 'db/hotness.php'; require_once QA_INCLUDE_DIR . 'db/selects.php'; @@ -63,12 +64,6 @@ 'nav_activity' => 'main/nav_activity', $hascustomhome ? 'nav_qa_not_home' : 'nav_qa_is_home' => $hascustomhome ? 'main/nav_qa' : 'admin/nav_qa_is_home', 'nav_questions' => 'main/nav_qs', - 'nav_hot' => 'main/nav_hot', - 'nav_unanswered' => 'main/nav_unanswered', - 'nav_tags' => 'main/nav_tags', - 'nav_categories' => 'main/nav_categories', - 'nav_users' => 'main/nav_users', - 'nav_ask' => 'main/nav_ask', ); $navpaths = array( @@ -77,7 +72,22 @@ 'nav_qa_not_home' => 'qa', 'nav_qa_is_home' => '', 'nav_questions' => 'questions', - 'nav_hot' => 'hot', +); + +if ((int)qa_opt('recalc_hotness_frequency') > QA_HOTNESS_RECALC_NEVER) { + $navoptions['nav_hot'] = 'main/nav_hot'; + $navpaths['nav_hot'] = 'hot'; +} + +$navoptions += array( + 'nav_unanswered' => 'main/nav_unanswered', + 'nav_tags' => 'main/nav_tags', + 'nav_categories' => 'main/nav_categories', + 'nav_users' => 'main/nav_users', + 'nav_ask' => 'main/nav_ask', +); + +$navpaths += array( 'nav_unanswered' => 'unanswered', 'nav_tags' => 'tags', 'nav_categories' => 'categories',