PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/spip/ecrire/inc/mots.php

https://github.com/eyeswebcrea/espace-couture-sittler.fr
PHP | 138 lines | 96 code | 21 blank | 21 comment | 25 complexity | 4112e82249e30334a71d4b8a345093b6 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2011 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined('_ECRIRE_INC_VERSION')) return;
  12. include_spip('inc/actions');
  13. // ne pas faire d'erreur si les chaines sont > 254 caracteres
  14. // http://doc.spip.org/@levenshtein255
  15. function levenshtein255 ($a, $b) {
  16. $a = substr($a, 0, 254);
  17. $b = substr($b, 0, 254);
  18. return @levenshtein($a,$b);
  19. }
  20. // reduit un mot a sa valeur translitteree et en minuscules
  21. // http://doc.spip.org/@reduire_mot
  22. function reduire_mot($mot) {
  23. return strtr(
  24. translitteration(trim($mot)),
  25. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  26. 'abcdefghijklmnopqrstuvwxyz'
  27. );
  28. }
  29. // http://doc.spip.org/@mots_ressemblants
  30. function mots_ressemblants($mot, $table_mots, $table_ids='') {
  31. $result = array();
  32. if (!$table_mots) return $result;
  33. $lim = 2;
  34. $nb = 0;
  35. $opt = 1000000;
  36. $mot_opt = '';
  37. $mot = reduire_mot($mot);
  38. $len = strlen($mot);
  39. while (!$nb AND $lim < 10) {
  40. reset($table_mots);
  41. if ($table_ids) reset($table_ids);
  42. while (list(, $val) = each($table_mots)) {
  43. if ($table_ids) list(, $id) = each($table_ids);
  44. else $id = $val;
  45. $val2 = trim($val);
  46. if ($val2) {
  47. if (!isset($distance[$id])) {
  48. $val2 = reduire_mot($val2);
  49. $len2 = strlen($val2);
  50. if ($val2 == $mot)
  51. $m = -2; # resultat exact
  52. else if (substr($val2, 0, $len) == $mot)
  53. $m = -1; # sous-chaine
  54. else {
  55. # distance
  56. $m = levenshtein255($val2, $mot);
  57. # ne pas compter la distance due a la longueur
  58. $m -= max(0, $len2 - $len);
  59. }
  60. $distance[$id] = $m;
  61. } else $m = 0;
  62. if ($m <= $lim) {
  63. $selection[$id] = $m;
  64. if ($m < $opt) {
  65. $opt = $m;
  66. $mot_opt = $val;
  67. }
  68. $nb++;
  69. }
  70. }
  71. }
  72. $lim += 2;
  73. }
  74. if (!$nb) return $result;
  75. reset($selection);
  76. if ($opt > -1) {
  77. $moy = 1;
  78. while(list(, $val) = each($selection)) $moy *= $val;
  79. if($moy) $moy = pow($moy, 1.0/$nb);
  80. $lim = ($opt + $moy) / 2;
  81. }
  82. else $lim = -1;
  83. reset($selection);
  84. while (list($key, $val) = each($selection)) {
  85. if ($val <= $lim) {
  86. $result[] = $key;
  87. }
  88. }
  89. return $result;
  90. }
  91. /*
  92. * Affiche la liste des mots-cles associes a l'objet specifie
  93. * plus le formulaire d'ajout de mot-cle
  94. */
  95. // http://doc.spip.org/@affiche_mots_ressemblant
  96. function affiche_mots_ressemblant($cherche_mot, $objet, $id_objet, $resultat, $table, $table_id, $url_base)
  97. {
  98. $les_mots = sql_in('id_mot', $resultat);
  99. $res = sql_allfetsel("*", "spip_mots", $les_mots, "", "titre", "17");
  100. foreach ($res as $k => $row) {
  101. $id_mot = $row['id_mot'];
  102. $titre = $row['titre'];
  103. $type = typo($row['type']);
  104. $descriptif = $row['descriptif'];
  105. $res[$k]= ajax_action_auteur('editer_mots', "$id_objet,,$table,$table_id,$objet,$id_mot", $url_base, "$table_id=$id_objet", array(typo($titre),' title="' . _T('info_ajouter_mot') .'"'),"&id_objet=$id_objet&objet=$objet") .
  106. (!$descriptif ? '' : ("\n(<span class='spip_xx-small'>".supprimer_tags(couper(propre($descriptif), 100)).")</span><br />\n"));
  107. }
  108. $res2 = ($type
  109. ? "<strong>$type</strong>&nbsp;: "
  110. : '' )
  111. . _T('info_plusieurs_mots_trouves', array('cherche_mot' => $cherche_mot))
  112. ."<br />";
  113. if (count($resultat) > 17)
  114. $res2 .= "<br /><strong>" ._T('info_trop_resultat', array('cherche_mot' => $cherche_mot)) ."</strong><br />\n";
  115. return $res2 . '<ul><li>' . join("</li>\n<li>", $res) . '</li></ul>';
  116. }
  117. ?>