2025 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* @package jrest
|
|
|
4 |
* @author Aurélien Peronnet <aurelien@tela-botania.org>
|
|
|
5 |
* @copyright 2010, 2013 Tela-Botanica
|
|
|
6 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
7 |
*
|
|
|
8 |
* Librairie de liaison d'images et d'observation à des mots clés en utilisant la méthode
|
|
|
9 |
* path enumeration
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
class GestionMotsClesChemin {
|
|
|
13 |
|
|
|
14 |
private $config;
|
|
|
15 |
private $mode;
|
|
|
16 |
|
|
|
17 |
private $table_liaison;
|
|
|
18 |
private $table_mots_cles;
|
|
|
19 |
|
2026 |
aurelien |
20 |
public function GestionMotsClesChemin($config, $mode = 'obs') {
|
2025 |
aurelien |
21 |
$this->config = $config;
|
|
|
22 |
//TODO: switch suivant mode
|
2026 |
aurelien |
23 |
$this->table_liaison = 'cel_obs_tags_path_liaison';
|
|
|
24 |
$this->table_mots_cles = 'cel_obs_tags_path';
|
2025 |
aurelien |
25 |
}
|
|
|
26 |
|
2026 |
aurelien |
27 |
public function viderTables() {
|
|
|
28 |
$requete = "TRUNCATE ".$this->table_mots_cles." ";
|
|
|
29 |
$vidage = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
30 |
|
|
|
31 |
$requete = "TRUNCATE ".$this->table_liaison." ";
|
|
|
32 |
$vidage = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
33 |
|
|
|
34 |
return $vidage;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function convertirNestedSetVersPathEnum() {
|
|
|
38 |
|
|
|
39 |
$time_start = microtime();
|
|
|
40 |
|
|
|
41 |
$requete_arbre = "SELECT * FROM cel_mots_cles_obs_srv ORDER BY id_utilisateur, niveau";
|
|
|
42 |
$mots_cles = Cel::db()->requeter($requete_arbre);
|
|
|
43 |
|
|
|
44 |
$requete_liaisons = "SELECT comc.id_observation, comc.id_mot_cle_obs, o.ce_utilisateur as id_utilisateur ".
|
|
|
45 |
"FROM cel_obs_mots_cles comc ".
|
|
|
46 |
"INNER JOIN cel_obs o ON o.id_observation = comc.id_observation ";
|
|
|
47 |
|
|
|
48 |
$liaisons = Cel::db()->requeter($requete_liaisons);
|
|
|
49 |
|
|
|
50 |
$mots_cles_hierarchiques = array();
|
|
|
51 |
$correspondances_id_mots_cles_obs = array();
|
|
|
52 |
|
|
|
53 |
foreach($mots_cles as &$mot) {
|
|
|
54 |
|
|
|
55 |
$chemin = '/';
|
|
|
56 |
if(isset($mots_cles_hierarchiques[$mot['ce_mot_cle_obs_parent']])) {
|
|
|
57 |
$chemin = $mots_cles_hierarchiques[$mot['ce_mot_cle_obs_parent']]['chemin'];
|
|
|
58 |
}
|
|
|
59 |
$chemin .= self::simplifier($mot['mot_cle']).'/';
|
|
|
60 |
$chemin = str_replace("//", "", $chemin);
|
|
|
61 |
|
|
|
62 |
$cle = $mot['id_mot_cle_obs'].'-'.$mot['id_utilisateur'];
|
|
|
63 |
|
|
|
64 |
$mots_cles_hierarchiques[$cle] = array(
|
|
|
65 |
'id_utilisateur' => $mot['id_utilisateur'],
|
|
|
66 |
'chemin' => $chemin,
|
|
|
67 |
'tag' => $mot['mot_cle']
|
|
|
68 |
);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$time_end = microtime();
|
|
|
72 |
$time = $time_end - $time_start;
|
|
|
73 |
|
|
|
74 |
$mots_cles_slice = array_chunk($mots_cles_hierarchiques, 800, true);
|
|
|
75 |
foreach($mots_cles_slice as $mots_cles_tranche) {
|
|
|
76 |
$requete_insertion_mots_cles = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, tag) ".
|
|
|
77 |
" VALUES ";
|
|
|
78 |
|
|
|
79 |
foreach($mots_cles_tranche as $cle => $mot_cle_tranche) {
|
|
|
80 |
$requete_insertion_mots_cles .= "(".
|
|
|
81 |
cel::db()->proteger($mot_cle_tranche['chemin']).", ".
|
|
|
82 |
cel::db()->proteger($mot_cle_tranche['id_utilisateur']).", ".
|
|
|
83 |
cel::db()->proteger($mot_cle_tranche['tag'])." ".
|
|
|
84 |
"), ";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$requete_insertion_mots_cles = rtrim($requete_insertion_mots_cles, ", ");
|
|
|
88 |
cel::db()->executer($requete_insertion_mots_cles);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$liaisons_slice = array_chunk($liaisons, 800, true);
|
|
|
92 |
foreach($liaisons_slice as &$liaison_tranche) {
|
|
|
93 |
$requete_insertion_liaisons = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
|
|
|
94 |
" VALUES ";
|
|
|
95 |
$lier = false;
|
|
|
96 |
|
|
|
97 |
foreach($liaison_tranche as &$liaison_obs_mot_cle) {
|
|
|
98 |
$cle = $liaison_obs_mot_cle['id_mot_cle_obs'].'-'.$liaison_obs_mot_cle['id_utilisateur'];
|
|
|
99 |
if(isset($mots_cles_hierarchiques[$cle])) {
|
|
|
100 |
$requete_insertion_liaisons .= "(".
|
|
|
101 |
cel::db()->proteger($liaison_obs_mot_cle['id_observation']).", ".
|
|
|
102 |
cel::db()->proteger($mots_cles_hierarchiques[$cle]['chemin']).", ".
|
|
|
103 |
cel::db()->proteger($liaison_obs_mot_cle['id_utilisateur'])." ".
|
|
|
104 |
"), ";
|
|
|
105 |
$lier = true;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if($lier) {
|
|
|
110 |
$requete_insertion_liaisons = rtrim($requete_insertion_liaisons, ", ");
|
|
|
111 |
cel::db()->executer($requete_insertion_liaisons);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
echo "La génération d'un tableau de ".count($mots_cles_hierarchiques).' éléments a pris '.$time.' secondes';
|
|
|
116 |
exit;
|
|
|
117 |
}
|
|
|
118 |
|
2025 |
aurelien |
119 |
public function obtenirArbre($id_utilisateur, $chemin = "/") {
|
|
|
120 |
|
|
|
121 |
$requete = "SELECT * FROM ".$this->table_mots_cles." ".
|
|
|
122 |
"WHERE id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." AND ".
|
2026 |
aurelien |
123 |
"chemin LIKE ".Cel::db()->proteger($chemin."%")." ".
|
|
|
124 |
"ORDER BY chemin";
|
2025 |
aurelien |
125 |
|
2026 |
aurelien |
126 |
$arbre = Cel::db()->requeter($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
127 |
usort($arbre, array('GestionMotsClesChemin', 'comparerProfNoeuds'));
|
2025 |
aurelien |
128 |
|
|
|
129 |
return $arbre;
|
|
|
130 |
}
|
|
|
131 |
|
2026 |
aurelien |
132 |
public function insererParChemin($mot_cle, $chemin_parent, $id_utilisateur) {
|
2025 |
aurelien |
133 |
|
2026 |
aurelien |
134 |
$chemin_mot_cle = self::getCheminHarmonise($chemin_parent, $mot_cle);
|
2025 |
aurelien |
135 |
|
|
|
136 |
$requete = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, tag) ".
|
|
|
137 |
"VALUES (".
|
|
|
138 |
Cel::db()->proteger($chemin_mot_cle).", ".
|
|
|
139 |
Cel::db()->proteger($id_utilisateur).", ".
|
2026 |
aurelien |
140 |
Cel::db()->proteger($mot_cle)." ".
|
2025 |
aurelien |
141 |
")";
|
|
|
142 |
|
|
|
143 |
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
144 |
|
|
|
145 |
return $insertion;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public function insererParIdParent($mot_cle, $id_parent, $id_utilisateur) {
|
|
|
149 |
|
|
|
150 |
$mot_cle_simp = self::simplifier($mot_cle);
|
|
|
151 |
|
|
|
152 |
$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
|
2026 |
aurelien |
153 |
"WHERE id_tag = ".Cel::db()->proteger($id_parent).")";
|
2025 |
aurelien |
154 |
|
|
|
155 |
$requete = "INSERT INTO ".$this->table_mots_cles."(chemin, id_utilisateur, tag) ".
|
|
|
156 |
"VALUES (".
|
2026 |
aurelien |
157 |
"CONCAT(".$sous_requete_chemin.",".Cel::db()->proteger($mot_cle_simp).",'/') ".
|
2025 |
aurelien |
158 |
Cel::db()->proteger($id_utilisateur).", ".
|
|
|
159 |
Cel::db()->proteger($tag).", ".
|
|
|
160 |
")";
|
|
|
161 |
|
|
|
162 |
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
163 |
|
|
|
164 |
return $insertion;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public function lierParId($id_mot_cle, $id_observation, $id_utilisateur) {
|
|
|
168 |
|
|
|
169 |
$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
|
|
|
170 |
"WHERE id_tag = ".Cel::db()->proteger($id_mot_cle).") ";
|
|
|
171 |
|
|
|
172 |
$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
|
|
|
173 |
"VALUES (".
|
|
|
174 |
Cel::db()->proteger($id_observation).", ".
|
2026 |
aurelien |
175 |
$sous_requete_chemin.",".
|
2025 |
aurelien |
176 |
Cel::db()->proteger($id_utilisateur)." ".
|
|
|
177 |
") ".
|
|
|
178 |
"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
|
|
|
179 |
|
|
|
180 |
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
181 |
|
|
|
182 |
return $liaison;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public function lierParChemin($chemin, $id_observation, $id_utilisateur) {
|
|
|
186 |
|
|
|
187 |
$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
|
|
|
188 |
"VALUES (".
|
|
|
189 |
Cel::db()->proteger($id_observation).", ".
|
|
|
190 |
Cel::db()->proteger($sous_requete_chemin).", ".
|
|
|
191 |
Cel::db()->proteger($id_utilisateur)." ".
|
|
|
192 |
") ".
|
|
|
193 |
"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
|
|
|
194 |
|
|
|
195 |
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
196 |
|
|
|
197 |
return $liaison;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// Déplacer un mot clé et le renommer est une même opération, dans le cas du renommage,
|
|
|
201 |
// seule la fin du chemin change
|
|
|
202 |
public function renommerChemin($ancien_chemin, $nouveau_chemin, $id_utilisateur) {
|
2026 |
aurelien |
203 |
|
|
|
204 |
$ancien_chemin = self::harmoniserChemin($ancien_chemin);
|
|
|
205 |
$nouveau_chemin = self::harmoniserChemin($nouveau_chemin);
|
|
|
206 |
|
|
|
207 |
$ancien_chemin_p = Cel::db()->proteger($ancien_chemin);
|
|
|
208 |
$nouveau_chemin_p = Cel::db()->proteger($nouveau_chemin);
|
|
|
209 |
|
2025 |
aurelien |
210 |
// TODO : triggers pour les tables liées ?
|
2026 |
aurelien |
211 |
$requete = "UPDATE ".$this->table_mots_cles." ".
|
|
|
212 |
"SET chemin = REPLACE(chemin,".$ancien_chemin_p.", ".$nouveau_chemin_p.") ".
|
2025 |
aurelien |
213 |
"WHERE chemin LIKE ".Cel::db()->proteger($ancien_chemin."%")." AND ".
|
|
|
214 |
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
|
2026 |
aurelien |
215 |
echo $requete.'<br />';
|
|
|
216 |
$deplacement = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
2025 |
aurelien |
217 |
|
2026 |
aurelien |
218 |
return $deplacement;
|
2025 |
aurelien |
219 |
}
|
|
|
220 |
|
|
|
221 |
public function supprimerChemin($chemin, $id_utilisateur) {
|
|
|
222 |
// TODO : triggers pour les tables liées ?
|
|
|
223 |
$requete = "DELETE FROM ".$this->$table_mots_cles." ".
|
|
|
224 |
"WHERE chemin LIKE ".Cel::db()->proteger($chemin."%")." AND ".
|
|
|
225 |
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
|
|
|
226 |
|
|
|
227 |
$suppression = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
228 |
|
|
|
229 |
return $suppression;
|
|
|
230 |
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
// Fonctions utilitaires
|
|
|
234 |
static public function comparerProfNoeuds($a, $b) {
|
|
|
235 |
|
|
|
236 |
$nb_slashs_a = substr_count($a['chemin'], '/');
|
|
|
237 |
$nb_slashs_b = substr_count($a['chemin'], '/');
|
|
|
238 |
$cmp = 0;
|
|
|
239 |
|
|
|
240 |
if($nb_slashs_a == $nb_slashs_b) {
|
|
|
241 |
$cmp = strcmp($a['chemin'], $b['chemin']);
|
|
|
242 |
} else {
|
|
|
243 |
$cmp = ($a['chemin'] > $b['chemin']) ? +1 : -1;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
return $cmp;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
/*
|
|
|
250 |
CREATE TRIGGER maj_chemin BEFORE UPDATE
|
|
|
251 |
ON cel_obs_tag_path FOR EACH ROW
|
|
|
252 |
BEGIN
|
|
|
253 |
UPDATE cel_obs_tag_path_liaison
|
|
|
254 |
SET cel_obs_tag_path_liaison.chemin = NEW.CHEMIN
|
|
|
255 |
WHERE cel_obs_tag_path_liaison.chemin LIKE CONCAT(OLD.chemin,'%')
|
|
|
256 |
AND cel_obs_tag_path_liaison.id_utilisateur = OLD.id_utilisateur
|
|
|
257 |
END;
|
|
|
258 |
|
|
|
259 |
CREATE TRIGGER suppr_chemin BEFORE UPDATE
|
|
|
260 |
ON cel_obs_tag_path FOR EACH ROW
|
|
|
261 |
BEGIN
|
|
|
262 |
DELETE FROM cel_obs_tag_path_liaison
|
|
|
263 |
WHERE cel_obs_tag_path_liaison.chemin LIKE CONCAT(OLD.chemin,'%')
|
|
|
264 |
AND cel_obs_tag_path_liaison.id_utilisateur = OLD.id_utilisateur
|
|
|
265 |
END;
|
|
|
266 |
|
|
|
267 |
CREATE TRIGGER suppr_obs BEFORE DELETE
|
|
|
268 |
ON cel_obs FOR EACH ROW
|
|
|
269 |
BEGIN
|
|
|
270 |
DELETE FROM cel_obs_tag_path_liaison
|
|
|
271 |
WHERE cel_obs_tag_path_liaison.id_obs = OLD.id_observation
|
|
|
272 |
END;
|
|
|
273 |
*/
|
|
|
274 |
|
2026 |
aurelien |
275 |
static public function getCheminHarmonise($chemin_parent, $mot_cle) {
|
|
|
276 |
return self::harmoniserChemin($chemin_parent.'/'.self::simplifier($mot_cle).'/');
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
static public function harmoniserChemin($chemin) {
|
|
|
280 |
$chemin = self::startsWith($chemin,'/') ? $chemin : '/'.$chemin;
|
|
|
281 |
$chemin = self::endsWith($chemin,'/') ? $chemin : $chemin.'/';
|
|
|
282 |
return str_replace('//', '/', $chemin);
|
|
|
283 |
}
|
|
|
284 |
|
2025 |
aurelien |
285 |
// fonction de slugification du mot clé
|
|
|
286 |
// http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
|
|
|
287 |
static public function simplifier($text)
|
|
|
288 |
{
|
2026 |
aurelien |
289 |
return trim(str_replace(array('\\','/'), '', $text));
|
2025 |
aurelien |
290 |
// replace non letter or digits by -
|
|
|
291 |
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
|
|
|
292 |
|
|
|
293 |
// trim
|
|
|
294 |
$text = trim($text, '-');
|
|
|
295 |
|
|
|
296 |
// transliterate
|
|
|
297 |
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
|
|
|
298 |
|
|
|
299 |
// lowercase
|
|
|
300 |
$text = strtolower($text);
|
|
|
301 |
|
|
|
302 |
// remove unwanted characters
|
|
|
303 |
$text = preg_replace('~[^-\w]+~', '', $text);
|
|
|
304 |
|
|
|
305 |
if (empty($text))
|
|
|
306 |
{
|
|
|
307 |
return 'n-a';
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
return $text;
|
|
|
311 |
}
|
2026 |
aurelien |
312 |
|
|
|
313 |
static public function startsWith($haystack, $needle) {
|
|
|
314 |
return $needle === "" || strpos($haystack, $needle) === 0;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
static public function endsWith($haystack, $needle) {
|
|
|
318 |
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
|
|
|
319 |
}
|
2025 |
aurelien |
320 |
}
|
|
|
321 |
|
|
|
322 |
?>
|