$count) { if ($count > QA_DB_MAX_WORD_COUNT) { $count = QA_DB_MAX_WORD_COUNT; } $rowstoadd[] = array($postid, $wordid, $count, $type, $questionid); } qa_db_query_sub( 'INSERT INTO ^contentwords (postid, wordid, count, type, questionid) VALUES #', $rowstoadd ); } } /** * Add rows into the database index of individual tag words, where $postid contains the words $wordids * @param $postid * @param $wordids */ function qa_db_tagwords_add_post_wordids($postid, $wordids) { if (count($wordids)) { $rowstoadd = array(); foreach ($wordids as $wordid) $rowstoadd[] = array($postid, $wordid); qa_db_query_sub( 'INSERT INTO ^tagwords (postid, wordid) VALUES #', $rowstoadd ); } } /** * Add rows into the database index of whole tags, where $postid contains the tags $wordids * @param $postid * @param $wordids */ function qa_db_posttags_add_post_wordids($postid, $wordids) { if (count($wordids)) { qa_db_query_sub( 'INSERT INTO ^posttags (postid, wordid, postcreated) SELECT postid, wordid, created FROM ^words, ^posts WHERE postid=# AND wordid IN ($)', $postid, $wordids ); } } /** * Return an array mapping each word in $words to its corresponding wordid in the database * @param $words * @return array */ function qa_db_word_mapto_ids($words) { if (count($words)) { return qa_db_read_all_assoc(qa_db_query_sub( 'SELECT wordid, word FROM ^words WHERE word IN ($)', $words ), 'word', 'wordid'); } return array(); } /** * Return an array mapping each word in $words to its corresponding wordid in the database, adding any that are missing * @param $words * @return array */ function qa_db_word_mapto_ids_add($words) { $wordtoid = qa_db_word_mapto_ids($words); $wordstoadd = array(); foreach ($words as $word) { if (!isset($wordtoid[$word])) $wordstoadd[] = $word; } if (count($wordstoadd)) { qa_db_query_sub('LOCK TABLES ^words WRITE'); // to prevent two requests adding the same word $wordtoid = qa_db_word_mapto_ids($words); // map it again in case table content changed before it was locked $rowstoadd = array(); foreach ($words as $word) { if (!isset($wordtoid[$word])) $rowstoadd[] = array($word); } qa_db_query_sub('INSERT IGNORE INTO ^words (word) VALUES $', $rowstoadd); qa_db_query_sub('UNLOCK TABLES'); $wordtoid = qa_db_word_mapto_ids($words); // do it one last time } return $wordtoid; } /** * Update the titlecount column in the database for the words in $wordids, based on how many posts they appear in the title of * @param $wordids */ function qa_db_word_titlecount_update($wordids) { if (qa_should_update_counts() && count($wordids)) { qa_db_query_sub( 'UPDATE ^words AS x, (SELECT ^words.wordid, COUNT(^titlewords.wordid) AS titlecount FROM ^words LEFT JOIN ^titlewords ON ^titlewords.wordid=^words.wordid WHERE ^words.wordid IN (#) GROUP BY wordid) AS a SET x.titlecount=a.titlecount WHERE x.wordid=a.wordid', $wordids ); } } /** * Update the contentcount column in the database for the words in $wordids, based on how many posts they appear in the content of * @param $wordids */ function qa_db_word_contentcount_update($wordids) { if (qa_should_update_counts() && count($wordids)) { qa_db_query_sub( 'UPDATE ^words AS x, (SELECT ^words.wordid, COUNT(^contentwords.wordid) AS contentcount FROM ^words LEFT JOIN ^contentwords ON ^contentwords.wordid=^words.wordid WHERE ^words.wordid IN (#) GROUP BY wordid) AS a SET x.contentcount=a.contentcount WHERE x.wordid=a.wordid', $wordids ); } } /** * Update the tagwordcount column in the database for the individual tag words in $wordids, based on how many posts they appear in the tags of * @param $wordids */ function qa_db_word_tagwordcount_update($wordids) { if (qa_should_update_counts() && count($wordids)) { qa_db_query_sub( 'UPDATE ^words AS x, (SELECT ^words.wordid, COUNT(^tagwords.wordid) AS tagwordcount FROM ^words LEFT JOIN ^tagwords ON ^tagwords.wordid=^words.wordid WHERE ^words.wordid IN (#) GROUP BY wordid) AS a SET x.tagwordcount=a.tagwordcount WHERE x.wordid=a.wordid', $wordids ); } } /** * Update the tagcount column in the database for the whole tags in $wordids, based on how many posts they appear as tags of * @param $wordids */ function qa_db_word_tagcount_update($wordids) { if (qa_should_update_counts() && count($wordids)) { qa_db_query_sub( 'UPDATE ^words AS x, (SELECT ^words.wordid, COUNT(^posttags.wordid) AS tagcount FROM ^words LEFT JOIN ^posttags ON ^posttags.wordid=^words.wordid WHERE ^words.wordid IN (#) GROUP BY wordid) AS a SET x.tagcount=a.tagcount WHERE x.wordid=a.wordid', $wordids ); } } /** * Update the cached count in the database of the number of questions (excluding hidden/queued) */ function qa_db_qcount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_qcount', COUNT(*) FROM ^posts " . "WHERE type = 'Q' " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of answers (excluding hidden/queued) */ function qa_db_acount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_acount', COUNT(*) FROM ^posts " . "WHERE type = 'A' " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of comments (excluding hidden/queued) */ function qa_db_ccount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_ccount', COUNT(*) FROM ^posts " . "WHERE type = 'C' " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of different tags used */ function qa_db_tagcount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_tagcount', COUNT(*) FROM ^words " . "WHERE tagcount > 0 " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of unanswered questions (excluding hidden/queued) */ function qa_db_unaqcount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_unaqcount', COUNT(*) FROM ^posts " . "WHERE type = 'Q' AND acount = 0 AND closedbyid IS NULL " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of questions with no answer selected (excluding hidden/queued) */ function qa_db_unselqcount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_unselqcount', COUNT(*) FROM ^posts " . "WHERE type = 'Q' AND selchildid IS NULL AND closedbyid IS NULL " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of questions with no upvoted answers (excluding hidden/queued) */ function qa_db_unupaqcount_update() { if (qa_should_update_counts()) { qa_db_query_sub( "INSERT INTO ^options (title, content) " . "SELECT 'cache_unupaqcount', COUNT(*) FROM ^posts " . "WHERE type = 'Q' AND amaxvote = 0 AND closedbyid IS NULL " . "ON DUPLICATE KEY UPDATE content = VALUES(content)" ); } } /** * Update the cached count in the database of the number of posts which are queued for moderation */ 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)" ); } }