3 require_once(dirname(__FILE__
).'/exalead.class.php');
5 function convert_url($string)
7 return str_replace('+', '%2B', $string);
10 $GLOBALS['query_all'] = 'a*';
20 var $currentCategories = array();
25 var $currentHitCategory;
27 var $currentTextSegment;
29 var $currentQueryTerm;
30 var $currentQueryParameter;
33 //url de base du produit Exalead
36 // Query to dump indexed database
39 /**** Constructeur *********/
42 function Exalead($base_cgi = '', $override_query_all = '')
44 $this->data
= new ExaleadData();
45 $this->currentGroup
= new ExaleadGroup();
46 $this->currentCategories
= array();
47 $this->currentSpelling
= new ExaleadSpelling();
48 $this->currentHit
= new ExaleadHit();
49 $this->currentHitField
= new ExaleadHitField();
50 $this->currentHitGroup
= new ExaleadHitGroup();
51 $this->currentHitCategory
= new ExaleadHitCategory();
52 $this->currentAction
= new ExaleadAction();
53 $this->currentTextSegment
= new ExaleadTextSegment();
54 $this->currentQuery
= new ExaleadQuery();
55 $this->currentQueryTerm
= new ExaleadQueryTerm();
56 $this->currentQueryParameter
= new ExaleadQueryParameter();
57 $this->currentKeyword
= new ExaleadKeyword();
59 //url de base du produit Exalead
60 $this->base_cgi
= $base_cgi;
61 if (!empty($override_query_all)) {
62 $this->query_all
= $override_query_all;
64 $this->query_all
= $GLOBALS['query_all'];
68 /**** Fonctions d'interface avec le cgi d'Exalead Corporate ******/
70 function set_base_cgi($base_cgi)
72 $this->base_cgi
= $base_cgi;
75 //retourne vrai si une requete a été faite, faux sinon
76 function query($varname = 'query')
78 if (!empty($_REQUEST[$varname])) {
80 $this->first_query(stripslashes($_REQUEST[$varname]));
83 } elseif (isset($_REQUEST['_C'])) {
85 $this->handle_request();
91 //a appeller pour faire la premiere requete
92 function first_query($query, $offset = 0)
94 if (empty($this->base_cgi
)) return false
;
96 //$tmp = parse_url($this->base_cgi);
97 //$view_name = substr($tmp['path'], 5);
98 //$query_exa = $this->base_cgi."?_q=".urlencode($query)."&_f=xml2&A=-1&_vn=".$view_name;
99 $query_exa = $this->base_cgi
."?_q=".urlencode($query)."&_f=xml2";
101 $query_exa .= "&_s=".$offset;
104 $xml_response = file_get_contents($query_exa);
105 $this->parse($xml_response);
108 //pour recuperer tous les résultats d'une base indexée
109 function get_db_dump()
111 $this->first_query($this->query_all
);
114 function handle_request()
116 if (empty($this->base_cgi
)) return false
;
117 if (empty($_REQUEST['_C'])) return false
;// _C est le contexte Exalead
118 $query_exa = $this->base_cgi
.'&_C='.str_replace(' ', '%20', $_REQUEST['_C']).'&_f=xml2';
119 if (!empty($_REQUEST['_c'])) {
120 $query_exa .= "&_c=".urlencode($_REQUEST['_c']);
122 if (!empty($_REQUEST['_s'])) {
123 $query_exa .= "&_s=".((int) $_REQUEST['_s']);
125 if (isset($_REQUEST['_z'])) {
126 $query_exa .= "&_z=".$_REQUEST['_z'];
128 if (isset($_REQUEST['_sf'])) {
129 $query_exa .= "&_sf=".$_REQUEST['_sf'];
131 $xml_response = file_get_contents($query_exa);
132 $this->parse($xml_response);
135 /******** Fonctions annexes relatives au parser ********/
137 function createParser()
139 $this->parserId
= xml_parser_create();
140 xml_set_element_handler($this->parserId
, array(&$this, "startElement"), array(&$this, "endElement"));
141 xml_set_character_data_handler($this->parserId
, array(&$this, "parsePCData"));
144 function freeParser()
146 xml_parser_free($this->parserId
);
149 function parseString($string)
151 if (!xml_parse($this->parserId
, $string, true
)) {
152 die(sprintf("XML error: %s at line %d",
153 xml_error_string(xml_get_error_code($this->parserId
)),
154 xml_get_current_line_number($this->parserId
)));
160 /******** Méthode qui lance le parser ***********/
162 function parse($string)
164 $this->createParser();
165 $this->parseString($string);
169 /********* fonctions spécifiques à chaque balise ******/
171 //Ces méthodes peuvent être surchargées
173 function startQuery(&$attrs)
175 $this->currentQuery
->query
= utf8_decode($attrs['QUERY']);
176 $this->currentQuery
->context
= $attrs['CONTEXT'];
177 $this->currentQuery
->time
= $attrs['TIME'];
178 if (isset($attrs['INTERRUPTED'])) $this->currentQuery
->interrupted
= $attrs['INTERRUPTED'];
179 if (isset($attrs['BROWSED'])) $this->currentQuery
->browsed
= $attrs['BROWSED'];
182 function StartQueryTerm(&$attrs)
184 $this->currentQueryTerm
->level
= $attrs['LEVEL'];
185 $this->currentQueryTerm
->regexp
= utf8_decode($attrs['REGEXP']);
188 function startQueryParameter(&$attrs)
190 $this->currentQueryParameter
->name
= $attrs['NAME'];
191 if (isset($attrs['VALUE'])) $this->currentQueryParameter
->value
= utf8_decode($attrs['VALUE']);
194 function startKeyword(&$attrs)
196 if (isset($attrs['NAME'])) $this->currentKeyword
->name
= $attrs['NAME'];
197 $this->currentKeyword
->display
= utf8_decode( $attrs['DISPLAY'] );
198 $this->currentKeyword
->count
= $attrs['COUNT'];
199 $this->currentKeyword
->automatic
= $attrs['AUTOMATIC'];
200 if (isset($attrs['REFINEHREF'])) $this->currentKeyword
->refine_href
= convert_url($attrs['REFINEHREF']);
201 if (isset($attrs['EXCLUDEHREF'])) $this->currentKeyword
->exclude_href
= $attrs['EXCLUDEHREF'];
202 if (isset($attrs['RESETHREF'])) $this->currentKeyword
->reset_href
= $attrs['RESETHREF'];
205 function startHits(&$attrs)
207 $this->data
->nmatches
= $attrs['NMATCHES'];
208 $this->data
->nhits
= $attrs['NHITS'];
209 if (isset($attrs['INTERRUPTED'])) $this->data
->interrupted
= $attrs['INTERRUPTED'];
210 $this->data
->last
= $attrs['LAST'];
211 $this->data
->end
= $attrs['END'];
212 $this->data
->start
= $attrs['START'];
215 function startHit(&$attrs)
217 $this->currentHit
->url
= $attrs['URL'];
218 $this->currentHit
->score
= $attrs['SCORE'];
221 function startHitGroup(&$attrs)
223 $this->currentHitGroup
->title
= utf8_decode($attrs['TITLE']);
224 $this->currentHitGroup
->gid
= $attrs['GID'];
227 function startHitCategory(&$attrs)
229 $this->currentHitCategory
->name
= $attrs['NAME'];
230 $this->currentHitCategory
->display
= utf8_decode($attrs['DISPLAY']);
231 $this->currentHitCategory
->cref
= $attrs['CREF'];
232 $this->currentHitCategory
->gid
= $attrs['GID'];
233 if (isset($attrs['BROWSEHREF'])) $this->currentHitCategory
->browsehref
= $attrs['BROWSEHREF'];
236 function startAction(&$attrs)
238 $this->currentAction
->display
= $attrs['DISPLAY'];
239 $this->currentAction
->kind
= $attrs['KIND'];
240 if (isset($attrs['EXECHREF']))$this->currentAction
->execHref
= $attrs['EXECHREF'];
243 function startHitField(&$attrs)
245 $this->currentHitField
->name
= $attrs['NAME'];
246 if (isset($attrs['VALUE'])) $this->currentHitField
->value
= utf8_decode($attrs['VALUE']);
249 function startTextSeg(&$attrs)
251 $this->currentTextSegment
->setHighlighted($attrs['HIGHLIGHTED']);
253 function startTextCut(&$attrs) { }
255 function startSpellingSuggestionVariant(&$attrs)
257 $this->currentSpelling
->setDisplay($attrs['DISPLAY']);
258 $this->currentSpelling
->setQueryHref($attrs['QUERY']);
261 function startGroup(&$attrs)
263 $this->currentGroup
->setGid(utf8_decode($attrs['GID']));
264 $this->currentGroup
->setTitle(utf8_decode($attrs['TITLE']));
265 $this->currentGroup
->setClipped($attrs['CLIPPED']);
266 $this->currentGroup
->setCount($attrs['COUNT']);
267 $this->currentGroup
->setBrowsed($attrs['BROWSED']);
268 if (isset($attrs['CLIPHREF'])) $this->currentGroup
->setClipHref($attrs['CLIPHREF']);
269 if (isset($attrs['RESETHREF'])) $this->currentGroup
->setResetHref($attrs['RESETHREF']);
272 function startCategory(&$attrs)
274 $currentCategory = new ExaleadCategory();
275 $currentCategory->name
= utf8_decode($attrs['NAME']);
276 $currentCategory->display
= utf8_decode($attrs['DISPLAY']);
277 $currentCategory->count
= $attrs['COUNT'];
278 $currentCategory->automatic
= $attrs['AUTOMATIC'];
279 $currentCategory->cref
= $attrs['CREF'];
280 if (isset($attrs['REFINEHREF'])) $currentCategory->refine_href
= convert_url($attrs['REFINEHREF']);
281 $currentCategory->exclude_href
= '_c=-'.$currentCategory->cref
;
282 if (isset($attrs['RESETHREF'])) {
283 $currentCategory->reset_href
= $attrs['RESETHREF'];
285 $currentCategory->gid
= $attrs['GID'];
286 $currentCategory->gcount
= $attrs['GCOUNT'];
287 $this->currentCategories
[] = $currentCategory;
290 function startSearch(&$attrs) { }
292 function startElement($parser, $name, $attrs)
294 //recupération des paramètres de query
297 $this->startQuery($attrs);
300 $this->startQueryTerm($attrs);
302 case 'QUERYPARAMETER':
303 $this->startQueryParameter($attrs);
306 $this->startKeyword($attrs);
309 $this->startHits($attrs);
312 $this->startHit($attrs);
315 $this->startHitField($attrs);
318 $this->startHitGroup($attrs);
321 $this->startHitCategory($attrs);
324 $this->startAction($attrs);
327 $this->startTextSeg($attrs);
330 $this->startTextCut($attrs);
332 case 'SPELLINGSUGGESTIONVARIANT':
333 $this->startSpellingSuggestionVariant($attrs);
336 $this->startGroup($attrs);
339 $this->startCategory($attrs);
342 $this->startSearch($attrs);
349 $this->data
->query
= $this->currentQuery
;
350 $this->currentQuery
->clear();
352 function endQueryTerm()
354 $this->currentQuery
->addTerm($this->currentQueryTerm
);
355 $this->currentQueryTerm
->clear();
357 function endQueryParameter()
359 $this->currentQuery
->addParameter($this->currentQueryParameter
);
360 $this->currentQueryParameter
->clear();
362 function endKeyword()
364 $this->data
->addKeyword($this->currentKeyword
);
365 $this->currentKeyword
->clear();
372 $this->data
->addHit($this->currentHit
);
373 $this->currentHit
->clear();
375 function endHitGroup()
377 $this->currentHit
->addHitGroup($this->currentHitGroup
);
378 $this->currentHitGroup
->clear();
380 function endHitCategory()
382 $this->currentHitGroup
->addHitCategory($this->currentHitCategory
);
383 $this->currentHitCategory
->clear();
387 $this->currentHit
->addAction($this->currentAction
);
388 $this->currentAction
->clear();
390 function endHitField()
392 $this->currentHit
->addHitField($this->currentHitField
);
393 $this->currentHitField
->clear();
395 function endTextSeg()
397 $this->currentHitField
->addTextSegment($this->currentTextSegment
);
398 $this->currentTextSegment
->clear();
400 function endTextCut()
402 $this->currentHitField
->setHasTextCut(true
);
404 function endSpellingSuggestionVariant()
406 $this->data
->addSpelling($this->currentSpelling
);
407 $this->currentSpelling
->clear();
411 $this->data
->addGroup($this->currentGroup
);
412 $this->currentGroup
->clear();
414 function endCategory()
416 //the parent element is a Group element ?
417 if (count($this->currentCategories
) == 1) {
418 $this->currentGroup
->addCategory(array_pop($this->currentCategories
));
420 //var_dump($this->currentCategories);
421 $category = array_pop($this->currentCategories
);
422 //reset($this->currentCategories);
423 end($this->currentCategories
);
424 //var_dump($this->currentCategories);
425 $parentCategory = &$this->currentCategories
[key($this->currentCategories
)];
426 //var_dump($parentCategory);
427 $parentCategory->addCategory($category);
430 function endSearch() { }
433 function endElement($parser, $name)
440 $this->endQueryTerm();
442 case 'QUERYPARAMETER':
443 $this->endQueryParameter();
455 $this->endHitField();
458 $this->endHitGroup();
461 $this->endHitCategory();
472 //gestion suggestions d'orthographe
473 case 'SPELLINGSUGGESTIONVARIANT':
474 $this->endSpellingSuggestionVariant();
476 //gestion des categories pour raffiner
481 $this->endCategory();
489 function parsePCData($parser, $text)
491 $this->currentTextSegment
->append(utf8_decode($text));