data = new ExaleadData(); $this->currentGroup = new ExaleadGroup(); $this->currentCategories = array(); $this->currentSpelling = new ExaleadSpelling(); $this->currentHit = new ExaleadHit(); $this->currentHitField = new ExaleadHitField(); $this->currentHitGroup = new ExaleadHitGroup(); $this->currentHitCategory = new ExaleadHitCategory(); $this->currentAction = new ExaleadAction(); $this->currentTextSegment = new ExaleadTextSegment(); $this->currentQuery = new ExaleadQuery(); $this->currentQueryTerm = new ExaleadQueryTerm(); $this->currentQueryParameter = new ExaleadQueryParameter(); $this->currentKeyword = new ExaleadKeyword(); //url de base du produit Exalead $this->base_cgi = $base_cgi; if (!empty($override_query_all)) { $this->query_all = $override_query_all; } else { $this->query_all = $GLOBALS['query_all']; } } /**** Fonctions d'interface avec le cgi d'Exalead Corporate ******/ function set_base_cgi($base_cgi) { $this->base_cgi = $base_cgi; } //retourne vrai si une requete a été faite, faux sinon function query($varname = 'query') { if (!empty($_REQUEST[$varname])) { $this->first_query(stripslashes($_REQUEST[$varname])); return true; } elseif (isset($_REQUEST['_C'])) { $this->handle_request(); return true; } return false; } //a appeller pour faire la premiere requete function first_query($query, $offset = 0) { if (empty($this->base_cgi)) return false; //$tmp = parse_url($this->base_cgi); //$view_name = substr($tmp['path'], 5); //$query_exa = $this->base_cgi."?_q=".urlencode($query)."&_f=xml2&A=-1&_vn=".$view_name; $query_exa = $this->base_cgi."?_q=".urlencode($query)."&_f=xml2"; if ($offset > 0) { $query_exa .= "&_s=".$offset; } $xml_response = file_get_contents($query_exa); $this->parse($xml_response); } //pour recuperer tous les résultats d'une base indexée function get_db_dump() { $this->first_query($this->query_all); } function handle_request() { if (empty($this->base_cgi)) return false; if (empty($_REQUEST['_C'])) return false;// _C est le contexte Exalead $query_exa = $this->base_cgi.'&_C='.str_replace(' ', '%20', $_REQUEST['_C']).'&_f=xml2'; if (!empty($_REQUEST['_c'])) { $query_exa .= "&_c=".urlencode($_REQUEST['_c']); } if (!empty($_REQUEST['_s'])) { $query_exa .= "&_s=".((int) $_REQUEST['_s']); } if (isset($_REQUEST['_z'])) { $query_exa .= "&_z=".$_REQUEST['_z']; } $xml_response = file_get_contents($query_exa); $this->parse($xml_response); } /******** Fonctions annexes relatives au parser ********/ function createParser() { $this->parserId = xml_parser_create(); xml_set_element_handler($this->parserId, array(&$this, "startElement"), array(&$this, "endElement")); xml_set_character_data_handler($this->parserId, array(&$this, "parsePCData")); } function freeParser() { xml_parser_free($this->parserId); } function parseString($string) { if (!xml_parse($this->parserId, $string, true)) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parserId)), xml_get_current_line_number($this->parserId))); } } /******** Méthode qui lance le parser ***********/ function parse($string) { $this->createParser(); $this->parseString($string); $this->freeParser(); } /********* fonctions spécifiques à chaque balise ******/ //Ces méthodes peuvent être surchargées function startQuery(&$attrs) { $this->currentQuery->query = utf8_decode($attrs['QUERY']); $this->currentQuery->context = $attrs['CONTEXT']; $this->currentQuery->time = $attrs['TIME']; if (isset($attrs['INTERRUPTED'])) $this->currentQuery->interrupted = $attrs['INTERRUPTED']; if (isset($attrs['BROWSED'])) $this->currentQuery->browsed = $attrs['BROWSED']; } function StartQueryTerm(&$attrs) { $this->currentQueryTerm->level = $attrs['LEVEL']; $this->currentQueryTerm->regexp = utf8_decode($attrs['REGEXP']); } function startQueryParameter(&$attrs) { $this->currentQueryParameter->name = $attrs['NAME']; if (isset($attrs['VALUE'])) $this->currentQueryParameter->value = utf8_decode($attrs['VALUE']); } function startKeyword(&$attrs) { if (isset($attrs['NAME'])) $this->currentKeyword->name = $attrs['NAME']; $this->currentKeyword->display = utf8_decode( $attrs['DISPLAY'] ); $this->currentKeyword->count = $attrs['COUNT']; $this->currentKeyword->automatic = $attrs['AUTOMATIC']; if (isset($attrs['REFINEHREF'])) $this->currentKeyword->refine_href = convert_url($attrs['REFINEHREF']); if (isset($attrs['EXCLUDEHREF'])) $this->currentKeyword->exclude_href = $attrs['EXCLUDEHREF']; if (isset($attrs['RESETHREF'])) $this->currentKeyword->reset_href = $attrs['RESETHREF']; } function startHits(&$attrs) { $this->data->nmatches = $attrs['NMATCHES']; $this->data->nhits = $attrs['NHITS']; if (isset($attrs['INTERRUPTED'])) $this->data->interrupted = $attrs['INTERRUPTED']; $this->data->last = $attrs['LAST']; $this->data->end = $attrs['END']; $this->data->start = $attrs['START']; } function startHit(&$attrs) { $this->currentHit->url = $attrs['URL']; $this->currentHit->score = $attrs['SCORE']; } function startHitGroup(&$attrs) { $this->currentHitGroup->title = utf8_decode($attrs['TITLE']); $this->currentHitGroup->gid = $attrs['GID']; } function startHitCategory(&$attrs) { $this->currentHitCategory->name = $attrs['NAME']; $this->currentHitCategory->display = utf8_decode($attrs['DISPLAY']); $this->currentHitCategory->cref = $attrs['CREF']; $this->currentHitCategory->gid = $attrs['GID']; if (isset($attrs['BROWSEHREF'])) $this->currentHitCategory->browsehref = $attrs['BROWSEHREF']; } function startAction(&$attrs) { $this->currentAction->display = $attrs['DISPLAY']; $this->currentAction->kind = $attrs['KIND']; if (isset($attrs['EXECHREF']))$this->currentAction->execHref = $attrs['EXECHREF']; } function startHitField(&$attrs) { $this->currentHitField->name = $attrs['NAME']; if (isset($attrs['VALUE'])) $this->currentHitField->value = utf8_decode($attrs['VALUE']); } function startTextSeg(&$attrs) { $this->currentTextSegment->setHighlighted($attrs['HIGHLIGHTED']); } function startTextCut(&$attrs) { } function startSpellingSuggestionVariant(&$attrs) { $this->currentSpelling->setDisplay($attrs['DISPLAY']); $this->currentSpelling->setQueryHref($attrs['QUERY']); } function startGroup(&$attrs) { $this->currentGroup->setGid(utf8_decode($attrs['GID'])); $this->currentGroup->setTitle(utf8_decode($attrs['TITLE'])); $this->currentGroup->setClipped($attrs['CLIPPED']); $this->currentGroup->setCount($attrs['COUNT']); $this->currentGroup->setBrowsed($attrs['BROWSED']); if (isset($attrs['CLIPHREF'])) $this->currentGroup->setClipHref($attrs['CLIPHREF']); if (isset($attrs['RESETHREF'])) $this->currentGroup->setResetHref($attrs['RESETHREF']); } function startCategory(&$attrs) { $currentCategory = new ExaleadCategory(); $currentCategory->name = utf8_decode($attrs['NAME']); $currentCategory->display = utf8_decode($attrs['DISPLAY']); $currentCategory->count = $attrs['COUNT']; $currentCategory->automatic = $attrs['AUTOMATIC']; $currentCategory->cref = $attrs['CREF']; if (isset($attrs['REFINEHREF'])) $currentCategory->refine_href = convert_url($attrs['REFINEHREF']); $currentCategory->exclude_href = '_c=-'.$currentCategory->cref; if (isset($attrs['RESETHREF'])) { $currentCategory->reset_href = $attrs['RESETHREF']; } $currentCategory->gid = $attrs['GID']; $currentCategory->gcount = $attrs['GCOUNT']; $this->currentCategories[] = $currentCategory; } function startSearch(&$attrs) { } function startElement($parser, $name, $attrs) { //recupération des paramètres de query switch ($name) { case 'QUERY': $this->startQuery($attrs); break; case 'QUERYTERM': $this->startQueryTerm($attrs); break; case 'QUERYPARAMETER': $this->startQueryParameter($attrs); break; case 'KEYWORD': $this->startKeyword($attrs); break; case 'HITS': $this->startHits($attrs); break; case 'HIT': $this->startHit($attrs); break; case 'HITFIELD': $this->startHitField($attrs); break; case 'HITGROUP': $this->startHitGroup($attrs); break; case 'HITCATEGORY': $this->startHitCategory($attrs); break; case 'ACTION': $this->startAction($attrs); break; case 'TEXTSEG': $this->startTextSeg($attrs); break; case 'TEXTCUT': $this->startTextCut($attrs); break; case 'SPELLINGSUGGESTIONVARIANT': $this->startSpellingSuggestionVariant($attrs); break; case 'GROUP': $this->startGroup($attrs); break; case 'CATEGORY': $this->startCategory($attrs); break; case 'SEARCH': $this->startSearch($attrs); break; } } function endQuery() { $this->data->query = $this->currentQuery; $this->currentQuery->clear(); } function endQueryTerm() { $this->currentQuery->addTerm($this->currentQueryTerm); $this->currentQueryTerm->clear(); } function endQueryParameter() { $this->currentQuery->addParameter($this->currentQueryParameter); $this->currentQueryParameter->clear(); } function endKeyword() { $this->data->addKeyword($this->currentKeyword); $this->currentKeyword->clear(); } function endHits() { } function endHit() { $this->data->addHit($this->currentHit); $this->currentHit->clear(); } function endHitGroup() { $this->currentHit->addHitGroup($this->currentHitGroup); $this->currentHitGroup->clear(); } function endHitCategory() { $this->currentHitGroup->addHitCategory($this->currentHitCategory); $this->currentHitCategory->clear(); } function endAction() { $this->currentHit->addAction($this->currentAction); $this->currentAction->clear(); } function endHitField() { $this->currentHit->addHitField($this->currentHitField); $this->currentHitField->clear(); } function endTextSeg() { $this->currentHitField->addTextSegment($this->currentTextSegment); $this->currentTextSegment->clear(); } function endTextCut() { $this->currentHitField->setHasTextCut(true); } function endSpellingSuggestionVariant() { $this->data->addSpelling($this->currentSpelling); $this->currentSpelling->clear(); } function endGroup() { $this->data->addGroup($this->currentGroup); $this->currentGroup->clear(); } function endCategory() { //the parent element is a Group element ? if (count($this->currentCategories) == 1) { $this->currentGroup->addCategory(array_pop($this->currentCategories)); } else { //var_dump($this->currentCategories); $category = array_pop($this->currentCategories); //reset($this->currentCategories); end($this->currentCategories); //var_dump($this->currentCategories); $parentCategory = &$this->currentCategories[key($this->currentCategories)]; //var_dump($parentCategory); $parentCategory->addCategory($category); } } function endSearch() { } function endElement($parser, $name) { switch ($name) { case 'QUERY': $this->endQuery(); break; case 'QUERYTERM': $this->endQueryTerm(); break; case 'QUERYPARAMETER': $this->endQueryParameter(); break; case 'KEYWORD': $this->endKeyword(); break; case 'HITS': $this->endHits(); break; case 'HIT': $this->endHit(); break; case 'HITFIELD': $this->endHitField(); break; case 'HITGROUP': $this->endHitGroup(); break; case 'HITCATEGORY': $this->endHitCategory(); break; case 'ACTION': $this->endAction(); break; case 'TEXTSEG': $this->endTextSeg(); break; case 'TEXTCUT': $this->endTextCut(); break; //gestion suggestions d'orthographe case 'SPELLINGSUGGESTIONVARIANT': $this->endSpellingSuggestionVariant(); break; //gestion des categories pour raffiner case 'GROUP': $this->endGroup(); break; case 'CATEGORY': $this->endCategory(); break; case 'SEARCH': $this->endSearch(); break; } } function parsePCData($parser, $text) { $this->currentTextSegment->append(utf8_decode($text)); } } ?>