cfe5796a6fab86eaf200f23054548c17b00ae47c
3 require_once('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['_s'])) {
120 $query_exa .= "&_s=".((int) $_REQUEST['_s']);
122 $xml_response = file_get_contents($query_exa);
123 $this->parse($xml_response);
126 /******** Fonctions annexes relatives au parser ********/
128 function createParser()
130 $this->parserId
= xml_parser_create();
131 xml_set_element_handler($this->parserId
, array(&$this, "startElement"), array(&$this, "endElement"));
132 xml_set_character_data_handler($this->parserId
, array(&$this, "parsePCData"));
135 function freeParser()
137 xml_parser_free($this->parserId
);
140 function parseString($string)
142 if (!xml_parse($this->parserId
, $string, true
)) {
143 die(sprintf("XML error: %s at line %d",
144 xml_error_string(xml_get_error_code($this->parserId
)),
145 xml_get_current_line_number($this->parserId
)));
151 /******** Méthode qui lance le parser ***********/
153 function parse($string)
155 $this->createParser();
156 $this->parseString($string);
160 /********* fonctions spécifiques à chaque balise ******/
162 //Ces méthodes peuvent être surchargées
164 function startQuery(&$attrs)
166 $this->currentQuery
->query
= utf8_decode($attrs['QUERY']);
167 $this->currentQuery
->context
= $attrs['CONTEXT'];
168 $this->currentQuery
->time
= $attrs['TIME'];
169 if (isset($attrs['INTERRUPTED'])) $this->currentQuery
->interrupted
= $attrs['INTERRUPTED'];
170 if (isset($attrs['BROWSED'])) $this->currentQuery
->browsed
= $attrs['BROWSED'];
173 function StartQueryTerm(&$attrs)
175 $this->currentQueryTerm
->level
= $attrs['LEVEL'];
176 $this->currentQueryTerm
->regexp
= utf8_decode($attrs['REGEXP']);
179 function startQueryParameter(&$attrs)
181 $this->currentQueryParameter
->name
= $attrs['NAME'];
182 if (isset($attrs['VALUE'])) $this->currentQueryParameter
->value
= utf8_decode($attrs['VALUE']);
185 function startKeyword(&$attrs)
187 if (isset($attrs['NAME'])) $this->currentKeyword
->name
= $attrs['NAME'];
188 $this->currentKeyword
->display
= utf8_decode( $attrs['DISPLAY'] );
189 $this->currentKeyword
->count
= $attrs['COUNT'];
190 $this->currentKeyword
->automatic
= $attrs['AUTOMATIC'];
191 if (isset($attrs['REFINEHREF'])) $this->currentKeyword
->refine_href
= convert_url($attrs['REFINEHREF']);
192 if (isset($attrs['EXCLUDEHREF'])) $this->currentKeyword
->exclude_href
= $attrs['EXCLUDEHREF'];
193 if (isset($attrs['RESETHREF'])) $this->currentKeyword
->reset_href
= $attrs['RESETHREF'];
196 function startHits(&$attrs)
198 $this->data
->nmatches
= $attrs['NMATCHES'];
199 $this->data
->nhits
= $attrs['NHITS'];
200 if (isset($attrs['INTERRUPTED'])) $this->data
->interrupted
= $attrs['INTERRUPTED'];
201 $this->data
->last
= $attrs['LAST'];
202 $this->data
->end
= $attrs['END'];
203 $this->data
->start
= $attrs['START'];
206 function startHit(&$attrs)
208 $this->currentHit
->url
= $attrs['URL'];
209 $this->currentHit
->score
= $attrs['SCORE'];
212 function startHitGroup(&$attrs)
214 $this->currentHitGroup
->title
= utf8_decode($attrs['TITLE']);
215 $this->currentHitGroup
->gid
= $attrs['GID'];
218 function startHitCategory(&$attrs)
220 $this->currentHitCategory
->name
= $attrs['NAME'];
221 $this->currentHitCategory
->display
= utf8_decode($attrs['DISPLAY']);
222 $this->currentHitCategory
->cref
= $attrs['CREF'];
223 $this->currentHitCategory
->gid
= $attrs['GID'];
224 if (isset($attrs['BROWSEHREF'])) $this->currentHitCategory
->browsehref
= $attrs['BROWSEHREF'];
227 function startAction(&$attrs)
229 $this->currentAction
->display
= $attrs['DISPLAY'];
230 $this->currentAction
->kind
= $attrs['KIND'];
231 if (isset($attrs['EXECHREF']))$this->currentAction
->execHref
= $attrs['EXECHREF'];
234 function startHitField(&$attrs)
236 $this->currentHitField
->name
= $attrs['NAME'];
237 if (isset($attrs['VALUE'])) $this->currentHitField
->value
= utf8_decode($attrs['VALUE']);
240 function startTextSeg(&$attrs)
242 $this->currentTextSegment
->setHighlighted($attrs['HIGHLIGHTED']);
244 function startTextCut(&$attrs) { }
246 function startSpellingSuggestionVariant(&$attrs)
248 $this->currentSpelling
->setDisplay($attrs['DISPLAY']);
249 $this->currentSpelling
->setQueryHref($attrs['QUERY']);
252 function startGroup(&$attrs)
254 $this->currentGroup
->setGid(utf8_decode($attrs['GID']));
255 $this->currentGroup
->setTitle(utf8_decode($attrs['TITLE']));
256 $this->currentGroup
->setClipped($attrs['CLIPPED']);
257 $this->currentGroup
->setCount($attrs['COUNT']);
258 $this->currentGroup
->setBrowsed($attrs['BROWSED']);
259 if (isset($attrs['CLIPHREF'])) $this->currentGroup
->setClipHref($attrs['CLIPHREF']);
260 if (isset($attrs['RESETHREF'])) $this->currentGroup
->setResetHref($attrs['RESETHREF']);
263 function startCategory(&$attrs)
265 $currentCategory = new ExaleadCategory();
266 $currentCategory->name
= utf8_decode($attrs['NAME']);
267 $currentCategory->display
= utf8_decode($attrs['DISPLAY']);
268 $currentCategory->count
= $attrs['COUNT'];
269 $currentCategory->automatic
= $attrs['AUTOMATIC'];
270 $currentCategory->cref
= $attrs['CREF'];
271 if (isset($attrs['REFINEHREF'])) $currentCategory->refine_href
= convert_url($attrs['REFINEHREF']);
272 $currentCategory->exclude_href
= '_c=-'.$currentCategory->cref
;
273 if (isset($attrs['RESETHREF'])) {
274 $currentCategory->reset_href
= $attrs['RESETHREF'];
276 $currentCategory->gid
= $attrs['GID'];
277 $currentCategory->gcount
= $attrs['GCOUNT'];
278 $this->currentCategories
[] = $currentCategory;
281 function startSearch(&$attrs) { }
283 function startElement($parser, $name, $attrs)
285 //recupération des paramètres de query
288 $this->startQuery($attrs);
291 $this->startQueryTerm($attrs);
293 case 'QUERYPARAMETER':
294 $this->startQueryParameter($attrs);
297 $this->startKeyword($attrs);
300 $this->startHits($attrs);
303 $this->startHit($attrs);
306 $this->startHitField($attrs);
309 $this->startHitGroup($attrs);
312 $this->startHitCategory($attrs);
315 $this->startAction($attrs);
318 $this->startTextSeg($attrs);
321 $this->startTextCut($attrs);
323 case 'SPELLINGSUGGESTIONVARIANT':
324 $this->startSpellingSuggestionVariant($attrs);
327 $this->startGroup($attrs);
330 $this->startCategory($attrs);
333 $this->startSearch($attrs);
340 $this->data
->query
= $this->currentQuery
;
341 $this->currentQuery
->clear();
343 function endQueryTerm()
345 $this->currentQuery
->addTerm($this->currentQueryTerm
);
346 $this->currentQueryTerm
->clear();
348 function endQueryParameter()
350 $this->currentQuery
->addParameter($this->currentQueryParameter
);
351 $this->currentQueryParameter
->clear();
353 function endKeyword()
355 $this->data
->addKeyword($this->currentKeyword
);
356 $this->currentKeyword
->clear();
363 $this->data
->addHit($this->currentHit
);
364 $this->currentHit
->clear();
366 function endHitGroup()
368 $this->currentHit
->addHitGroup($this->currentHitGroup
);
369 $this->currentHitGroup
->clear();
371 function endHitCategory()
373 $this->currentHitGroup
->addHitCategory($this->currentHitCategory
);
374 $this->currentHitCategory
->clear();
378 $this->currentHit
->addAction($this->currentAction
);
379 $this->currentAction
->clear();
381 function endHitField()
383 $this->currentHit
->addHitField($this->currentHitField
);
384 $this->currentHitField
->clear();
386 function endTextSeg()
388 $this->currentHitField
->addTextSegment($this->currentTextSegment
);
389 $this->currentTextSegment
->clear();
391 function endTextCut()
393 $this->currentHitField
->setHasTextCut(true
);
395 function endSpellingSuggestionVariant()
397 $this->data
->addSpelling($this->currentSpelling
);
398 $this->currentSpelling
->clear();
402 $this->data
->addGroup($this->currentGroup
);
403 $this->currentGroup
->clear();
405 function endCategory()
407 //the parent element is a Group element ?
408 if (count($this->currentCategories
) == 1) {
409 $this->currentGroup
->addCategory(array_pop($this->currentCategories
));
411 //var_dump($this->currentCategories);
412 $category = array_pop($this->currentCategories
);
413 //reset($this->currentCategories);
414 end($this->currentCategories
);
415 //var_dump($this->currentCategories);
416 $parentCategory = &$this->currentCategories
[key($this->currentCategories
)];
417 //var_dump($parentCategory);
418 $parentCategory->addCategory($category);
421 function endSearch() { }
424 function endElement($parser, $name)
431 $this->endQueryTerm();
433 case 'QUERYPARAMETER':
434 $this->endQueryParameter();
446 $this->endHitField();
449 $this->endHitGroup();
452 $this->endHitCategory();
463 //gestion suggestions d'orthographe
464 case 'SPELLINGSUGGESTIONVARIANT':
465 $this->endSpellingSuggestionVariant();
467 //gestion des categories pour raffiner
472 $this->endCategory();
480 function parsePCData($parser, $text)
482 $this->currentTextSegment
->append(utf8_decode($text));