Пример #1
0
function print_categories_form($selected = 0)
{
    global $db, $dblang, $globals;
    $metas = SitesMgr::get_metas();
    foreach ($metas as &$meta) {
        $meta->categories = SitesMgr::get_categories($meta->id);
    }
    unset($meta);
    $vars = compact('selected', 'metas');
    return Haanga::Load('form_categories.html', $vars);
}
Пример #2
0
function print_categories_checkboxes($user)
{
    global $db, $current_user;
    // Get selected categories
    $selected_set = $db->get_col("SELECT pref_value FROM prefs WHERE pref_user_id = {$user->id} and pref_key = 'category_" . SitesMgr::my_id() . "'");
    $selected = array();
    if ($selected_set) {
        foreach ($selected_set as $cat) {
            $selected[$cat] = true;
        }
    }
    $metas = SitesMgr::get_metas();
    $categories = array();
    foreach ($metas as $meta) {
        $categories[$meta->id] = SitesMgr::get_categories($meta->id);
        if ($selected) {
            // Check if all categories are selected for the current meta
            $all = true;
            foreach ($categories[$meta->id] as $sel) {
                if (!isset($selected[$sel->id])) {
                    $all = false;
                }
            }
            if ($all) {
                $selected[$meta->id] = true;
            }
        }
    }
    Haanga::Load('user/categories.html', compact('user', 'metas', 'categories', 'selected'));
}
Пример #3
0
function sphinx_do_search($by_date = false, $start = 0, $count = 10, $proximity = true)
{
    global $globals;
    $response = array();
    $queries = array();
    $recorded = array();
    $start_time = microtime(true);
    $indices = $_REQUEST['w'] . ' ' . $_REQUEST['w'] . '_delta';
    $cl = sphinx_client();
    if (!$cl) {
        return $response;
    }
    $cl->SetLimits($start, $count);
    if ($_REQUEST['w'] == 'links') {
        $cl->SetFieldWeights(array('title' => 3, 'tags' => 3, 'url' => 1, 'content' => 1));
    } else {
        $cl->SetFieldWeights(array('content' => 1));
    }
    // Function to filter by sub site
    if ($_REQUEST['w'] == 'links') {
        $allowed_categories = SitesMgr::get_categories();
        if ($allowed_categories) {
            $cats = array();
            foreach ($allowed_categories as $c) {
                $cats[] = (int) $c->id;
            }
            $cl->SetFilter('category', $cats);
        }
    }
    $response['rows'] = 0;
    $response['time'] = 0;
    if (empty($_REQUEST['words'])) {
        return $response;
    }
    $words_array = preg_split('/\\s+/', $_REQUEST['words'], -1, PREG_SPLIT_NO_EMPTY);
    $words_count = count($words_array);
    $words = $_REQUEST['words'];
    if ($_REQUEST['t']) {
        $max_date = time();
        $min_date = intval($_REQUEST['t']);
        $cl->SetFilterRange('date', $min_date, $max_date);
    }
    if ($_REQUEST['h']) {
        $max_date = time();
        $min_date = $max_date - intval($_REQUEST['h']) * 3600;
        $cl->SetFilterRange('date', $min_date, $max_date);
    }
    if ($_REQUEST['yymm']) {
        $yymm = intval($_REQUEST['yymm']);
        $yy = intval($yymm / 100);
        $mm = $yymm - $yy * 100;
        $min_date = mktime(0, 0, 0, $mm, 1, $yy);
        if ($mm == 12) {
            $mm = 1;
            $yy++;
        } else {
            $mm++;
        }
        $max_date = mktime(0, 0, 0, $mm, 1, $yy);
        $cl->SetFilterRange('date', $min_date, $max_date);
    }
    if ($_REQUEST['w'] == 'links' && $_REQUEST['s']) {
        if (preg_match('/^ *! */', $_REQUEST['s'])) {
            // Allows to reject a status
            $_REQUEST['s'] = preg_replace('/^ *! */', '', $_REQUEST['s']);
            $s_reject = true;
        } else {
            $s_reject = false;
        }
        // Allow multiple statuses
        $statuses = preg_split('/\\s+/', $_REQUEST['s'], -1, PREG_SPLIT_NO_EMPTY);
        $s_id = array();
        foreach ($statuses as $s) {
            if (isset($globals['status_values'][$s])) {
                array_push($s_id, $globals['status_values'][$s]);
            }
        }
        if (count($s_id) > 0) {
            $cl->SetFilter('status', $s_id, $s_reject);
        }
    }
    if ($_REQUEST['u']) {
        $u = new User();
        $u->username = $_REQUEST['u'];
        $u->read();
        $cl->SetFilterRange('user', $u->id, $u->id);
    }
    if ($_REQUEST['w'] == 'links' && $_REQUEST['p']) {
        $f = '@' . $_REQUEST['p'];
    } else {
        $f = '@*';
    }
    if ($by_date || $_REQUEST['o'] == 'date' || $_REQUEST['p'] == 'url') {
        $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'date');
    } elseif ($_REQUEST['o'] == 'pure') {
        $cl->SetSortMode(SPH_SORT_RELEVANCE);
    } else {
        // If "root_time", it will center the search on that timestamp
        if ($_REQUEST['root_time'] > 0) {
            $now = intval($_REQUEST['root_time']);
        } else {
            $now = time();
        }
        // expressions to decrease weights logarimically
        if ($_REQUEST['w'] == 'links') {
            $p = $globals['status_values']['published'];
            $q = $globals['status_values']['queued'];
            $b = log(0.9) / 720;
            $fp = "@weight * max(0.4, exp({$b}*abs({$now}-date)/3600))";
            $b = log(0.5) / 720;
            $fq = "@weight * max(0.25, exp({$b}*abs({$now}-date)/3600))";
            $b = log(0.2) / 720;
            $fo = "@weight * max(0.1, exp({$b}*abs({$now}-date)/3600))";
            $exp = "if (status-{$p} = 0, {$fp} , if (status-{$q} = 0, {$fq}, {$fo}))";
        } else {
            $b = log(0.95) / 720;
            $exp = "@weight * max(0.5, exp({$b}*abs({$now}-date)/3600))";
        }
        $cl->SetSortMode(SPH_SORT_EXPR, $exp);
    }
    $cl->SetMatchMode(SPH_MATCH_EXTENDED2);
    if ($words_count == 1 || $_REQUEST['p'] == 'url') {
        $cl->SetRankingMode(SPH_RANK_NONE);
    } elseif ($proximity) {
        $cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
    } else {
        $cl->SetRankingMode(SPH_RANK_BM25);
    }
    // Used for related links
    if ($_REQUEST['p'] == 'url') {
        // It allows to search for several domains/url
        for ($i = 0; $i < count($words_array); $i++) {
            $words_array[$i] = '="' . $cl->EscapeString($words_array[$i]) . '"';
        }
        $query = implode(" | ", $words_array);
        // Add the "OR" for several domain
        $q = $cl->AddQuery("{$f} {$query}", $indices);
        array_push($queries, $q);
    } else {
        if ($words_count < 5) {
            $q = $cl->AddQuery("{$f} {$words}", $indices);
            array_push($queries, $q);
        }
    }
    // If there are no boolean opertions, add a new search for ANY of the terms
    // Take in account phrases in between " and '
    if (!preg_match('/( and | or | [\\-\\+\\&\\|])/i', $words) && $words_count > 1 && $_REQUEST['p'] != 'url') {
        $words = '';
        $quotes = 0;
        $c = 0;
        foreach ($words_array as $w) {
            if ($c > 0 && $quotes == 0) {
                $words .= ' | ';
            }
            if ($quotes == 0 && preg_match('/^["\']/', $w)) {
                $quotes++;
            }
            if ($quotes > 0 && preg_match('/["\'](\\~{0,1}\\d+){0,1}$/', $w)) {
                $quotes--;
            }
            $words .= " {$w}";
            $c++;
        }
        $q = $cl->AddQuery("{$f} {$words}", $indices);
        array_push($queries, $q);
    }
    $results = $cl->RunQueries();
    $n = 0;
    $response['error'] = $results['error'];
    foreach ($queries as $q) {
        $res = $results[$q];
        if (is_array($res["matches"])) {
            $response['rows'] += $res["total_found"];
            foreach ($res["matches"] as $doc => $docinfo) {
                if (!$recorded[$doc]) {
                    $response['ids'][$n] = $doc;
                    $response['weights']["{$doc}"] = $docinfo['weight'];
                    $recorded[$doc] = true;
                    $n++;
                } else {
                    $response['rows']--;
                }
            }
        }
    }
    $response['time'] = microtime(true) - $start_time;
    return $response;
}