1 |
<?php |
2 |
/* |
3 |
+----------------------------------------------------------------------+ |
4 |
| Classe Swish | |
5 |
+----------------------------------------------------------------------+ |
6 |
| php4 - 2001 | |
7 |
+----------------------------------------------------------------------+ |
8 |
| La classe Swish permet d'interroger swish-e | |
9 |
| Cette classe supporte le passage des nombreux arguments de | |
10 |
| swish-e et retourne un tableau de résultats ainsi que diverses | |
11 |
| informations. | |
12 |
+----------------------------------------------------------------------+ |
13 |
| Auteur: Olivier Meunier | |
14 |
| Version: 1.1 | |
15 |
+----------------------------------------------------------------------+ |
16 |
*/ |
17 |
|
18 |
class swish |
19 |
{ |
20 |
/** |
21 |
* Déclarations des proriétés |
22 |
*/ |
23 |
|
24 |
var $str_engine = "/data/swish/swish-e"; //Ligne de commande |
25 |
var $str_index_file; |
26 |
|
27 |
var $str_separator = '@@@@@'; |
28 |
|
29 |
var $words; |
30 |
var $get_params; |
31 |
var $sort_params; |
32 |
var $first_result; |
33 |
var $num_results; |
34 |
|
35 |
var $number_results; |
36 |
|
37 |
var $search_time; |
38 |
var $run_time; |
39 |
|
40 |
//Les noms des champs titre, url et score sont modifiables |
41 |
var $titre_ligne = "TITLE"; |
42 |
var $url_ligne = "URL"; |
43 |
var $score_ligne = "SCORE"; |
44 |
var $nr = "NR"; |
45 |
var $err = FALSE; |
46 |
|
47 |
/** |
48 |
* Constructeur |
49 |
* |
50 |
* @str_index_file string Chemin du fichier d'index |
51 |
* @str_engine string Chemin pour le moteur |
52 |
* |
53 |
* return void |
54 |
*/ |
55 |
function swish($str_index_file,$str_engine="") |
56 |
{ |
57 |
$this->str_index_file = $str_index_file; |
58 |
|
59 |
if ($str_engine != "") |
60 |
$this->str_engine = $str_engine; |
61 |
} |
62 |
|
63 |
|
64 |
/** |
65 |
* Méthode: set_params |
66 |
* |
67 |
* @words string Chaine recherchée |
68 |
* @get_params array Tableau des paramètres |
69 |
* @sort_params string Paramètre de tri |
70 |
* @first_result integer Indice du premier résultat |
71 |
* @num_resultse integer Nombre max de résultats |
72 |
* |
73 |
* return void |
74 |
*/ |
75 |
function set_params($words, $get_params=array(), $sort_params="", $first_result="", $num_results="") |
76 |
{ |
77 |
$this->words = $words; |
78 |
$this->get_params = $get_params; |
79 |
$this->sort_params = $sort_params; |
80 |
$this->first_result = $first_result; |
81 |
$this->num_results = $num_results; |
82 |
} |
83 |
|
84 |
|
85 |
/** |
86 |
* Méthode: exec_swish |
87 |
* |
88 |
* return void |
89 |
*/ |
90 |
function exec_swish() |
91 |
{ |
92 |
//Prépare la ligne de commande |
93 |
$cmd = $this->str_engine." ". |
94 |
' -f '.$this->str_index_file. |
95 |
' -w "'.escapeshellcmd($this->words).'"'. |
96 |
' -d '.$this->str_separator; |
97 |
|
98 |
//Ajout du paramètre -p si il y a des paramètres |
99 |
if(count($this->get_params) > 0) |
100 |
{ |
101 |
$ligne_params = implode(" ",$this->get_params); |
102 |
$cmd .= " -p ".$ligne_params; |
103 |
} |
104 |
|
105 |
//Ajout du paramètre de tri s'il existe |
106 |
if($this->sort_params != "") { |
107 |
$cmd .= " -s ".$this->sort_params; |
108 |
} |
109 |
|
110 |
//Ajout du paramètre -b pour démarrer au résultat n |
111 |
if($this->first_result != "") { |
112 |
$cmd .= " -b ".$this->first_result; |
113 |
} |
114 |
|
115 |
//Ajout du paramètre -m pour s'arrêter à n lignes |
116 |
if($this->num_results != "") { |
117 |
$cmd .= " -m ".$this->num_results; |
118 |
} |
119 |
|
120 |
//La commande est prete, on l'éxécute |
121 |
$this->cmd = $cmd; |
122 |
exec($cmd,$this->arry_swish); |
123 |
//Le résultat est stockée dans $this->arry_swish |
124 |
} |
125 |
|
126 |
|
127 |
/** |
128 |
* Traitement du résultat |
129 |
* |
130 |
* return void |
131 |
*/ |
132 |
function make_result() |
133 |
{ |
134 |
$i=0; |
135 |
|
136 |
//On passe en revue chaque ligne du tableau |
137 |
foreach($this->arry_swish as $value) |
138 |
{ |
139 |
//Si on trouve une ligne qui commence par "err", on arrête tout et |
140 |
//on initialise la propriété $err |
141 |
if(ereg("^err",$value)) |
142 |
{ |
143 |
$this->err = TRUE; |
144 |
if(ereg("^err: no results$",$value)) { |
145 |
$this->number_results = -1; |
146 |
$this->search_time = 0; |
147 |
$this->run_time = 0; |
148 |
} |
149 |
break 1; |
150 |
} |
151 |
|
152 |
//Dans les lignes d'info, on récupère le nombre de résultats |
153 |
if(ereg("^# Number of hits: ([0-9]*)",$value,$Tnb)) { |
154 |
$this->number_results = $Tnb[1]; |
155 |
} |
156 |
|
157 |
if(ereg("^# Search time: ([0-9\.]*)",$value,$Tnb)) { |
158 |
$this->search_time = $Tnb[1]; |
159 |
} |
160 |
|
161 |
if(ereg("^# Run time: ([0-9\.]*)",$value,$Tnb)) { |
162 |
$this->run_time = $Tnb[1]; |
163 |
} |
164 |
|
165 |
//Ligne de résultats |
166 |
if(!ereg("^[.#]",$value)) |
167 |
{ |
168 |
//On passe en tableau tous les champs |
169 |
$arry_tmp = explode($this->str_separator,$value); |
170 |
|
171 |
//On récupère le score, l'url et le titre |
172 |
$arry_int[$this->score_ligne] = $arry_tmp[0]; |
173 |
$arry_int[$this->url_ligne] = $arry_tmp[1]; |
174 |
$arry_int[$this->titre_ligne] = $arry_tmp[2]; |
175 |
$arry_int['DOCSIZE'] = $arry_tmp[3]; |
176 |
$arry_int[$this->nr] = $this->first_result+$i+1; |
177 |
|
178 |
//Traitement des propriétés |
179 |
reset($this->get_params); |
180 |
for($j=4; $j<count($arry_tmp); $j++) |
181 |
{ |
182 |
$arry_int[key($this->get_params)] = $arry_tmp[$j]; |
183 |
next($this->get_params); |
184 |
} |
185 |
$this->arry_res[$i] = $arry_int; |
186 |
|
187 |
$i++; |
188 |
} |
189 |
} |
190 |
} |
191 |
|
192 |
|
193 |
/** |
194 |
* Execution complète |
195 |
* |
196 |
* return array Tableau associatif de résultats |
197 |
*/ |
198 |
function get_result() |
199 |
{ |
200 |
$this->exec_swish(); |
201 |
$this->make_result(); |
202 |
return $this->arry_res; |
203 |
} |
204 |
|
205 |
}//Fin de la classe |
206 |
?> |