Subversion Repositories eFlore/Applications.coel

Compare Revisions

Ignore whitespace Rev 1829 → Rev 1943

/branches/v1.11-okuzgozu/jrest/services/bibliotheque/WdHTMLParser.php
New file
0,0 → 1,144
<?php
/**
*
* @author Olivier Laviale
* @see http://www.weirdog.com/blog/php/un-parser-html-des-plus-leger.html
*
*/
class WdHTMLParser {
private $encoding;
private $matches;
private $escaped;
private $opened = array();
public $malformed;
 
public function parse($html, $namespace=NULL, $encoding='utf-8') {
$this->malformed = false;
$this->encoding = $encoding;
// we take care of escaping comments and processing options. they will not be parsed
// and will end as text nodes
$html = $this->escapeSpecials($html);
// in order to create a tree, we first need to split the HTML using the markups,
// creating a nice flat array of texts and opening and closing markups.
//
// the array can be read as follows :
//
// i+0 => some text
// i+1 => '/' for closing markups, nothing otherwise
// i+2 => the markup it self, without the '<' '>'
//
// note that i+2 might end with a '/' indicating an auto-closing markup
$this->matches = preg_split('#<(/?)' . $namespace . '([^>]*)>#', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
// the flat representation is now ready, we can create our tree
$tree = $this->buildTree();
 
// if comments or processing options where escaped, we can
// safely unescape them now
if ($this->escaped) {
$tree = $this->unescapeSpecials($tree);
}
return $tree;
}
private function escapeSpecials($html) {
// here we escape comments
$html = preg_replace_callback('#<\!--.+-->#sU', array($this, 'escapeSpecials_callback'), $html);
 
// and processing options
$html = preg_replace_callback('#<\?.+\?>#sU', array($this, 'escapeSpecials_callback'), $html);
return $html;
}
private function escapeSpecials_callback($m) {
$this->escaped = true;
$text = $m[0];
$text = str_replace(array('<', '>'), array("\x01", "\x02"), $text);
return $text;
}
 
private function unescapeSpecials($tree) {
return is_array($tree) ? array_map(array($this, 'unescapeSpecials'), $tree) : str_replace(array("\x01", "\x02"), array('<', '>'), $tree);
}
 
private function buildTree() {
$nodes = array();
$i = 0;
$text = NULL;
while (($value = array_shift($this->matches)) !== NULL) {
switch ($i++ % 3) {
case 0:
// if the trimed value is not empty we preserve the value,
// otherwise we discard it.
if (trim($value)){
$nodes[] = $value;
}
break;
case 1:
$closing = ($value == '/');
break;
case 2:
if (substr($value, -1, 1) == '/') {
// auto closing
$nodes[] = $this->parseMarkup(substr($value, 0, -1));
} else if ($closing) {
// closing markup
$open = array_pop($this->opened);
if ($value != $open) {
$this->error($value, $open);
}
 
return $nodes;
} else {
// this is an open markup with possible children
$node = $this->parseMarkup($value);
// push the markup name into the opened markups
$this->opened[] = $node['name'];
// create the node and parse its children
$node['children'] = $this->buildTree($this->matches);
$nodes[] = $node;
}
break;
}
}
return $nodes;
}
public function parseMarkup($markup) {
// get markup's name
preg_match('#^[^\s]+#', $markup, $matches);
$name = $matches[0];
 
// get markup's arguments
preg_match_all('#\s+([^=]+)\s*=\s*"([^"]+)"#', $markup, $matches, PREG_SET_ORDER);
// transform the matches into a nice key/value array
$args = array();
foreach ($matches as $m) {
// we unescape the html entities of the argument's value
$args[$m[1]] = html_entity_decode($m[2], ENT_QUOTES, $this->encoding);
}
 
return array('name' => $name, 'args' => $args);
}
public function error($markup, $expected) {
$this->malformed = true;
printf('unexpected closing markup "%s", should be "%s"', $markup, $expected);
}
}
 
?>
/branches/v1.11-okuzgozu/jrest/services/bibliotheque/Ontologie.php
New file
0,0 → 1,71
<?php
class Ontologie {
private $ontologie = null;
/**
* Le contructeur transforme la sortie d'une requête sur la table coel_meta_liste_valeur en tableau Ontologie au format :
* $ontologie[id_valeur] = array(
* 'nom' => 'nom de la valeur',
* 'abr' => 'abréviation de la valeur,
* 'dsc' => 'description de la vaeur');
* @param $ontologie tableau listant les valeurs de l'ontologie.
*/
public function __construct(Array $metadonnees) {
$ontologie = array(0 => array('nom' => 'Non renseigné', 'abr' => 'NR'));
foreach ($metadonnees as $meta) {
$id = $meta['cmlv_id_valeur'];
$nom = isset($meta['cmlv_nom']) ? $meta['cmlv_nom'] : null;
$abr = isset($meta['cmlv_abreviation']) ? $meta['cmlv_abreviation'] : null;
$dsc = isset($meta['cmlv_description']) ? $meta['cmlv_description'] : null;
$ontologie[$id] = array('nom' => $nom, 'abr' => $abr, 'dsc' => $dsc);
}
$this->ontologie = $ontologie;
}
 
/**
* Retourne le nom de la valeur de l'ontologie correspondant à l'id.
*
* @param int $id l'identifiant de la valeur de l'ontologie.
* @return string le nom de la valeur.
*/
public function getNom($id) {
$nom = null;
if (isset($this->ontologie[$id])) {
$nom = $this->ontologie[$id]['nom'];
}
return $nom;
}
/**
* Retourne l'abréviation de la valeur de l'ontologie correspondant à l'id.
*
* @param int $id l'identifiant de la valeur de l'ontologie.
* @return string l'abreviation de la valeur.
*/
public function getAbreviation($id) {
$abr = null;
if (isset($this->ontologie[$id])) {
$abr = $this->ontologie[$id]['abr'];
}
return $abr;
}
 
/**
* Le tableau de retour Ontologie est au format :
* $ontologie[id_valeur] = array(
* 'nom' => 'nom de la valeur',
* 'abr' => 'abréviation de la valeur);
*
* @param int $id l'identifiant de la valeur de l'ontologie.
* @return $ontologie tableau listant les valeurs de l'ontologie.
*/
public function getTableauValeur($id) {
$valeur = null;
if (isset($this->ontologie[$id])) {
$valeur = $this->ontologie[$id];
}
return $valeur;
}
}
/branches/v1.11-okuzgozu/jrest/services/bibliotheque/UtilTruck.php
New file
0,0 → 1,284
<?php
/**
* Classe permettant de traiter plus facilement les champs dégueulasses de la base de données COEL.
*
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version $Id$
* @copyright 2010
*/
class UtilTruck {
const TYPE_AUTRE = 'AUTRE';
const TYPE_TOTAL = 'TOTAL';
const SEPARATEUR_TYPE_VALEUR = '##';
const SEPARATEUR_VALEURS = ';;';
const SEPARATEUR_DONNEES = '||';
const VALEUR_NULL = 'NC';
private $ontologie = null;
public function __construct(Ontologie $ontologie = null) {
$this->setOntologie($ontologie);
}
public function setOntologie(Ontologie $ontologie) {
$this->ontologie = $ontologie;
}
public function construireTxtTruckSimple($chaine_a_analyser) {
return $this->construireTxtTruck($chaine_a_analyser, false, false);
}
public static function construireTxtTruckSansMajuscule($chaine_a_analyser) {
return $this->construireTxtTruck($chaine_a_analyser, false, true);
}
public function construireTxtTruckSansPointFinal($chaine_a_analyser) {
return $this->construireTxtTruck($chaine_a_analyser, true, false);
}
private function construireTxtTruck($chaine_a_analyser, $majuscule = true, $point_final = true) {
$termes = $this->traiterTxtTruck($chaine_a_analyser);
$chaine_a_retourner = self::formaterTableauDeTxt($termes, $majuscule, $point_final);
return $chaine_a_retourner;
}
public function traiterTxtTruck($chaine_a_analyser) {
$termes = array();
if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
$nbre_valeurs = count($valeurs);
if ($nbre_valeurs > 0) {
for ($i = 0; $i < $nbre_valeurs; $i++) {
$valeur = trim($valeurs[$i]);
if ($valeur != '') {
$valeur_formatee = $this->formaterValeurTruck($valeur);
$termes[] = $valeur_formatee;
}
}
}
}
return $termes;
}
public function getTxtTruckParPosition($chaine_a_analyser, $position = 1) {
$retour = '';
if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
$nbre_valeurs = count($valeurs);
if ($nbre_valeurs > 0) {
$position = $position - 1;
$valeur = trim($valeurs[$position]);
if ($valeur != '') {
$retour = $this->formaterValeurTruck($valeur);
}
}
}
return $retour;
}
public function getTableauTruck($chaine_a_analyser) {
$tableau_retour = array();
if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
$nbre_valeurs = count($valeurs);
if ($nbre_valeurs > 0) {
for ($i = 0; $i < $nbre_valeurs; $i++) {
$valeur = trim($valeurs[$i]);
if ($valeur != '') {
$tableau_retour[] = $valeur;
}
}
}
}
return $tableau_retour;
}
public function getNbreValeur($chaine_a_analyser) {
$nbre_valeurs = null;
if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
$nbre_valeurs = count($valeurs);
}
return $nbre_valeurs;
}
private function formaterValeurTruck($valeur) {
$chaine_a_retourner = '';
if (preg_match('/^[^#]+##[^$]+$/', $valeur)) {
$cle_valeur = explode(self::SEPARATEUR_TYPE_VALEUR, $valeur);
$chaine_a_retourner = (($cle_valeur[1] == '' || $cle_valeur[1] == 'null') ? self::VALEUR_NULL : $cle_valeur[1]);
$chaine_a_retourner .= ' '.$this->formaterParenthese($cle_valeur[0]);
} else if ($valeur != '') {
$chaine_a_retourner = $valeur;
} else {
trigger_error("Valeur truck posant problème :$valeur", E_USER_NOTICE);
}
return $chaine_a_retourner;
}
public function formaterParenthese($chaine_a_afficher) {
if ($chaine_a_afficher != '') {
$chaine_a_afficher = '('.$chaine_a_afficher.')';
}
return $chaine_a_afficher;
}
public static function formaterTableauDeTxt(Array $tableau_de_txt, $majuscule = true, $point_final = true) {
$chaine_a_afficher = '';
$taille_du_tableau = count($tableau_de_txt);
if ($taille_du_tableau > 0) {
$index_avt_dernier = $taille_du_tableau - 1;
for ($i = 0; $i < $taille_du_tableau; $i++) {
$mot = $tableau_de_txt[$i];
if ($i != $index_avt_dernier) {
$chaine_a_afficher .= $mot.', ';
} else {
$chaine_a_afficher .= self::nettoyerPointFinal($mot);
if ($point_final) {
$chaine_a_afficher .= '.';
}
}
}
}
if ($majuscule) {
$chaine_a_afficher = ucfirst($chaine_a_afficher);
}
return $chaine_a_afficher;
}
private static function formaterAutre($chaine_a_afficher) {
if ($chaine_a_afficher != '') {
$chaine_a_afficher = ' [Autre : '.$chaine_a_afficher.']';
}
return $chaine_a_afficher;
}
private static function formaterOuiNon($chaine_a_formater) {
$txt_a_retourner = '';
if ($chaine_a_formater == '0') {
$txt_a_retourner = 'non';
} else if ($chaine_a_formater == '1') {
$txt_a_retourner = 'oui';
}
return $txt_a_retourner;
}
private static function nettoyerPointFinal($mot) {
$mot = preg_replace('/[.]$/', '', $mot);
return $mot;
}
public function construireTxtListeOntologie($chaineAAnalyser, $valeurEstOntologie = true, $typeEstOntologie = true, $donneeEstOntologie = false) {
$termes = array();
$autres = array();
$chaineAAnalyser = trim($chaineAAnalyser);
if ($chaineAAnalyser != '') {
$valeurs = explode(self::SEPARATEUR_VALEURS, $chaineAAnalyser);
$nbreValeurs = count($valeurs);
if ($nbreValeurs > 0) {
for ($i = 0; $i < $nbreValeurs; $i++) {
$valeur = $valeurs[$i];
// VALEUR SANS TYPE
// La valeur sans type est une entrée de l'ontologie
if ($valeurEstOntologie && preg_match('/^[0-9]+$/u', $valeur)) {
if ($valeur == '0') {
$valeur = '';
} else {
if (isset($this->ontologie)) {
$valeurOntologie = $this->ontologie->getTableauValeur($valeur);
if (isset($valeurOntologie)) {
$valeur = $valeurOntologie['nom'];
}
} else {
$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
trigger_error($e, E_USER_WARNING);
}
}
}
// VALEUR AVEC TYPE
// Type : AUTRE
$valeurTypeAutre = self::TYPE_AUTRE.self::SEPARATEUR_TYPE_VALEUR;
if (preg_match('/^'.$valeurTypeAutre.'.+$/u', $valeur)) {
$txtAutre = preg_replace('/^'.$valeurTypeAutre.'/u', '', $valeur);
if ($txtAutre != '') {
$autres[] = $txtAutre;
}
$valeur = '';
}
// Type correspondant à une entrée de l'ontologie
if ($typeEstOntologie) {
$valeurTypeOntologie = '([0-9]+)'.self::SEPARATEUR_TYPE_VALEUR;
$valeurTypeAutre = '([[:alnum:]]+)'.self::SEPARATEUR_TYPE_VALEUR;
if (preg_match('/^'.$valeurTypeOntologie.'.*$/u', $valeur, $match)) {// Cas type : réf. numérique
$type = $match[1];
if (isset($this->ontologie)) {
$valeurOntologieNom = $this->ontologie->getNom($type);
if (isset($valeurOntologieNom)) {
$valeurOntologieNom .= ' : ';
$valeur = preg_replace('/^'.$type.'/u', $valeurOntologieNom, $valeur);
}
} else {
$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
trigger_error($e, E_USER_WARNING);
}
} else if (preg_match('/^'.$valeurTypeAutre.'.*$/u', $valeur, $match)) {// Cas type : AUTRE
$type = $match[1];
$valeur = preg_replace('/^'.$type.'/u', $type.' : ', $valeur);
}
}
// Donnée correspondant à une entrée de l'ontologie
if ($donneeEstOntologie) {
$donneeOntologie = self::SEPARATEUR_TYPE_VALEUR.'([0-9]+)';
if (preg_match('/^.+'.$donneeOntologie.'$/u', $valeur, $match)) {
$donnee = $match[1];
$donnee = str_replace(self::SEPARATEUR_TYPE_VALEUR, '', $donnee);
if (isset($this->ontologie)) {
$valeurOntologieNom = $this->ontologie->getNom($donnee);
if (isset($valeurOntologieNom)) {
$valeur = preg_replace('/'.$donnee.'$/u', $valeurOntologieNom, $valeur);
}
} else {
$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
trigger_error($e, E_USER_WARNING);
}
}
}
// Nettoyage final
$valeur = preg_replace('/'.self::SEPARATEUR_TYPE_VALEUR.'/', '', $valeur);
if ($valeur != '') {
$termes[] = $valeur;
}
}
}
}
$chaineTermes = self::formaterTableauDeTxt($termes);
$chaineAutres = self::formaterTableauDeTxt($autres);
$chaineARetourner = $chaineTermes.self::formaterAutre($chaineAutres);
return $chaineARetourner;
}
public static function extraireNbrePart($truk_unite_base) {
$types = explode(self::SEPARATEUR_VALEURS, $truk_unite_base);
$nbre = 0;
foreach ($types as $type) {
$unite_base = explode(self::SEPARATEUR_TYPE_VALEUR, $type);
$nbre_part = 0;
if (isset($unite_base[1])) {
$unite_base_info = explode('||', $unite_base[1]);
$nbre_part = $unite_base_info[3];
}
$nbre += $nbre_part;
}
return $nbre;
}
}