/** * Helper for solr raw searches. * Deals with the language, meta_installation, section and visibility filters + solr sharding if any * @param array $params * @param string $requestType * @param bool $useDefaultFilters * @return array */ public static function rawSearch( $params, $requestType = 'php', $useDefaultFilters = true, $includeIsInvisible = true ) { eZDebug::accumulatorStart( __CLASS__ . '::' . __FUNCTION__, 'Merck' ); $findINI = eZINI::instance( 'ezfind.ini' ); $solrINI = eZINI::instance( 'solr.ini' ); $siteINI = eZINI::instance(); $currentLanguage = $siteINI->variable( 'RegionalSettings', 'ContentObjectLocale' ); // always use extended Dismax query handler when available if( isset($params['qt']) && $params['qt'] == 'ezpublish' ) $params['defType'] = 'edismax'; if ( $useDefaultFilters ) { if ( !isset( $params['fq'] ) ) $params['fq'] = ''; else $params['fq'] .= ' AND '; $params['fq'] .= implode( ' AND ', array( 'meta_installation_id_ms:' . eZSolr::installationID(), '(attr_offline_date_dt:"1970-01-01T01:00:00Z" OR attr_offline_date_dt:[NOW TO *])', '( meta_section_id_si:1 OR meta_section_id_si:3 )', ) ); if ($includeIsInvisible) { $params['fq'] .= ' AND ' . 'attr_is_invisible_' . ClusterTool::clusterIdentifier() . '_b:false'; } } if ( $findINI->variable( 'LanguageSearch', 'MultiCore' ) == 'enabled' ) { $languageMapping = $findINI->variable( 'LanguageSearch', 'LanguagesCoresMap' ); $shardMapping = $solrINI->variable( 'SolrBase', 'Shards' ); $fullSolrURI = $shardMapping[$languageMapping[$currentLanguage]]; } else { $fullSolrURI = $solrINI->variable( 'SolrBase', 'SearchServerURI' ); // Autocomplete search should be done in current language and fallback languages $validLanguages = array_unique( array_merge( LocaleTool::languageList(), array( $currentLanguage ) ) ); if( $useDefaultFilters ) $params['fq'] .= ' AND meta_language_code_ms:(' . implode( ' OR ', $validLanguages ) . ')'; } solrTool::solrStopWordsFilter( $params ); //excluding stopwords self::parseBooleanOperators( $params ); // translations for bookean operators $solrBase = new eZSolrBase( $fullSolrURI ); $result = $solrBase->rawSolrRequest( '/select', $params, $requestType ); if ( !$result ) self::addNoCacheHeaders(); eZDebug::accumulatorStop( __CLASS__ . '::' . __FUNCTION__ ); return $result; }
/** * i18n operator works like : source|context([argument1, argument2 ...]) * Arguments are optional and are comma separated * * @return Callable[] * * Examples : * * Simple without argument: * {{# i18n }} * MORE|merck() * {{/ i18n }} * * Or with argument: * {{# i18n }} * %nb RESULTS|merck(3) * {{/ i18n }} */ private static function getHelpers($application = null) { return array( 'i18n' => function($text) { preg_match("#(?<source>[^\|]+)\|(?<context>[^(]+)\((?<params>[^\)]+)?\)#", $text, $m); if($m && $m["source"] && $m["context"]) { $arguments = array(); if($m["params"]) { $params = explode(",", $m["params"]); preg_match_all("#(?<arguments>%\w+)#", $m["source"], $mParams); if($mParams && $mParams["arguments"]) { for($i = 0; $i < count($mParams["arguments"]); $i++) { $arguments[$mParams["arguments"][$i]] = $params[$i]; } } } return ezpI18n::tr($m["context"], $m["source"], null, $arguments); } return $text; }, 'taxonomies_filter' => function($text) use ($application) { $filterTaxonomies = $application->getCustomParameter('TaxonomiesFilter'); $html = ''; foreach($filterTaxonomies as $category) { $taxonomies = FacetFilteringTool::getTaxonomyTranslationsByCategory($category); $tpl = eZTemplate::factory(); $tpl->setVariable('taxonomies', $taxonomies); $tpl->setVariable('category', $category); $html .= $tpl->fetch( 'design:esibuild/app_content/channel/filter.tpl' ); } return $html; }, 'editors_choice_content' => function($text) use ($application) { $taxonomyCategory = $application->getCustomParameter('EditorsChoiceFilter'); $taxonomyValue = $application->getCustomParameter('EditorsChoiceValue'); $limit = $application->getCustomParameter('EditorsChoiceLimit'); $html = ''; $language = eZINI::instance('site.ini')->variable('RegionalSettings', 'ContentObjectLocale'); $fields = array( 'node_id' => 'meta_node_id_si', 'object_id' => 'meta_id_si', 'language' => 'meta_language_code_ms', 'url' => 'attr_'.ClusterTool::clusterIdentifier().'_url_s', 'headline' => 'attr_headline_s', 'promo_description' => 'attr_promo_description_t', 'rating' => 'attr_content_rating_'.ClusterTool::clusterIdentifier().'_f', 'views' => 'attr_view_counter_'.ClusterTool::clusterIdentifier().'_i', 'app_identifiers' => 'subattr_parent_application___identifier____s', 'apps' => 'subattr_local_application___source_mixed____s', 'app_id' => 'subattr_local_application___source_id____s', 'online_date' => 'attr_online_date_dt', 'score' => 'score', 'publisher_path' => 'subattr_publisher_folder___source_id____s', 'has_image' => 'attr_has_image_'.ClusterTool::clusterIdentifier().'_bst', 'media_types' => 'attr_media_content_types_'.ClusterTool::clusterIdentifier().'_bst', 'internal_id' => 'attr_publisher_internal_id_s', 'link_url' => 'attr_media_content_link_'.ClusterTool::clusterIdentifier().'____ms' ); $filters = array( "subattr_{$taxonomyCategory[0]}___source_id____s: \"{$taxonomyValue[0]}\"", "meta_language_code_ms: \"{$language}\"" ); $params = array( 'indent' => 'on', 'q' => '*:*', 'fq' => implode(' AND ' , $filters), 'start' => '0', 'rows' => $limit, 'fl' => implode(',',$fields), 'qt' => 'ezpublish', 'explainOther' => '', 'hl.fl' => '', 'sort' => "attr_online_date_dt desc", ); $result = SolrTool::rawSearch($params); foreach ($result[response][docs] as $key => $value) { $result[response][docs][$key]['headline'] = $value['attr_headline_s']; $result[response][docs][$key]['node_id'] = $value['meta_node_id_si']; $result[response][docs][$key]['object_id'] = $value['meta_id_si']; $result[response][docs][$key]['language'] = $value['meta_language_code_ms']; $result[response][docs][$key]['promo_description'] = $value['attr_promo_description_t']; $app = CacheApplicationTool::buildLocalizedApplicationByApplication( $value['subattr_local_application___source_id____s'][0] ); $applicationObject = $app->applicationObject(); $applicationType = $applicationObject->applicationType()->attribute('type_identifier'); $result[response][docs][$key]['application_name'] = $app->attribute('url_alias'); $publisherPath = $value['subattr_publisher_folder___source_id____s'][0]; $publisher = PublisherFolder::getPublisherFromPath($publisherPath); $publisherFolderId = $publisher->getTranslation()->attribute('publisher_folder_id'); $publisherName = $publisher->getTranslation()->attribute('name'); $result[response][docs][$key]['publisher_name'] = $publisherName; $url = $value['attr_'.ClusterTool::clusterIdentifier().'_url_s']; $url = ($applicationType == 'first-child') ? $app->attribute('url_alias') : $app->attribute('url_alias') . '/' . $publisherFolderId . '/' . $url; $result[response][docs][$key]['url'] = $url; $result[response][docs][$key]['online_date'] = solrTool::getDateFromSolr($value['attr_online_date_dt']); $result[response][docs][$key]['publisher_name'] = $publisherName; $result[response][docs][$key]['has_image'] = json_decode( base64_decode($value['attr_has_image_'.ClusterTool::clusterIdentifier().'_bst']), true ); $mediaCase = ImageArticleTool::NEWS_APPLICATION_CASE; $hasImage = $result[response][docs][$key]['has_image'][$mediaCase]; if ($hasImage) { $result[response][docs][$key]["dt_url"] = SolrSafeOperatorHelper::getImageArticleUrl($mediaCase, $result[response][docs][$key]['object_id'], $result[response][docs][$key]['language'], 'dt_full'); $result[response][docs][$key]["mb_url"] = SolrSafeOperatorHelper::getImageArticleUrl($mediaCase, $result[response][docs][$key]['object_id'], $result[response][docs][$key]['language'], 'm_full'); } } $tpl = eZTemplate::factory(); $tpl->setVariable('editorsChoice', $result); $html .= $tpl->fetch( 'design:esibuild/app_content/channel/editors_choice.tpl' ); return $html; } ); }
/** * This function construct different parameter for an article for template mustache * @param $articles * @param $additionalMappingTaxonomies * @param ChannelBlock $block * @return array */ private function constructArticles($articles, $additionalMappingTaxonomies, ChannelBlock $block) { $taxonomyTranslations = $constructedArticles = array(); foreach($articles as $article) { // add view parameter channel in url $article["url"] .= "/(channel)/".$block->attribute("id"); // info for sorting articles cross applications $article["info_sort"] = array(); if($article["online_date"]) { $article["info_sort"]["date"] = solrTool::getTimestampFromSolr($article["online_date"]); $article["online_date"] = solrTool::getDateFromSolr($article["online_date"]); } $article["info_sort"]["rating"] = $article["rating"]; $article["info_sort"]["views"] = $article["views"]; $article["info_sort"]["headline"] = $article["headline"]; $article["info_sort"]["score"] = $article["score"]; $mediaCase = ImageArticleTool::MULTIMEDIA_APPLICATION_CASE; $hasImage = SolrSafeOperatorHelper::hasImageArticleFromSolr($article, $mediaCase); if( !$hasImage ) { $mediaCase = ImageArticleTool::NEWS_APPLICATION_CASE; $hasImage = SolrSafeOperatorHelper::hasImageArticleFromSolr($article, $mediaCase); } // image information $article["hasDtImage"] = $hasImage; $article["hasMbImage"] = $hasImage; if ( $hasImage ) { $article["dt_url"] = SolrSafeOperatorHelper::getImageArticleUrl($mediaCase, $article['object_id'], $article['language'], 'dt_full'); $article["mb_url"] = SolrSafeOperatorHelper::getImageArticleUrl($mediaCase, $article['object_id'], $article['language'], 'm_full'); } $promoTaxonomy = $this->getCustomParameter('PromoTaxonomy'); foreach ($article['media_types'] as $type => $value) { $article["has_media_{$type}"] = true; } $article['hasPromoTaxonomy'] = false; // construct taxonomies fields foreach($additionalMappingTaxonomies as $taxonomyCategory => $solrField) { if ($taxonomyCategory == $promoTaxonomy) { if (count($article[$taxonomyCategory]) == 1 && $article[$taxonomyCategory][0] != '') { $article['hasPromoTaxonomy'] = true; $article['promoTaxonomy'] = FacetFilteringTool::getTaxonomyTranslationByCode($article[$taxonomyCategory][0]); } } // special case for retrieve value of taxonomy publisher folder if($taxonomyCategory == "publisher_folder") { $publisher = PublisherFolder::getPublisherFromPath($article["publisher_folder"][0]); if($publisher) { $article["publisher_folder"] = $publisher->getTranslation()->attribute("name"); } else { $article["publisher_folder"] = null; } } else { if(!empty($article[$taxonomyCategory]) && $article[$taxonomyCategory][0] != "") { if(empty($taxonomyTranslations[$taxonomyCategory])) { $taxonomyTranslations[$taxonomyCategory] = FacetFilteringTool::getTaxonomyTranslation($taxonomyCategory); } $article[$taxonomyCategory] = $taxonomyTranslations[$taxonomyCategory][$article[$taxonomyCategory][0]]; } else { // if the category has not value, we remove the entry in article unset($article[$taxonomyCategory]); } } } $this->constructAdditionalInformations($article); // rebuild array for delete keys for mustache $constructedArticles[] = $article; } return $constructedArticles; }
'indent' => 'on', 'q' => $keyword, 'fq' => implode( ' AND ', $filters ), 'start' => '', 'rows' => '15', 'fl' => 'attr_headline_s', 'qt' => 'ezpublish', 'explainOther' => '', 'hl.fl' => '', 'sort' => 'score desc', ); $additionalParams = $resultHandler->solrHighlightParams(); $params = array_merge( $params, $additionalParams ); $results = solrTool::rawSearch( $params ); $response = array( 'term' => $keyword, 'count' => $results['response']['numFound'], 'result_url' => contextTool::instance()->domain() . '/' . $globalSearchURi . '/#?q=' . urlencode( $keyword ) ); ob_clean(); switch( $responseFormat ) { case 'xml': header( 'Content-Type: application/xml' ); $xml = new SimpleXMLElement( '<response/>' ); $xml->addChild( 'term', $response['term'] );