function qa_db_posts_recount($firstpostid, $lastpostid) { require_once QA_INCLUDE_DIR . 'qa-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_query_sub('UPDATE ^posts AS x, (SELECT parents.postid, COUNT(children.postid) AS acount FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type=\'A\' WHERE parents.postid>=# AND parents.postid<=# GROUP BY postid) AS a SET x.acount=a.acount WHERE x.postid=a.postid', $firstpostid, $lastpostid); qa_db_hotness_update($firstpostid, $lastpostid); }
function qa_vote_set($post, $userid, $handle, $cookieid, $vote) { require_once QA_INCLUDE_DIR . 'qa-db-points.php'; require_once QA_INCLUDE_DIR . 'qa-db-hotness.php'; require_once QA_INCLUDE_DIR . 'qa-db-votes.php'; require_once QA_INCLUDE_DIR . 'qa-app-limits.php'; $vote = (int) min(1, max(-1, $vote)); $oldvote = (int) qa_db_uservote_get($post['postid'], $userid); qa_db_uservote_set($post['postid'], $userid, $vote); qa_db_post_recount_votes($post['postid']); $postisanswer = $post['basetype'] == 'A'; $columns = array(); if ($vote > 0 || $oldvote > 0) { $columns[] = $postisanswer ? 'aupvotes' : 'qupvotes'; } if ($vote < 0 || $oldvote < 0) { $columns[] = $postisanswer ? 'adownvotes' : 'qdownvotes'; } qa_db_points_update_ifuser($userid, $columns); qa_db_points_update_ifuser($post['userid'], array($postisanswer ? 'avoteds' : 'qvoteds', 'upvoteds', 'downvoteds')); if ($post['basetype'] == 'Q') { qa_db_hotness_update($post['postid']); } if ($vote < 0) { $action = $postisanswer ? 'a_vote_down' : 'q_vote_down'; } elseif ($vote > 0) { $action = $postisanswer ? 'a_vote_up' : 'q_vote_up'; } else { $action = $postisanswer ? 'a_vote_nil' : 'q_vote_nil'; } qa_report_write_action($userid, null, $action, $postisanswer ? null : $post['postid'], $postisanswer ? $post['postid'] : null, null); qa_report_event($action, $userid, $handle, $cookieid, array('postid' => $post['postid'], 'vote' => $vote, 'oldvote' => $oldvote)); }
function qa_question_set_status($oldquestion, $status, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null) { require_once QA_INCLUDE_DIR . 'qa-app-format.php'; require_once QA_INCLUDE_DIR . 'qa-app-updates.php'; $washidden = $oldquestion['type'] == 'Q_HIDDEN'; $wasqueued = $oldquestion['type'] == 'Q_QUEUED'; $wasrequeued = $wasqueued && isset($oldquestion['updated']); qa_post_unindex($oldquestion['postid']); foreach ($answers as $answer) { qa_post_unindex($answer['postid']); } foreach ($commentsfollows as $comment) { if ($comment['basetype'] == 'C') { qa_post_unindex($comment['postid']); } } if (@$closepost['parentid'] == $oldquestion['postid']) { qa_post_unindex($closepost['postid']); } $setupdated = false; $event = null; if ($status == QA_POST_STATUS_QUEUED) { $newtype = 'Q_QUEUED'; if (!$wasqueued) { $event = 'q_requeue'; } // same event whether it was hidden or shown before } elseif ($status == QA_POST_STATUS_HIDDEN) { $newtype = 'Q_HIDDEN'; if (!$washidden) { $event = $wasqueued ? 'q_reject' : 'q_hide'; if (!$wasqueued) { $setupdated = true; } } } elseif ($status == QA_POST_STATUS_NORMAL) { $newtype = 'Q'; if ($wasqueued) { $event = 'q_approve'; } elseif ($washidden) { $event = 'q_reshow'; $setupdated = true; } } else { qa_fatal_error('Unknown status in qa_question_set_status(): ' . $status); } qa_db_post_set_type($oldquestion['postid'], $newtype, $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE); if ($wasqueued && $status == QA_POST_STATUS_NORMAL && qa_opt('moderate_update_time')) { // ... for approval of a post, can set time to now instead if ($wasrequeued) { // reset edit time to now if there was one, since we're approving the edit... qa_db_post_set_updated($oldquestion['postid'], null); } else { // ... otherwise we're approving original created post qa_db_post_set_created($oldquestion['postid'], null); qa_db_hotness_update($oldquestion['postid']); } } qa_update_counts_for_q($oldquestion['postid']); qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects')); if ($wasqueued || $status == QA_POST_STATUS_QUEUED) { qa_db_queuedcount_update(); } if ($oldquestion['flagcount']) { qa_db_flaggedcount_update(); } if ($status == QA_POST_STATUS_NORMAL) { qa_post_index($oldquestion['postid'], 'Q', $oldquestion['postid'], $oldquestion['parentid'], $oldquestion['title'], $oldquestion['content'], $oldquestion['format'], qa_viewer_text($oldquestion['content'], $oldquestion['format']), $oldquestion['tags'], $oldquestion['categoryid']); foreach ($answers as $answer) { if ($answer['type'] == 'A') { // even if question visible, don't index hidden or queued answers qa_post_index($answer['postid'], $answer['type'], $oldquestion['postid'], $answer['parentid'], null, $answer['content'], $answer['format'], qa_viewer_text($answer['content'], $answer['format']), null, $answer['categoryid']); } } foreach ($commentsfollows as $comment) { if ($comment['type'] == 'C') { $answer = @$answers[$comment['parentid']]; if (!isset($answer) || $answer['type'] == 'A') { // don't index comment if it or its parent is hidden qa_post_index($comment['postid'], $comment['type'], $oldquestion['postid'], $comment['parentid'], null, $comment['content'], $comment['format'], qa_viewer_text($comment['content'], $comment['format']), null, $comment['categoryid']); } } } if ($closepost['parentid'] == $oldquestion['postid']) { qa_post_index($closepost['postid'], $closepost['type'], $oldquestion['postid'], $closepost['parentid'], null, $closepost['content'], $closepost['format'], qa_viewer_text($closepost['content'], $closepost['format']), null, $closepost['categoryid']); } } $eventparams = array('postid' => $oldquestion['postid'], 'parentid' => $oldquestion['parentid'], 'parent' => isset($oldquestion['parentid']) ? qa_db_single_select(qa_db_full_post_selectspec(null, $oldquestion['parentid'])) : null, 'title' => $oldquestion['title'], 'content' => $oldquestion['content'], 'format' => $oldquestion['format'], 'text' => qa_viewer_text($oldquestion['content'], $oldquestion['format']), 'tags' => $oldquestion['tags'], 'categoryid' => $oldquestion['categoryid'], 'name' => $oldquestion['name']); if (isset($event)) { qa_report_event($event, $userid, $handle, $cookieid, $eventparams + array('oldquestion' => $oldquestion)); } if ($wasqueued && $status == QA_POST_STATUS_NORMAL && !$wasrequeued) { require_once QA_INCLUDE_DIR . 'qa-db-selects.php'; require_once QA_INCLUDE_DIR . 'qa-util-string.php'; qa_report_event('q_post', $oldquestion['userid'], $oldquestion['handle'], $oldquestion['cookieid'], $eventparams + array('notify' => isset($oldquestion['notify']), 'email' => qa_email_validate($oldquestion['notify']) ? $oldquestion['notify'] : null, 'delayed' => $oldquestion['created'])); } }
function qa_post_set_created($postid, $created) { $oldpost = qa_post_get_full($postid); qa_db_post_set_created($postid, $created); switch ($oldpost['basetype']) { case 'Q': qa_db_hotness_update($postid); break; case 'A': qa_db_hotness_update($oldpost['parentid']); break; } }
function qa_vote_set($post, $userid, $handle, $cookieid, $vote) { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } require_once QA_INCLUDE_DIR . 'db/points.php'; require_once QA_INCLUDE_DIR . 'db/hotness.php'; require_once QA_INCLUDE_DIR . 'db/votes.php'; require_once QA_INCLUDE_DIR . 'db/post-create.php'; require_once QA_INCLUDE_DIR . 'app/limits.php'; $vote = (int) min(1, max(-1, $vote)); $oldvote = (int) qa_db_uservote_get($post['postid'], $userid); qa_db_uservote_set($post['postid'], $userid, $vote); qa_db_post_recount_votes($post['postid']); $postisanswer = $post['basetype'] == 'A'; if ($postisanswer) { qa_db_post_acount_update($post['parentid']); qa_db_unupaqcount_update(); } $columns = array(); if ($vote > 0 || $oldvote > 0) { $columns[] = $postisanswer ? 'aupvotes' : 'qupvotes'; } if ($vote < 0 || $oldvote < 0) { $columns[] = $postisanswer ? 'adownvotes' : 'qdownvotes'; } qa_db_points_update_ifuser($userid, $columns); qa_db_points_update_ifuser($post['userid'], array($postisanswer ? 'avoteds' : 'qvoteds', 'upvoteds', 'downvoteds')); if ($post['basetype'] == 'Q') { qa_db_hotness_update($post['postid']); } if ($vote < 0) { $event = $postisanswer ? 'a_vote_down' : 'q_vote_down'; } elseif ($vote > 0) { $event = $postisanswer ? 'a_vote_up' : 'q_vote_up'; } else { $event = $postisanswer ? 'a_vote_nil' : 'q_vote_nil'; } qa_report_event($event, $userid, $handle, $cookieid, array('postid' => $post['postid'], 'userid' => $post['userid'], 'vote' => $vote, 'oldvote' => $oldvote)); }
function qa_do_content_stats($qa_content) { if (isset($qa_content['inc_views_postid'])) { require_once QA_INCLUDE_DIR . 'db/hotness.php'; qa_db_hotness_update($qa_content['inc_views_postid'], null, true); return true; } return false; }
function qa_db_posts_answers_recount($firstpostid, $lastpostid) { require_once QA_INCLUDE_DIR . 'qa-db-hotness.php'; qa_db_query_sub('UPDATE ^posts AS x, (SELECT parents.postid, COUNT(children.postid) AS acount, COALESCE(GREATEST(MAX(children.netvotes), 0), 0) AS amaxvote FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type=\'A\' WHERE parents.postid>=# AND parents.postid<=# GROUP BY postid) AS a SET x.acount=a.acount, x.amaxvote=a.amaxvote WHERE x.postid=a.postid', $firstpostid, $lastpostid); qa_db_hotness_update($firstpostid, $lastpostid); }
function qa_answer_create($userid, $handle, $cookieid, $content, $format, $text, $notify, $email, $question, $queued = false) { $postid = qa_db_post_create($queued ? 'A_QUEUED' : 'A', $question['postid'], $userid, isset($userid) ? null : $cookieid, qa_remote_ip_address(), null, $content, $format, null, qa_combine_notify_email($userid, $notify, $email), $question['categoryid']); qa_db_posts_calc_category_path($postid); if (!$queued) { if ($question['type'] == 'Q') { // don't index answer if parent question is hidden or queued qa_post_index($postid, 'A', $question['postid'], $question['postid'], null, $content, $format, $text, null, $question['categoryid']); } qa_db_post_acount_update($question['postid']); qa_db_hotness_update($question['postid']); qa_db_points_update_ifuser($userid, 'aposts'); qa_db_acount_update(); qa_db_unaqcount_update(); } qa_report_event($queued ? 'a_queue' : 'a_post', $userid, $handle, $cookieid, array('postid' => $postid, 'parentid' => $question['postid'], 'parent' => $question, 'content' => $content, 'format' => $format, 'text' => $text, 'categoryid' => $question['categoryid'], 'notify' => $notify, 'email' => $email)); return $postid; }
function qa_answer_to_comment($oldanswer, $parentid, $content, $format, $text, $notify, $userid, $handle, $cookieid, $question, $answers, $commentsfollows) { $parent = isset($answers[$parentid]) ? $answers[$parentid] : $question; qa_post_unindex($oldanswer['postid']); $setupdated = strcmp($oldanswer['content'], $content) || strcmp($oldanswer['format'], $format); $setuserid = $setupdated ? $userid : null; $setip = $setupdated ? qa_remote_ip_address() : null; qa_db_post_set_type($oldanswer['postid'], $oldanswer['hidden'] ? 'C_HIDDEN' : 'C', $setuserid, $setip); qa_db_post_set_parent($oldanswer['postid'], $parentid, $setuserid, $setip); qa_db_post_set_content($oldanswer['postid'], $oldanswer['title'], $content, $format, $oldanswer['tags'], $notify, $setuserid, $setip); foreach ($commentsfollows as $commentfollow) { if ($commentfollow['parentid'] == $oldanswer['postid']) { // do same thing for comments and follows qa_db_post_set_parent($commentfollow['postid'], $parentid, null, null); } } qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds', 'cposts')); qa_db_post_acount_update($question['postid']); qa_db_hotness_update($question['postid']); qa_db_acount_update(); qa_db_ccount_update(); qa_db_unaqcount_update(); if (!($oldanswer['hidden'] || $question['hidden'] || $parent['hidden'])) { // only index if none of the things it depends on are hidden require_once QA_INCLUDE_DIR . 'qa-app-format.php'; qa_post_index($oldanswer['postid'], 'C', $question['postid'], null, $text, null); } qa_report_event('a_to_c', $userid, $handle, $cookieid, array('postid' => $oldanswer['postid'], 'parentid' => $parentid, 'parenttype' => $parent['basetype'], 'questionid' => $question['postid'], 'content' => $content, 'format' => $format, 'text' => $text, 'oldcontent' => $oldanswer['content'], 'oldformat' => $oldanswer['format'])); }
function qa_answer_create($userid, $handle, $cookieid, $content, $format, $text, $notify, $email, $question) { $postid = qa_db_post_create('A', $question['postid'], $userid, isset($userid) ? null : $cookieid, qa_remote_ip_address(), null, $content, $format, null, qa_combine_notify_email($userid, $notify, $email), $question['categoryid']); qa_db_posts_calc_category_path($postid); if (!$question['hidden']) { // don't index answer if parent question is hidden qa_post_index($postid, 'A', $question['postid'], null, $text, null); } qa_db_post_acount_update($question['postid']); qa_db_hotness_update($question['postid']); qa_db_points_update_ifuser($userid, 'aposts'); qa_db_acount_update(); qa_db_unaqcount_update(); if (isset($question['notify']) && !qa_post_is_by_user($question, $userid, $cookieid)) { require_once QA_INCLUDE_DIR . 'qa-app-emails.php'; require_once QA_INCLUDE_DIR . 'qa-app-options.php'; require_once QA_INCLUDE_DIR . 'qa-util-string.php'; $blockwordspreg = qa_get_block_words_preg(); $sendtitle = qa_block_words_replace($question['title'], $blockwordspreg); $sendtext = qa_block_words_replace($text, $blockwordspreg); qa_send_notification($question['userid'], $question['notify'], @$question['handle'], qa_lang('emails/q_answered_subject'), qa_lang('emails/q_answered_body'), array('^a_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'), '^q_title' => $sendtitle, '^a_content' => $sendtext, '^url' => qa_path(qa_q_request($question['postid'], $sendtitle), null, qa_opt('site_url'), null, qa_anchor('A', $postid)))); } // notify all members of the category about the new post require_once QA_INCLUDE_DIR . 'qa-app-emails.php'; require_once QA_INCLUDE_DIR . 'mp-db-users.php'; require_once QA_INCLUDE_DIR . 'mp-app-users.php'; // find list of members of the category $recipients = mp_get_category_userids(mp_get_categoryid()); // remove the current user from the list of recipients unset($recipients[$userid]); // get details for current category $currentcategory = mp_get_categoryinfo(mp_get_categoryid()); foreach ($recipients as $recipient) { // retrieve the user flags $userflags = mp_get_user_flags($recipient['userid']); // check user flags to determine whether user should be notified or not // of the new answer post if (!($userflags & QA_USER_FLAGS_NOTIFY_ANSWERS)) { // for each member, send notification qa_send_notification($recipient['userid'], null, null, qa_lang('emails/q_answered_with_category_subject'), qa_lang('emails/q_answered_with_category_body'), array('^a_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'), '^q_title' => $question['title'], '^a_content' => $text, '^url' => qa_path(qa_q_request($question['postid'], $sendtitle), null, qa_opt('site_url'), null, qa_anchor('A', $postid)), '^category_title' => $currentcategory['title'] . $userflags)); } } qa_report_event('a_post', $userid, $handle, $cookieid, array('postid' => $postid, 'parentid' => $question['postid'], 'content' => $content, 'format' => $format, 'text' => $text, 'categoryid' => $question['categoryid'], 'notify' => $notify, 'email' => $email)); return $postid; }
function qa_update_q_counts_for_a($questionid) { qa_db_post_acount_update($questionid); qa_db_hotness_update($questionid); qa_db_acount_update(); qa_db_unaqcount_update(); qa_db_unupaqcount_update(); }
} $qa_content['script'] = $script; // Load the appropriate theme class and output the page $themeclass = qa_load_theme_class(qa_opt('site_theme'), $qa_template, $qa_content, $qa_request); header('Content-type: ' . $qa_content['content_type']); $themeclass->doctype(); $themeclass->html(); $themeclass->finish(); // End of output phase if (QA_DEBUG_PERFORMANCE) { qa_usage_mark('theme'); } // Increment question view counter (do at very end so page can be output first) if (isset($qa_content['inc_views_postid'])) { require_once QA_INCLUDE_DIR . 'qa-db-hotness.php'; qa_db_hotness_update($qa_content['inc_views_postid'], null, true); if (QA_DEBUG_PERFORMANCE) { qa_usage_mark('stats'); } } // Output the usage to the page if (QA_DEBUG_PERFORMANCE) { qa_usage_output(); } } // Disconnect from the database qa_base_db_disconnect(); // Functions used in this file, or made available to any other files that generate Q2A pages function qa_page_db_fail_handler($type, $errno = null, $error = null, $query = null) { $pass_failure_type = $type;
function qa_answer_to_comment($oldanswer, $parentid, $content, $format, $text, $notify, $userid, $handle, $cookieid, $question, $answers, $commentsfollows) { $parent = isset($answers[$parentid]) ? $answers[$parentid] : $question; qa_post_unindex($oldanswer['postid']); $contentchanged = strcmp($oldanswer['content'], $content) || strcmp($oldanswer['format'], $format); qa_db_post_set_type($oldanswer['postid'], substr_replace($oldanswer['type'], 'C', 0, 1), $userid, qa_remote_ip_address(), QA_UPDATE_TYPE); qa_db_post_set_parent($oldanswer['postid'], $parentid); qa_db_post_set_content($oldanswer['postid'], $oldanswer['title'], $content, $format, $oldanswer['tags'], $notify, $contentchanged ? $userid : null, $contentchanged ? qa_remote_ip_address() : null); foreach ($commentsfollows as $commentfollow) { if ($commentfollow['parentid'] == $oldanswer['postid']) { // do same thing for comments and follows qa_db_post_set_parent($commentfollow['postid'], $parentid); } } qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds', 'cposts')); qa_db_post_acount_update($question['postid']); qa_db_hotness_update($question['postid']); qa_db_acount_update(); qa_db_ccount_update(); qa_db_unaqcount_update(); qa_db_unupaqcount_update(); if ($oldanswer['type'] == 'A' && $question['type'] == 'Q' && ($parent['type'] == 'Q' || $parent['type'] == 'A')) { // only if all fully visible qa_post_index($oldanswer['postid'], 'C', $question['postid'], $parentid, null, $content, $format, $text, null, $oldanswer['categoryid']); } qa_report_event('a_to_c', $userid, $handle, $cookieid, array('postid' => $oldanswer['postid'], 'parentid' => $parentid, 'parenttype' => $parent['basetype'], 'questionid' => $question['postid'], 'content' => $content, 'format' => $format, 'text' => $text, 'oldcontent' => $oldanswer['content'], 'oldformat' => $oldanswer['format'], 'oldanswer' => $oldanswer, 'contentchanged' => $contentchanged)); }