예제 #1
0
function fetch_search_cache($qry, $start, $count, $logic, $srch_type, $order, $forum_limiter, &$total)
{
    if (strncmp($GLOBALS['usr']->lang, 'chinese', 7)) {
        $cs = array('!\\W!', '!\\s+!');
        $cd = array(' ', ' ');
        $qry = trim(preg_replace($cs, $cd, $qry));
        $w = array_unique(explode(' ', strtolower($qry)));
        $qr = '';
        $i = 0;
        foreach ($w as $v) {
            $v = trim($v);
            if (strlen($v) <= 2) {
                continue;
            } else {
                if ($i++ == 10) {
                    /* limit query length to 10 words */
                    break;
                }
            }
            $qr .= " '" . addslashes($v) . "',";
        }
        if (!$qr) {
            return;
        } else {
            $qr = substr($qr, 0, -1);
        }
    } else {
        /* handling for multibyte languages */
        fud_use('isearch.inc');
        if (!($w = mb_word_split($qry))) {
            return;
        }
        $qr = implode(',', $w);
        $i = count($w);
    }
    if ($srch_type == 'all') {
        $tbl = 'index';
        $qt = '0';
    } else {
        $tbl = 'title_index';
        $qt = '1';
    }
    $qry_lck = md5($qr);
    /* remove expired cache */
    q('DELETE FROM phpgw_fud_search_cache WHERE expiry<' . (__request_timestamp__ - $GLOBALS['SEARCH_CACHE_EXPIRY']));
    if (!($total = q_singleval("SELECT count(*) FROM phpgw_fud_search_cache WHERE query_type=" . $qt . " AND srch_query='" . $qry_lck . "'"))) {
        if (__dbtype__ == 'mysql') {
            q("INSERT IGNORE INTO phpgw_fud_search_cache (srch_query, query_type, expiry, msg_id, n_match) SELECT '" . $qry_lck . "', " . $qt . ", " . __request_timestamp__ . ", msg_id, count(*) as word_count FROM phpgw_fud_search s INNER JOIN phpgw_fud_" . $tbl . " i ON i.word_id=s.id WHERE word IN(" . $qr . ") GROUP BY msg_id ORDER BY word_count DESC LIMIT 500");
            if (!($total = (int) db_affected())) {
                return;
            }
        } else {
            q("BEGIN; DELETE FROM phpgw_fud_search_cache; INSERT INTO phpgw_fud_search_cache (srch_query, query_type, expiry, msg_id, n_match) SELECT '" . $qry_lck . "', " . $qt . ", " . __request_timestamp__ . ", msg_id, count(*) as word_count FROM phpgw_fud_search s INNER JOIN phpgw_fud_" . $tbl . " i ON i.word_id=s.id WHERE word IN(" . $qr . ") GROUP BY msg_id ORDER BY word_count DESC LIMIT 500; COMMIT;");
        }
    }
    if ($forum_limiter) {
        if ($forum_limiter[0] != 'c') {
            $qry_lmt = ' AND f.id=' . (int) $forum_limiter . ' ';
        } else {
            $qry_lmt = ' AND c.id=' . (int) substr($forum_limiter, 1) . ' ';
        }
    } else {
        $qry_lmt = '';
    }
    $qry_lck = "'" . $qry_lck . "'";
    $total = q_singleval('SELECT count(*)
		FROM phpgw_fud_search_cache sc
		INNER JOIN phpgw_fud_msg m ON m.id=sc.msg_id
		INNER JOIN phpgw_fud_thread t ON m.thread_id=t.id
		INNER JOIN phpgw_fud_forum f ON t.forum_id=f.id
		INNER JOIN phpgw_fud_cat c ON f.cat_id=c.id
		INNER JOIN phpgw_fud_group_cache g1 ON g1.user_id=' . (_uid ? '2147483647' : '0') . ' AND g1.resource_id=f.id
		LEFT JOIN phpgw_fud_mod mm ON mm.forum_id=f.id AND mm.user_id=' . _uid . '
		LEFT JOIN phpgw_fud_group_cache g2 ON g2.user_id=' . _uid . ' AND g2.resource_id=f.id
		WHERE
			sc.query_type=' . $qt . ' AND sc.srch_query=' . $qry_lck . $qry_lmt . '
			' . ($logic == 'AND' ? ' AND sc.n_match>=' . $i : '') . '
			' . ($GLOBALS['usr']->users_opt & 1048576 ? '' : ' AND (mm.id IS NOT NULL OR ((CASE WHEN g2.id IS NOT NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 262146) >= 262146)'));
    if (!$total) {
        return;
    }
    return uq('SELECT u.alias, f.name AS forum_name, f.id AS forum_id,
			m.poster_id, m.id, m.thread_id, m.subject, m.poster_id, m.foff, m.length, m.post_stamp, m.file_id, m.icon
		FROM phpgw_fud_search_cache sc
		INNER JOIN phpgw_fud_msg m ON m.id=sc.msg_id
		INNER JOIN phpgw_fud_thread t ON m.thread_id=t.id
		INNER JOIN phpgw_fud_forum f ON t.forum_id=f.id
		INNER JOIN phpgw_fud_cat c ON f.cat_id=c.id
		INNER JOIN phpgw_fud_group_cache g1 ON g1.user_id=' . (_uid ? '2147483647' : '0') . ' AND g1.resource_id=f.id
		LEFT JOIN phpgw_fud_users u ON m.poster_id=u.id
		LEFT JOIN phpgw_fud_mod mm ON mm.forum_id=f.id AND mm.user_id=' . _uid . '
		LEFT JOIN phpgw_fud_group_cache g2 ON g2.user_id=' . _uid . ' AND g2.resource_id=f.id
		WHERE
			sc.query_type=' . $qt . ' AND sc.srch_query=' . $qry_lck . $qry_lmt . '
			' . ($logic == 'AND' ? ' AND sc.n_match>=' . $i : '') . '
			' . ($GLOBALS['usr']->users_opt & 1048576 ? '' : ' AND (mm.id IS NOT NULL OR ((CASE WHEN g2.id IS NOT NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 262146) >= 262146)') . '
		ORDER BY sc.n_match DESC, m.post_stamp ' . $order . ' LIMIT ' . qry_limit($count, $start));
}
예제 #2
0
function index_text($subj, $body, $msg_id)
{
    /* Remove Stuff In Quotes */
    while (preg_match('!<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1"><tr><td class="SmallText"><b>(.*?)</b></td></tr><tr><td class="quote"><br>(.*?)<br></td></tr></table>!is', $body)) {
        $body = preg_replace('!<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1"><tr><td class="SmallText"><b>(.*?)</b></td></tr><tr><td class="quote"><br>(.*?)<br></td></tr></table>!is', '', $body);
    }
    /* this is mostly a hack for php verison < 4.3 because isset(string[bad offset]) returns a warning */
    error_reporting(0);
    if (strncmp($GLOBALS['usr']->lang, 'chinese', 7)) {
        $cs = array('!\\W!', '!\\s+!');
        $cd = array(' ', ' ');
        reverse_fmt($subj);
        $subj = trim(preg_replace($cs, $cd, strip_tags(strtolower($subj))));
        reverse_fmt($body);
        $body = trim(preg_replace($cs, $cd, strip_tags(strtolower($body))));
        /* build full text index */
        $t1 = array_unique(explode(' ', $subj));
        $t2 = array_unique(explode(' ', $body));
        foreach ($t1 as $v) {
            if (isset($v[51]) || !isset($v[3])) {
                continue;
            }
            $w1[] = "'" . addslashes($v) . "'";
        }
        if (isset($w1)) {
            $w2 = $w1;
        }
        foreach ($t2 as $v) {
            if (isset($v[51]) || !isset($v[3])) {
                continue;
            }
            $w2[] = "'" . addslashes($v) . "'";
        }
    } else {
        /* handling for multibyte languages */
        $w1 = mb_word_split($subj);
        if ($w1) {
            $w2 = array_merge($w1, mb_word_split($body));
        } else {
            unset($w1);
        }
    }
    if (!$w2) {
        return;
    }
    $w2 = array_unique($w2);
    if (__dbtype__ == 'mysql') {
        ins_m('phpgw_fud_search', 'word', $w2);
    } else {
        ins_m('phpgw_fud_search', 'word', $w2, 'text');
    }
    /* This allows us to return right away, meaning we don't need to wait
     * for any locks to be released etc... */
    if (__dbtype__ == 'mysql') {
        $del = 'DELAYED';
    } else {
        $del = '';
    }
    if (isset($w1)) {
        db_li('INSERT ' . $del . ' INTO phpgw_fud_title_index (word_id, msg_id) SELECT id, ' . $msg_id . ' FROM phpgw_fud_search WHERE word IN(' . implode(',', $w1) . ')', $ef);
    }
    db_li('INSERT ' . $del . ' INTO phpgw_fud_index (word_id, msg_id) SELECT id, ' . $msg_id . ' FROM phpgw_fud_search WHERE word IN(' . implode(',', $w2) . ')', $ef);
}