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 $xml_response = file_get_contents($query_exa);
129 $this->parse($xml_response);
132 /******** Fonctions annexes relatives au parser ********/
134 function createParser()
136 $this->parserId
= xml_parser_create();
137 xml_set_element_handler($this->parserId
, array(&$this, "startElement"), array(&$this, "endElement"));
138 xml_set_character_data_handler($this->parserId
, array(&$this, "parsePCData"));
141 function freeParser()
143 xml_parser_free($this->parserId
);
146 function parseString($string)
148 if (!xml_parse($this->parserId
, $string, true
)) {
149 die(sprintf("XML error: %s at line %d",
150 xml_error_string(xml_get_error_code($this->parserId
)),
151 xml_get_current_line_number($this->parserId
)));
157 /******** Méthode qui lance le parser ***********/
159 function parse($string)
161 $this->createParser();
162 $this->parseString($string);
166 /********* fonctions spécifiques à chaque balise ******/
168 //Ces méthodes peuvent être surchargées
170 function startQuery(&$attrs)
172 $this->currentQuery
->query
= utf8_decode($attrs['QUERY']);
173 $this->currentQuery
->context
= $attrs['CONTEXT'];
174 $this->currentQuery
->time
= $attrs['TIME'];
175 if (isset($attrs['INTERRUPTED'])) $this->currentQuery
->interrupted
= $attrs['INTERRUPTED'];
176 if (isset($attrs['BROWSED'])) $this->currentQuery
->browsed
= $attrs['BROWSED'];
179 function StartQueryTerm(&$attrs)
181 $this->currentQueryTerm
->level
= $attrs['LEVEL'];
182 $this->currentQueryTerm
->regexp
= utf8_decode($attrs['REGEXP']);
185 function startQueryParameter(&$attrs)
187 $this->currentQueryParameter
->name
= $attrs['NAME'];
188 if (isset($attrs['VALUE'])) $this->currentQueryParameter
->value
= utf8_decode($attrs['VALUE']);
191 function startKeyword(&$attrs)
193 if (isset($attrs['NAME'])) $this->currentKeyword
->name
= $attrs['NAME'];
194 $this->currentKeyword
->display
= utf8_decode( $attrs['DISPLAY'] );
195 $this->currentKeyword
->count
= $attrs['COUNT'];
196 $this->currentKeyword
->automatic
= $attrs['AUTOMATIC'];
197 if (isset($attrs['REFINEHREF'])) $this->currentKeyword
->refine_href
= convert_url($attrs['REFINEHREF']);
198 if (isset($attrs['EXCLUDEHREF'])) $this->currentKeyword
->exclude_href
= $attrs['EXCLUDEHREF'];
199 if (isset($attrs['RESETHREF'])) $this->currentKeyword
->reset_href
= $attrs['RESETHREF'];
202 function startHits(&$attrs)
204 $this->data
->nmatches
= $attrs['NMATCHES'];
205 $this->data
->nhits
= $attrs['NHITS'];
206 if (isset($attrs['INTERRUPTED'])) $this->data
->interrupted
= $attrs['INTERRUPTED'];
207 $this->data
->last
= $attrs['LAST'];
208 $this->data
->end
= $attrs['END'];
209 $this->data
->start
= $attrs['START'];
212 function startHit(&$attrs)
214 $this->currentHit
->url
= $attrs['URL'];
215 $this->currentHit
->score
= $attrs['SCORE'];
218 function startHitGroup(&$attrs)
220 $this->currentHitGroup
->title
= utf8_decode($attrs['TITLE']);
221 $this->currentHitGroup
->gid
= $attrs['GID'];
224 function startHitCategory(&$attrs)
226 $this->currentHitCategory
->name
= $attrs['NAME'];
227 $this->currentHitCategory
->display
= utf8_decode($attrs['DISPLAY']);
228 $this->currentHitCategory
->cref
= $attrs['CREF'];
229 $this->currentHitCategory
->gid
= $attrs['GID'];
230 if (isset($attrs['BROWSEHREF'])) $this->currentHitCategory
->browsehref
= $attrs['BROWSEHREF'];
233 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)
242 $this->currentHitField
->name
= $attrs['NAME'];
243 if (isset($attrs['VALUE'])) $this->currentHitField
->value
= utf8_decode($attrs['VALUE']);
246 function startTextSeg(&$attrs)
248 $this->currentTextSegment
->setHighlighted($attrs['HIGHLIGHTED']);
250 function startTextCut(&$attrs) { }
252 function startSpellingSuggestionVariant(&$attrs)
254 $this->currentSpelling
->setDisplay($attrs['DISPLAY']);
255 $this->currentSpelling
->setQueryHref($attrs['QUERY']);
258 function startGroup(&$attrs)
260 $this->currentGroup
->setGid(utf8_decode($attrs['GID']));
261 $this->currentGroup
->setTitle(utf8_decode($attrs['TITLE']));
262 $this->currentGroup
->setClipped($attrs['CLIPPED']);
263 $this->currentGroup
->setCount($attrs['COUNT']);
264 $this->currentGroup
->setBrowsed($attrs['BROWSED']);
265 if (isset($attrs['CLIPHREF'])) $this->currentGroup
->setClipHref($attrs['CLIPHREF']);
266 if (isset($attrs['RESETHREF'])) $this->currentGroup
->setResetHref($attrs['RESETHREF']);
269 function startCategory(&$attrs)
271 $currentCategory = new ExaleadCategory();
272 $currentCategory->name
= utf8_decode($attrs['NAME']);
273 $currentCategory->display
= utf8_decode($attrs['DISPLAY']);
274 $currentCategory->count
= $attrs['COUNT'];
275 $currentCategory->automatic
= $attrs['AUTOMATIC'];
276 $currentCategory->cref
= $attrs['CREF'];
277 if (isset($attrs['REFINEHREF'])) $currentCategory->refine_href
= convert_url($attrs['REFINEHREF']);
278 $currentCategory->exclude_href
= '_c=-'.$currentCategory->cref
;
279 if (isset($attrs['RESETHREF'])) {
280 $currentCategory->reset_href
= $attrs['RESETHREF'];
282 $currentCategory->gid
= $attrs['GID'];
283 $currentCategory->gcount
= $attrs['GCOUNT'];
284 $this->currentCategories
[] = $currentCategory;
287 function startSearch(&$attrs) { }
289 function startElement($parser, $name, $attrs)
291 //recupération des paramètres de query
294 $this->startQuery($attrs);
297 $this->startQueryTerm($attrs);
299 case 'QUERYPARAMETER':
300 $this->startQueryParameter($attrs);
303 $this->startKeyword($attrs);
306 $this->startHits($attrs);
309 $this->startHit($attrs);
312 $this->startHitField($attrs);
315 $this->startHitGroup($attrs);
318 $this->startHitCategory($attrs);
321 $this->startAction($attrs);
324 $this->startTextSeg($attrs);
327 $this->startTextCut($attrs);
329 case 'SPELLINGSUGGESTIONVARIANT':
330 $this->startSpellingSuggestionVariant($attrs);
333 $this->startGroup($attrs);
336 $this->startCategory($attrs);
339 $this->startSearch($attrs);
346 $this->data
->query
= $this->currentQuery
;
347 $this->currentQuery
->clear();
349 function endQueryTerm()
351 $this->currentQuery
->addTerm($this->currentQueryTerm
);
352 $this->currentQueryTerm
->clear();
354 function endQueryParameter()
356 $this->currentQuery
->addParameter($this->currentQueryParameter
);
357 $this->currentQueryParameter
->clear();
359 function endKeyword()
361 $this->data
->addKeyword($this->currentKeyword
);
362 $this->currentKeyword
->clear();
369 $this->data
->addHit($this->currentHit
);
370 $this->currentHit
->clear();
372 function endHitGroup()
374 $this->currentHit
->addHitGroup($this->currentHitGroup
);
375 $this->currentHitGroup
->clear();
377 function endHitCategory()
379 $this->currentHitGroup
->addHitCategory($this->currentHitCategory
);
380 $this->currentHitCategory
->clear();
384 $this->currentHit
->addAction($this->currentAction
);
385 $this->currentAction
->clear();
387 function endHitField()
389 $this->currentHit
->addHitField($this->currentHitField
);
390 $this->currentHitField
->clear();
392 function endTextSeg()
394 $this->currentHitField
->addTextSegment($this->currentTextSegment
);
395 $this->currentTextSegment
->clear();
397 function endTextCut()
399 $this->currentHitField
->setHasTextCut(true
);
401 function endSpellingSuggestionVariant()
403 $this->data
->addSpelling($this->currentSpelling
);
404 $this->currentSpelling
->clear();
408 $this->data
->addGroup($this->currentGroup
);
409 $this->currentGroup
->clear();
411 function endCategory()
413 //the parent element is a Group element ?
414 if (count($this->currentCategories
) == 1) {
415 $this->currentGroup
->addCategory(array_pop($this->currentCategories
));
417 //var_dump($this->currentCategories);
418 $category = array_pop($this->currentCategories
);
419 //reset($this->currentCategories);
420 end($this->currentCategories
);
421 //var_dump($this->currentCategories);
422 $parentCategory = &$this->currentCategories
[key($this->currentCategories
)];
423 //var_dump($parentCategory);
424 $parentCategory->addCategory($category);
427 function endSearch() { }
430 function endElement($parser, $name)
437 $this->endQueryTerm();
439 case 'QUERYPARAMETER':
440 $this->endQueryParameter();
452 $this->endHitField();
455 $this->endHitGroup();
458 $this->endHitCategory();
469 //gestion suggestions d'orthographe
470 case 'SPELLINGSUGGESTIONVARIANT':
471 $this->endSpellingSuggestionVariant();
473 //gestion des categories pour raffiner
478 $this->endCategory();
486 function parsePCData($parser, $text)
488 $this->currentTextSegment
->append(utf8_decode($text));