[SPIP] v3.2.1-->v3.2.2
[lhc/web/www.git] / www / ecrire / inc / recherche_to_array.php
index 14ab04c..8c18a25 100644 (file)
@@ -3,7 +3,7 @@
 /***************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2014                                                *
+ *  Copyright (c) 2001-2019                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
@@ -11,7 +11,9 @@
 \***************************************************************************/
 
 
-if (!defined('_ECRIRE_INC_VERSION')) return;
+if (!defined('_ECRIRE_INC_VERSION')) {
+       return;
+}
 
 
 // methodes sql
@@ -20,11 +22,11 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
        // options par defaut
        $options = array_merge(
                array(
-               'score' => true,
-               'champs' => false,
-               'toutvoir' => false,
-               'matches' => false,
-               'jointures' => false
+                       'score' => true,
+                       'champs' => false,
+                       'toutvoir' => false,
+                       'matches' => false,
+                       'jointures' => false
                ),
                $options
        );
@@ -33,19 +35,19 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
        include_spip('inc/autoriser');
 
        $requete = array(
-               "SELECT"=>array(),
-               "FROM"=>array(),
-               "WHERE"=>array(),
-               "GROUPBY"=>array(),
-               "ORDERBY"=>array(),
-               "LIMIT"=>"",
-               "HAVING"=>array()
+               "SELECT" => array(),
+               "FROM" => array(),
+               "WHERE" => array(),
+               "GROUPBY" => array(),
+               "ORDERBY" => array(),
+               "LIMIT" => "",
+               "HAVING" => array()
        );
 
        $table = sinon($options['table'], 'article');
-       if ($options['champs'])
+       if ($options['champs']) {
                $champs = $options['champs'];
-       else {
+       else {
                $l = liste_des_champs();
                $champs = $l['article'];
        }
@@ -61,42 +63,46 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
 
        // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ','
        // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs)
-       if (strpos($_id_table,",")!==false){
-               $_id_table = explode(',',$_id_table);
+       if (strpos($_id_table, ",") !== false) {
+               $_id_table = explode(',', $_id_table);
                $_id_table = reset($_id_table);
        }
 
-       $requete['SELECT'][] = "t.".$_id_table;
+       $requete['SELECT'][] = "t." . $_id_table;
        $a = array();
        // Recherche fulltext
        foreach ($champs as $champ => $poids) {
-               if (is_array($champ)){
+               if (is_array($champ)) {
                        spip_log("requetes imbriquees interdites");
                } else {
-                       if (strpos($champ,".")===FALSE)
+                       if (strpos($champ, ".") === false) {
                                $champ = "t.$champ";
+                       }
                        $requete['SELECT'][] = $champ;
-                       $a[] = $champ.' '.$methode.' '.$q;
+                       $a[] = $champ . ' ' . $methode . ' ' . $q;
                }
        }
-       if ($a) $requete['WHERE'][] = join(" OR ", $a);
-       $requete['FROM'][] = table_objet_sql($table).' AS t';
+       if ($a) {
+               $requete['WHERE'][] = join(" OR ", $a);
+       }
+       $requete['FROM'][] = table_objet_sql($table) . ' AS t';
 
        $results = array();
 
        $s = sql_select(
                $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
-               implode(" ",$requete['GROUPBY']),
+               implode(" ", $requete['GROUPBY']),
                $requete['ORDERBY'], $requete['LIMIT'],
                $requete['HAVING'], $serveur
        );
 
-       while ($t = sql_fetch($s,$serveur)
-       AND (!isset($t['score']) OR $t['score']>0)) {
+       while ($t = sql_fetch($s, $serveur)
+               and (!isset($t['score']) or $t['score'] > 0)) {
                $id = intval($t[$_id_table]);
 
                if ($options['toutvoir']
-               OR autoriser('voir', $table, $id)) {
+                       or autoriser('voir', $table, $id)
+               ) {
                        // indiquer les champs concernes
                        $champs_vus = array();
                        $score = 0;
@@ -104,41 +110,50 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
 
                        $vu = false;
                        foreach ($champs as $champ => $poids) {
-                               $champ = explode('.',$champ);
+                               $champ = explode('.', $champ);
                                $champ = end($champ);
                                // translitteration_rapide uniquement si on est deja en utf-8
-                               $value = ($GLOBALS['meta']['charset']=='utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
+                               $value = ($GLOBALS['meta']['charset'] == 'utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
                                if ($n =
                                        ($options['score'] || $options['matches'])
-                                       ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
-                                       : preg_match($preg, $value)
+                                               ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
+                                               : preg_match($preg, $value)
                                ) {
                                        $vu = true;
 
-                                       if ($options['champs'])
+                                       if ($options['champs']) {
                                                $champs_vus[$champ] = $t[$champ];
-                                       if ($options['score'])
+                                       }
+                                       if ($options['score']) {
                                                $score += $n * $poids;
-                                       if ($options['matches'])
+                                       }
+                                       if ($options['matches']) {
                                                $matches[$champ] = $regs;
+                                       }
 
                                        if (!$options['champs']
-                                       AND !$options['score']
-                                       AND !$options['matches'])
+                                               and !$options['score']
+                                               and !$options['matches']
+                                       ) {
                                                break;
+                                       }
                                }
                        }
 
                        if ($vu) {
-                               if (!isset($results))
+                               if (!isset($results)) {
                                        $results = array();
+                               }
                                $results[$id] = array();
-                               if ($champs_vus)
+                               if ($champs_vus) {
                                        $results[$id]['champs'] = $champs_vus;
-                               if ($score)
+                               }
+                               if ($score) {
                                        $results[$id]['score'] = $score;
-                               if ($matches)
+                               }
+                               if ($matches) {
                                        $results[$id]['matches'] = $matches;
+                               }
                        }
                }
        }
@@ -148,76 +163,85 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
        // ici on est un peu naze : pas capables de reconstruire une jointure complexe
        // on ne sait passer que par table de laison en 1 coup
        if (isset($jointures[$table])
-       AND $joints = recherche_en_base(
+               and $joints = recherche_en_base(
                        $recherche,
                        $jointures[$table],
                        array_merge($options, array('jointures' => false))
                )
        ) {
                include_spip('action/editer_liens');
-               $trouver_table = charger_fonction('trouver_table','base');
+               $trouver_table = charger_fonction('trouver_table', 'base');
                $cle_depart = id_table_objet($table);
-               $table_depart = table_objet($table,$serveur);
-               $desc_depart = $trouver_table($table_depart,$serveur);
+               $table_depart = table_objet($table, $serveur);
+               $desc_depart = $trouver_table($table_depart, $serveur);
                $depart_associable = objet_associable($table);
                foreach ($joints as $table_liee => $ids_trouves) {
                        // on peut definir une fonction de recherche jointe pour regler les cas particuliers
                        if (
-                               !(
-                                       $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}","inc",true)
-                                       or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}","inc",true)
-                                       or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie","inc",true)
-                               )
-                       ){
-                               $cle_arrivee =  id_table_objet($table_liee);
-                               $table_arrivee = table_objet($table_liee,$serveur);
-                               $desc_arrivee = $trouver_table($table_arrivee,$serveur);
+                       !(
+                               $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", "inc", true)
+                               or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", "inc", true)
+                               or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", "inc", true)
+                       )
+                       ) {
+                               $cle_arrivee = id_table_objet($table_liee);
+                               $table_arrivee = table_objet($table_liee, $serveur);
+                               $desc_arrivee = $trouver_table($table_arrivee, $serveur);
                                // cas simple : $cle_depart dans la table_liee
-                               if (isset($desc_arrivee['field'][$cle_depart])){
-                                       $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
-                               }
-                               // cas simple : $cle_arrivee dans la table
-                               elseif (isset($desc_depart['field'][$cle_arrivee])){
-                                       $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
+                               if (isset($desc_arrivee['field'][$cle_depart])) {
+                                       $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'],
+                                               sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
+                               } // cas simple : $cle_arrivee dans la table
+                               elseif (isset($desc_depart['field'][$cle_arrivee])) {
+                                       $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'],
+                                               sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
                                }
                                // sinon cherchons une table de liaison
                                // cas recherche principale article, objet lie document : passer par spip_documents_liens
-                               elseif ($l = objet_associable($table_liee)){
+                               elseif ($l = objet_associable($table_liee)) {
                                        list($primary, $table_liens) = $l;
-                                       $s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens, array("objet='$table'",sql_in($primary, array_keys($ids_trouves))), '','','','',$serveur);
-                               }
-                               // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
-                               elseif ($l = $depart_associable){
+                                       $s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens,
+                                               array("objet='$table'", sql_in($primary, array_keys($ids_trouves))), '', '', '', '', $serveur);
+                               // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
+                               elseif ($l = $depart_associable) {
                                        list($primary, $table_liens) = $l;
-                                       $s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens, array("objet='$table_liee'",sql_in('id_objet', array_keys($ids_trouves))), '','','','',$serveur);
-                               }
-                               // cas table de liaison generique spip_xxx_yyy
-                               elseif($t=$trouver_table($table_arrivee."_".$table_depart,$serveur)
-                                 OR $t=$trouver_table($table_depart."_".$table_arrivee,$serveur)){
-                                       $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
+                                       $s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens,
+                                               array("objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))), '', '', '', '', $serveur);
+                               } // cas table de liaison generique spip_xxx_yyy
+                               elseif ($t = $trouver_table($table_arrivee . "_" . $table_depart, $serveur)
+                                       or $t = $trouver_table($table_depart . "_" . $table_arrivee, $serveur)
+                               ) {
+                                       $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
+                                               '', '', '', '', $serveur);
                                }
+                       } else {
+                               list($cle_depart, $cle_arrivee, $s) = $rechercher_joints($table, $table_liee, array_keys($ids_trouves),
+                                       $serveur);
                        }
-                       else
-                               list($cle_depart,$cle_arrivee,$s) = $rechercher_joints($table,$table_liee,array_keys($ids_trouves), $serveur);
 
-                       while ($t = is_array($s)?array_shift($s):sql_fetch($s)) {
+                       while ($t = is_array($s) ? array_shift($s) : sql_fetch($s)) {
                                $id = $t[$cle_depart];
                                $joint = $ids_trouves[$t[$cle_arrivee]];
-                               if (!isset($results))
+                               if (!isset($results)) {
                                        $results = array();
-                               if (!isset($results[$id]))
+                               }
+                               if (!isset($results[$id])) {
                                        $results[$id] = array();
+                               }
                                if (isset($joint['score']) and $joint['score']) {
+                                       if (!isset($results[$id]['score'])) {
+                                               $results[$id]['score'] = 0;
+                                       }
                                        $results[$id]['score'] += $joint['score'];
                                }
                                if (isset($joint['champs']) and $joint['champs']) {
-                                       foreach($joint['champs'] as $c => $val) {
-                                               $results[$id]['champs'][$table_liee.'.'.$c] = $val;
+                                       foreach ($joint['champs'] as $c => $val) {
+                                               $results[$id]['champs'][$table_liee . '.' . $c] = $val;
                                        }
                                }
                                if (isset($joint['matches']) and $joint['matches']) {
-                                               foreach($joint['matches'] as $c => $val) {
-                                               $results[$id]['matches'][$table_liee.'.'.$c] = $val;
+                                       foreach ($joint['matches'] as $c => $val) {
+                                               $results[$id]['matches'][$table_liee . '.' . $c] = $val;
                                        }
                                }
                        }
@@ -226,6 +250,3 @@ function inc_recherche_to_array_dist($recherche, $options = array()) {
 
        return $results;
 }
-
-
-?>