Ejemplo n.º 1
0
function recherche_en_base($recherche = '', $tables = NULL, $options = array(), $serveur = '')
{
    include_spip('base/abstract_sql');
    if (!is_array($tables)) {
        $liste = liste_des_champs();
        if (is_string($tables) and $tables != '') {
            $toutes = array();
            foreach (explode(',', $tables) as $t) {
                if (isset($liste[$t])) {
                    $toutes[$t] = $liste[$t];
                }
            }
            $tables = $toutes;
            unset($toutes);
        } else {
            $tables = $liste;
        }
    }
    if (!strlen($recherche) or !count($tables)) {
        return array();
    }
    include_spip('inc/autoriser');
    // options par defaut
    $options = array_merge(array('preg_flags' => 'UimsS', 'toutvoir' => false, 'champs' => false, 'score' => false, 'matches' => false, 'jointures' => false, 'serveur' => $serveur), $options);
    $results = array();
    // Utiliser l'iterateur (DATA:recherche)
    // pour recuperer les couples (id_objet, score)
    // Le resultat est au format {
    //      id1 = { 'score' => x, attrs => { } },
    //      id2 = { 'score' => x, attrs => { } },
    // }
    include_spip('inc/memoization');
    foreach ($tables as $table => $champs) {
        # lock via memoization, si dispo
        if (function_exists('cache_lock')) {
            cache_lock($lock = 'recherche ' . $table . ' ' . $recherche);
        }
        spip_timer('rech');
        // TODO: ici plutot charger un iterateur via l'API iterateurs
        include_spip('inc/recherche_to_array');
        $to_array = charger_fonction('recherche_to_array', 'inc');
        $results[$table] = $to_array($recherche, array_merge($options, array('table' => $table, 'champs' => $champs)));
        ##var_dump($results[$table]);
        spip_log("recherche {$table} ({$recherche}) : " . count($results[$table]) . " resultats " . spip_timer('rech'), 'recherche');
        if (isset($lock)) {
            cache_unlock($lock);
        }
    }
    return $results;
}
Ejemplo n.º 2
0
function cache_gc($dir = NULL, $start = 1, $purgeall = FALSE)
{
    static $dirs = 0, $files = 0, $deleted = 0, $ignored = 0, $faileddelete = 0, $empty = 0;
    if ($start == 1) {
        cache_debug("Running GC on {$dir}");
        if (!function_exists("getcwd")) {
            $cwd = substr(`pwd`, 0, -1);
        } else {
            $cwd = getcwd();
        }
        $dirs = $files = $deleted = $ignored = $faileddelete = $empty = 0;
    }
    if (cache_iftype($dir, NULL)) {
        $dir = THIS_CACHE_DIR;
    }
    $dp = opendir($dir);
    if (!$dp) {
        cache_debug("Error opening {$dir} for cleanup");
        return FALSE;
    }
    chdir($dir);
    $dirs++;
    while (!cache_iftype($de = readdir($dp), FALSE)) {
        if (is_dir($de)) {
            if ($de == '.' || $de == '..') {
                continue;
            }
            cache_gc($de, 0, $purgeall);
            chdir('..');
            continue;
        }
        if (preg_match("/^phpCache./i", $de)) {
            $files++;
            $absfile = $de;
            $cachestuff = cache_read($absfile);
            $thecache = unserialize($cachestuff);
            if (is_array($thecache)) {
                if ($purgeall || $cdata["cachetime"] != "0" && $thecache["expire"] <= time()) {
                    cache_lock($absfile, TRUE);
                    if (@unlink($absfile)) {
                        $deleted++;
                        cache_debug("{$dir} Deleted {$absfile}");
                    } else {
                        $faileddelete++;
                        cache_debug("{$dir} Failed to delete {$absfile}");
                    }
                    cache_lock($absfile, FALSE);
                } else {
                    cache_debug("{$dir} {$absfile} expires in " . ($thecache["expire"] - time()));
                }
            } else {
                cache_debug("{$dir} {$absfile} is empty, being processed in another process?");
                $empty++;
            }
        } else {
            $ignored++;
        }
    }
    closedir($dp);
    if ($start == 1) {
        $str = "{$dir} GC Processed: {$dirs}/dirs\t{$files}/files\t{$deleted}/deleted\t{$ignored}/ignored\t{$faileddelete}/faileddelete\t{$empty}/empty";
        cache_debug($str);
        chdir($cwd);
        return $str;
    }
}