示例#1
0
/**
 * Préparer les listes `id_article IN (...)` pour les parties WHERE
 * et calcul des `points` pour la partie SELECT des requêtes du moteur de recherche
 * 
 * Le paramètre $serveur est utilisé pour savoir sur quelle base on cherche
 * mais l'index des résultats est toujours stocké sur le serveur principal
 * car on ne sait pas si la base distante dispose d'une table spip_resultats
 * ni meme si on aurait le droit d'ecrire dedans
 *
 * @param string $recherche
 *    chaine recherchee
 * @param string $table
 *    table dans laquelle porte la recherche
 * @param bool $cond
 *    critere conditionnel sur {recherche?}
 * @param string $serveur
 *    serveur de base de donnees
 * @param array $modificateurs
 *    modificateurs de boucle, ie liste des criteres presents
 * @param string $primary
 *    cle primaire de la table de recherche
 * @return array
 */
function inc_prepare_recherche_dist($recherche, $table = 'articles', $cond = false, $serveur = '', $modificateurs = array(), $primary = '')
{
    static $cache = array();
    $delai_fraicheur = min(_DELAI_CACHE_resultats, time() - (isset($GLOBALS['meta']['derniere_modif']) ? $GLOBALS['meta']['derniere_modif'] : 0));
    // si recherche n'est pas dans le contexte, on va prendre en globals
    // ca permet de faire des inclure simple.
    if (!isset($recherche) and isset($GLOBALS['recherche'])) {
        $recherche = $GLOBALS['recherche'];
    }
    // traiter le cas {recherche?}
    if ($cond and !strlen($recherche)) {
        return array("0 as points", '');
    }
    $rechercher = false;
    if (!isset($cache[$serveur][$table][$recherche])) {
        $hash_serv = $serveur ? substr(md5($serveur), 0, 16) : '';
        $hash = substr(md5($recherche . $table), 0, 16);
        $where = "(resultats.recherche='{$hash}' AND resultats.table_objet=" . sql_quote($table) . " AND resultats.serveur='{$hash_serv}')";
        $row = sql_fetsel('UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(resultats.maj) AS fraicheur', 'spip_resultats AS resultats', $where, '', 'fraicheur DESC', '0,1');
        if (!$row or $row['fraicheur'] > $delai_fraicheur or defined('_VAR_MODE') and _VAR_MODE == 'recalcul') {
            $rechercher = true;
        }
    }
    // si on n'a pas encore traite les donnees dans une boucle precedente
    if ($rechercher) {
        //$tables = liste_des_champs();
        $x = objet_type($table);
        $points = recherche_en_base($recherche, $x, array('score' => true, 'toutvoir' => true, 'jointures' => true), $serveur);
        // pas de résultat, pas de point
        $points = isset($points[$x]) ? $points[$x] : array();
        // permettre aux plugins de modifier le resultat
        $points = pipeline('prepare_recherche', array('args' => array('type' => $x, 'recherche' => $recherche, 'serveur' => $serveur, 'modificateurs' => $modificateurs), 'data' => $points));
        // supprimer les anciens resultats de cette recherche
        // et les resultats trop vieux avec une marge
        // pas de AS resultats dans un delete (mysql)
        $whered = str_replace(array("resultats.recherche", "resultats.table_objet", "resultats.serveur"), array("recherche", "table_objet", "serveur"), $where);
        sql_delete('spip_resultats', 'NOT(' . sql_date_proche('maj', 0 - ($delai_fraicheur + 100), " SECOND") . ") OR ({$whered})");
        // inserer les resultats dans la table de cache des resultats
        if (count($points)) {
            $tab_couples = array();
            foreach ($points as $id => $p) {
                $tab_couples[] = array('recherche' => $hash, 'id' => $id, 'points' => $p['score'], 'table_objet' => $table, 'serveur' => $hash_serv);
            }
            sql_insertq_multi('spip_resultats', $tab_couples, array());
        }
    }
    if (!isset($cache[$serveur][$table][$recherche])) {
        if (!$serveur) {
            $cache[$serveur][$table][$recherche] = array("resultats.points AS points", $where);
        } else {
            if (sql_countsel('spip_resultats as resultats', $where)) {
                $rows = sql_allfetsel('resultats.id,resultats.points', 'spip_resultats as resultats', $where);
            }
            $cache[$serveur][$table][$recherche] = generer_select_where_explicites($table, $primary, $rows, $serveur);
        }
    }
    return $cache[$serveur][$table][$recherche];
}
示例#2
0
/**
 * Optimiser la base de donnee en supprimant les forums orphelins
 *
 * @param int $n
 * @return int
 */
function petitions_optimiser_base_disparus($flux)
{
    $n =& $flux['data'];
    $mydate = $flux['args']['date'];
    //
    // Signatures poubelles
    //
    sql_delete("spip_petitions", "statut='poubelle' AND maj < {$mydate}");
    // rejeter les signatures non confirmees trop vieilles (20jours)
    if (!defined('_PETITIONS_DELAI_SIGNATURES_REJETEES')) {
        define('_PETITIONS_DELAI_SIGNATURES_REJETEES', 20);
    }
    sql_delete("spip_signatures", "NOT (statut='publie' OR statut='poubelle') AND NOT(" . sql_date_proche('date_time', -_PETITIONS_DELAI_SIGNATURES_REJETEES, ' DAY') . ')');
    return $flux;
}
示例#3
0
/**
 * Mettre a jour le site
 * Attention, cette fonction ne doit pas etre appellee simultanement
 * sur un meme site: un verrouillage a du etre pose en amont.
 * => elle doit toujours etre appelee par job_queue_add
 *
 * http://doc.spip.org/@syndic_a_jour
 *
 * @param int $now_id_syndic
 * @return bool|string
 */
function syndic_a_jour($now_id_syndic)
{
    include_spip('inc/texte');
    $call = debug_backtrace();
    if ($call[1]['function'] !== 'queue_start_job') {
        spip_log("syndic_a_jour doit etre appelee par JobQueue Cf. http://trac.rezo.net/trac/spip/changeset/10294", _LOG_ERREUR);
    }
    $row = sql_fetsel("*", "spip_syndic", "id_syndic=" . intval($now_id_syndic));
    if (!$row) {
        return;
    }
    $url_syndic = $row['url_syndic'];
    $url_site = $row['url_site'];
    if ($row['moderation'] == 'oui') {
        $moderation = 'dispo';
    } else {
        $moderation = 'publie';
    }
    // en ligne sans validation
    // determiner le statut a poser en cas d'echec : sus par defaut
    // off si le site est deja off, ou sus depuis trop longtemps
    $statut = 'sus';
    if ($row['statut'] == 'off' or $row['statut'] == 'sus' and time() - strtotime($row['date_syndic']) > _PERIODE_SYNDICATION_SUSPENDUE * 60) {
        $statut = 'off';
    }
    sql_updateq('spip_syndic', array('syndication' => $statut, 'date_syndic' => date('Y-m-d H:i:s')), "id_syndic=" . intval($now_id_syndic));
    // Aller chercher les donnees du RSS et les analyser
    include_spip('inc/distant');
    $rss = recuperer_page($url_syndic, true);
    if (!$rss) {
        $articles = _T('sites:avis_echec_syndication_02');
    } else {
        $articles = analyser_backend($rss, $url_syndic);
    }
    // Renvoyer l'erreur le cas echeant
    if (!is_array($articles)) {
        return $articles;
    }
    // Les enregistrer dans la base
    $faits = array();
    foreach ($articles as $data) {
        inserer_article_syndique($data, $now_id_syndic, $moderation, $url_site, $url_syndic, $row['resume'], $row['documents'], $faits);
    }
    // moderation automatique des liens qui sont sortis du feed
    if (count($faits) > 0) {
        $faits = sql_in("id_syndic_article", $faits, 'NOT');
        if ($row['miroir'] == 'oui') {
            sql_update('spip_syndic_articles', array('statut' => "'off'", 'maj' => 'maj'), "id_syndic={$now_id_syndic} AND {$faits}");
        }
        // suppression apres 2 mois des liens qui sont sortis du feed
        if ($row['oubli'] == 'oui') {
            sql_delete('spip_syndic_articles', "id_syndic={$now_id_syndic} AND NOT(" . sql_date_proche('maj', -2, 'MONTH') . ') AND NOT(' . sql_date_proche('date', -2, 'MONTH') . ") AND {$faits}");
        }
    }
    // Noter que la syndication est OK
    sql_updateq("spip_syndic", array("syndication" => 'oui'), "id_syndic=" . intval($now_id_syndic));
    return false;
    # c'est bon
}
function inc_stats_visites_to_array_dist($unite, $duree, $id_article, $options = array())
{
    $now = time();
    if (!in_array($unite, array('jour', 'mois'))) {
        $unite = 'jour';
    }
    $serveur = '';
    $table = "spip_visites";
    $order = "date";
    $where = array();
    if ($duree) {
        $where[] = sql_date_proche($order, -$duree, 'day', $serveur);
    }
    if ($id_article) {
        $table = "spip_visites_articles";
        $where[] = "id_article=" . intval($id_article);
    }
    $where = implode(" AND ", $where);
    $format = $unite == 'jour' ? '%Y-%m-%d' : '%Y-%m-01';
    $res = sql_select("SUM(visites) AS v, DATE_FORMAT({$order},'{$format}') AS d", $table, $where, "d", "d", "", '', $serveur);
    $format = str_replace('%', '', $format);
    $periode = $unite == 'jour' ? 24 * 3600 : 365 * 24 * 3600 / 12;
    $step = intval(round($periode * 1.1, 0));
    $glisse = constant('MOYENNE_GLISSANTE_' . strtoupper($unite));
    moyenne_glissante();
    $data = array();
    $r = sql_fetch($res, $serveur);
    if (!$r) {
        $r = array('d' => date($format, $now), 'v' => 0);
    }
    do {
        $data[$r['d']] = array('visites' => $r['v'], 'moyenne' => moyenne_glissante($r['v'], $glisse));
        $last = $r['d'];
        // donnee suivante
        $r = sql_fetch($res, $serveur);
        // si la derniere n'est pas la date courante, l'ajouter
        if (!$r and $last != date($format, $now)) {
            $r = array('d' => date($format, $now), 'v' => 0);
        }
        // completer les trous manquants si besoin
        if ($r) {
            $next = strtotime($last);
            $current = strtotime($r['d']);
            while (($next += $step) < $current and $d = date($format, $next)) {
                if (!isset($data[$d])) {
                    $data[$d] = array('visites' => 0, 'moyenne' => moyenne_glissante(0, $glisse));
                }
                $last = $d;
                $next = strtotime($last);
            }
        }
    } while ($r);
    // projection pour la derniere barre :
    // mesure courante
    // + moyenne au pro rata du temps qui reste
    $moyenne = end($data);
    $moyenne = prev($data);
    $moyenne = ($moyenne and isset($moyenne['moyenne'])) ? $moyenne['moyenne'] : 0;
    $data[$last]['moyenne'] = $moyenne;
    // temps restant
    $remaining = strtotime(date($format, strtotime(date($format, $now)) + $step)) - $now;
    $prorata = $remaining / $periode;
    // projection
    $data[$last]['prevision'] = $data[$last]['visites'] + intval(round($moyenne * $prorata));
    /*
     * Compter les fichiers en attente de depouillement dans tmp/visites/
     * pour affiner la prediction.
     * A activer dans le mes_options si l'hebergement tient le coup en cas de gros pics de traffic
     */
    if (!$id_article and defined('_STATS_COMPTE_EN_ATTENTE') and _STATS_COMPTE_EN_ATTENTE) {
        // eviter un depassement memoire en mesurant un echantillon pour commencer
        $n = count(glob(_DIR_RACINE . "tmp/visites/0*"));
        if ($n < 10000) {
            $n = count(glob(_DIR_RACINE . "tmp/visites/*"));
        } else {
            $n += count(glob(_DIR_RACINE . "tmp/visites/4*"));
            $n += count(glob(_DIR_RACINE . "tmp/visites/8*"));
            $n += count(glob(_DIR_RACINE . "tmp/visites/c*"));
            $n = 4 * $n;
        }
        $data[$last]['prevision'] += $n;
    }
    return $data;
}
示例#5
0
function inc_stats_visites_to_array_dist($unite, $duree, $id_article, $options = array())
{
    $now = time();
    if (!in_array($unite, array('jour', 'mois'))) {
        $unite = 'jour';
    }
    $serveur = '';
    $table = "spip_visites";
    $order = "date";
    $where = array();
    if ($duree) {
        $where[] = sql_date_proche($order, -$duree, 'day', $serveur);
    }
    if ($id_article) {
        $table = "spip_visites_articles";
        $where[] = "id_article=" . intval($id_article);
    }
    $where = implode(" AND ", $where);
    $format = $unite == 'jour' ? '%Y-%m-%d' : '%Y-%m-01';
    $res = sql_select("SUM(visites) AS v, DATE_FORMAT({$order},'{$format}') AS d", $table, $where, "d", "d", "", '', $serveur);
    $format = str_replace('%', '', $format);
    $periode = $unite == 'jour' ? 24 * 3600 : 365 * 24 * 3600 / 12;
    $step = intval(round($periode * 1.1, 0));
    $glisse = constant('MOYENNE_GLISSANTE_' . strtoupper($unite));
    moyenne_glissante();
    $data = array();
    $r = sql_fetch($res, $serveur);
    if (!$r) {
        $r = array('d' => date($format, $now), 'v' => 0);
    }
    do {
        $data[$r['d']] = array('visites' => $r['v'], 'moyenne' => moyenne_glissante($r['v'], $glisse));
        $last = $r['d'];
        // donnee suivante
        $r = sql_fetch($res, $serveur);
        // si la derniere n'est pas la date courante, l'ajouter
        if (!$r and $last != date($format, $now)) {
            $r = array('d' => date($format, $now), 'v' => 0);
        }
        // completer les trous manquants si besoin
        if ($r) {
            $next = strtotime($last);
            $current = strtotime($r['d']);
            while (($next += $step) < $current and $d = date($format, $next)) {
                if (!isset($data[$d])) {
                    $data[$d] = array('visites' => 0, 'moyenne' => moyenne_glissante(0, $glisse));
                }
                $last = $d;
                $next = strtotime($last);
            }
        }
    } while ($r);
    // projection pour la derniere barre :
    // mesure courante
    // + moyenne au pro rata du temps qui reste
    $moyenne = end($data);
    $moyenne = prev($data);
    $moyenne = ($moyenne and isset($moyenne['moyenne'])) ? $moyenne['moyenne'] : 0;
    $data[$last]['moyenne'] = $moyenne;
    // temps restant
    $remaining = strtotime(date($format, strtotime(date($format, $now)) + $step)) - $now;
    $prorata = $remaining / $periode;
    // projection
    $data[$last]['prevision'] = $data[$last]['visites'] + intval(round($moyenne * $prorata));
    return $data;
}
/**
 * @param $champ
 * @param $interval
 * @param $unite
 * @return string
 */
function nospam_sql_date_proche($champ, $interval, $unite)
{
    if (function_exists("sql_date_proche")) {
        return sql_date_proche($champ, $interval, $unite);
    }
    return '(' . $champ . ($interval <= 0 ? '>' : '<') . ($interval <= 0 ? 'DATE_SUB' : 'DATE_ADD') . '(' . sql_quote(date('Y-m-d H:i:s')) . ', INTERVAL ' . ($interval > 0 ? $interval : 0 - $interval) . ' ' . $unite . '))';
}