[SPIP Zone] r4817 - in /_plugins_/_ze_laboratoire_/ortho: ./ inc/ inc/ortho.php plugin.xml

Author: christian_lefebvre@laposte.net
Date: Tue Aug 22 23:25:47 2006
New Revision: 4817

Log:
brouillon de modif sur ortho.php pour aller chercher la liste des serveurs
sur spip.net

Added:
    _plugins_/_ze_laboratoire_/ortho/
    _plugins_/_ze_laboratoire_/ortho/inc/
    _plugins_/_ze_laboratoire_/ortho/inc/ortho.php
    _plugins_/_ze_laboratoire_/ortho/plugin.xml

Added: _plugins_/_ze_laboratoire_/ortho/inc/ortho.php

--- _plugins_/_ze_laboratoire_/ortho/inc/ortho.php (added)
+++ _plugins_/_ze_laboratoire_/ortho/inc/ortho.php Tue Aug 22 23:25:47 2006
@@ -0,0 +1,678 @@
+<?php
+
+/***************************************************************************\
+ * SPIP, Systeme de publication pour l’internet *
+ * *
+ * Copyright (c) 2001-2006 *
+ * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
+ * *
+ * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
+ * Pour plus de details voir le fichier COPYING.txt ou l’aide en ligne. *
+\***************************************************************************/
+
+if (!defined("_ECRIRE_INC_VERSION")) return;
+
+// Mettre a jour la liste locale des miroirs
+// http://doc.spip.org/@maj_miroirs_ortho
+function maj_miroirs_ortho() {
+ $liste = explode(" « , $GLOBALS[‹ meta ›][« liste_miroirs_ortho »]);
+ $miroirs_old = array();
+ foreach ($liste as $index) {
+ list($url) = explode( » « , $GLOBALS[‹ meta ›][« miroir_ortho_$index »]);
+ $miroirs_old[$url] = $index;
+ }
+
+ // TODO: recuperer la liste dynamiquement depuis ortho.spip.net
+ $page = recuperer_page($GLOBALS[‹ services ›][‹ ortho ›]);
+ $urls=array();
+ foreach(explode( »\n", $page) as $ligne) {
+ $t= explode(";", $ligne);
+ $urls[$t[1]]=$t[0];
+ }
+
+# $urls = array(
+# ‹ http://tony.ortho.spip.net/ortho_serveur.php ›,
+# ‹ http://spip.destination-linux.org/ortho_serveur.php ›,
+# ‹ http://spip-ortho.linagora.org:18080/ortho_serveur.php ›,
+# ‹ http://ortho.spip.net/ortho_serveur.php ›
+# );
+
+ $liste = array();
+ $miroirs_new = array();
+ $index = 1;
+ foreach ($urls as $url) {
+ if ($index_old = $miroirs_old[$url]) {
+ $s = $GLOBALS[‹ meta ›][« miroir_ortho_$index_old »];
+ }
+ else {
+ $s = $url." « .time();
+ }
+ $miroirs_new[$index] = $s;
+ $liste[] = $index++;
+ }
+ foreach ($miroirs_old as $index) {
+ effacer_meta(« miroir_ortho_$index »);
+ }
+ foreach ($miroirs_new as $index => $s) {
+ ecrire_meta(« miroir_ortho_$index », $s);
+ }
+ ecrire_meta(« liste_miroirs_ortho », join( » « , $liste));
+}
+
+// Lire la liste des miroirs et les langues associees
+// http://doc.spip.org/@lire_miroirs_ortho
+function lire_miroirs_ortho() {
+ global $miroirs_ortho, $index_miroirs_ortho, $duree_cache_miroirs_ortho;
+
+ $miroirs_ortho = array();
+ $index_miroirs_ortho = array();
+
+ $t = time();
+ $maj = $GLOBALS[‹ meta ›][« maj_miroirs_ortho »];
+ if ($maj < $t - $duree_cache_miroirs_ortho) {
+ maj_miroirs_ortho();
+ ecrire_meta(« maj_miroirs_ortho », $t);
+ lire_metas();
+ }
+
+ $liste = explode( » « , $GLOBALS[‹ meta ›][« liste_miroirs_ortho »]);
+ foreach ($liste as $index) {
+ $s = explode( » « , $GLOBALS[‹ meta ›][« miroir_ortho_$index »]);
+ $url = $s[0];
+ $maj = $s[1];
+ $langs = explode( »,", $s[2]);
+ // Reinitialiser periodiquement la liste des langues non-supportees
+ if ($maj < $t - $duree_cache_miroirs_ortho) {
+ foreach ($langs as $key => $lang) {
+ if (substr($lang, 0, 1) == ‹ ! ›) unset($langs[$key]);
+ }
+ $s[1] = $t;
+ $s[2] = join(",", $langs);
+ ecrire_meta(« miroir_ortho_$index », join(" « , $s));
+ }
+ $index_miroirs_ortho[$url] = $index;
+ $miroirs_ortho[$url] = array();
+ foreach ($langs as $lang) {
+ if ($lang) $miroirs_ortho[$url][$lang] = $lang;
+ }
+ }
+ lire_metas();
+ mt_srand(time());
+}
+
+// Sauvegarder les infos de langues pour le miroir
+// http://doc.spip.org/@ecrire_miroir_ortho
+function ecrire_miroir_ortho($url, $langs) {
+ global $index_miroirs_ortho;
+
+ $index = $index_miroirs_ortho[$url];
+ $s = explode( » « , $GLOBALS[‹ meta ›][« miroir_ortho_$index »]);
+ $s[2] = join( »,", $langs);
+ ecrire_meta(« miroir_ortho_$index », join(" « , $s));
+}
+
+// http://doc.spip.org/@ajouter_langue_miroir
+function ajouter_langue_miroir($url, $lang) {
+ global $miroirs_ortho;
+ $langs = $miroirs_ortho[$url];
+ $langs[$lang] = $lang;
+ unset($langs[ »!$lang"]);
+ ecrire_miroir_ortho($url, $langs);
+}
+
+// http://doc.spip.org/@enlever_langue_miroir
+function enlever_langue_miroir($url, $lang) {
+ global $miroirs_ortho;
+ $langs = $miroirs_ortho[$url];
+ unset($langs[$lang]);
+ $langs["!$lang"] = « !$lang »;
+ ecrire_miroir_ortho($url, $langs);
+}
+
+// http://doc.spip.org/@reset_miroir
+function reset_miroir($url) {
+ global $miroirs_ortho;
+ ecrire_miroir_ortho($url, array());
+}
+
+//
+// Renvoie la liste des miroirs utilisables pour une langue donnee
+//
+// http://doc.spip.org/@chercher_miroirs_ortho
+function chercher_miroirs_ortho($lang) {
+ global $miroirs_ortho;
+
+ $result = array();
+ $chercher = true;
+ foreach ($miroirs_ortho as $url => $langs) {
+ if ($langs[$lang]) {
+ $result[] = $url;
+ }
+ else if ($chercher && !$langs["!$lang"]) {
+ //echo « test $lang $url<br /> »;
+ if (verifier_langue_miroir($url, $lang)) $result[] = $url;
+ // Ne recuperer la langue d’un miroir qu’une seule fois par requete
+ if ($result) $chercher = false;
+ }
+ }
+ return $result;
+}
+
+// http://doc.spip.org/@choisir_miroirs_ortho
+function choisir_miroirs_ortho($lang) {
+ $liste = chercher_miroirs_ortho($lang);
+ if (!count($liste)) return false;
+ foreach ($liste as $url) {
+ $miroirs[md5(mt_rand().$url.rand())] = $url;
+ }
+ ksort($miroirs);
+ return $miroirs;
+}
+
+//
+// Envoyer une requete a un serveur d’orthographe
+//
+// http://doc.spip.org/@post_ortho
+function post_ortho($url, $texte, $lang) {
+
+ $gz = ($GLOBALS[‹ flag_gz ›] && strlen($texte) >= 200);
+ $boundary = ‹  ›;
+ $vars = array(
+ ‹ op › => ‹ spell ›,
+ ‹ lang › => $lang,
+ ‹ texte › => $texte,
+ ‹ gz › => $gz ? 1 : 0
+ );
+ // Si le texte est petit, l’overhead du multipart est dispendieux
+ // Sinon, on passe en multipart pour compresser la chaine a corriger
+ if ($gz) {
+ // Il faut eliminer les caracteres 0 sinon PHP ne lit pas la suite du parametre
+ // passe en multipart/form-data (gros hack bien sale)
+ $texte_gz = gzcompress($texte);
+ for ($echap = 255; $echap > 0; $echap--) {
+ $str_echap = chr($echap ^ 1).chr($echap).chr($echap).chr($echap ^ 2);
+ if (!is_int(strpos($texte_gz, $str_echap))) break;
+ }
+ $texte_gz = str_replace("\x00", $str_echap, $texte_gz);
+ $vars[‹ texte ›] = $texte_gz;
+ $vars[‹ nul_echap ›] = $str_echap;
+ $boundary = substr(md5(rand().‹ ortho ›), 0, 8);
+ }
+
+ $r = recuperer_page($url, false, false, 1048576, $vars, $boundary, true);
+
+ // decompression de GZ ortho
+ if ($gz) $r = gzuncompress($r);
+ return $r;
+
+/*
+ * Note a propos de la compression : si on ne refuse pas le gz dans recuperer_page(),
+ * le serveur d’ortho va retourner des donnees compressees deux fois ; le code
+ * saurait les decompresser deux fois, mais on perd alors beaucoup de temps (on
+ * passe, dans un test, de 5 s a 25 s de delai !)
+ */
+
+}
+
+//
+// Verifier si un serveur gere une langue donnee
+//
+// http://doc.spip.org/@verifier_langue_miroir
+function verifier_langue_miroir($url, $lang) {
+ // Envoyer une requete bidon
+ $result = post_ortho($url, " « , $lang);
+ if (!preg_match(’,<ortho>.*</ortho>,s’, $result)) {
+ reset_miroir($url);
+ return false;
+ }
+ if (!preg_match(’,<erreur>.*<code>E_LANG_ABSENT</code>.*</erreur>,s’, $result)) {
+ ajouter_langue_miroir($url, $lang);
+ return true;
+ }
+ enlever_langue_miroir($url, $lang);
+ return false;
+}
+
+
+//
+// Gestion du dictionnaire local
+//
+// http://doc.spip.org/@suggerer_dico_ortho
+function suggerer_dico_ortho(&$mots, $lang) {
+ $result = spip_query(« SELECT mot FROM spip_ortho_dico WHERE lang= » . spip_abstract_quote($lang) . " AND mot IN ( ».join(", « , array_map(‹ spip_abstract_quote ›, $mots)). »)");
+
+ $mots = array_flip($mots);
+ $bons = array();
+ if (isset($mots[’’])) unset($mots[’’]);
+ while ($row = spip_fetch_array($result)) {
+ $mot = $row[‹ mot ›];
+ if (isset($mots[$mot])) {
+ unset($mots[$mot]);
+ $bons[] = $mot;
+ }
+ }
+
+ if (count($mots)) $mots = array_flip($mots);
+ else $mots = array();
+ return $bons;
+}
+
+// http://doc.spip.org/@ajouter_dico_ortho
+function ajouter_dico_ortho($mot, $lang) {
+ global $connect_id_auteur;
+
+ spip_query(« INSERT IGNORE INTO spip_ortho_dico (lang, mot, id_auteur) VALUES ( » . spip_abstract_quote($lang) . « , " . spip_abstract_quote($mot) . « , $connect_id_auteur) »);
+
+}
+
+// http://doc.spip.org/@supprimer_dico_ortho
+function supprimer_dico_ortho($mot, $lang) {
+ spip_query(« DELETE FROM spip_ortho_dico WHERE lang= » . spip_abstract_quote($lang) . " AND mot= » . spip_abstract_quote($mot));
+
+}
+
+// http://doc.spip.org/@gerer_dico_ortho
+function gerer_dico_ortho($lang) {
+ global $ajout_ortho, $supp_ortho;
+ if ($mot = strval($ajout_ortho)) {
+ ajouter_dico_ortho($mot, $lang);
+ }
+ if ($mot = strval($supp_ortho)) {
+ supprimer_dico_ortho($mot, $lang);
+ }
+}
+
+
+//
+// Gestion du cache de corrections
+//
+// http://doc.spip.org/@suggerer_cache_ortho
+function suggerer_cache_ortho(&$mots, $lang) {
+ global $duree_cache_ortho;
+
+ $result = spip_query(« SELECT mot, ok, suggest FROM spip_ortho_cache WHERE lang= » . spip_abstract_quote($lang) . " AND mot IN (".join(", « , array_map(‹ spip_abstract_quote ›, $mots)). ») AND maj > FROM_UNIXTIME(".(time() - $duree_cache_ortho).")");
+
+
+ $mots = array_flip($mots);
+ $suggest = array();
+ if (isset($mots[’’])) unset($mots[’’]);
+ while ($row = spip_fetch_array($result)) {
+ $mot = $row[‹ mot ›];

[... 395 lines stripped ...]