| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | if (!defined("_ECRIRE_INC_VERSION")) exit; |
|---|
| 26 | |
|---|
| 27 | function entableau($tag) { |
|---|
| 28 | return array('groupe' => $tag->type, 'tag' => $tag->titre); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | function ajouter_liste_mots($tags, |
|---|
| 48 | $id, |
|---|
| 49 | $groupe_defaut='', |
|---|
| 50 | $nom_objet='documents', |
|---|
| 51 | $id_objet='id_document', |
|---|
| 52 | $clear = false) { |
|---|
| 53 | $tags = new ListeTags($tags,$groupe_defaut); |
|---|
| 54 | $tags->ajouter($id, $nom_objet, $id_objet,$clear); |
|---|
| 55 | } |
|---|
| 56 | function ajouter_mots($liste_tags, |
|---|
| 57 | $id, |
|---|
| 58 | $groupe_defaut='', |
|---|
| 59 | $nom_objet='documents', |
|---|
| 60 | $id_objet='id_document', |
|---|
| 61 | $clear = false) { |
|---|
| 62 | $tags = new ListeTags($liste_tags,$groupe_defaut); |
|---|
| 63 | $tags->ajouter($id, $nom_objet, $id_objet,$clear); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | function retirer_liste_mots($tags, |
|---|
| 81 | $id, |
|---|
| 82 | $groupe_defaut='', |
|---|
| 83 | $nom_objet='documents', |
|---|
| 84 | $id_objet='id_document') { |
|---|
| 85 | $tags = new ListeTags($tags,$groupe_defaut); |
|---|
| 86 | $tags->retirer($id, $nom_objet, $id_objet); |
|---|
| 87 | } |
|---|
| 88 | function retirer_mots($liste_tags, |
|---|
| 89 | $id, |
|---|
| 90 | $groupe_defaut='', |
|---|
| 91 | $nom_objet='documents', |
|---|
| 92 | $id_objet='id_document') { |
|---|
| 93 | $tags = new ListeTags($liste_tags,$groupe_defaut); |
|---|
| 94 | $tags->retirer($id, $nom_objet, $id_objet); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | function parser_liste($liste_tags) { |
|---|
| 99 | $tags = new ListeTags($liste_tags,''); |
|---|
| 100 | return array_map('entableau',$tags->getTags()); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | class Tag { |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | var $titre; |
|---|
| 109 | var $type; |
|---|
| 110 | var $id_mot; |
|---|
| 111 | var $id_groupe; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | function Tag($titre, $type='', $id_groupe='') { |
|---|
| 115 | $this->titre = $titre; |
|---|
| 116 | $this->id_groupe = $id_groupe; |
|---|
| 117 | $this->type = $type; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | function getID() {return $this->id_mot;} |
|---|
| 122 | function getIDGroupe() {return $this->id_groupe;} |
|---|
| 123 | function getTitre() {return $this->titre;} |
|---|
| 124 | function getType() {return $this->type;} |
|---|
| 125 | function getTitreEchappe() { |
|---|
| 126 | return (strpos($this->titre,' ') || strpos($this->titre,':') || strpos($this->titre,',')) ? '"'.$this->titre.'"' : $this->titre; |
|---|
| 127 | } |
|---|
| 128 | function getTypeEchappe() { |
|---|
| 129 | return (strpos($this->type,' ') || strpos($this->type,':') || strpos($this->type,',')) ? '"'.$this->type.'"' : $this->type; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | function echapper() { |
|---|
| 134 | $cgroupe = $this->type; |
|---|
| 135 | $ctag = $this->titre; |
|---|
| 136 | |
|---|
| 137 | $cgroupe = (strpos($cgroupe,' ') || strpos($cgroupe,':') || strpos($cgroupe,','))?'"'.$cgroupe.'"':$cgroupe; |
|---|
| 138 | $ctag = (strpos($ctag,' ') || strpos($ctag,':') || strpos($ctag,','))?'"'.$ctag.'"':$ctag; |
|---|
| 139 | |
|---|
| 140 | return (($cgroupe)? ($cgroupe.':'):'').$ctag; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | function verifier_groupe() { |
|---|
| 151 | |
|---|
| 152 | include_spip ('base/abstract_sql'); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | static $groupes_verifie; |
|---|
| 156 | static $groupes_verifie_id; |
|---|
| 157 | |
|---|
| 158 | if($this->type) { |
|---|
| 159 | |
|---|
| 160 | if(!isset($groupes_verifie[$this->type])) { |
|---|
| 161 | $select_groupe = sql_select( |
|---|
| 162 | array('id_groupe','unseul'), |
|---|
| 163 | 'spip_groupes_mots', |
|---|
| 164 | array(array('=', 'titre', _q($this->type))) |
|---|
| 165 | ); |
|---|
| 166 | |
|---|
| 167 | if($groupe_ligne = sql_fetch($select_groupe)) { |
|---|
| 168 | $id = $groupe_ligne['id_groupe']; |
|---|
| 169 | $unseul = $groupe_ligne['unseul']; |
|---|
| 170 | $groupes_verifie[$this->type] = array($id,$unseul,$this->type); |
|---|
| 171 | } |
|---|
| 172 | if ($select_groupe) sql_free($select_groupe); |
|---|
| 173 | } |
|---|
| 174 | return $groupes_verifie[$this->type]; |
|---|
| 175 | |
|---|
| 176 | } |
|---|
| 177 | else if($this->id_groupe) { |
|---|
| 178 | |
|---|
| 179 | if(!isset($groupes_verifie_id[$this->id_groupe])) { |
|---|
| 180 | |
|---|
| 181 | $select_groupe = sql_select( |
|---|
| 182 | array('titre','unseul'), |
|---|
| 183 | 'spip_groupes_mots', |
|---|
| 184 | array(array('=', 'id_groupe', $this->id_groupe)) |
|---|
| 185 | ); |
|---|
| 186 | |
|---|
| 187 | if($groupe_ligne = sql_fetch($select_groupe)) { |
|---|
| 188 | $type = $groupe_ligne['titre']; |
|---|
| 189 | $unseul = $groupe_ligne['unseul']; |
|---|
| 190 | $groupes_verifie_id[$this->id_groupe] = array($this->id_groupe,$unseul,$type); |
|---|
| 191 | } |
|---|
| 192 | if ($select_groupe) sql_free($select_groupe); |
|---|
| 193 | } |
|---|
| 194 | return $groupes_verifie_id[$this->id_groupe]; |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | return array(0,''); |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | function verifier($nom_objet) { |
|---|
| 207 | |
|---|
| 208 | include_spip('base/abstract_sql'); |
|---|
| 209 | |
|---|
| 210 | list($id_groupe,$unseul,$titre) = $this->verifier_groupe(); |
|---|
| 211 | |
|---|
| 212 | if($id_groupe > 0) { |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | if ($unseul == 'oui') { |
|---|
| 216 | |
|---|
| 217 | $celcount = sql_select( |
|---|
| 218 | 'count(id_mot) as tot', |
|---|
| 219 | array( |
|---|
| 220 | 'spip_mots as mots', |
|---|
| 221 | "spip_mots_$nom_objet as objets" |
|---|
| 222 | ), |
|---|
| 223 | array( |
|---|
| 224 | "mots.id_groupe = $id_groupe", |
|---|
| 225 | "mots.id_mot = objets.id_mot" |
|---|
| 226 | ), |
|---|
| 227 | 'mots.id_groupe' |
|---|
| 228 | ); |
|---|
| 229 | |
|---|
| 230 | if($numrow = sql_fetch($celcount) && $numrow['tot'] > 0) |
|---|
| 231 | return false; |
|---|
| 232 | if ($celcount) sql_free($celcount); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | } |
|---|
| 236 | else if($this->type) { |
|---|
| 237 | |
|---|
| 238 | spip_log("création du groupe ".$this->type); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if(!lire_meta("tag-machine:colonne_.$nom_objet")) { |
|---|
| 242 | sql_alter("TABLE spip_groupes_mots ADD $nom_objet CHAR( 3 ) NOT NULL DEFAULT 'non';"); |
|---|
| 243 | ecrire_meta("tag-machine:colonne_.$nom_objet",1); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | $id_groupe = sql_insertq( |
|---|
| 247 | "spip_groupes_mots", |
|---|
| 248 | array( |
|---|
| 249 | 'titre' => $this->type, |
|---|
| 250 | $nom_objet => 'oui', |
|---|
| 251 | 'minirezo' => 'oui' |
|---|
| 252 | ) |
|---|
| 253 | ); |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | return $id_groupe; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | function creer($nom_objet) { |
|---|
| 267 | |
|---|
| 268 | include_spip ('base/abstract_sql'); |
|---|
| 269 | |
|---|
| 270 | if(!$this->id_mot && ($this->id_groupe = $this->verifier($nom_objet)) > 0) { |
|---|
| 271 | |
|---|
| 272 | $where = array(array('=', 'titre', _q($this->titre))); |
|---|
| 273 | |
|---|
| 274 | if($this->type) |
|---|
| 275 | $where[] = array('=', 'type', _q($this->type)); |
|---|
| 276 | else if($this->id_groupe) |
|---|
| 277 | $where[] = array('=', 'id_groupe', $this->id_groupe); |
|---|
| 278 | |
|---|
| 279 | $result = sql_select('id_mot', 'spip_mots', $where); |
|---|
| 280 | |
|---|
| 281 | if ($row = sql_fetch($result)) |
|---|
| 282 | $this->id_mot = $row['id_mot']; |
|---|
| 283 | else if($this->id_groupe) { |
|---|
| 284 | spip_log("Creer le mot $this->type:$this->titre ($this->id_mot)"); |
|---|
| 285 | |
|---|
| 286 | $this->id_mot = sql_insertq( |
|---|
| 287 | 'spip_mots', |
|---|
| 288 | array( |
|---|
| 289 | 'id_groupe' => $this->id_groupe, |
|---|
| 290 | 'type' => $this->type, |
|---|
| 291 | 'titre' => $this->titre |
|---|
| 292 | ) |
|---|
| 293 | ); |
|---|
| 294 | } |
|---|
| 295 | if ($result) sql_free($result); |
|---|
| 296 | |
|---|
| 297 | } |
|---|
| 298 | return $this->id_mot; |
|---|
| 299 | |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | function ajouter($id, $nom_objet, $id_objet) { |
|---|
| 305 | |
|---|
| 306 | include_spip ('base/abstract_sql'); |
|---|
| 307 | |
|---|
| 308 | if($id) { |
|---|
| 309 | |
|---|
| 310 | if(!$this->id_mot) { |
|---|
| 311 | $this->creer($nom_objet); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | $where = array( |
|---|
| 315 | array('=', 'id_mot', $this->id_mot), |
|---|
| 316 | array('=', $id_objet, $id) |
|---|
| 317 | ); |
|---|
| 318 | |
|---|
| 319 | $result = sql_select('id_mot', "spip_mots_$nom_objet", $where); |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | if (sql_count($result) == 0) { |
|---|
| 323 | sql_insertq( |
|---|
| 324 | "spip_mots_$nom_objet", |
|---|
| 325 | array( |
|---|
| 326 | 'id_mot' => $this->id_mot, |
|---|
| 327 | $id_objet => $id |
|---|
| 328 | ) |
|---|
| 329 | ); |
|---|
| 330 | } |
|---|
| 331 | if ($result) sql_free($result); |
|---|
| 332 | } |
|---|
| 333 | else spip_log("id_objet non défini"); |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | function retirer($id, $nom_objet, $id_objet) { |
|---|
| 339 | |
|---|
| 340 | include_spip ('base/abstract_sql'); |
|---|
| 341 | |
|---|
| 342 | if ($this->id_mot){ |
|---|
| 343 | sql_delete( |
|---|
| 344 | "spip_mots_$nom_objet", |
|---|
| 345 | "id_mot = ".intval($this->id_mot)." and ".$id_objet." = ".intval($id) |
|---|
| 346 | ); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | class ListeTags { |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | var $tags = array(); |
|---|
| 359 | var $groupe_defaut; |
|---|
| 360 | var $id_groupe; |
|---|
| 361 | var $id_objet; |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | function ListeTags($liste_tags, $groupe_defaut='', $id_groupe='') { |
|---|
| 365 | |
|---|
| 366 | if(!$groupe_defaut && !$id_groupe) |
|---|
| 367 | $groupe_defaut = $this->creer_groupe_defaut(); |
|---|
| 368 | |
|---|
| 369 | $this->groupe_defaut = $groupe_defaut; |
|---|
| 370 | $this->id_groupe = $id_groupe; |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | if(!is_array($liste_tags)) |
|---|
| 374 | $this->tags = $this->parser_liste($liste_tags); |
|---|
| 375 | else |
|---|
| 376 | $this->tags = $liste_tags; |
|---|
| 377 | |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | function getTags() {return $this->tags;} |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | function getTagsIDs() { |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | include_spip ('base/abstract_sql'); |
|---|
| 392 | $ids_mot = array(); |
|---|
| 393 | |
|---|
| 394 | foreach ($this->tags as $mot) { |
|---|
| 395 | if (strlen($mot->titre)) { |
|---|
| 396 | $where = array(array('=', 'titre', _q($mot->titre))); |
|---|
| 397 | |
|---|
| 398 | if(strlen($mot->type)) |
|---|
| 399 | $where[] = array('=', 'type', _q($mot->type)); |
|---|
| 400 | |
|---|
| 401 | $results = sql_select('id_mot', 'spip_mots', $where); |
|---|
| 402 | |
|---|
| 403 | list($id) = sql_fetch($results,SPIP_NUM); |
|---|
| 404 | if ($id) $ids_mot[] = $id; |
|---|
| 405 | if ($results) sql_free($results); |
|---|
| 406 | } |
|---|
| 407 | } |
|---|
| 408 | return $ids_mot; |
|---|
| 409 | |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | function toStringArray() { |
|---|
| 414 | |
|---|
| 415 | $retour = array(); |
|---|
| 416 | foreach($this->tags as $tag) { |
|---|
| 417 | $retour[] = $tag->echapper(); |
|---|
| 418 | } |
|---|
| 419 | return $retour; |
|---|
| 420 | |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | function ajouter($id, $nom_objet='documents', $id_objet='id_document', $clear=false) { |
|---|
| 430 | |
|---|
| 431 | include_spip ('base/abstract_sql'); |
|---|
| 432 | |
|---|
| 433 | if($id) { |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | if ($clear) { |
|---|
| 437 | $result = sql_select( |
|---|
| 438 | 'id_mot', |
|---|
| 439 | 'spip_mots', |
|---|
| 440 | "spip_mots.type = "._q($this->groupe_defaut)." OR spip_mots.id_groupe = "._q($this->id_groupe) |
|---|
| 441 | ); |
|---|
| 442 | |
|---|
| 443 | $mots_a_effacer = array('0'); |
|---|
| 444 | |
|---|
| 445 | while ($row = sql_fetch($result)) { |
|---|
| 446 | $mots_a_effacer[] = $row['id_mot']; |
|---|
| 447 | } |
|---|
| 448 | if ($result) sql_free($result); |
|---|
| 449 | |
|---|
| 450 | spip_log("Enleve les mots: (".join(',',$mots_a_effacer).") Ã (".$id_objet.", ".intval($id).")"); |
|---|
| 451 | sql_delete( |
|---|
| 452 | "spip_mots_$nom_objet", |
|---|
| 453 | $id_objet." = ".intval($id)." and id_mot in (".join(',', $mots_a_effacer).")" |
|---|
| 454 | ); |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | foreach($this->tags as $mot) { |
|---|
| 459 | if (trim($mot->titre) != "") |
|---|
| 460 | $mot->ajouter($id,$nom_objet,$id_objet); |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | function retirer($id, $nom_objet='documents', $id_objet='id_document') { |
|---|
| 469 | |
|---|
| 470 | include_spip ('base/abstract_sql'); |
|---|
| 471 | |
|---|
| 472 | if($id) { |
|---|
| 473 | foreach($this->tags as $mot) { |
|---|
| 474 | $mot->retirer($id,$nom_objet,$id_objet); |
|---|
| 475 | } |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | function parser_liste($liste_tags) { |
|---|
| 486 | |
|---|
| 487 | $liste_tags = trim($liste_tags); |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | $liste_tags = strtr($liste_tags, "\t", " "); |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | $liste_tags = ' '.preg_replace('/[[:space:],]/', '\\0\\0', $liste_tags).' '; |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | $liste_tags = preg_replace('/[[:space:],]+("?)(.*?)\\1[[:space:],]/', "\t\\2", $liste_tags); |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | $liste_tags = preg_replace('/([[:space:],])\\1/', '\\1', $liste_tags); |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | $tags = split("\t", substr($liste_tags,1)); |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | foreach ($tags as $i => $tag) { |
|---|
| 506 | $tag = str_replace('"', '', $tag); |
|---|
| 507 | preg_match('/((.*):)?(.*)/', $tag, $regs); |
|---|
| 508 | $groupe = $regs[2]; |
|---|
| 509 | if(!$groupe) |
|---|
| 510 | $groupe = $this->groupe_defaut; |
|---|
| 511 | $tags[$i] = new Tag($regs[3], $groupe, $this->id_groupe); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | return $tags; |
|---|
| 515 | |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | function containsSeparateur($char) { |
|---|
| 519 | return (strpos($char,' ') || strpos($char,':') || strpos($char,',')); |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | function creer_groupe_defaut() { |
|---|
| 524 | |
|---|
| 525 | include_spip('base/abstract_sql'); |
|---|
| 526 | |
|---|
| 527 | $s = sql_select( |
|---|
| 528 | array('id_groupe', 'titre'), |
|---|
| 529 | 'spip_groupes_mots', |
|---|
| 530 | array(array('=', 'titre', 'tags')) |
|---|
| 531 | ); |
|---|
| 532 | |
|---|
| 533 | if ($t=sql_fetch($s)) |
|---|
| 534 | return $this->id_groupe = $t['id_groupe']; |
|---|
| 535 | else { |
|---|
| 536 | return $this->id_groupe = sql_insertq( |
|---|
| 537 | 'spip_groupes_mots', |
|---|
| 538 | array( |
|---|
| 539 | 'titre' => 'tags' |
|---|
| 540 | ) |
|---|
| 541 | ); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | ?> |
|---|