2011-03-07 17:16:53 +00:00
< ? php
require_once 'framework/common/strings.php' ;
require_once realpath ( dirname ( __FILE__ )) . '/sphinxapi.php' ;
class SphinxCriteres
{
var $findMe = '' ;
var $tabFiltres = array ();
var $position = 0 ;
var $nbRep = 20 ;
var $max = 200 ;
var $any = false ;
}
class SphinxSearch
{
/**
* Effectue une recherche en interrogeant le moteur Sphinx
*
* @ param string $index
* @ param SphinxCriteres $criteres Critères de recherche
* @ return array $return Tableau des résultats
*/
2011-07-06 07:17:24 +00:00
public function search ( $index , $criteres )
2011-03-07 17:16:53 +00:00
{
debugLog ( 'I' , " Search Sphinx dans $name de " . $criteres -> findMe . " ( " . $criteres -> deb . " , " . $criteres -> nbRep . " , " . $criteres -> max . " ) avec " . implode ( ',' , $criteres -> tabFiltres ), __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
$cl = new SphinxClient ();
2011-07-06 07:17:24 +00:00
switch ( strtolower ( $index )) {
2011-03-07 17:16:53 +00:00
case 'entrep' :
break ;
case 'dir' :
break ;
case 'histo' :
$cl -> SetServer ( SPHINX_HISTO_HOST , SPHINX_HISTO_PORT );
$cl -> SetConnectTimeout ( 1 );
$cl -> SetLimits ( $criteres -> position , $criteres -> nbRep , $criteres -> max );
$cl -> SetMatchMode ( SPH_MATCH_EXTENDED );
foreach ( $criteres -> tabFiltres as $nomFiltre => $valFiltre ) {
$cl -> SetFilter ( $nomFiltre , array ( 0 => $valFiltre ));
}
$cl -> SetRankingMode ( SPH_RANK_PROXIMITY_BM25 );
$res = $cl -> Query ( $criteres -> findMe , 'histo' );
if ( $res === false ) {
//Erreur
debugLog ( 'I' , " Search Sphinx : Pas de réponse pour " . $criteres -> findMe . " avec " . implode ( ',' , $criteres -> tabFiltres ) . ' (' . $cl -> GetLastError () . ')' , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
return array (
'results' => false ,
'nbRet' => 0 ,
'nbTot' => 0 ,
'duration' => 0 );
} else {
if ( $cl -> GetLastWarning () ) {
debugLog ( 'I' , " Search Sphinx : Warning pour $criteres->findMe - " . $cl -> GetLastWarning (), __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
print " WARNING: " . $cl -> GetLastWarning () . " \n \n " ;
}
2011-07-06 07:17:24 +00:00
debugLog ( 'I' , " 'Search Sphinx dans $index de $criteres->findMe (Filtre= " . implode ( ',' , $criteres -> tabFiltres ) . " ), Deb= " . $criteres -> position . " , nbRep= " . $criteres -> nbRep . " , max= " . $criteres -> max . " , any= " . $criteres -> any , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
2011-03-07 17:16:53 +00:00
$tabRet = array ();
if ( is_array ( $res [ 'matches' ])) {
$iDb = new WDB ( 'histobodacc' );
$iDb -> query ( " SET NAMES 'latin1'; " );
foreach ( $res [ 'matches' ] as $doc => $docinfo ) {
$listeEtab = $iDb -> select (
'bodacc_ocr' ,
" 'Histo' as Loc, id, nomFichier, annee1, bod, texte " ,
" id= $doc " );
$etab = $listeEtab [ 0 ];
$tabRet [] = array (
'Localisation' => $etab [ 'Loc' ],
'id' => $doc ,
'Pertinence' => $docinfo [ 'weight' ],
'Fichier' => $etab [ 'nomFichier' ],
'Annee' => $etab [ 'annee1' ],
'Code' => $etab [ 'bod' ],
'Texte' => $etab [ 'texte' ],
);
}
}
}
debugLog ( 'I' , 'Search Sphinx : Retourne ' . $res [ total ] . '/' . $res [ total_found ] . ' en ' . $res [ time ] . 'secondes' , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
return array (
'results' => $tabRet ,
'nbRet' => $res [ total ],
'nbTot' => $res [ total_found ],
'duration' => $res [ time ],
'words' => $res [ 'words' ],
);
break ;
}
}
/**
* Méthode magique __call () permettant d ' appeller une méthode virtuelle
* du type searchByEnt (), searchByDir () ou searchByHisto () ...
*
* @ param string $method Nom de la méthode virtuelle appelée
* @ param array $args Tableau des critères de recherche
* @ return array | null $return Tableau des résultats ou NULL
*/
public function __call ( $method , $args )
{
if ( preg_match ( '#^searchBy#i' , $method ))
{
$name = str_replace ( 'searchBy' , '' , $method );
$criteres = array (
0 => 'findMe' ,
1 => 'tabFiltres' ,
2 => 'position' ,
3 => 'nbRep' ,
4 => 'max' ,
5 => 'any' ,
);
$searchArgs = new stdClass ();
$i = 0 ;
foreach ( $args as $argument ) {
$searchArgs -> { $criteres [ $i ]} = $argument ;
$i ++ ;
}
return $this -> search ( $name , $searchArgs );
}
return null ;
}
}