enrichissement/library/Metier/sphinx2/rechercheFonc.php
2012-01-13 13:35:11 +00:00

201 lines
5.3 KiB
PHP

<?php
require_once realpath(dirname(__FILE__)).'/configuration.php';
require_once realpath(dirname(__FILE__)).'/SDmotor.class.php';
Class rechercheFonc extends SDmotor
{
public function __construct($debug = false) {
$this->debug = $debug;
}
public function rechercheAct($formR, $deb, $nbRep, $max)
{
require_once 'sphinxapi/sphinxapi-2.0.2.php';
$sphinx = new SphinxClient();
$sphinx->SetServer(ConfigurationAct::$sphinx_host, ConfigurationAct::$sphinx_port);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex(ConfigurationAct::$index);
$this->setDeb($deb);
$this->_initForm($formR);
$this->setNumberResults($nbRep);
$this->setMaxResults($max);
$this->setSphinx($sphinx);
return ($this->execute($sphinx));
}
public function rechercheDir($formR, $deb, $nbRep, $max)
{
require_once 'sphinxapi/sphinxapi-2.0.2.php';
$sphinx = new SphinxClient();
$sphinx->SetServer(ConfigurationDir::$sphinx_host, ConfigurationDir::$sphinx_port);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex(ConfigurationDir::$index);
$this->setDeb($deb);
$this->_initForm($formR);
$this->setNumberResults($nbRep);
$this->setMaxResults($max);
$this->setSphinx($sphinx);
return ($this->execute($sphinx));
}
public function rechercheEnt($formR, $deb, $nbRep, $max, $sirenValide = false)
{
require_once 'sphinxapi/sphinxapi-2.0.2.php';
$sphinx = new SphinxClient();
$sphinx->SetServer(ConfigurationEnt::$sphinx_host, ConfigurationEnt::$sphinx_port);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex(ConfigurationEnt::$index);
$this->setDeb($deb);
$this->_initForm($formR);
$this->setNumberResults($nbRep);
$this->setMaxResults($max);
$this->setSphinx($sphinx);
return ($this->execute($sphinx));
}
public function rechercheHisto($formR, $deb, $nbRep, $max)
{
$sphinx = new SphinxClient();
$sphinx->SetServer(ConfigurationHisto::$sphinx_host, ConfigurationHisto::$sphinx_port);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex(ConfigurationHisto::$index);
$this->setNumberResults($nbRep);
$this->setMaxResults($max);
$this->setSphinx($sphinx);
return ($this->HistoSearch());
}
public function recherche($formR, $deb = 0, $nbRep = 20, $max = 1000)
{
if ($formR['type'] == 'ent') {
return $this->rechercheEnt($formR, $deb, $nbRep, $max);
} else if ($formR['type'] == 'dir') {
return $this->rechercheDir($formR, $deb, $nbRep, $max);
} else if ($formR['type'] == 'act') {
return $this->rechercheAct($formR, $deb, $nbRep, $max);
} else {
return array('nbReponses' => 0,
'nbReponsesTotal' => 0,
'duree' => 0,
'erreur' => 'Type de recherche inconnu');
}
}
public function sommeDeControle($sn)
{
$s = 0;
$n = strlen($sn);
for ($i = 0; $i < $n; ++$i) {
if ($i & 1) {
$t = substr($sn, $n - $i - 1, 1) << 1;
$s += ($t > 9)? $t - 9 : $t;
} else {
$s += substr($sn, $n - $i - 1, 1);
}
}
return $s % 10;
}
public function verificationDuSiret($siret)
{
if (strlen($siret) == 14) {
$sn = substr($siret, 0, 9);
$st = $siret;
} else if (strlen($siret) == 9) {
$sn = $siret;
$st = 0;
} else {
// TODO: message d'erreur et/ou correction
return false;
}
// Verification du siren
$s = sommeDeControle($sn);
if ($s != 0) {
// Correction du chiffre de controle
$sr = (substr($sn, 8, 1) - $s + 10) % 10;
$liste = array(substr($sn, 0, 8).$sr);
// Transpositions
for ($i = 0; $i < 8; ++$i) {
$n1 = substr($sn, $i, 1);
$n2 = substr($sn, $i + 1, 1);
if ($n1 == $n2) {
continue;
}
$p = '';
if ($i > 0) {
$p .= substr($sn, 0, $i);
}
$p .= $n2;
if ($i < 7) {
$p .= $n1;
}
if ($i < 6) {
$p .= substr($sn, $i + 2, 6 - $i);
}
if ($i & 1) {
if ($i < 7) {
if ($n1 >= 5) { $n1 -= 9; }
if ($n2 >= 5) { $n2 -= 9; }
} else {
$n1 = ($n1 >= 5) ? ($n1 << 1) - 9 : ($n1 << 1);
$n2 = ($n2 >= 5) ? ($n2 << 1) - 9 : ($n2 << 1);
}
$p .= ($sr + $n1 - $n2 + 10) % 10;
} else {
if ($n2 >= 5) { $n2 -= 9; }
if ($n1 >= 5) { $n1 -= 9; }
$p .= ($sr + $n2 - $n1 + 10) % 10;
}
$liste[] = $p;
}
// Substitutions
for ($i = 0; $i < 8; ++$i) {
$n = substr($sn, $i, 1);
for ($j = 0; $j < 10; ++$j) {
if ($j == $n) {
continue;
}
$p = '';
if ($i > 0) {
$p .= substr($sn, 0, $i);
}
$p .= $j;
if ($i < 7) {
$p .= substr($sn, $i + 1, 7 - $i);
}
if ($i & 1) {
$n1 = ($n << 1); if ($n1 > 9) { $n1 -= 9; }
$n2 = ($j << 1); if ($n2 > 9) { $n2 -= 9; }
$p .= ($sr + $n1 - $n2 + 10) % 10;
} else {
$p .= ($sr + $n - $j + 10) % 10;
}
$liste[] = $p;
}
}
return $liste;
}
if ($st == 0) {
return false;
}
// Verification du siret
if (sommeDeControle($st) != 0) {
return array($sn);
}
return false;
}
}
?>