$qa_content['form_message'] = array('tags' => 'method="post" action="' . qa_self_html() . '"', 'style' => 'tall', 'fields' => array('message' => array('type' => $messagesent ? 'static' : '', 'label' => qa_lang_html_sub('misc/message_for_x', qa_get_one_user_html($handle, false)), 'tags' => 'name="message" id="message"', 'value' => qa_html(@$inmessage, $messagesent), 'rows' => 8, 'note' => qa_lang_html_sub('misc/message_explanation', qa_html(qa_opt('site_title'))), 'error' => qa_html(@$errors['message']))), 'buttons' => array('send' => array('tags' => 'onclick="qa_show_waiting_after(this, false);"', 'label' => qa_lang_html('main/send_button'))), 'hidden' => array('domessage' => '1', 'code' => qa_get_form_security_code('message-' . $handle))); $qa_content['focusid'] = 'message'; if ($messagesent) { $qa_content['form_message']['ok'] = qa_lang_html('misc/message_sent'); unset($qa_content['form_message']['buttons']); if (qa_opt('show_message_history')) { unset($qa_content['form_message']['fields']['message']); } else { unset($qa_content['form_message']['fields']['message']['note']); unset($qa_content['form_message']['fields']['message']['label']); } } // If relevant, show recent message history if (qa_opt('show_message_history')) { $recent = array_merge($torecent, $fromrecent); qa_sort_by($recent, 'created'); $showmessages = array_slice(array_reverse($recent, true), 0, QA_DB_RETRIEVE_MESSAGES); if (count($showmessages)) { $qa_content['message_list'] = array('title' => qa_lang_html_sub('misc/message_recent_history', qa_html($toaccount['handle']))); $options = qa_message_html_defaults(); foreach ($showmessages as $message) { $qa_content['message_list']['messages'][] = qa_message_html_fields($message, $options); } } } $qa_content['raw']['account'] = $toaccount; // for plugin layers to access return $qa_content; /* Omit PHP closing tag to help avoid accidental output */
function qa_recalc_perform_step(&$state) { $continue = false; @(list($operation, $length, $next, $done) = explode("\t", $state)); switch ($operation) { case 'doreindexcontent': qa_recalc_transition($state, 'doreindexcontent_pagereindex'); break; case 'doreindexcontent_pagereindex': $pages = qa_db_pages_get_for_reindexing($next, 10); if (count($pages)) { require_once QA_INCLUDE_DIR . 'app/format.php'; $lastpageid = max(array_keys($pages)); foreach ($pages as $pageid => $page) { if (!($page['flags'] & QA_PAGE_FLAGS_EXTERNAL)) { $searchmodules = qa_load_modules_with('search', 'unindex_page'); foreach ($searchmodules as $searchmodule) { $searchmodule->unindex_page($pageid); } $searchmodules = qa_load_modules_with('search', 'index_page'); if (count($searchmodules)) { $indextext = qa_viewer_text($page['content'], 'html'); foreach ($searchmodules as $searchmodule) { $searchmodule->index_page($pageid, $page['tags'], $page['heading'], $page['content'], 'html', $indextext); } } } } $next = 1 + $lastpageid; $done += count($pages); $continue = true; } else { qa_recalc_transition($state, 'doreindexcontent_postcount'); } break; case 'doreindexcontent_postcount': qa_db_qcount_update(); qa_db_acount_update(); qa_db_ccount_update(); qa_recalc_transition($state, 'doreindexcontent_postreindex'); break; case 'doreindexcontent_postreindex': $posts = qa_db_posts_get_for_reindexing($next, 10); if (count($posts)) { require_once QA_INCLUDE_DIR . 'app/format.php'; $lastpostid = max(array_keys($posts)); qa_db_prepare_for_reindexing($next, $lastpostid); qa_suspend_update_counts(); foreach ($posts as $postid => $post) { qa_post_unindex($postid); qa_post_index($postid, $post['type'], $post['questionid'], $post['parentid'], $post['title'], $post['content'], $post['format'], qa_viewer_text($post['content'], $post['format']), $post['tags'], $post['categoryid']); } $next = 1 + $lastpostid; $done += count($posts); $continue = true; } else { qa_db_truncate_indexes($next); qa_recalc_transition($state, 'doreindexposts_wordcount'); } break; case 'doreindexposts_wordcount': $wordids = qa_db_words_prepare_for_recounting($next, 1000); if (count($wordids)) { $lastwordid = max($wordids); qa_db_words_recount($next, $lastwordid); $next = 1 + $lastwordid; $done += count($wordids); $continue = true; } else { qa_db_tagcount_update(); // this is quick so just do it here qa_recalc_transition($state, 'doreindexposts_complete'); } break; case 'dorecountposts': qa_recalc_transition($state, 'dorecountposts_postcount'); break; case 'dorecountposts_postcount': qa_db_qcount_update(); qa_db_acount_update(); qa_db_ccount_update(); qa_db_unaqcount_update(); qa_db_unselqcount_update(); qa_recalc_transition($state, 'dorecountposts_votecount'); break; case 'dorecountposts_votecount': $postids = qa_db_posts_get_for_recounting($next, 1000); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_votes_recount($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_recalc_transition($state, 'dorecountposts_acount'); } break; case 'dorecountposts_acount': $postids = qa_db_posts_get_for_recounting($next, 1000); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_answers_recount($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_db_unupaqcount_update(); qa_recalc_transition($state, 'dorecountposts_complete'); } break; case 'dorecalcpoints': qa_recalc_transition($state, 'dorecalcpoints_usercount'); break; case 'dorecalcpoints_usercount': qa_db_userpointscount_update(); // for progress update - not necessarily accurate qa_db_uapprovecount_update(); // needs to be somewhere and this is the most appropriate place qa_recalc_transition($state, 'dorecalcpoints_recalc'); break; case 'dorecalcpoints_recalc': $recalccount = 10; $userids = qa_db_users_get_for_recalc_points($next, $recalccount + 1); // get one extra so we know where to start from next $gotcount = count($userids); $recalccount = min($recalccount, $gotcount); // can't recalc more than we got if ($recalccount > 0) { $lastuserid = $userids[$recalccount - 1]; qa_db_users_recalc_points($next, $lastuserid); $done += $recalccount; } else { $lastuserid = $next; } // for truncation if ($gotcount > $recalccount) { // more left to do $next = $userids[$recalccount]; // start next round at first one not recalculated $continue = true; } else { qa_db_truncate_userpoints($lastuserid); qa_db_userpointscount_update(); // quick so just do it here qa_recalc_transition($state, 'dorecalcpoints_complete'); } break; case 'dorefillevents': qa_recalc_transition($state, 'dorefillevents_qcount'); break; case 'dorefillevents_qcount': qa_db_qcount_update(); qa_recalc_transition($state, 'dorefillevents_refill'); break; case 'dorefillevents_refill': $questionids = qa_db_qs_get_for_event_refilling($next, 1); if (count($questionids)) { require_once QA_INCLUDE_DIR . 'app/events.php'; require_once QA_INCLUDE_DIR . 'app/updates.php'; require_once QA_INCLUDE_DIR . 'util/sort.php'; $lastquestionid = max($questionids); foreach ($questionids as $questionid) { // Retrieve all posts relating to this question list($question, $childposts, $achildposts) = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $questionid), qa_db_full_child_posts_selectspec(null, $questionid), qa_db_full_a_child_posts_selectspec(null, $questionid)); // Merge all posts while preserving keys as postids $posts = array($questionid => $question); foreach ($childposts as $postid => $post) { $posts[$postid] = $post; } foreach ($achildposts as $postid => $post) { $posts[$postid] = $post; } // Creation and editing of each post foreach ($posts as $postid => $post) { $followonq = $post['basetype'] == 'Q' && $postid != $questionid; if ($followonq) { $updatetype = QA_UPDATE_FOLLOWS; } elseif ($post['basetype'] == 'C' && @$posts[$post['parentid']]['basetype'] == 'Q') { $updatetype = QA_UPDATE_C_FOR_Q; } elseif ($post['basetype'] == 'C' && @$posts[$post['parentid']]['basetype'] == 'A') { $updatetype = QA_UPDATE_C_FOR_A; } else { $updatetype = null; } qa_create_event_for_q_user($questionid, $postid, $updatetype, $post['userid'], @$posts[$post['parentid']]['userid'], $post['created']); if (isset($post['updated']) && !$followonq) { qa_create_event_for_q_user($questionid, $postid, $post['updatetype'], $post['lastuserid'], $post['userid'], $post['updated']); } } // Tags and categories of question qa_create_event_for_tags($question['tags'], $questionid, null, $question['userid'], $question['created']); qa_create_event_for_category($question['categoryid'], $questionid, null, $question['userid'], $question['created']); // Collect comment threads $parentidcomments = array(); foreach ($posts as $postid => $post) { if ($post['basetype'] == 'C') { $parentidcomments[$post['parentid']][$postid] = $post; } } // For each comment thread, notify all previous comment authors of each comment in the thread (could get slow) foreach ($parentidcomments as $parentid => $comments) { $keyuserids = array(); qa_sort_by($comments, 'created'); foreach ($comments as $comment) { foreach ($keyuserids as $keyuserid => $dummy) { if ($keyuserid != $comment['userid'] && $keyuserid != @$posts[$parentid]['userid']) { qa_db_event_create_not_entity($keyuserid, $questionid, $comment['postid'], QA_UPDATE_FOLLOWS, $comment['userid'], $comment['created']); } } if (isset($comment['userid'])) { $keyuserids[$comment['userid']] = true; } } } } $next = 1 + $lastquestionid; $done += count($questionids); $continue = true; } else { qa_recalc_transition($state, 'dorefillevents_complete'); } break; case 'dorecalccategories': qa_recalc_transition($state, 'dorecalccategories_postcount'); break; case 'dorecalccategories_postcount': qa_db_acount_update(); qa_db_ccount_update(); qa_recalc_transition($state, 'dorecalccategories_postupdate'); break; case 'dorecalccategories_postupdate': $postids = qa_db_posts_get_for_recategorizing($next, 100); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_recalc_categoryid($next, $lastpostid); qa_db_posts_calc_category_path($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_recount'); } break; case 'dorecalccategories_recount': $categoryids = qa_db_categories_get_for_recalcs($next, 10); if (count($categoryids)) { $lastcategoryid = max($categoryids); foreach ($categoryids as $categoryid) { qa_db_ifcategory_qcount_update($categoryid); } $next = 1 + $lastcategoryid; $done += count($categoryids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_backpaths'); } break; case 'dorecalccategories_backpaths': $categoryids = qa_db_categories_get_for_recalcs($next, 10); if (count($categoryids)) { $lastcategoryid = max($categoryids); qa_db_categories_recalc_backpaths($next, $lastcategoryid); $next = 1 + $lastcategoryid; $done += count($categoryids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_complete'); } break; case 'dodeletehidden': qa_recalc_transition($state, 'dodeletehidden_comments'); break; case 'dodeletehidden_comments': $posts = qa_db_posts_get_for_deleting('C', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'app/posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_answers'); } break; case 'dodeletehidden_answers': $posts = qa_db_posts_get_for_deleting('A', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'app/posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_questions'); } break; case 'dodeletehidden_questions': $posts = qa_db_posts_get_for_deleting('Q', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'app/posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_complete'); } break; case 'doblobstodisk': qa_recalc_transition($state, 'doblobstodisk_move'); break; case 'doblobstodisk_move': $blob = qa_db_get_next_blob_in_db($next); if (isset($blob)) { require_once QA_INCLUDE_DIR . 'app/blobs.php'; require_once QA_INCLUDE_DIR . 'db/blobs.php'; if (qa_write_blob_file($blob['blobid'], $blob['content'], $blob['format'])) { qa_db_blob_set_content($blob['blobid'], null); } $next = 1 + $blob['blobid']; $done++; $continue = true; } else { qa_recalc_transition($state, 'doblobstodisk_complete'); } break; case 'doblobstodb': qa_recalc_transition($state, 'doblobstodb_move'); break; case 'doblobstodb_move': $blob = qa_db_get_next_blob_on_disk($next); if (isset($blob)) { require_once QA_INCLUDE_DIR . 'app/blobs.php'; require_once QA_INCLUDE_DIR . 'db/blobs.php'; $content = qa_read_blob_file($blob['blobid'], $blob['format']); qa_db_blob_set_content($blob['blobid'], $content); qa_delete_blob_file($blob['blobid'], $blob['format']); $next = 1 + $blob['blobid']; $done++; $continue = true; } else { qa_recalc_transition($state, 'doblobstodb_complete'); } break; default: $state = ''; break; } if ($continue) { $state = $operation . "\t" . $length . "\t" . $next . "\t" . $done; } return $continue && $done < $length; }
function get_post_voters_flaggers($post, $postid) { require_once QA_INCLUDE_DIR . 'qa-util-sort.php'; if (!isset($this->qa_voters_flaggers_cache[$postid])) { $this->queue_post_voters_flaggers($post); $this->retrieve_queued_voters_flaggers(); } $votersflaggers = @$this->qa_voters_flaggers_cache[$postid]; if (isset($votersflaggers)) { qa_sort_by($votersflaggers, 'handle'); } return $votersflaggers; }
function qa_any_sort_and_dedupe($questions) { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } require_once QA_INCLUDE_DIR . 'util/sort.php'; foreach ($questions as $key => $question) { // collect information about action referenced by each $question if (isset($question['opostid'])) { $questions[$key]['_time'] = $question['otime']; $questions[$key]['_type'] = $question['obasetype']; $questions[$key]['_userid'] = @$question['ouserid']; } else { $questions[$key]['_time'] = $question['created']; $questions[$key]['_type'] = 'Q'; $questions[$key]['_userid'] = $question['userid']; } $questions[$key]['sort'] = -$questions[$key]['_time']; } qa_sort_by($questions, 'sort'); $keepquestions = array(); // now remove duplicate references to same question foreach ($questions as $question) { // going in order from most recent to oldest $laterquestion = @$keepquestions[$question['postid']]; if (isset($laterquestion)) { // the two events were within 5 minutes of each other $close_events = abs($laterquestion['_time'] - $question['_time']) < 300; $later_edit = @$laterquestion['oupdatetype'] && !@$question['oupdatetype'] && $laterquestion['_type'] == $question['_type'] && $laterquestion['_userid'] == $question['_userid']; // the same user made the later edit // this question (in an update list) is personal to the user, but the other one was not $this_personal = @$question['opersonal'] && !@$laterquestion['opersonal']; if ($close_events && ($later_edit || $this_personal)) { // Remove any previous instance of the post to force a new position unset($keepquestions[$question['postid']]); $keepquestions[$question['postid']] = $question; } } else { // keep this reference if there is no more recent one $keepquestions[$question['postid']] = $question; } } return $keepquestions; }
$qa_content['q_view']['c_form'] = qa_page_q_edit_c_form($qa_content, 'c' . $formpostid, $commentsfollows[$formpostid], @$ceditin[$formpostid], @$cediterrors[$formpostid]); $jumptoanchor = 'c' . $formpostid; $commentsall = $questionid; } $qa_content['q_view']['c_list'] = qa_page_q_comment_follow_list($question, $question, $commentsfollows, $commentsall == $questionid, $usershtml, $formrequested, $formpostid); // ...for viewing // Prepare content for existing answers (could be added to by Ajax) $qa_content['a_list'] = array('tags' => 'id="a_list"', 'as' => array()); // sort according to the site preferences if (qa_opt('sort_answers_by') == 'votes') { foreach ($answers as $answerid => $answer) { $answers[$answerid]['sortvotes'] = $answer['downvotes'] - $answer['upvotes']; } qa_sort_by($answers, 'sortvotes', 'created'); } else { qa_sort_by($answers, 'created'); } // further changes to ordering to deal with queued, hidden and selected answers $countfortitle = $question['acount']; $nextposition = 10000; $answerposition = array(); foreach ($answers as $answerid => $answer) { if ($answer['viewable']) { $position = $nextposition++; if ($answer['hidden']) { $position += 10000; } elseif ($answer['queued']) { $position -= 10000; $countfortitle++; // include these in displayed count } elseif ($answer['isselected'] && qa_opt('show_selected_first')) {
/** * Post-process $outresult according to $selectspec, applying 'sortasc', 'sortdesc', 'arrayvalue' and 'single'. */ function qa_db_post_select(&$outresult, $selectspec) { // PHP's sorting algorithm is not 'stable', so we use '_order_' element to keep stability. // By contrast, MySQL's ORDER BY does seem to give the results in a reliable order. if (isset($selectspec['sortasc'])) { require_once QA_INCLUDE_DIR . 'util/sort.php'; $index = 0; foreach ($outresult as $key => $value) { $outresult[$key]['_order_'] = $index++; } qa_sort_by($outresult, $selectspec['sortasc'], '_order_'); } elseif (isset($selectspec['sortdesc'])) { require_once QA_INCLUDE_DIR . 'util/sort.php'; if (isset($selectspec['sortdesc_2'])) { qa_sort_by($outresult, $selectspec['sortdesc'], $selectspec['sortdesc_2']); } else { $index = count($outresult); foreach ($outresult as $key => $value) { $outresult[$key]['_order_'] = $index--; } qa_sort_by($outresult, $selectspec['sortdesc'], '_order_'); } $outresult = array_reverse($outresult, true); } if (isset($selectspec['arrayvalue'])) { foreach ($outresult as $key => $value) { $outresult[$key] = $value[$selectspec['arrayvalue']]; } } if (@$selectspec['single']) { $outresult = count($outresult) ? reset($outresult) : null; } }
$a_view['c_form'] = qa_page_q_edit_c_form($formpostid, $answerid); } } // Determine this answer's place in the order on the page if ($answer['hidden']) { $a_view['priority'] = 10000 + $priority++; } elseif ($answer['isselected'] && qa_opt('show_selected_first')) { $a_view['priority'] = 0; } else { $a_view['priority'] = 5000 + $priority++; } // Add the answer to the list $qa_content['a_list']['as'][] = $a_view; } } qa_sort_by($qa_content['a_list']['as'], 'priority'); $countanswers = $question['acount']; if ($countanswers == 1) { $qa_content['a_list']['title'] = qa_lang_html('question/1_answer_title'); } else { $qa_content['a_list']['title'] = qa_lang_html_sub('question/x_answers_title', $countanswers); } // Prepare content for form to add an answer if ($formtype == 'a_add') { // Form for adding answers $answerform = null; switch (qa_user_permit_error('permit_post_a')) { case 'login': $answerform = array('style' => 'tall', 'title' => qa_insert_login_links(qa_lang_html('question/answer_must_login'), $qa_request)); break; case 'confirm':
function qa_any_sort_and_dedupe($questions) { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } require_once QA_INCLUDE_DIR . 'qa-util-sort.php'; foreach ($questions as $key => $question) { // collect information about action referenced by each $question if (isset($question['opostid'])) { $questions[$key]['_time'] = $question['otime']; $questions[$key]['_type'] = $question['obasetype']; $questions[$key]['_userid'] = @$question['ouserid']; } else { $questions[$key]['_time'] = $question['created']; $questions[$key]['_type'] = 'Q'; $questions[$key]['_userid'] = $question['userid']; } $questions[$key]['sort'] = -$questions[$key]['_time']; } qa_sort_by($questions, 'sort'); $keepquestions = array(); // now remove duplicate references to same question foreach ($questions as $question) { // going in order from most recent to oldest $laterquestion = @$keepquestions[$question['postid']]; if (!isset($laterquestion) || @$laterquestion['oupdatetype'] && !@$question['oupdatetype'] && $laterquestion['_type'] == $question['_type'] && $laterquestion['_userid'] == $question['_userid'] && abs($laterquestion['_time'] - $question['_time']) < 300 || @$question['opersonal'] && !@$laterquestion['opersonal'] && abs($laterquestion['_time'] - $question['_time']) < 300) { $keepquestions[$question['postid']] = $question; } } return $keepquestions; }
require_once QA_INCLUDE_DIR . 'pages/question-view.php'; require_once QA_INCLUDE_DIR . 'pages/question-submit.php'; require_once QA_INCLUDE_DIR . 'util/sort.php'; // Try to create the new comment $usecaptcha = qa_user_use_captcha(qa_user_level_for_post($question)); $commentid = qa_page_q_add_c_submit($question, $parent, $children, $usecaptcha, $in, $errors); // If successful, page content will be updated via Ajax if (isset($commentid)) { $children = qa_db_select_with_pending(qa_db_full_child_posts_selectspec($userid, $parentid)); $parent = $parent + qa_page_q_post_rules($parent, $questionid == $parentid ? null : $question, null, $children); // in theory we should retrieve the parent's siblings for the above, but they're not going to be relevant foreach ($children as $key => $child) { $children[$key] = $child + qa_page_q_post_rules($child, $parent, $children, null); } $usershtml = qa_userids_handles_html($children, true); qa_sort_by($children, 'created'); $c_list = qa_page_q_comment_follow_list($question, $parent, $children, true, $usershtml, false, null); $themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-comments', null, null); echo "QA_AJAX_RESPONSE\n1\n"; // Send back the ID of the new comment echo qa_anchor('C', $commentid) . "\n"; // Send back the HTML $themeclass->c_list_items($c_list['cs']); return; } } echo "QA_AJAX_RESPONSE\n0\n"; // fall back to non-Ajax submission if there were any problems /* Omit PHP closing tag to help avoid accidental output */
$showpluginforms = true; } if (!empty($pluginfiles)) { $metadataUtil = new Q2A_Util_Metadata(); $sortedPluginFiles = array(); foreach ($pluginfiles as $pluginFile) { $metadata = $metadataUtil->fetchFromAddonPath(dirname($pluginFile)); if (empty($metadata)) { // limit plugin parsing to first 8kB $contents = file_get_contents($pluginFile, false, null, -1, 8192); $metadata = qa_addon_metadata($contents, 'Plugin'); } $metadata['name'] = isset($metadata['name']) && !empty($metadata['name']) ? qa_html($metadata['name']) : qa_lang_html('admin/unnamed_plugin'); $sortedPluginFiles[$pluginFile] = $metadata; } qa_sort_by($sortedPluginFiles, 'name'); $pluginIndex = -1; foreach ($sortedPluginFiles as $pluginFile => $metadata) { $pluginIndex++; $plugindirectory = dirname($pluginFile); $hash = qa_admin_plugin_directory_hash($plugindirectory); $showthisform = $showpluginforms && qa_get('show') == $hash; $namehtml = $metadata['name']; if (isset($metadata['uri']) && strlen($metadata['uri'])) { $namehtml = '<a href="' . qa_html($metadata['uri']) . '">' . $namehtml . '</a>'; } $namehtml = '<b>' . $namehtml . '</b>'; $metaver = isset($metadata['version']) && strlen($metadata['version']); if ($metaver) { $namehtml .= ' v' . qa_html($metadata['version']); }
function qa_recalc_perform_step(&$state) { $continue = false; @(list($operation, $length, $next, $done) = explode(',', $state)); switch ($operation) { case 'doreindexcontent': qa_recalc_transition($state, 'doreindexcontent_pagereindex'); break; case 'doreindexcontent_pagereindex': $pages = qa_db_pages_get_for_reindexing($next, 10); if (count($pages)) { require_once QA_INCLUDE_DIR . 'qa-app-format.php'; $lastpageid = max(array_keys($pages)); foreach ($pages as $pageid => $page) { if (!($page['flags'] & QA_PAGE_FLAGS_EXTERNAL)) { $searchmodules = qa_load_modules_with('search', 'unindex_page'); foreach ($searchmodules as $searchmodule) { $searchmodule->unindex_page($pageid); } $searchmodules = qa_load_modules_with('search', 'index_page'); if (count($searchmodules)) { $indextext = qa_viewer_text($page['content'], 'html'); foreach ($searchmodules as $searchmodule) { $searchmodule->index_page($pageid, $page['tags'], $page['heading'], $page['content'], 'html', $indextext); } } } } $next = 1 + $lastpageid; $done += count($pages); $continue = true; } else { qa_recalc_transition($state, 'doreindexcontent_postcount'); } break; case 'doreindexcontent_postcount': qa_db_qcount_update(); qa_db_acount_update(); qa_db_ccount_update(); qa_recalc_transition($state, 'doreindexcontent_postreindex'); break; case 'doreindexcontent_postreindex': $posts = qa_db_posts_get_for_reindexing($next, 10); if (count($posts)) { require_once QA_INCLUDE_DIR . 'qa-app-format.php'; $lastpostid = max(array_keys($posts)); qa_db_prepare_for_reindexing($next, $lastpostid); qa_suspend_update_counts(); foreach ($posts as $postid => $post) { qa_post_unindex($postid); qa_post_index($postid, $post['type'], $post['questionid'], $post['parentid'], $post['title'], $post['content'], $post['format'], qa_viewer_text($post['content'], $post['format']), $post['tags'], $post['categoryid']); } $next = 1 + $lastpostid; $done += count($posts); $continue = true; } else { qa_db_truncate_indexes($next); qa_recalc_transition($state, 'doreindexposts_wordcount'); } break; case 'doreindexposts_wordcount': $wordids = qa_db_words_prepare_for_recounting($next, 1000); if (count($wordids)) { $lastwordid = max($wordids); qa_db_words_recount($next, $lastwordid); $next = 1 + $lastwordid; $done += count($wordids); $continue = true; } else { qa_db_tagcount_update(); // this is quick so just do it here qa_recalc_transition($state, 'doreindexposts_complete'); } break; case 'dorecountposts': qa_recalc_transition($state, 'dorecountposts_postcount'); break; case 'dorecountposts_postcount': qa_db_qcount_update(); qa_db_acount_update(); qa_db_ccount_update(); qa_db_unaqcount_update(); qa_db_unselqcount_update(); qa_recalc_transition($state, 'dorecountposts_votecount'); break; case 'dorecountposts_votecount': $postids = qa_db_posts_get_for_recounting($next, 1000); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_votes_recount($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_recalc_transition($state, 'dorecountposts_acount'); } break; case 'dorecountposts_acount': $postids = qa_db_posts_get_for_recounting($next, 1000); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_answers_recount($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_db_unupaqcount_update(); qa_recalc_transition($state, 'dorecountposts_complete'); } break; case 'dorecalcpoints': qa_recalc_transition($state, 'dorecalcpoints_usercount'); break; case 'dorecalcpoints_usercount': qa_db_userpointscount_update(); // for progress update - not necessarily accurate qa_recalc_transition($state, 'dorecalcpoints_recalc'); break; case 'dorecalcpoints_recalc': $userids = qa_db_users_get_for_recalc_points($next, 10); if (count($userids)) { $lastuserid = max($userids); qa_db_users_recalc_points($next, $lastuserid); $next = 1 + $lastuserid; $done += count($userids); $continue = true; } else { qa_db_truncate_userpoints($next); qa_db_userpointscount_update(); // quick so just do it here qa_recalc_transition($state, 'dorecalcpoints_complete'); } break; case 'dorefillevents': qa_recalc_transition($state, 'dorefillevents_qcount'); break; case 'dorefillevents_qcount': qa_db_qcount_update(); qa_recalc_transition($state, 'dorefillevents_refill'); break; case 'dorefillevents_refill': $questionids = qa_db_qs_get_for_event_refilling($next, 1); if (count($questionids)) { require_once QA_INCLUDE_DIR . 'qa-app-events.php'; require_once QA_INCLUDE_DIR . 'qa-app-updates.php'; require_once QA_INCLUDE_DIR . 'qa-util-sort.php'; $lastquestionid = max($questionids); foreach ($questionids as $questionid) { // Retrieve all posts relating to this question list($question, $childposts, $achildposts) = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $questionid), qa_db_full_child_posts_selectspec(null, $questionid), qa_db_full_a_child_posts_selectspec(null, $questionid)); // Merge all posts while preserving keys as postids $posts = array($questionid => $question); foreach ($childposts as $postid => $post) { $posts[$postid] = $post; } foreach ($achildposts as $postid => $post) { $posts[$postid] = $post; } // Creation and editing of each post foreach ($posts as $postid => $post) { $followonq = $post['basetype'] == 'Q' && $postid != $questionid; qa_create_event_for_q_user($questionid, $postid, $followonq ? QA_UPDATE_FOLLOWS : null, $post['userid'], @$posts[$post['parentid']]['userid'], $post['created']); if (isset($post['updated']) && !$followonq) { qa_create_event_for_q_user($questionid, $postid, $post['updatetype'], $post['lastuserid'], $post['userid'], $post['updated']); } } // Tags and categories of question qa_create_event_for_tags($question['tags'], $questionid, null, $question['userid'], $question['created']); qa_create_event_for_category($question['categoryid'], $questionid, null, $question['userid'], $question['created']); // Collect comment threads $parentidcomments = array(); foreach ($posts as $postid => $post) { if ($post['basetype'] == 'C') { $parentidcomments[$post['parentid']][$postid] = $post; } } // For each comment thread, notify all previous comment authors of each comment in the thread (could get slow) foreach ($parentidcomments as $parentid => $comments) { $keyuserids = array(); qa_sort_by($comments, 'created'); foreach ($comments as $comment) { foreach ($keyuserids as $keyuserid => $dummy) { if ($keyuserid != $comment['userid'] && $keyuserid != @$posts[$parentid]['userid']) { qa_db_event_create_not_entity($keyuserid, $questionid, $comment['postid'], null, $comment['userid'], $comment['created']); } } if (isset($comment['userid'])) { $keyuserids[$comment['userid']] = true; } } } } $next = 1 + $lastquestionid; $done += count($questionids); $continue = true; } else { qa_recalc_transition($state, 'dorefillevents_complete'); } break; case 'dorecalccategories': qa_recalc_transition($state, 'dorecalccategories_postcount'); break; case 'dorecalccategories_postcount': qa_db_acount_update(); qa_db_ccount_update(); qa_recalc_transition($state, 'dorecalccategories_postupdate'); break; case 'dorecalccategories_postupdate': $postids = qa_db_posts_get_for_recategorizing($next, 100); if (count($postids)) { $lastpostid = max($postids); qa_db_posts_recalc_categoryid($next, $lastpostid); qa_db_posts_calc_category_path($next, $lastpostid); $next = 1 + $lastpostid; $done += count($postids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_recount'); } break; case 'dorecalccategories_recount': $categoryids = qa_db_categories_get_for_recalcs($next, 10); if (count($categoryids)) { $lastcategoryid = max($categoryids); foreach ($categoryids as $categoryid) { qa_db_ifcategory_qcount_update($categoryid); } $next = 1 + $lastcategoryid; $done += count($categoryids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_backpaths'); } break; case 'dorecalccategories_backpaths': $categoryids = qa_db_categories_get_for_recalcs($next, 10); if (count($categoryids)) { $lastcategoryid = max($categoryids); qa_db_categories_recalc_backpaths($next, $lastcategoryid); $next = 1 + $lastcategoryid; $done += count($categoryids); $continue = true; } else { qa_recalc_transition($state, 'dorecalccategories_complete'); } break; case 'dodeletehidden': qa_recalc_transition($state, 'dodeletehidden_comments'); break; case 'dodeletehidden_comments': $posts = qa_db_posts_get_for_deleting('C', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'qa-app-posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_answers'); } break; case 'dodeletehidden_answers': $posts = qa_db_posts_get_for_deleting('A', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'qa-app-posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_questions'); } break; case 'dodeletehidden_questions': $posts = qa_db_posts_get_for_deleting('Q', $next, 1); if (count($posts)) { require_once QA_INCLUDE_DIR . 'qa-app-posts.php'; $postid = $posts[0]; qa_post_delete($postid); $next = 1 + $postid; $done++; $continue = true; } else { qa_recalc_transition($state, 'dodeletehidden_complete'); } break; default: $state = ''; break; } if ($continue) { $state = $operation . ',' . $length . ',' . $next . ',' . $done; } return $continue && $done < $length; }