/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/decorateurs/OntologieChampsProjetDecorateur.php |
---|
New file |
0,0 → 1,38 |
<?php |
class OntologieChampsProjetDecorateur extends OntologieDecorateur { |
private $ontologieDecorateur = null; |
private $champs = array(); |
protected $correspondances = array(); |
public function __construct(OntologieDecorateur $ontologieDecorateur, Array $champs) { |
$this->ontologieDecorateur = $ontologieDecorateur; |
$this->champs = $champs; |
$this->correspondances = array_flip($this->champs); |
} |
public function traiterChampsRetour(Array $champsRetour) { |
//die(print_r($this->correspondances,true)); |
foreach ($champsRetour as $champ) { |
if (array_key_exists($champ, $this->correspondances)) { |
$champBdd = $this->correspondances[$champ]; |
$champSortie = $champ; |
//die(print_r($this->ontologieDecorateur->terme,true)); |
if ($this->ontologieDecorateur->terme->verifierTag($champBdd)) { |
$valeur = $this->ontologieDecorateur->terme->getTag($champBdd); |
$this->ontologieDecorateur->termeFormate[$champSortie] = $valeur; |
} |
} |
} |
} |
public function ajouterChampsSupplementaires() { |
foreach ($this->champs as $champBdd => $champSortie) { |
if ($this->ontologieDecorateur->terme->verifierTag($champBdd)) { |
$valeur = $this->ontologieDecorateur->terme->getTag($champBdd); |
$this->ontologieDecorateur->termeFormate[$champSortie] = $valeur; |
} |
} |
} |
} |
?> |
/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/decorateurs/OntologieDecorateur.php |
---|
New file |
0,0 → 1,92 |
<?php |
class OntologieDecorateur implements OntologieResponsabilite { |
protected $terme = null; |
protected $termeFormate = array(); |
private $detailsHrefTpl = null; |
private $langueDemandee = null; |
protected $correspondances = array( |
'id' => 'Id', |
'nom' => 'Intitule', |
'description' => 'Description', |
'href' => 'Href', |
'classe' => 'Classe', |
'classe.id' => 'ClasseId', |
'classe.href' => 'ClasseHref', |
'classe.*' => 'ClasseId,Classe,ClasseHref'); |
public function __construct(OntologieDO $termeADecorer, $detailsHrefTpl, $langueDemandee) { |
$this->terme = $termeADecorer; |
$this->detailsHrefTpl = $detailsHrefTpl; |
$this->langueDemandee = $langueDemandee; |
$this->initialiserTermeFormate(); |
} |
public function traiterChampsRetour(Array $champsRetour) { |
foreach ($champsRetour as $champ) { |
if (array_key_exists($champ, $this->correspondances)) { |
$methodesAExecuter = explode(',', $this->correspondances[$champ]); |
foreach ($methodesAExecuter as $methodeNom) { |
$methodeAjouter = 'ajouter'.$methodeNom; |
if (method_exists($this, $methodeAjouter)) { |
$this->$methodeAjouter(); |
} |
} |
} |
} |
} |
public function ajouterId() { |
$this->termeFormate['id'] = (int) $this->terme->getTag('id_terme'); |
} |
public function ajouterIntitule() { |
if ($this->langueDemandee == 'en') { |
$this->termeFormate['nom'] = $this->terme->getTag('nom_en'); |
} else { |
$this->termeFormate['nom'] = $this->terme->getTag('nom'); |
} |
} |
public function ajouterDescription() { |
if ($this->langueDemandee == 'en') { |
if ($this->terme->verifierTag('description_en')) { |
$this->termeFormate['description'] = $this->terme->getTag('description_en'); |
} |
} else { |
if ($this->terme->verifierTag('description')) { |
$this->termeFormate['description'] = $this->terme->getTag('description'); |
} |
} |
} |
public function ajouterHref() { |
if ($this->terme->verifierTag('id_terme')) { |
$href = sprintf($this->detailsHrefTpl, $this->terme->getTag('id_terme')); |
$this->termeFormate['href'] = $href; |
} |
} |
public function ajouterClasseId() { |
$this->termeFormate['classe.id'] = (int) $this->terme->getTag('ce_type'); |
} |
public function ajouterClasse() { |
$this->termeFormate['classe'] = (string) $this->terme->getTag('type'); |
} |
public function ajouterClasseHref() { |
if ($this->terme->verifierTag('ce_type')) { |
$href = sprintf($this->detailsHrefTpl, $this->terme->getTag('ce_type')); |
$this->termeFormate['classe.href'] = $href; |
} |
} |
public function initialiserTermeFormate() { |
$this->termeFormate = array(); |
} |
public function getTermeFormate() { |
return $this->termeFormate; |
} |
} |
?> |
/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/decorateurs/OntologieResponsabilite.php |
---|
New file |
0,0 → 1,5 |
<?php |
interface OntologieResponsabilite { |
public function traiterChampsRetour(Array $champsRetour); |
} |
?> |
/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/OntologieDO.php |
---|
New file |
0,0 → 1,21 |
<?php |
class OntologieDO { |
private $infos; |
public function __construct(Array $infos) { |
$this->infos = $infos; |
} |
public function getTag($tag) { |
return isset($this->infos[$tag]) ? $this->infos[$tag] : null; |
} |
public function verifierTag($tag) { |
$existe = true; |
if ($this->getTag($tag) == null || $this->getTag($tag) == '') { |
$existe = false; |
} |
return $existe; |
} |
} |
?> |
/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/OntologieDAO.php |
---|
New file |
0,0 → 1,202 |
<?php |
class OntologieDAO { |
private $bdd = null; |
private $versions = null; |
private $requeteNbreTermesTotal = null; |
private $masquesStrictes = array(); |
private $paramsMasque = array(); |
public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd, Versions $versions) { |
$this->ressources = $ressources; |
$this->parametres = $parametres; |
$this->bdd = $bdd; |
$this->versions = $versions; |
$this->masquesStrictes = array('code'); |
$this->paramsMasque = array( |
'' => 'nom', |
'code' => 'id_terme', |
'nom' => $this->getChampPourLangue('nom'), |
'description' => $this->getChampPourLangue('description')); |
} |
public function getDetails() { |
$table = $this->getTableTerme(); |
$tableType = $this->getTableType(); |
$detailsId = $this->ressources->getDetailsId(); |
$detailsId = $this->bdd->proteger($detailsId); |
$requete = |
'SELECT o.*, type '. |
"FROM $table AS o ". |
" LEFT JOIN $tableType ON (ce_type = id_type) ". |
"WHERE id_terme = $detailsId "; |
$resultats = $this->bdd->recuperer($requete); |
$terme = new OntologieDO($resultats); |
return $terme; |
} |
public function rechercher() { |
$clause = $this->getClauseSelectSpeciale(); |
$table = $this->getTableTerme(); |
$tableType = $this->getTableType(); |
$tablePublication = $this->getTablePublication(); |
$tableAuteur = $this->getTableAuteur(); |
$tableImage = $this->getTableImage(); |
$conditions = $this->getConditions(); |
$where = $this->getWhere($conditions); |
$ordre = $this->getOrdre(); |
$navigation = $this->getNavigation(); |
$requete = "SELECT $clause o.*, t.type, p.*, a.*, i.* ". |
"FROM $table AS o ". |
" LEFT JOIN $tableType AS t ON (ce_type = id_type) ". |
" LEFT JOIN $tablePublication AS p ON (ce_publication = id_publication) ". |
" LEFT JOIN $tableAuteur AS a ON (ce_auteur = id_auteur) ". |
" LEFT JOIN $tableImage AS i ON (ce_image = id_image) ". |
$where.' '.$conditions.' '.$ordre.' '. |
"LIMIT $navigation "; |
//die($requete); |
$this->requeteNbreNomsTotal = $this->transformerRequetePourNbreTermesTotal($requete); |
$resultats = $this->bdd->recupererTous($requete); |
return $resultats; |
} |
private function getClauseSelectSpeciale() { |
$clause = (Config::get('bdd_protocole') == 'mysql') ? 'SQL_CALC_FOUND_ROWS' : ''; |
return $clause; |
} |
private function getTableTerme() { |
return sprintf($this->getNomTableTpl(), 'terme'); |
} |
private function getTableType() { |
return sprintf($this->getNomTableTpl(), 'type'); |
} |
private function getTablePublication() { |
return sprintf($this->getNomTableTpl(), 'publication'); |
} |
private function getTableAuteur() { |
return sprintf($this->getNomTableTpl(), 'auteur'); |
} |
private function getTableImage() { |
return sprintf($this->getNomTableTpl(), 'image'); |
} |
private function getTableHierarchie() { |
return sprintf($this->getNomTableTpl(), 'hierarchie'); |
} |
private function getNomTableTpl() { |
$versions = $this->versions->getVersions(); |
$derniereVersion = end($versions); |
$projetNom = strtolower($this->ressources->getProjetNom()); |
return $projetNom.'_ontologies_%s_v'.$derniereVersion; |
} |
private function getConditions() { |
$operateurParDefaut = $this->getOperateurCondition(); |
$conditionsSql = array(); |
foreach ($this->paramsMasque as $typeMasque => $champ) { |
$operateur = in_array($typeMasque, $this->masquesStrictes) ? '=' : $operateurParDefaut; |
if ($valeurMasque = $this->parametres->getMasquePourBdd($typeMasque)) { |
if ($operateur == 'SOUNDEX') { |
$tpl = '(SOUNDEX(%s) = SOUNDEX(%s)) OR (SOUNDEX(REVERSE(%s)) = SOUNDEX(REVERSE(%s))) '; |
$conditionsSql[] = sprintf($tpl, $champ, $valeurMasque, $champ, $valeurMasque); |
} else { |
$conditionsSql[] = "$champ $operateur $valeurMasque"; |
} |
} |
} |
return implode(' AND ', $conditionsSql); |
} |
private function getOperateurCondition() { |
$operateur = ''; |
$recherche = $this->parametres->get('recherche'); |
if ($recherche == 'stricte') { |
$operateur = '='; |
} else if ($recherche == 'etendue') { |
$operateur = 'LIKE'; |
} else if ($recherche == 'floue') { |
$operateur = 'SOUNDEX'; |
} |
return $operateur; |
} |
private function getWhere($conditions = '') { |
$where = ''; |
if ($conditions != '') { |
$where = 'WHERE '; |
} |
return $where; |
} |
private function getOrdre() { |
$champNom = $this->getChampPourLangue('nom'); |
$ordre = "ORDER BY $champNom ASC "; |
return $ordre; |
} |
private function getChampPourLangue($champ) { |
$lg = $this->parametres->get('retour.langue'); |
if ($lg == 'en') { |
$champ .= '_en'; |
} |
return $champ; |
} |
private function getNavigation() { |
$debut = (int) $this->parametres->get('navigation.depart'); |
$nbre = $this->parametres->get('navigation.limite'); |
$navigation = "$debut,$nbre"; |
return $navigation; |
} |
private function transformerRequetePourNbreTermesTotal($requete) { |
$requete = preg_replace('/SELECT .* FROM/', 'SELECT COUNT(*) AS nbre FROM', $requete); |
$requete = preg_replace('/LIMIT [0-9]+,[0-9]+/', '', $requete); |
return $requete; |
} |
public function trierResultatsFloue($termes) { |
foreach ($this->paramsMasque as $typeMasque => $champ) { |
if ($termeDemande = $this->parametres->getMasquePourBdd($typeMasque)) { |
$termeDemandeSimple = strtolower(Chaine::supprimerAccents($termeDemande)); |
foreach ($termes as $id => $terme) { |
$termeFlouSimple = strtolower(Chaine::supprimerAccents($terme[$this->getChampPourLangue('nom')])); |
// Prime pour la ressemblance globale : |
$leven = levenshtein($termeFlouSimple, $termeDemandeSimple); |
// On affine |
$similar = similar_text($termeDemandeSimple, $termeFlouSimple) * 3; |
// Prime Soundex |
$soundex = (soundex($termeDemandeSimple) == soundex($termeFlouSimple)) ? 1000 : 0; |
// Calcul du score |
$score = 500 - $leven + $similar + $soundex; |
$termes[$id]['score'] = $score; |
$termes[$id]['score_calcul'] = "$termeDemandeSimple / $termeFlouSimple : 500 - $leven + $similar + $soundex = $score"; |
} |
$termes = Tableau::trierMD($termes, array('score' => SORT_DESC)); |
} |
} |
//echo 'ici<pre>'.print_r($termes, true).'</pre>';die(); |
return $termes; |
} |
public function recupererNombreTermesTotal() { |
if (Config::get('bdd_protocole') == 'mysql') { |
$requete = 'SELECT FOUND_ROWS() AS nbre'; |
} else { |
$requete = $this->requeteNbreNomsTotal; |
} |
$nombre = $this->bdd->recuperer($requete); |
return (int) $nombre['nbre']; |
} |
} |
?> |
/tags/v5.0-agropyraie-20130829/services/bibliotheque/ontologie/OntologieFormateur.php |
---|
New file |
0,0 → 1,89 |
<?php |
class OntologieFormateur { |
private $termeAFormater = null; |
private $decorateurs = array(); |
private $bdd = null; |
private $champsProjet = array(); |
private $champsRetour = null; |
private $detailsHrefTpl = null; |
private $langueDemandee = null; |
public function setTermeAFormater(OntologieDO $ontologieDO) { |
$this->termeAFormater = $ontologieDO; |
} |
public function setChampsRetour(Array $champsRetour) { |
$this->champsRetour = $champsRetour; |
} |
public function setChampsProjet(Array $champsProjet) { |
$this->champsProjet = $champsProjet; |
} |
public function setDetailsHrefTpl($tpl) { |
$this->detailsHrefTpl = $tpl; |
} |
public function setLangueDemandee($langue) { |
$this->langueDemandee = $langue; |
} |
public function setBdd($bdd) { |
$this->bdd = $bdd; |
} |
public function formaterDetails() { |
$termeDeco = new OntologieDecorateur($this->termeAFormater, $this->detailsHrefTpl, $this->langueDemandee); |
$projetDeco = new OntologieChampsProjetDecorateur($termeDeco, $this->champsProjet); |
if ($this->avoirDemandeChampsRetour()) { |
$this->decorateurs[] = $termeDeco; |
$this->decorateurs[] = $projetDeco; |
$this->traiterChampsRetour(); |
} else { |
$termeDeco->ajouterId(); |
$termeDeco->ajouterIntitule(); |
$termeDeco->ajouterDescription(); |
$termeDeco->ajouterClasseId(); |
$termeDeco->ajouterClasse(); |
$termeDeco->ajouterClasseHref(); |
$projetDeco->ajouterChampsSupplementaires(); |
} |
return $termeDeco->getTermeFormate(); |
} |
public function formaterListe() { |
$termeDeco = new OntologieDecorateur($this->termeAFormater, $this->detailsHrefTpl, $this->langueDemandee); |
$termeDeco->ajouterId(); |
$termeDeco->ajouterIntitule(); |
$termeDeco->ajouterHref(); |
if ($this->avoirDemandeChampsRetour()) { |
$this->decorateurs[] = $termeDeco; |
$this->decorateurs[] = new OntologieChampsProjetDecorateur($termeDeco, $this->champsProjet); |
$this->traiterChampsRetour(); |
} |
return $termeDeco->getTermeFormate(); |
} |
private function avoirDemandeChampsRetour() { |
$demande = true; |
if ($this->champsRetour === null || count($this->champsRetour) == 0) { |
$demande = false; |
} |
return $demande; |
} |
private function traiterChampsRetour() { |
foreach ($this->decorateurs as $deco) { |
$deco->traiterChampsRetour($this->champsRetour); |
} |
} |
} |
?> |