Example #1
0
function qa_merge_do_merge()
{
    qa_opt('merge_question_merged', qa_post_text('merge_question_merged'));
    $from = (int) qa_post_text('merge_from');
    $to = (int) qa_post_text('merge_to');
    $titles = qa_db_read_all_assoc(qa_db_query_sub("SELECT postid,title,acount FROM ^posts WHERE postid IN (#,#)", qa_post_text('merge_from'), qa_post_text('merge_to')));
    if (count($titles) != 2) {
        $error1 = null;
        $error2 = null;
        if (empty($titles)) {
            $error1 = 'Post not found.';
            $error2 = $error1;
        } else {
            if ($titles[0]['postid'] == $from) {
                $error2 = 'Post not found.';
            } else {
                if ($titles[0]['postid'] == $to) {
                    $error1 = 'Post not found.';
                } else {
                    $error1 = 'unknown error.';
                }
            }
        }
        return array($error1, $error2);
    } else {
        $acount = (int) $titles[0]['acount'] + (int) $titles[1]['acount'];
        $text = '<div class="qa-content-merged"> ' . str_replace('^post', qa_path(qa_q_request((int) qa_post_text('merge_to'), $titles[0]['postid'] == $to ? $titles[0]['title'] : $titles[1]['title']), null, qa_opt('site_url')), qa_opt('merge_question_merged')) . ' </div>';
        qa_db_query_sub("UPDATE ^posts SET parentid=# WHERE parentid=#", $to, $from);
        qa_db_query_sub("UPDATE ^posts SET acount=# WHERE postid=#", $acount, $to);
        qa_db_query_sub('CREATE TABLE IF NOT EXISTS ^postmeta (
				meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				post_id bigint(20) unsigned NOT NULL,
				meta_key varchar(255) DEFAULT \'\',
				meta_value longtext,
				PRIMARY KEY (meta_id),
				KEY post_id (post_id),
				KEY meta_key (meta_key)
				) ENGINE=MyISAM  DEFAULT CHARSET=utf8');
        qa_db_query_sub("INSERT INTO ^postmeta (post_id,meta_key,meta_value) VALUES (#,'merged_with',#)", $from, $to);
        require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
        qa_post_delete($from);
        return true;
    }
}
Example #2
0
function qa_admin_single_click($postid, $action)
{
    require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
    $post = qa_post_get_full($postid);
    if (isset($post)) {
        $userid = qa_get_logged_in_userid();
        $queued = substr($post['type'], 1) == '_QUEUED';
        switch ($action) {
            case 'approve':
                if ($queued && !qa_user_permit_error('permit_moderate')) {
                    qa_post_set_hidden($postid, false, $userid);
                    return true;
                }
                break;
            case 'reject':
                if ($queued && !qa_user_permit_error('permit_moderate')) {
                    qa_post_set_hidden($postid, true, $userid);
                    return true;
                }
                break;
            case 'hide':
                if (!$queued && !qa_user_permit_error('permit_hide_show')) {
                    qa_post_set_hidden($postid, true, $userid);
                    return true;
                }
                break;
            case 'reshow':
                if ($post['hidden'] && !qa_user_permit_error('permit_hide_show')) {
                    qa_post_set_hidden($postid, false, $userid);
                    return true;
                }
                break;
            case 'delete':
                if ($post['hidden'] && !qa_user_permit_error('permit_delete_hidden')) {
                    qa_post_delete($postid);
                    return true;
                }
                break;
            case 'clearflags':
                require_once QA_INCLUDE_DIR . 'qa-app-votes.php';
                if (!qa_user_permit_error('permit_hide_show')) {
                    qa_flags_clear_all($post, $userid, qa_get_logged_in_handle(), null);
                    return true;
                }
                break;
        }
    }
    return false;
}
Example #3
0
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;
}
Example #4
0
function qa_admin_single_click($entityid, $action)
{
    $userid = qa_get_logged_in_userid();
    if (!QA_FINAL_EXTERNAL_USERS && ($action == 'userapprove' || $action == 'userblock')) {
        // approve/block moderated users
        require_once QA_INCLUDE_DIR . 'db/selects.php';
        $useraccount = qa_db_select_with_pending(qa_db_user_account_selectspec($entityid, true));
        if (isset($useraccount) && qa_get_logged_in_level() >= QA_USER_LEVEL_MODERATOR) {
            switch ($action) {
                case 'userapprove':
                    if ($useraccount['level'] <= QA_USER_LEVEL_APPROVED) {
                        // don't demote higher level users
                        require_once QA_INCLUDE_DIR . 'app/users-edit.php';
                        qa_set_user_level($useraccount['userid'], $useraccount['handle'], QA_USER_LEVEL_APPROVED, $useraccount['level']);
                        return true;
                    }
                    break;
                case 'userblock':
                    require_once QA_INCLUDE_DIR . 'app/users-edit.php';
                    qa_set_user_blocked($useraccount['userid'], $useraccount['handle'], true);
                    return true;
                    break;
            }
        }
    } else {
        // something to do with a post
        require_once QA_INCLUDE_DIR . 'app/posts.php';
        $post = qa_post_get_full($entityid);
        if (isset($post)) {
            $queued = substr($post['type'], 1) == '_QUEUED';
            switch ($action) {
                case 'approve':
                    if ($queued && !qa_user_post_permit_error('permit_moderate', $post)) {
                        qa_post_set_hidden($entityid, false, $userid);
                        return true;
                    }
                    break;
                case 'reject':
                    if ($queued && !qa_user_post_permit_error('permit_moderate', $post)) {
                        qa_post_set_hidden($entityid, true, $userid);
                        return true;
                    }
                    break;
                case 'hide':
                    if (!$queued && !qa_user_post_permit_error('permit_hide_show', $post)) {
                        qa_post_set_hidden($entityid, true, $userid);
                        return true;
                    }
                    break;
                case 'reshow':
                    if ($post['hidden'] && !qa_user_post_permit_error('permit_hide_show', $post)) {
                        qa_post_set_hidden($entityid, false, $userid);
                        return true;
                    }
                    break;
                case 'delete':
                    if ($post['hidden'] && !qa_user_post_permit_error('permit_delete_hidden', $post)) {
                        qa_post_delete($entityid);
                        return true;
                    }
                    break;
                case 'clearflags':
                    require_once QA_INCLUDE_DIR . 'app/votes.php';
                    if (!qa_user_post_permit_error('permit_hide_show', $post)) {
                        qa_flags_clear_all($post, $userid, qa_get_logged_in_handle(), null);
                        return true;
                    }
                    break;
            }
        }
    }
    return false;
}
Example #5
0
 function do_delete($data, $post)
 {
     $userlevel = qa_user_level_for_post($post);
     $deleteable = $post['hidden'] && !qa_user_permit_error('permit_delete_hidden', null, $userlevel);
     $postid = (int) @$data['action_id'];
     if (!$deleteable) {
         return false;
     }
     require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
     qa_post_delete($postid);
     return true;
 }
Example #6
0
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;
}