X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=www%2Fecrire%2Finc%2Fprepare_recherche.php;fp=www%2Fecrire%2Finc%2Fprepare_recherche.php;h=680c3819a161f412b245878587a60c961bf4ca69;hb=4f443dce95ff6f8221c189880a70c74ce1c1f238;hp=1570293272a7e2078918ac3d2cc560d1b9c146b1;hpb=4a628e9b277d3617535f99d663ca79fa2e891177;p=lhc%2Fweb%2Fwww.git diff --git a/www/ecrire/inc/prepare_recherche.php b/www/ecrire/inc/prepare_recherche.php index 15702932..680c3819 100644 --- a/www/ecrire/inc/prepare_recherche.php +++ b/www/ecrire/inc/prepare_recherche.php @@ -3,26 +3,34 @@ /***************************************************************************\ * SPIP, Systeme de publication pour l'internet * * * - * Copyright (c) 2001-2016 * + * Copyright (c) 2001-2017 * * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * * * * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * \***************************************************************************/ +/** + * Gestion des préparatifs de recherches + * + * @package SPIP\Core\Recherche + **/ -if (!defined('_ECRIRE_INC_VERSION')) return; +if (!defined('_ECRIRE_INC_VERSION')) { + return; +} include_spip('inc/rechercher'); -if (!defined('_DELAI_CACHE_resultats')) define('_DELAI_CACHE_resultats', 600); +if (!defined('_DELAI_CACHE_resultats')) { + define('_DELAI_CACHE_resultats', 600); +} /** - * Preparer les listes id_article IN (...) pour les parties WHERE - * et points = des requetes du moteur de recherche - * http://doc.spip.org/@inc_prepare_recherche_dist - * - * Le parametre $serveur est utilise pour savoir sur quelle base on cherche - * mais l'index des resultats est toujours stock� sur le serveur principal + * 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 * @@ -40,31 +48,46 @@ if (!defined('_DELAI_CACHE_resultats')) define('_DELAI_CACHE_resultats', 600); * cle primaire de la table de recherche * @return array */ -function inc_prepare_recherche_dist($recherche, $table='articles', $cond=false, $serveur='', $modificateurs = array(), $primary='') { +function inc_prepare_recherche_dist( + $recherche, + $table = 'articles', + $cond = false, + $serveur = '', + $modificateurs = array(), + $primary = '' +) { static $cache = array(); - $delai_fraicheur = min(_DELAI_CACHE_resultats,time()-$GLOBALS['meta']['derniere_modif']); + $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'])) + if (!isset($recherche) and isset($GLOBALS['recherche'])) { $recherche = $GLOBALS['recherche']; + } // traiter le cas {recherche?} - if ($cond AND !strlen($recherche)) - return array("0 as points" /* as points */, /* where */ ''); - - + if ($cond and !strlen($recherche)) { + return array( + "0 as points" /* as points */, /* where */ + '' + ); + } + + $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 (!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; + or ($row['fraicheur'] > $delai_fraicheur) + or (defined('_VAR_MODE') and _VAR_MODE == 'recalcul') + ) { + $rechercher = true; } } @@ -78,27 +101,34 @@ function inc_prepare_recherche_dist($recherche, $table='articles', $cond=false, '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 + $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)"); + $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)){ + if (count($points)) { $tab_couples = array(); - foreach ($points as $id => $p){ + foreach ($points as $id => $p) { $tab_couples[] = array( 'recherche' => $hash, 'id' => $id, @@ -107,16 +137,17 @@ function inc_prepare_recherche_dist($recherche, $table='articles', $cond=false, 'serveur' => $hash_serv, ); } - sql_insertq_multi('spip_resultats',$tab_couples,array()); + 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); + 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); } } @@ -136,25 +167,23 @@ function inc_prepare_recherche_dist($recherche, $table='articles', $cond=false, * @param string $serveur * @return array */ -function generer_select_where_explicites($table, $primary, $rows, $serveur){ +function generer_select_where_explicites($table, $primary, $rows, $serveur) { # calculer le {id_article IN()} et le {... as points} - if (!count($rows)){ + if (!count($rows)) { return array("''", "0=1"); - } - else { + } else { $listes_ids = array(); $select = '0'; - foreach ($rows as $r) + foreach ($rows as $r) { $listes_ids[$r['points']][] = $r['id']; + } - foreach ($listes_ids as $p => $ids) - $select .= "+$p*(". - sql_in("$table.$primary", $ids,'',$serveur) - .") "; + foreach ($listes_ids as $p => $ids) { + $select .= "+$p*(" . + sql_in("$table.$primary", $ids, '', $serveur) + . ") "; + } - return array("$select AS points ",calcul_mysql_in("$table.$primary",array_map('reset',$rows),'',$serveur)); + return array("$select AS points ", sql_in("$table.$primary", array_map('reset', $rows), '', $serveur)); } } - - -?>