$orderid = 0;
     $start_limit = 0;
     $page = 1;
 }
 //定义相同的查询语句前部分
 $query_sql = "SELECT a.articleid,a.title,a.dateline FROM {$db_prefix}articles a WHERE a.visible='1'";
 $userid = (int) $_GET['userid'];
 $mid = sax_addslashes($_GET['mid']);
 $searchid = (int) $_GET['searchid'];
 if ($mid) {
     $r = $DB->fetch_one_array("SELECT mid, name, count, type FROM {$db_prefix}metas WHERE mid='{$mid}'");
     if (!$r) {
         wap_header('系统消息');
         wap_message('记录不存在', array('title' => '返回日志列表', 'link' => 'index.php?action=article'));
     }
     $aids = get_cids($r['mid']);
     $total = $r['count'];
     if ($total && $aids) {
         $query_sql .= " AND a.articleid IN ({$aids}) ORDER BY a.dateline DESC LIMIT {$start_limit}, " . $pagenum;
     }
     $pageurl = 'index.php?action=article&mid=' . $mid;
     if ($r['type'] == 'category') {
         $catename = '分类:' . $r['name'];
     } else {
         $catename = 'Tag:' . $r['name'];
     }
     // 查看搜索结果的文章
 } elseif ($searchid) {
     $search = $DB->fetch_one_array("SELECT * FROM {$db_prefix}searchindex WHERE searchid='{$searchid}' AND expiration > '{$timestamp}'");
     if (!$search) {
         $DB->unbuffered_query("DELETE FROM {$db_prefix}searchindex WHERE expiration < '{$timestamp}'");
if ($action == 'update') {
    if (is_array($_POST['displayorder'])) {
        foreach ($_POST['displayorder'] as $mid => $order) {
            $DB->unbuffered_query("UPDATE {$db_prefix}metas SET displayorder='" . intval($order) . "' WHERE mid='" . intval($mid) . "'");
        }
    }
    $location = getlink('category', $goaction, array('message' => 8));
    header("Location: {$location}");
    exit;
}
if ($action == 'delete') {
    if ($mids = implode_ids($_POST['selectall'])) {
        $a_total = 0;
        // 删除分类
        $DB->query("DELETE FROM {$db_prefix}metas WHERE mid IN ({$mids})");
        $aids = get_cids($mids);
        $DB->query("DELETE FROM {$db_prefix}relationships WHERE mid IN ({$mids})");
        if ($aids) {
            $query = $DB->query("SELECT uid, visible FROM {$db_prefix}articles WHERE articleid IN ({$aids})");
            while ($article = $DB->fetch_array($query)) {
                if ($article['visible']) {
                    $a_total++;
                    $DB->query("UPDATE {$db_prefix}users SET articles=articles-1 WHERE userid='" . $article['uid'] . "'");
                }
            }
            //end while
            // 加载附件相关函数
            require_once SABLOG_ROOT . 'include/func/attachment.func.php';
            // 删除该分类下文章中的附件
            $query = $DB->query("SELECT attachmentid,filepath,thumb_filepath FROM {$db_prefix}attachments WHERE articleid IN ({$aids})");
            $nokeep = array();
     $andor = ' OR ';
     $sqltxtsrch = '0';
     $keywords = preg_replace("/( OR |\\|)/is", "+", $keywords);
 }
 $keywords = str_replace('*', '%', addcslashes($keywords, '%_'));
 foreach (explode("+", $keywords) as $text) {
     $text = trim($text);
     if ($text) {
         $sqltxtsrch .= $andor;
         $sqltxtsrch .= $_POST['searchin'] == 'content' ? "(content LIKE '%" . $text . "%' OR description LIKE '%" . $text . "%' OR title LIKE '%" . $text . "%')" : "title LIKE '%" . $text . "%'";
     }
 }
 $query_sql = "SELECT " . ($_POST['searchin'] == 'content' ? 'DISTINCT' : '') . " articleid FROM {$db_prefix}articles WHERE visible='1'";
 $aids = '';
 if ($cids) {
     $aids = get_cids($cids);
     $query_sql .= " AND articleid IN ({$aids})";
 }
 $query_sql .= " AND ({$sqltxtsrch}) ORDER BY dateline DESC LIMIT 500";
 //搜索500个出来足够了.保证效率.反正一般BLOG也没有多少数据.
 $totals = 0;
 $ids = $comma = '';
 $query = $DB->query($query_sql);
 while ($article = $DB->fetch_array($query)) {
     $ids .= $comma . $article['articleid'];
     $comma = ',';
     $totals++;
 }
 $DB->free_result($query);
 $DB->query("INSERT INTO {$db_prefix}searchindex (keywords, searchstring, dateline, expiration, totals, ids, ipaddress, uid) VALUES ('" . char_cv($keywords) . "', '{$searchstring}', '{$timestamp}', '" . ($timestamp + 3600) . "', '{$totals}', '{$ids}', '{$onlineip}', '{$sax_uid}')");
 $searchid = $DB->insert_id();
function get_cids($cats, $cids = array())
{
    if (!empty($cats)) {
        foreach ($cats as $cat) {
            $cids[] = $cat->cat_id;
            if (property_exists($cat, 'children')) {
                return get_cids($cat->children, $cids);
            } else {
                return $cids;
            }
        }
    }
}