public function display(Template $oTemplate, $bIsPreview = false) { $sTemplateName = $this->oPage->getTemplateNameUsed(); $sLanguageId = Session::language(); $oListTemplate = null; $oItemTemplatePrototype = null; try { $oListTemplate = new Template("search_results/{$sTemplateName}"); $oItemTemplatePrototype = new Template("search_results/{$sTemplateName}_item"); } catch (Exception $e) { $oListTemplate = new Template("search_results/default"); $oItemTemplatePrototype = new Template("search_results/default_item"); } $aResults = array(); $sWords = isset($_REQUEST['q']) ? $_REQUEST['q'] : ''; if ($sWords) { $aWords = StringUtil::getWords($sWords, false, '%'); $oSearchWordQuery = SearchIndexWordQuery::create(); foreach ($aWords as $sWord) { $sWord = Synonyms::rootFor($sWord); $sComparison = Criteria::EQUAL; if (strpos($sWord, '%') !== false) { $sComparison = Criteria::LIKE; } $oSearchWordQuery->addOr(SearchIndexWordPeer::WORD, $sWord, $sComparison); } $oSearchWordQuery->joinSearchIndex()->useQuery('SearchIndex')->joinPage()->useQuery('Page')->active(true)->filterByIsProtected(false)->endUse()->endUse(); foreach ($oSearchWordQuery->find() as $oSearchIndexWord) { $iId = $oSearchIndexWord->getSearchIndexId(); if (isset($aResults[$iId])) { $aResults[$iId] += $oSearchIndexWord->getCount(); } else { $aResults[$iId] = $oSearchIndexWord->getCount(); } } arsort($aResults); } $oListTemplate->replaceIdentifier('count', count($aResults)); $oListTemplate->replaceIdentifier('search_string', $sWords); if (count($aResults) === 0) { $oListTemplate->replaceIdentifier('no_results', TranslationPeer::getString('wns.search.no_results', null, null, array('search_string' => $sWords))); } foreach ($aResults as $iIndexId => $iCount) { $oIndex = SearchIndexQuery::create()->findPk(array($iIndexId, $sLanguageId)); if (!$oIndex || !$oIndex->getPage()) { continue; } $oItemTemplate = clone $oItemTemplatePrototype; $oIndex->renderListItem($oItemTemplate); $oItemTemplate->replaceIdentifier('count', $iCount); $oListTemplate->replaceIdentifierMultiple('items', $oItemTemplate); } $oTemplate->replaceIdentifier('search_results', $oListTemplate); }
private static function init() { if (self::$SYNONYMS === null) { self::$SYNONYMS = array(); self::$ROOT_WORDS = array(); $sSynonyms = Settings::getInstance('synonyms')->getSettingsArray(); foreach ($sSynonyms as $sLanguageId => $aSynonymList) { self::$SYNONYMS[$sLanguageId] = array(); self::$ROOT_WORDS[$sLanguageId] = array(); foreach ($aSynonymList as $sRootWord => $aSynonyms) { $sRootWord = StringUtil::normalize($sRootWord); foreach ($aSynonyms as $iKey => $sSynonym) { self::$SYNONYMS[$sLanguageId][StringUtil::normalize($sSynonym)] = $sRootWord; } self::$ROOT_WORDS[$sLanguageId][$sRootWord] = true; } } } }
private function index(array $aPath) { $oNavigationItem = $this->oRootNavigationItem; PageNavigationItem::clearCache(); while (count($aPath) > 0) { $oNavigationItem = $oNavigationItem->namedChild(array_shift($aPath), $this->sLanguageId, true, true); } FilterModule::getFilters()->handleNavigationPathFound($this->oRootNavigationItem, $oNavigationItem); FrontendManager::$CURRENT_NAVIGATION_ITEM = $oNavigationItem; $oPageNavigationItem = $oNavigationItem; while (!$oPageNavigationItem instanceof PageNavigationItem) { $oPageNavigationItem = $oPageNavigationItem->getParent(); } FrontendManager::$CURRENT_PAGE = $oPageNavigationItem->getMe(); $oPage = FrontendManager::$CURRENT_PAGE; $bIsNotFound = false; FilterModule::getFilters()->handlePageHasBeenSet($oPage, $bIsNotFound, $oNavigationItem); FilterModule::getFilters()->handleRequestStarted(); FilterModule::getFilters()->handlePageNotFoundDetectionComplete($bIsNotFound, $oPage, $oNavigationItem, array(&$bIsNotFound)); if ($bIsNotFound) { return false; } $sDescription = $oNavigationItem->getDescription($this->sLanguageId); if ($sDescription === null) { $sDescription = $oPage->getDescription($this->sLanguageId); } $aKeywords = array(); foreach ($oPage->getConsolidatedKeywords($this->sLanguageId, true) as $sKeyword) { $aKeywords = array_merge($aKeywords, StringUtil::getWords($sKeyword)); } $sTitle = $oNavigationItem->getTitle($this->sLanguageId); $sLinkText = $oNavigationItem->getLinkText($this->sLanguageId); if (!$sLinkText) { $sLinkText = $sTitle; } $sName = $oNavigationItem->getName(); // Page type can prevent indexing if (!self::doIndex($oPage->getPageType(), $oNavigationItem)) { return false; } $oPageType = PageTypeModule::getModuleInstance($oPage->getPageType(), $oPage, $oNavigationItem); $aWords = $oPageType->getWords(); $aWords = array_merge($aWords, StringUtil::getWords($sDescription), $aKeywords, StringUtil::getWords($sTitle), StringUtil::getWords($sLinkText), array($sName)); $aPagePath = $oPage->getLink(); $aNavigationItemPath = $oNavigationItem->getLink(); $sPath = implode('/', array_diff($aNavigationItemPath, $aPagePath)); $oSearchIndex = new SearchIndex(); $oSearchIndex->setPageId($oPage->getId()); $oSearchIndex->setPath($sPath); $oSearchIndex->setLinkText($sLinkText); $oSearchIndex->setPageTitle($sTitle); $oSearchIndex->setLanguageId($this->sLanguageId); $oSearchIndex->save(); foreach ($aWords as $sWord) { $sWord = Synonyms::rootFor($sWord, $this->sLanguageId); $oSearchIndexWord = SearchIndexWordQuery::create()->filterBySearchIndex($oSearchIndex)->filterByWord($sWord)->findOne(); if ($oSearchIndexWord === null) { $oSearchIndexWord = new SearchIndexWord(); $oSearchIndexWord->setSearchIndex($oSearchIndex); $oSearchIndexWord->setWord($sWord); } else { $oSearchIndexWord->incrementCount(); } $oSearchIndexWord->save(); } return true; }
<?php namespace Hasantayyar\Synonyms; include __DIR__ . '/vendor/autoload.php'; $syn = new Synonyms('money'); $thesaurusData = $syn->getThesaurus(); print_r($thesaurusData->getWords()); echo $thesaurusData->getFormatted(); echo "_______________\n"; $bighugelabsData = $syn->getBighugelabs(); echo $thesaurusData->getFormatted(); echo "_______________\n"; $turkishSynonyms = $syn->getTurkishSynonyms(); echo $thesaurusData->getFormatted();