254 caracteres function levenshtein255 ($a, $b) { $a = substr($a, 0, 254); $b = substr($b, 0, 254); return @levenshtein($a,$b); } function mots_ressemblants($mot, $table_mots, $table_ids='') { $lim = 2; $nb = 0; $opt = 1000000; $mot_opt = ''; $mot = translitteration(strtolower(trim($mot))); $len = strlen($mot); if (!$table_mots) return ''; while (!$nb AND $lim < 10) { reset($table_mots); if ($table_ids) reset($table_ids); while (list(, $val) = each($table_mots)) { if ($table_ids) list(, $id) = each($table_ids); else $id = $val; $val2 = trim($val); if ($val2) { if (!($m = $distance[$id])) { $val2 = translitteration(strtolower($val2)); $len2 = strlen($val2); if ($val2 == $mot) $m = -2; # resultat exact else if (substr($val2, 0, $len) == $mot) $m = -1; # sous-chaine else { # distance $m = levenshtein255($val2, $mot); # ne pas compter la distance due a la longueur $m -= max(0, $len2 - $len); } $distance[$id] = $m; } if ($m <= $lim) { $selection[$id] = $m; if ($m < $opt) { $opt = $m; $mot_opt = $val; } $nb++; } } } $lim += 2; } if (!$nb) return ''; reset($selection); if ($opt > -1) { $moy = 1; while(list(, $val) = each($selection)) $moy *= $val; if($moy) $moy = pow($moy, 1.0/$nb); $lim = ($opt + $moy) / 2; } else $lim = -1; reset($selection); while (list($key, $val) = each($selection)) { if ($val <= $lim) { $result[] = $key; } } return $result; } /* * Affiche la liste des mots-cles associes a l'objet * specifie, plus le formulaire d'ajout de mot-cle */ function formulaire_mots($table, $id_objet, $nouv_mot, $supp_mot, $cherche_mot, $flag_editable) { global $connect_statut, $options; global $spip_lang_rtl, $spip_lang_right; $select_groupe = $GLOBALS['select_groupe']; if ($table == 'articles') { $id_table = 'id_article'; $objet = 'article'; $url_base = "articles.php3?id_article=$id_objet"; } else if ($table == 'breves') { $id_table = 'id_breve'; $objet = 'breve'; $url_base = "breves_voir.php3?id_breve=$id_objet"; } else if ($table == 'rubriques') { $id_table = 'id_rubrique'; $objet = 'rubrique'; $url_base = "naviguer.php3?id_rubrique=$id_objet"; } else if ($table == 'syndic') { $id_table = 'id_syndic'; $objet = 'syndic'; $url_base = "sites.php3?id_syndic=$id_objet"; } list($nombre_mots) = spip_fetch_array(spip_query("SELECT COUNT(*) FROM spip_mots AS mots, spip_mots_$table AS lien WHERE lien.$id_table=$id_objet AND mots.id_mot=lien.id_mot")); if (!$nombre_mots) { if (!$flag_editable) return; list($nombre_groupes) = spip_fetch_array(spip_query("SELECT COUNT(*) FROM spip_groupes_mots WHERE $table = 'oui' AND ".substr($connect_statut,1)." = 'oui'")); if (!$nombre_groupes) return; } echo ""; if ($flag_editable){ if ($nouv_mot.$cherche_mot.$supp_mot) $bouton = bouton_block_visible("lesmots"); else $bouton = bouton_block_invisible("lesmots"); } debut_cadre_enfonce("mot-cle-24.gif", false, "", $bouton._T('titre_mots_cles').aide ("artmots")); ////////////////////////////////////////////////////// // Recherche de mot-cle // if ($nouv_mot) $nouveaux_mots[] = $nouv_mot; $tous_les_mots = split(" *[,;] *", $cherche_mot); while ((list(,$cherche_mot) = each ($tous_les_mots)) AND $cherche_mot) { echo "
";
$query = "SELECT id_mot, titre FROM spip_mots WHERE id_groupe='$select_groupe'";
$result = spip_query($query);
unset($table_mots);
unset($table_ids);
while ($row = spip_fetch_array($result)) {
$table_ids[] = $row['id_mot'];
$table_mots[] = $row['titre'];
}
$resultat = mots_ressemblants($cherche_mot, $table_mots, $table_ids);
debut_boite_info();
if (!$resultat) {
echo ""._T('info_non_resultat', array('cherche_mot' => $cherche_mot))."
";
}
else if (count($resultat) == 1) {
list(, $nouv_mot) = each($resultat);
$nouveaux_mots[] = $nouv_mot;
echo ""._T('info_mot_cle_ajoute')." ";
if ($table == 'articles') echo _T('info_l_article');
else if ($table == 'breves') echo _T('info_la_breve');
else if ($table == 'rubriques') echo _T('info_la_rubrique');
echo " :
";
$query = "SELECT * FROM spip_mots WHERE id_mot=$nouv_mot";
$result = spip_query($query);
echo "
\n"; } echo "
"; } ////////////////////////////////////////////////////// // Appliquer les modifications sur les mots-cles // if ($nouveaux_mots && $flag_editable) { while ((list(,$nouv_mot) = each($nouveaux_mots)) AND $nouv_mot!='x') { $query = "SELECT * FROM spip_mots_$table WHERE id_mot=$nouv_mot AND $id_table=$id_objet"; $result = spip_query($query); if (!spip_num_rows($result)) { $query = "INSERT INTO spip_mots_$table (id_mot,$id_table) VALUES ($nouv_mot, $id_objet)"; $result = spip_query($query); } } $reindexer = true; } if ($supp_mot && $flag_editable) { if ($supp_mot == -1) $mots_supp = ""; else $mots_supp = " AND id_mot=$supp_mot"; $query = "DELETE FROM spip_mots_$table WHERE $id_table=$id_objet $mots_supp"; $result = spip_query($query); $reindexer = true; } if ($reindexer AND lire_meta('activer_moteur') == 'oui') { include_ecrire ("inc_index.php3"); marquer_indexer($objet, $id_objet); } // // Afficher les mots-cles // $query = "SELECT DISTINCT type FROM spip_mots"; $result = spip_query($query); $plusieurs_types = (spip_num_rows($result) > 1); unset($les_mots); $query = "SELECT mots.* FROM spip_mots AS mots, spip_mots_$table AS lien WHERE lien.$id_table=$id_objet AND mots.id_mot=lien.id_mot ORDER BY mots.type, mots.titre"; $result = spip_query($query); if (spip_num_rows($result) > 0) { echo "