1 | <?php |
---|
2 | /** |
---|
3 | * Plugin / Squelettes Gribouille |
---|
4 | * © Fil |
---|
5 | * Distribue sous licence GNU/GPL |
---|
6 | */ |
---|
7 | |
---|
8 | if (!defined("_ECRIRE_INC_VERSION")) { |
---|
9 | return; |
---|
10 | } |
---|
11 | |
---|
12 | // {recherche} ou {recherche susan} |
---|
13 | // https://www.spip.net/@recherche |
---|
14 | // https://code.spip.net/@critere_recherche_dist |
---|
15 | function critere_recherche($idb, &$boucles, $crit) { |
---|
16 | |
---|
17 | $boucle = &$boucles[ $idb ]; |
---|
18 | |
---|
19 | if (isset($crit->param[0])) { |
---|
20 | $quoi = calculer_liste($crit->param[0], array(), $boucles, $boucles[ $idb ]->id_parent); |
---|
21 | } |
---|
22 | else { |
---|
23 | $quoi = '@$Pile[0]["recherche"]'; |
---|
24 | } |
---|
25 | $tout = $boucle->modificateur["tout"] ? 1 : 0; |
---|
26 | $boucle->hash .= ' |
---|
27 | // RECHERCHE |
---|
28 | $prepare_recherche = charger_fonction(\'prepare_recherche\', \'inc\'); |
---|
29 | list($rech_select, $rech_where) = $prepare_recherche(' . $quoi . ', "' . $boucle->id_table . '", "' . $crit->cond . '","' . $boucle->sql_serveur . '",' . $tout . '); |
---|
30 | '; |
---|
31 | |
---|
32 | $t = $boucle->id_table . '.' . $boucle->primary; |
---|
33 | if (!in_array($t, $boucles[ $idb ]->select)) { |
---|
34 | $boucle->select[] = $t; |
---|
35 | } # pour postgres, neuneu ici |
---|
36 | $boucle->join['resultats'] = array( |
---|
37 | "'" . $boucle->id_table . "'", |
---|
38 | "'id'", |
---|
39 | "'" . $boucle->primary . "'", |
---|
40 | ); |
---|
41 | $boucle->from['resultats'] = 'spip_resultats'; |
---|
42 | $boucle->select[] = '$rech_select'; |
---|
43 | //$boucle->where[]= "\$rech_where?'resultats.id=".$boucle->id_table.".".$boucle->primary."':''"; |
---|
44 | |
---|
45 | // et la recherche trouve |
---|
46 | $boucle->where[] = '$rech_where?$rech_where:\'\''; |
---|
47 | } |
---|
48 | |
---|
49 | @define('_DELAI_CACHE_resultats', 600); |
---|
50 | |
---|
51 | // Preparer les listes id_article IN (...) pour les parties WHERE |
---|
52 | // et points = des requetes du moteur de recherche |
---|
53 | // https://code.spip.net/@inc_prepare_recherche_dist |
---|
54 | function inc_prepare_recherche($recherche, $table = 'articles', $cond = false, $serveur = '', $tout = false) { |
---|
55 | include_spip('inc/rechercher'); |
---|
56 | static $cache = array(); |
---|
57 | $delai_fraicheur = min(_DELAI_CACHE_resultats, time() - $GLOBALS['meta']['derniere_modif']); |
---|
58 | |
---|
59 | // si recherche n'est pas dans le contexte, on va prendre en globals |
---|
60 | // ca permet de faire des inclure simple. |
---|
61 | if (!isset($recherche) AND isset($GLOBALS['recherche'])) { |
---|
62 | $recherche = $GLOBALS['recherche']; |
---|
63 | } |
---|
64 | |
---|
65 | // traiter le cas {recherche?} |
---|
66 | if ($cond AND !strlen($recherche)) { |
---|
67 | return array( |
---|
68 | "0 as points" |
---|
69 | /* as points */, |
---|
70 | /* where */ |
---|
71 | '', |
---|
72 | ); |
---|
73 | } |
---|
74 | |
---|
75 | $rechercher = false; |
---|
76 | |
---|
77 | if (!isset($cache[ $recherche ][ $table ])) { |
---|
78 | $hash = substr(md5($recherche . $table), 0, 16); |
---|
79 | $row = sql_fetsel('UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(maj) AS fraicheur', 'spip_resultats', "recherche='$hash'", '', 'fraicheur DESC', '0,1', '', $serveur); |
---|
80 | if (!$row OR ($row['fraicheur'] > $delai_fraicheur)) { |
---|
81 | $rechercher = true; |
---|
82 | } |
---|
83 | $cache[ $recherche ][ $table ] = array( |
---|
84 | "resultats.points AS points", |
---|
85 | "recherche='$hash'", |
---|
86 | ); |
---|
87 | } |
---|
88 | |
---|
89 | // si on n'a pas encore traite les donnees dans une boucle precedente |
---|
90 | if ($rechercher) { |
---|
91 | //$tables = liste_des_champs(); |
---|
92 | $x = preg_replace(',s$,', '', $table); // eurk |
---|
93 | if ($x == 'syndic') { |
---|
94 | $x = 'site'; |
---|
95 | } |
---|
96 | $points = recherche_en_base($recherche, |
---|
97 | $x, |
---|
98 | array( |
---|
99 | 'score' => true, |
---|
100 | 'toutvoir' => true, |
---|
101 | 'jointures' => true, |
---|
102 | ), |
---|
103 | $serveur); |
---|
104 | $points = $points[ $x ]; |
---|
105 | |
---|
106 | // permettre aux plugins de modifier le resultat |
---|
107 | $points = pipeline('prepare_recherche', array( |
---|
108 | 'args' => array( |
---|
109 | 'type' => $x, |
---|
110 | 'recherche' => $recherche, |
---|
111 | 'serveur' => $serveur, |
---|
112 | 'tout' => $tout, |
---|
113 | ), |
---|
114 | 'data' => $points, |
---|
115 | )); |
---|
116 | |
---|
117 | // supprimer les anciens resultats de cette recherche |
---|
118 | // et les resultats trop vieux avec une marge |
---|
119 | sql_delete('spip_resultats', '(maj<DATE_SUB(NOW(), INTERVAL ' . ($delai_fraicheur + 100) . " SECOND)) OR (recherche='$hash')", $serveur); |
---|
120 | |
---|
121 | // inserer les resultats dans la table de cache des resultats |
---|
122 | if (count($points)) { |
---|
123 | $tab_couples = array(); |
---|
124 | foreach ($points as $id => $p) { |
---|
125 | $tab_couples[] = array( |
---|
126 | 'recherche' => $hash, |
---|
127 | 'id' => $id, |
---|
128 | 'points' => $p['score'], |
---|
129 | ); |
---|
130 | } |
---|
131 | sql_insertq_multi('spip_resultats', $tab_couples, array(), $serveur); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | return $cache[ $recherche ][ $table ]; |
---|
136 | } |
---|
137 | |
---|
138 | if(!function_exists('analyse_droits_rapide')) { |
---|
139 | function analyse_droits_rapide() { |
---|
140 | return true; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|