3 require_once('exalead.class.php');
5 function convert_url($string){
6 return str_replace('+', '%2B', $string);
9 $GLOBALS['query_all'] = 'a*';
18 var $currentCategories = array();
23 var $currentHitCategory;
25 var $currentTextSegment;
27 var $currentQueryTerm;
28 var $currentQueryParameter;
31 //url de base du produit Exalead
34 // Query to dump indexed database
37 /**** Constructeur *********/
40 function Exalead($base_cgi = '', $override_query_all = ''){
41 $this->data
= new ExaleadData();
42 $this->currentGroup
= new ExaleadGroup();
43 $this->currentCategories
= array();
44 $this->currentSpelling
= new ExaleadSpelling();
45 $this->currentHit
= new ExaleadHit();
46 $this->currentHitField
= new ExaleadHitField();
47 $this->currentHitGroup
= new ExaleadHitGroup();
48 $this->currentHitCategory
= new ExaleadHitCategory();
49 $this->currentAction
= new ExaleadAction();
50 $this->currentTextSegment
= new ExaleadTextSegment();
51 $this->currentQuery
= new ExaleadQuery();
52 $this->currentQueryTerm
= new ExaleadQueryTerm();
53 $this->currentQueryParameter
= new ExaleadQueryParameter();
54 $this->currentKeyword
= new ExaleadKeyword();
56 //url de base du produit Exalead
57 $this->base_cgi
= $base_cgi;
58 if(!empty($override_query_all)){
59 $this->query_all
= $override_query_all;
62 $this->query_all
= $GLOBALS['query_all'];
66 /**** Fonctions d'interface avec le cgi d'Exalead Corporate ******/
68 function set_base_cgi($base_cgi){
69 $this->base_cgi
= $base_cgi;
72 //retourne vrai si une requete a été faite, faux sinon
73 function query($varname = 'query'){
74 if(!empty($_REQUEST[$varname])){
76 $this->first_query(stripslashes($_REQUEST[$varname]));
79 elseif(isset($_REQUEST['_C'])){
81 $this->handle_request();
87 //a appeller pour faire la premiere requete
88 function first_query($query, $offset = 0){
89 if(empty($this->base_cgi
)) return false
;
91 //$tmp = parse_url($this->base_cgi);
92 //$view_name = substr($tmp['path'], 5);
93 //$query_exa = $this->base_cgi."?_q=".urlencode($query)."&_f=xml2&A=-1&_vn=".$view_name;
94 $query_exa = $this->base_cgi
."?_q=".urlencode($query)."&_f=xml2";
96 $query_exa .= "&_s=".$offset;
99 $xml_response = file_get_contents($query_exa);
100 /*$xml_response = '';
101 $query_explode = parse_url($query_exa);
103 $fp = fsockopen("murphy.m4x.org", 10000, $errno, $errstr, 30);
105 echo "$errstr ($errno)<br />\n";
107 $out = "GET {$query_explode['path']}?{$query_explode['query']} HTTP/1.1\r\n";
108 $out .= "Host: murphy.m4x.org:10000\r\n";
109 $out .= "Accept: text/xml\r\n";
110 $out .= "Accept-Charset: utf-8\r\n";
111 $out .= "Connection: Close\r\n\r\n";
116 $s = fgets($fp, 1024);
124 //echo $xml_response;exit;
125 $this->parse($xml_response);
129 //pour recuperer tous les résultats d'une base indexée
130 function get_db_dump(){
131 $this->first_query($this->query_all
);
134 function handle_request(){
135 if(empty($this->base_cgi
)) return false
;
136 if(empty($_REQUEST['_C'])) return false
;// _C est le contexte Exalead
137 $query_exa = $this->base_cgi
.'/_C='.str_replace(' ', '%20', $_REQUEST['_C']).'&_f=xml2';
138 if(!empty($_REQUEST['_s'])){
139 $query_exa .= "&_s=".((int) $_REQUEST['_s']);
141 $xml_response = file_get_contents($query_exa);
142 $this->parse($xml_response);
145 /******** Fonctions annexes relatives au parser ********/
147 function createParser(){
148 $this->parserId
= xml_parser_create();
149 xml_set_element_handler($this->parserId
, array(&$this, "startElement"), array(&$this, "endElement"));
150 xml_set_character_data_handler($this->parserId
, array(&$this, "parsePCData"));
153 function freeParser(){
154 xml_parser_free($this->parserId
);
157 function parseString($string){
158 if (!xml_parse($this->parserId
, $string, true
)) {
159 die(sprintf("XML error: %s at line %d",
160 xml_error_string(xml_get_error_code($this->parserId
)),
161 xml_get_current_line_number($this->parserId
)));
167 /******** Méthode qui lance le parser ***********/
169 function parse($string){
170 $this->createParser();
171 $this->parseString($string);
175 /********* fonctions spécifiques à chaque balise ******/
177 //Ces méthodes peuvent être surchargées
179 function startQuery(&$attrs){
180 $this->currentQuery
->query
= utf8_decode($attrs['QUERY']);
181 $this->currentQuery
->context
= $attrs['CONTEXT'];
182 $this->currentQuery
->time
= $attrs['TIME'];
183 if(isset($attrs['INTERRUPTED'])) $this->currentQuery
->interrupted
= $attrs['INTERRUPTED'];
184 if(isset($attrs['BROWSED'])) $this->currentQuery
->browsed
= $attrs['BROWSED'];
187 function StartQueryTerm(&$attrs){
188 $this->currentQueryTerm
->level
= $attrs['LEVEL'];
189 $this->currentQueryTerm
->regexp
= utf8_decode($attrs['REGEXP']);
192 function startQueryParameter(&$attrs){
193 $this->currentQueryParameter
->name
= $attrs['NAME'];
194 if(isset($attrs['VALUE'])) $this->currentQueryParameter
->value
= utf8_decode($attrs['VALUE']);
197 function startKeyword(&$attrs){
198 if(isset($attrs['NAME'])) $this->currentKeyword
->name
= $attrs['NAME'];
199 $this->currentKeyword
->display
= utf8_decode( $attrs['DISPLAY'] );
200 $this->currentKeyword
->count
= $attrs['COUNT'];
201 $this->currentKeyword
->automatic
= $attrs['AUTOMATIC'];
202 if(isset($attrs['REFINEHREF'])) $this->currentKeyword
->refine_href
= convert_url($attrs['REFINEHREF']);
203 if(isset($attrs['EXCLUDEHREF'])) $this->currentKeyword
->exclude_href
= $attrs['EXCLUDEHREF'];
204 if(isset($attrs['RESETHREF'])) $this->currentKeyword
->reset_href
= $attrs['RESETHREF'];
207 function startHits(&$attrs){
208 $this->data
->nmatches
= $attrs['NMATCHES'];
209 $this->data
->nhits
= $attrs['NHITS'];
210 if(isset($attrs['INTERRUPTED'])) $this->data
->interrupted
= $attrs['INTERRUPTED'];
211 $this->data
->last
= $attrs['LAST'];
212 $this->data
->end
= $attrs['END'];
213 $this->data
->start
= $attrs['START'];
216 function startHit(&$attrs){
217 $this->currentHit
->url
= $attrs['URL'];
218 $this->currentHit
->score
= $attrs['SCORE'];
221 function startHitGroup(&$attrs){
222 $this->currentHitGroup
->title
= utf8_decode($attrs['TITLE']);
223 $this->currentHitGroup
->gid
= $attrs['GID'];
226 function startHitCategory(&$attrs){
227 $this->currentHitCategory
->name
= $attrs['NAME'];
228 $this->currentHitCategory
->display
= utf8_decode($attrs['DISPLAY']);
229 $this->currentHitCategory
->cref
= $attrs['CREF'];
230 $this->currentHitCategory
->gid
= $attrs['GID'];
231 if(isset($attrs['BROWSEHREF'])) $this->currentHitCategory
->browsehref
= $attrs['BROWSEHREF'];
234 function startAction(&$attrs){
235 $this->currentAction
->display
= $attrs['DISPLAY'];
236 $this->currentAction
->kind
= $attrs['KIND'];
237 if(isset($attrs['EXECHREF']))$this->currentAction
->execHref
= $attrs['EXECHREF'];
240 function startHitField(&$attrs){
241 $this->currentHitField
->name
= $attrs['NAME'];
242 if(isset($attrs['VALUE'])) $this->currentHitField
->value
= utf8_decode($attrs['VALUE']);
245 function startTextSeg(&$attrs){
246 $this->currentTextSegment
->setHighlighted($attrs['HIGHLIGHTED']);
248 function startTextCut(&$attrs){}
250 function startSpellingSuggestionVariant(&$attrs){
251 $this->currentSpelling
->setDisplay($attrs['DISPLAY']);
252 $this->currentSpelling
->setQueryHref($attrs['QUERY']);
255 function startGroup(&$attrs){
256 $this->currentGroup
->setGid(utf8_decode($attrs['GID']));
257 $this->currentGroup
->setTitle(utf8_decode($attrs['TITLE']));
258 $this->currentGroup
->setClipped($attrs['CLIPPED']);
259 $this->currentGroup
->setCount($attrs['COUNT']);
260 $this->currentGroup
->setBrowsed($attrs['BROWSED']);
261 if(isset($attrs['CLIPHREF'])) $this->currentGroup
->setClipHref($attrs['CLIPHREF']);
262 if(isset($attrs['RESETHREF'])) $this->currentGroup
->setResetHref($attrs['RESETHREF']);
265 function startCategory(&$attrs){
266 $currentCategory = new ExaleadCategory();
267 $currentCategory->name
= utf8_decode($attrs['NAME']);
268 $currentCategory->display
= utf8_decode($attrs['DISPLAY']);
269 $currentCategory->count
= $attrs['COUNT'];
270 $currentCategory->automatic
= $attrs['AUTOMATIC'];
271 $currentCategory->cref
= $attrs['CREF'];
272 if(isset($attrs['REFINEHREF'])) $currentCategory->refine_href
= convert_url($attrs['REFINEHREF']);
273 $currentCategory->exclude_href
= '_c=-'.$currentCategory->cref
;
274 if(isset($attrs['RESETHREF'])){
275 $currentCategory->reset_href
= $attrs['RESETHREF'];
277 $currentCategory->gid
= $attrs['GID'];
278 $currentCategory->gcount
= $attrs['GCOUNT'];
279 $this->currentCategories
[] = $currentCategory;
282 function startSearch(&$attrs){}
284 function startElement($parser, $name, $attrs) {
285 //echo "start $name<br />";
286 //recupération des paramètres de query
287 if($name == 'QUERY'){
288 $this->startQuery($attrs);
290 elseif($name == 'QUERYTERM'){
291 $this->startQueryTerm($attrs);
293 elseif($name == 'QUERYPARAMETER'){
294 $this->startQueryParameter($attrs);
296 //gestion des mots-clés
297 elseif($name == 'KEYWORD'){
298 $this->startKeyword($attrs);
300 //gestion des resultats
301 elseif($name == 'HITS'){
302 $this->startHits($attrs);
304 elseif($name == 'HIT'){
305 $this->startHit($attrs);
307 elseif($name == 'HITFIELD'){
308 $this->startHitField($attrs);
310 elseif($name == 'HITGROUP'){
311 $this->startHitGroup($attrs);
313 elseif($name == 'HITCATEGORY'){
314 $this->startHitCategory($attrs);
316 elseif($name == 'ACTION'){
317 $this->startAction($attrs);
319 elseif($name == 'TEXTSEG'){
320 $this->startTextSeg($attrs);
322 elseif($name == 'TEXTCUT'){
323 $this->startTextCut($attrs);
325 //gestion suggestions d'orthographe
326 elseif($name == 'SPELLINGSUGGESTIONVARIANT'){
327 $this->startSpellingSuggestionVariant($attrs);
329 //gestion des categories pour raffiner
330 elseif($name == 'GROUP'){
331 $this->startGroup($attrs);
333 elseif($name == 'CATEGORY'){
334 $this->startCategory($attrs);
336 elseif($name == 'SEARCH'){
337 $this->startSearch($attrs);
342 $this->data
->query
= $this->currentQuery
;
343 $this->currentQuery
->clear();
345 function endQueryTerm(){
346 $this->currentQuery
->addTerm($this->currentQueryTerm
);
347 $this->currentQueryTerm
->clear();
349 function endQueryParameter(){
350 $this->currentQuery
->addParameter($this->currentQueryParameter
);
351 $this->currentQueryParameter
->clear();
353 function endKeyword(){
354 $this->data
->addKeyword($this->currentKeyword
);
355 $this->currentKeyword
->clear();
360 $this->data
->addHit($this->currentHit
);
361 $this->currentHit
->clear();
363 function endHitGroup(){
364 $this->currentHit
->addHitGroup($this->currentHitGroup
);
365 $this->currentHitGroup
->clear();
367 function endHitCategory(){
368 $this->currentHitGroup
->addHitCategory($this->currentHitCategory
);
369 $this->currentHitCategory
->clear();
371 function endAction(){
372 $this->currentHit
->addAction($this->currentAction
);
373 $this->currentAction
->clear();
375 function endHitField(){
376 $this->currentHit
->addHitField($this->currentHitField
);
377 $this->currentHitField
->clear();
379 function endTextSeg(){
380 $this->currentHitField
->addTextSegment($this->currentTextSegment
);
381 $this->currentTextSegment
->clear();
383 function endTextCut(){
384 $this->currentHitField
->setHasTextCut(true
);
386 function endSpellingSuggestionVariant(){
387 $this->data
->addSpelling($this->currentSpelling
);
388 $this->currentSpelling
->clear();
391 $this->data
->addGroup($this->currentGroup
);
392 $this->currentGroup
->clear();
394 function endCategory(){
395 //the parent element is a Group element ?
396 if(count($this->currentCategories
) == 1){
397 $this->currentGroup
->addCategory(array_pop($this->currentCategories
));
400 //var_dump($this->currentCategories);
401 $category = array_pop($this->currentCategories
);
402 //reset($this->currentCategories);
403 end($this->currentCategories
);
404 //var_dump($this->currentCategories);
405 $parentCategory = &$this->currentCategories
[key($this->currentCategories
)];
406 //var_dump($parentCategory);
407 $parentCategory->addCategory($category);
410 function endSearch(){
414 function endElement($parser, $name) {
415 //echo "end $name<br >";
416 if($name == 'QUERY'){
419 elseif($name == 'QUERYTERM'){
420 $this->endQueryTerm();
422 elseif($name == 'QUERYPARAMETER'){
423 $this->endQueryParameter();
425 elseif($name == 'KEYWORD'){
428 elseif($name == 'HITS'){
431 elseif($name == 'HIT'){
434 elseif($name == 'HITFIELD'){
435 $this->endHitField();
437 elseif($name == 'HITGROUP'){
438 $this->endHitGroup();
440 elseif($name == 'HITCATEGORY'){
441 $this->endHitCategory();
443 elseif($name == 'ACTION'){
446 elseif($name == 'TEXTSEG'){
449 elseif($name == 'TEXTCUT'){
452 //gestion suggestions d'orthographe
453 elseif($name == 'SPELLINGSUGGESTIONVARIANT'){
454 $this->endSpellingSuggestionVariant();
456 //gestion des categories pour raffiner
457 elseif($name == 'GROUP'){
460 elseif($name == 'CATEGORY'){
461 $this->endCategory();
463 elseif($name == 'SEARCH'){
468 function parsePCData($parser, $text){
469 $this->currentTextSegment
->append(utf8_decode($text));