1 | <?php |
---|
2 | |
---|
3 | // inc_tag-machine.php |
---|
4 | // Librairies pour ajouter des mots clefs sur les objets spip à partir |
---|
5 | // d'un simple champ texte. |
---|
6 | // Distribué sans garantie sous licence GPL. |
---|
7 | // |
---|
8 | // Authors BoOz, Pierre ANDREWS |
---|
9 | // |
---|
10 | // This program is free software; you can redistribute it and/or modify |
---|
11 | // it under the terms of the GNU General Public License as published by |
---|
12 | // the Free Software Foundation; either version 2 of the License, or any later version. |
---|
13 | // |
---|
14 | // This program is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | // GNU General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU General Public License |
---|
20 | // along with this program; if not, write to the Free Software |
---|
21 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
22 | |
---|
23 | |
---|
24 | if (!defined("_ECRIRE_INC_VERSION")) exit; |
---|
25 | |
---|
26 | |
---|
27 | function entableau($tag) { |
---|
28 | return array('groupe' => $tag->type, 'tag' => $tag->titre); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | Ajoute les mots clefs dans la liste passée en paramètre au bon objet. |
---|
34 | Si le mot clef n'existe pas, on le crée, |
---|
35 | Si le groupe n'existe pas, on le crée. |
---|
36 | |
---|
37 | IN: |
---|
38 | $tags: tableau de tag ('groupe' => groupe, 'tag' => tag), |
---|
39 | $id: id de l'objet sur lequel ajouter les mots clefs, |
---|
40 | [groupe_defaut]: groupe par défaut pour les mots qui n'ont pas de groupe dans la chaîne |
---|
41 | [$nom_objet]: type d'objet sur lequel ajouter les mots clefs (une table: spip_mots_$nom_objet doit exister dans la base de donnée), |
---|
42 | [$id_objet]: colone de la table de cet objet qui contient les ids. |
---|
43 | OUT: rien; |
---|
44 | */ |
---|
45 | |
---|
46 | function ajouter_liste_mots($tags, |
---|
47 | $id, |
---|
48 | $groupe_defaut='', |
---|
49 | $nom_objet='documents', |
---|
50 | $id_objet='id_document', |
---|
51 | $clear = false) { |
---|
52 | $tags = new ListeTags($tags,$groupe_defaut); |
---|
53 | $tags->ajouter($id, $nom_objet, $id_objet,$clear); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | function ajouter_mots($liste_tags, |
---|
58 | $id, |
---|
59 | $groupe_defaut='', |
---|
60 | $nom_objet='documents', |
---|
61 | $id_objet='id_document', |
---|
62 | $clear = false) { |
---|
63 | $tags = new ListeTags($liste_tags,$groupe_defaut); |
---|
64 | $tags->ajouter($id, $nom_objet, $id_objet,$clear); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | enleve les mots clefs passé en paramètre |
---|
69 | |
---|
70 | IN: |
---|
71 | $tags: tableau de tag ('groupe' => groupe, 'tag' => tag), |
---|
72 | $id: id de l'objet sur lequel ajouter les mots clefs, |
---|
73 | [groupe_defaut]: groupe par défaut pour les mots qui n'ont pas de groupe dans la chaîne |
---|
74 | [$nom_objet]: type d'objet sur lequel ajouter les mots clefs (une table: spip_mots_$nom_objet doit exister dans la base de donnée), |
---|
75 | [$id_objet]: colone de la table de cet objet qui contient les ids. |
---|
76 | OUT: rien; |
---|
77 | */ |
---|
78 | function retirer_liste_mots($tags, |
---|
79 | $id, |
---|
80 | $groupe_defaut='', |
---|
81 | $nom_objet='documents', |
---|
82 | $id_objet='id_document') { |
---|
83 | $tags = new ListeTags($tags,$groupe_defaut); |
---|
84 | $tags->retirer($id, $nom_objet, $id_objet); |
---|
85 | } |
---|
86 | |
---|
87 | function retirer_mots($liste_tags, |
---|
88 | $id, |
---|
89 | $groupe_defaut='', |
---|
90 | $nom_objet='documents', |
---|
91 | $id_objet='id_document') { |
---|
92 | $tags = new ListeTags($liste_tags,$groupe_defaut); |
---|
93 | $tags->retirer($id, $nom_objet, $id_objet); |
---|
94 | } |
---|
95 | |
---|
96 | function parser_liste($liste_tags) { |
---|
97 | $tags = new ListeTags($liste_tags,''); |
---|
98 | return array_map('entableau',$tags->getTags()); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | /*des objets*/ |
---|
103 | |
---|
104 | class Tag { |
---|
105 | var $titre; |
---|
106 | var $type; |
---|
107 | var $id_mot; |
---|
108 | var $id_groupe; |
---|
109 | |
---|
110 | function Tag($titre, $type='', $id_groupe='') { |
---|
111 | $this->titre = $titre; |
---|
112 | $this->id_groupe = $id_groupe; |
---|
113 | $this->type = $type; |
---|
114 | } |
---|
115 | |
---|
116 | /* function Tag($id_mot,$id_groupe) { |
---|
117 | $this->id_mot = $id_mot; |
---|
118 | $this->id_groupe = $id_groupe; |
---|
119 | }*/ |
---|
120 | |
---|
121 | function getID() { |
---|
122 | return $this->id_mot; |
---|
123 | } |
---|
124 | |
---|
125 | /*public*/ function getIDGroupe() { |
---|
126 | return $this->id_groupe; |
---|
127 | } |
---|
128 | |
---|
129 | /*public*/ function getTitre() { |
---|
130 | return $this->titre; |
---|
131 | } |
---|
132 | |
---|
133 | /*public*/ function getType() { |
---|
134 | return $this->type; |
---|
135 | } |
---|
136 | |
---|
137 | /*public*/ function getTitreEchappe() { |
---|
138 | return (strpos($this->titre,' ') || strpos($this->titre,':') || strpos($this->titre,','))?'"'.$this->titre.'"':$this->titre; |
---|
139 | } |
---|
140 | |
---|
141 | /*public*/ function getTypeEchappe() { |
---|
142 | return (strpos($this->type,' ') || strpos($this->type,':') || strpos($this->type,','))?'"'.$this->type.'"':$this->type; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | function echapper() { |
---|
147 | $cgroupe = $this->type; |
---|
148 | $ctag = $this->titre; |
---|
149 | |
---|
150 | $cgroupe = (strpos($cgroupe,' ') || strpos($cgroupe,':') || strpos($cgroupe,','))?'"'.$cgroupe.'"':$cgroupe; |
---|
151 | $ctag = (strpos($ctag,' ') || strpos($ctag,':') || strpos($ctag,','))?'"'.$ctag.'"':$ctag; |
---|
152 | |
---|
153 | return (($cgroupe)? ($cgroupe.':'):'').$ctag; |
---|
154 | } |
---|
155 | |
---|
156 | //---------------------------------------------------------------------- |
---|
157 | |
---|
158 | /*private*/ function verifier($nom_objet) { |
---|
159 | include_spip('base/abstract_sql'); |
---|
160 | list($id_groupe,$unseul,$titre) = $this->verifier_groupe(); |
---|
161 | if($id_groupe > 0) { |
---|
162 | if ($unseul == 'oui') { |
---|
163 | // on verifie qu'il y a pas déjà un mot associé |
---|
164 | $celcount = spip_abstract_select(array('count(id_mot) as tot'), |
---|
165 | array('spip_mots as mots', |
---|
166 | "spip_mots_$nom_objet as objets"), |
---|
167 | array("mots.id_groupe = $id_groupe", |
---|
168 | "mots.id_mot = objets.id_mot"), |
---|
169 | 'mots.id_groupe'); |
---|
170 | if($numrow = spip_abstract_fetch($celcount) && $numrow['tot'] > 0) { |
---|
171 | return false; |
---|
172 | } |
---|
173 | spip_abstract_free($celcount); |
---|
174 | } |
---|
175 | } else if($this->type) { |
---|
176 | spip_log("création du groupe $this->type"); |
---|
177 | global $table_prefix; |
---|
178 | if(!lire_meta("tag-machine:colonne_.$nom_objet")) { |
---|
179 | spip_query("ALTER TABLE `".$table_prefix."_groupes_mots` ADD `".$nom_objet."` CHAR( 3 ) NOT NULL DEFAULT 'non';"); |
---|
180 | ecrire_meta("tag-machine:colonne_.$nom_objet"); |
---|
181 | ecrire_meta(); |
---|
182 | } |
---|
183 | |
---|
184 | $id_groupe = spip_abstract_insert("spip_groupes_mots", |
---|
185 | "(titre, $nom_objet, minirezo)", |
---|
186 | "('".addslashes($this->type)."','oui','oui')"); |
---|
187 | } |
---|
188 | |
---|
189 | return $id_groupe; |
---|
190 | } |
---|
191 | |
---|
192 | /*private*/ function verifier_groupe() { |
---|
193 | include_spip ('base/abstract_sql'); |
---|
194 | static $groupes_verifie; |
---|
195 | static $groupes_verifie_id; |
---|
196 | if($this->type) { |
---|
197 | if(!isset($groupes_verifie[$this->type])) { |
---|
198 | $select_groupe = spip_abstract_select(array('id_groupe','unseul'), |
---|
199 | array('spip_groupes_mots'), |
---|
200 | array("titre = '".addslashes($this->type)."'")); |
---|
201 | if($groupe_row = spip_abstract_fetch($select_groupe)) { |
---|
202 | $id = $groupe_row['id_groupe']; |
---|
203 | $unseul = $groupe_row['unseul']; |
---|
204 | $groupes_verifie[$this->type] = array($id,$unseul,$this->type); |
---|
205 | } |
---|
206 | |
---|
207 | spip_abstract_free($select_groupe); |
---|
208 | } |
---|
209 | return $groupes_verifie[$this->type]; |
---|
210 | } else if($this->id_groupe) { |
---|
211 | if(!isset($groupes_verifie_id[$this->id_groupe])) { |
---|
212 | $select_groupe = spip_abstract_select(array('titre','unseul'), |
---|
213 | array('spip_groupes_mots'), |
---|
214 | array("id_groupe = $this->id_groupe")); |
---|
215 | if($groupe_row = spip_abstract_fetch($select_groupe)) { |
---|
216 | $type = $groupe_row['titre']; |
---|
217 | $unseul = $groupe_row['unseul']; |
---|
218 | $groupes_verifie_id[$this->id_groupe] = array($this->id_groupe,$unseul,$type); |
---|
219 | } |
---|
220 | |
---|
221 | spip_abstract_free($select_groupe); |
---|
222 | } |
---|
223 | return $groupes_verifie_id[$this->id_groupe]; |
---|
224 | } |
---|
225 | return array(0,''); |
---|
226 | } |
---|
227 | |
---|
228 | //---------------------------------------------------------------------- |
---|
229 | |
---|
230 | function creer($nom_objet) { |
---|
231 | include_spip ('base/abstract_sql'); |
---|
232 | if(!$this->id_mot && ($this->id_groupe = $this->verifier($nom_objet)) > 0) { |
---|
233 | $select = array('id_mot'); |
---|
234 | $from = array('spip_mots'); |
---|
235 | $where = array("titre = '".addslashes($this->titre)."'"); |
---|
236 | if($this->type) {$where[] = "type='".addslashes($this->type)."'";} |
---|
237 | else if($this->id_groupe) {$where[] = "id_groupe='".$this->id_groupe."'";} |
---|
238 | $result = spip_abstract_select($select,$from,$where); |
---|
239 | if ($row = spip_fetch_array($result)) { |
---|
240 | $this->id_mot = $row['id_mot']; |
---|
241 | } else { |
---|
242 | if($this->id_groupe) { |
---|
243 | spip_log("Creer le mot $this->type:$this->titre ($this->id_mot)"); |
---|
244 | $this->id_mot = spip_abstract_insert("spip_mots", |
---|
245 | '(id_groupe, type, titre)', |
---|
246 | "('".$this->id_groupe."','".addslashes($this->type)."','".addslashes($this->titre)."')"); |
---|
247 | } |
---|
248 | } |
---|
249 | spip_abstract_free($result); |
---|
250 | } |
---|
251 | return $this->id_mot; |
---|
252 | } |
---|
253 | |
---|
254 | function ajouter($id, $nom_objet, $id_objet) { |
---|
255 | include_spip ('base/abstract_sql'); |
---|
256 | if($id) { |
---|
257 | if(!$this->id_mot) { |
---|
258 | $this->creer($nom_objet); |
---|
259 | } |
---|
260 | $select = array('id_mot'); |
---|
261 | $from = array("spip_mots_$nom_objet"); |
---|
262 | $where = array("id_mot = '$this->id_mot'", |
---|
263 | "$id_objet = '$id'"); |
---|
264 | $result = spip_abstract_select($select,$from,$where); |
---|
265 | if (spip_abstract_count($result) == 0) { |
---|
266 | spip_abstract_insert("spip_mots_$nom_objet", |
---|
267 | "(id_mot,$id_objet)", |
---|
268 | '('.$this->id_mot.",$id)"); |
---|
269 | } |
---|
270 | spip_abstract_free($result); |
---|
271 | } else |
---|
272 | spip_log("id_objet non défini"); |
---|
273 | } |
---|
274 | |
---|
275 | function retirer($id, $nom_objet, $id_objet) { |
---|
276 | include_spip ('base/abstract_sql'); |
---|
277 | if ($this->id_mot){ |
---|
278 | spip_query("DELETE FROM spip_mots_$nom_objet WHERE id_mot=".$this->id_mot." AND $id_objet=$id"); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | } |
---|
284 | |
---|
285 | class ListeTags { |
---|
286 | |
---|
287 | //------------------------------- Variables ------------------------------------- |
---|
288 | |
---|
289 | /*private*/ var $tags = array(); |
---|
290 | var $groupe_defaut; |
---|
291 | var $id_objet; |
---|
292 | |
---|
293 | //------------------------------- Constructeurs --------------------------------- |
---|
294 | |
---|
295 | /*public*/ function ListeTags($liste_tags, |
---|
296 | $groupe_defaut='', |
---|
297 | $id_groupe='') { |
---|
298 | if(!$groupe_defaut && !$id_groupe) |
---|
299 | $groupe_defaut = $this->creer_groupe_defaut(); |
---|
300 | |
---|
301 | $this->groupe_defaut = $groupe_defaut; |
---|
302 | $this->id_groupe = $id_groupe; |
---|
303 | |
---|
304 | if(!is_array($liste_tags)) { |
---|
305 | $this->tags = $this->parser_liste($liste_tags); |
---|
306 | } else { |
---|
307 | $this->tags = $liste_tags; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | /*public*/ /*function ListeTags($id, |
---|
313 | $groupe_defaut='', |
---|
314 | $nom_objet='documents', |
---|
315 | $id_objet='id_document') { |
---|
316 | $result = spip_abstract_select(array('titre','type'), |
---|
317 | array("spip_mots_$nom_objet",'spip_mots'), |
---|
318 | array("spip_mots_$nom_objet.id_mot=spip_mots.id_mot", |
---|
319 | "spip_mots_$nom_objet.$id_objet = $id")); |
---|
320 | |
---|
321 | while ($row = spip_abstract_fetch($result)) { |
---|
322 | $this->tags[] = new Tag($row['titre'],$row['type']); |
---|
323 | } |
---|
324 | spip_abstract_free($result); |
---|
325 | $this->groupe_defaut = $groupe_defaut; |
---|
326 | } |
---|
327 | */ |
---|
328 | |
---|
329 | //------------------------------- Info ----------------------------------------- |
---|
330 | |
---|
331 | function getTags() { |
---|
332 | return $this->tags; |
---|
333 | } |
---|
334 | |
---|
335 | // prend une liste de tags et retourne les id_mot reconnus (sans en creer) |
---|
336 | function getTagsIDs() { |
---|
337 | //?? Aller chercher les tags dans la boite |
---|
338 | //?? pour faire plus generique : se baser sur id_$objet et/ou url_propre |
---|
339 | //?? car " dans l'url arrive ici sous la forme " (#ENV{tags} et non #ENV*{tags}) |
---|
340 | |
---|
341 | include_spip ('base/abstract_sql'); |
---|
342 | $ids_mot = array(); |
---|
343 | foreach ($this->tags as $mot) { |
---|
344 | if (strlen($mot->titre)) { |
---|
345 | $where = array(" titre='".addslashes($mot->titre)."'"); |
---|
346 | if(strlen($mot->type)) { |
---|
347 | $where[] = 'type=\''.addslashes($mot->type).'\''; |
---|
348 | } |
---|
349 | $results = spip_abstract_select(array('id_mot'), |
---|
350 | array('spip_mots'), |
---|
351 | $where |
---|
352 | ); //+ url_propre ? id_objet ? |
---|
353 | list($id) = spip_fetch_array($results,SPIP_NUM); |
---|
354 | if ($id) |
---|
355 | $ids_mot[] = $id; |
---|
356 | spip_abstract_free($results); |
---|
357 | } |
---|
358 | } |
---|
359 | return $ids_mot; |
---|
360 | } |
---|
361 | |
---|
362 | function toStringArray() { |
---|
363 | $to_ret = array(); |
---|
364 | foreach($this->tags as $tag) { |
---|
365 | $to_ret[] = $tag->echapper(); |
---|
366 | } |
---|
367 | return $to_ret; |
---|
368 | } |
---|
369 | |
---|
370 | //--------------------------------- Modif DB ----------------------------------- |
---|
371 | |
---|
372 | function ajouter($id, |
---|
373 | $nom_objet='documents', |
---|
374 | $id_objet='id_document', |
---|
375 | $clear=false) { |
---|
376 | include_spip ('base/abstract_sql'); |
---|
377 | if($id) { |
---|
378 | if ($clear) { |
---|
379 | $result = spip_abstract_select(array('id_mot'), |
---|
380 | array('spip_mots'), |
---|
381 | array("spip_mots.type = '".addslashes($this->groupe_defaut)."' OR spip_mots.id_groupe = '".addslashes($this->id_groupe)."'") |
---|
382 | ); |
---|
383 | $motsaeffacer = array('0'); |
---|
384 | while ($row = spip_abstract_fetch($result)) { |
---|
385 | $motsaeffacer[] = $row['id_mot']; |
---|
386 | } |
---|
387 | spip_abstract_free($result); |
---|
388 | spip_log("Enleve les mots: ".join(',',$motsaeffacer)); |
---|
389 | spip_query("DELETE FROM spip_mots_$nom_objet WHERE $id_objet=$id AND id_mot IN (".join(',',$motsaeffacer).")"); |
---|
390 | } |
---|
391 | foreach($this->tags as $mot) { |
---|
392 | if (trim($mot->titre) != "") |
---|
393 | $mot->ajouter($id,$nom_objet,$id_objet); |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | function retirer($id, |
---|
399 | $nom_objet='documents', |
---|
400 | $id_objet='id_document') { |
---|
401 | |
---|
402 | include_spip ('base/abstract_sql'); |
---|
403 | if($id) { |
---|
404 | foreach($this->tags as $mot) { |
---|
405 | $mot->retirer($id,$nom_objet,$id_objet); |
---|
406 | } |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | //-------------------------------- Statique ------------------------------------ |
---|
412 | |
---|
413 | //-- privé |
---|
414 | |
---|
415 | /*private*/ /*static*/ function parser_liste($liste_tags) { |
---|
416 | |
---|
417 | $liste_tags = trim($liste_tags); |
---|
418 | |
---|
419 | // supprimer les tab (notre separateur final) |
---|
420 | $liste_tags = strtr($liste_tags, "\t", " "); |
---|
421 | |
---|
422 | // doubler les caracteres qui peuvent faire office de separateur |
---|
423 | $liste_tags = ' '.preg_replace('/[[:space:],]/', '\\0\\0', $liste_tags).' '; |
---|
424 | |
---|
425 | // trouver les tags et les separer par \t |
---|
426 | $liste_tags = preg_replace('/[[:space:],]+("?)(.*?)\\1[[:space:],]/', "\t\\2", $liste_tags); |
---|
427 | |
---|
428 | // remettre les caracteres doubles en simple |
---|
429 | $liste_tags = preg_replace('/([[:space:],])\\1/', '\\1', $liste_tags); |
---|
430 | |
---|
431 | // exploser selon les tab |
---|
432 | $tags = preg_split("/\t/", substr($liste_tags,1)); |
---|
433 | |
---|
434 | // recuperer les groupes sous la forme <groupe:mot> |
---|
435 | foreach ($tags as $i => $tag) { |
---|
436 | $tag = str_replace('"', '', $tag); |
---|
437 | preg_match('/((.*):)?(.*)/', $tag, $regs); |
---|
438 | $groupe = $regs[2]; |
---|
439 | if(!$groupe) |
---|
440 | $groupe = $this->groupe_defaut; |
---|
441 | $tags[$i] = new Tag($regs[3], $groupe, $this->id_groupe); |
---|
442 | } |
---|
443 | |
---|
444 | return $tags; |
---|
445 | } |
---|
446 | |
---|
447 | function containsSeparateur($char) { |
---|
448 | return (strpos($char,' ') || strpos($char,':') || strpos($char,',')); |
---|
449 | } |
---|
450 | |
---|
451 | |
---|
452 | function creer_groupe_defaut() { |
---|
453 | $s = spip_query("SELECT id_groupe FROM spip_groupes_mots |
---|
454 | WHERE titre='tags'"); |
---|
455 | if ($t=spip_fetch_array($s)) |
---|
456 | return $this->groupe_defaut = $t['id_groupe']; |
---|
457 | else { |
---|
458 | spip_query("INSERT spip_groupes_mots (titre) VALUES ('tags')"); |
---|
459 | return $this->groupe_defaut = spip_insert_id(); |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | ?> |
---|