webservice/library/Metier/sphinx2/rechercheFonc.php
2011-11-16 14:57:23 +00:00

207 lines
5.4 KiB
PHP

<?php
require_once realpath(dirname(__FILE__)) . '/sphinxapi.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)
{
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_ACT_HOST, SPHINX_ACT_PORT);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex('act');
$this->setDeb($deb);
$this->setNbRep($nbRep);
$this->setMax($max);
$this->_initForm($formR);
$this->setSphinx($sphinx);
$this->setSequance();
return ($this->execute($sphinx));
}
public function rechercheDir($formR, $deb, $nbRep, $max)
{
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_DIR_HOST, SPHINX_DIR_PORT);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex('dir');
$this->setDeb($deb);
$this->setNbRep($nbRep);
$this->setMax($max);
$this->_initForm($formR);
$this->setSphinx($sphinx);
$this->setSequance();
return ($this->execute($sphinx));
}
public function rechercheEnt($formR, $deb, $nbRep, $max, $sirenValide = false)
{
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_ENT_HOST, SPHINX_ENT_PORT);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex('ent');
$this->setDeb($deb);
$this->setNbRep($nbRep);
$this->setMax($max);
$this->_initForm($formR);
$this->setSphinx($sphinx);
$this->setSequance();
return ($this->execute($sphinx));
}
public function rechercheHisto($formR, $deb, $nbRep, $max)
{
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_ENT_HOST, SPHINX_ENT_PORT);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$this->setIndex('histo');
$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;
}
}
?>