/** * Provided an Article, queries the database for all titles that are redirects to that page. * @see \Wikia\Search\IndexService\AbstractService::execute() * @return array */ public function execute() { $service = $this->getService(); $key = $service->getGlobal('AppStripsHtml') ? Utilities::field('redirect_titles') : 'redirect_titles'; $titles = $service->getRedirectTitlesForPageId($this->currentPageId); $result = array($key => $titles, 'redirect_titles_mv_em' => $titles); return $result; }
/** * Return a string of query fields based on configuration * @return string */ protected function getQueryFieldsString() { $queryFieldsString = ''; $config = $this->getConfig(); $language = $config->getLanguageCode(); foreach ($config->getQueryFieldsToBoosts() as $field => $boost) { $queryFieldsString .= sprintf('%s^%s ', Utilities::field($field, $language), $boost); } return trim($queryFieldsString); }
/** * We want all files (and maybe just videos) from video wiki and the current wiki * @see \Wikia\Search\QueryService\Select\Dismax\AbstractDismax::getQueryClausesString() * @return string */ protected function getQueryClausesString() { $config = $this->getConfig(); $queryClauses = []; $queryClauses[] = sprintf('+((wid:%d AND (%s)) OR wid:%d)', Video::VIDEO_WIKI_ID, $this->getTopicsAsQuery(), $config->getWikiId()); $queryClauses[] = Utilities::valueForField('ns', \NS_FILE); if ($config->getCombinedMediaSearchIsVideoOnly()) { $queryClauses[] = Utilities::valueForField('is_video', 'true'); } if ($config->getCombinedMediaSearchIsImageOnly()) { $queryClauses[] = Utilities::valueForField('is_image', 'true'); } return sprintf('(%s)', implode(' AND ', $queryClauses)); }
/** * Does a little prep on a result object, applies highlighting if exists, and adds to result array. * @param Result $result * @throws WikiaException * @return Base provides fluent interface */ protected function addResult(Result $result) { $id = $result['id']; $highlighting = $this->searchResultObject->getHighlighting(); if ($highlighting !== null && ($hlResult = $highlighting->getResult($id)) && ($field = $hlResult->getField(Utilities::field('html')))) { $result->setText($field[0]); } if ($result['created']) { $result->setVar('fmt_timestamp', $this->service->getMediaWikiFormattedTimestamp($result['created'])); $result->setVar('created_30daysago', time() - strtotime($result['created']) > 2592000); } $result->setVar('cityArticlesNum', $result['wikiarticles'])->setVar('wikititle', $result[Utilities::field('wikititle')]); $this->results[$id] = $result; return $this; }
/** * @group Slow * @slowExecutionTime 0.10161 ms * @covers Wikia\Search\QueryService\Select\Dismax\OnWiki::registerFilterQueryForMatch */ public function testRegisterFilterQueryForMatch() { $mockConfig = $this->getMock('Wikia\\Search\\Config', array('hasArticleMatch', 'getArticleMatch', 'setFilterQuery')); $mockMatch = $this->getMockBuilder('Wikia\\Search\\Match\\Article')->disableOriginalConstructor()->setMethods(array('getResult'))->getMock(); $mockResult = $this->getMockBuilder('Wikia\\Search\\Result')->setMethods(array('getVar'))->getMock(); $dc = new Wikia\Search\QueryService\DependencyContainer(array('config' => $mockConfig)); $mockSelect = $this->getMockBuilder('Wikia\\Search\\QueryService\\Select\\Dismax\\OnWiki')->setConstructorArgs(array($dc))->setMethods(null)->getMock(); $mockConfig->expects($this->once())->method('hasArticleMatch')->will($this->returnValue(true)); $mockConfig->expects($this->once())->method('getArticleMatch')->will($this->returnValue($mockMatch)); $mockMatch->expects($this->once())->method('getResult')->will($this->returnValue($mockResult)); $mockResult->expects($this->once())->method('getVar')->with('id')->will($this->returnValue(123)); $mockConfig->expects($this->once())->method('setFilterQuery')->with(Wikia\Search\Utilities::valueForField('id', 123, array('negate' => true)), 'ptt'); $register = new ReflectionMethod('Wikia\\Search\\QueryService\\Select\\Dismax\\OnWiki', 'registerFilterQueryForMatch'); $register->setAccessible(true); $this->assertEquals($mockSelect, $register->invoke($mockSelect)); }
/** * @group Slow * @slowExecutionTime 0.0834 ms * @covers Wikia\Search\IndexService\DefaultContent::Field */ public function testField() { $dynamicField = \Wikia\Search\Utilities::field('html'); $mockService = $this->getMock('Wikia\\Search\\MediaWikiService', array('getGlobal')); $utils = $this->getMock('Wikia\\Search\\Utilities', array('field')); $this->mockClass('Wikia\\Search\\MediaWikiService', $mockService); $this->mockClass('Wikia\\Search\\Utilities', $utils); $dc = new DefaultContent(); $field = new ReflectionMethod('Wikia\\Search\\IndexService\\DefaultContent', 'field'); $field->setAccessible(true); $mockService->expects($this->at(0))->method('getGlobal')->with('AppStripsHtml')->will($this->returnValue(true)); $utils->staticExpects($this->once())->method('field')->with('html')->will($this->returnValue($dynamicField)); $this->assertEquals($dynamicField, $field->invoke($dc, 'html')); $mockService->expects($this->at(0))->method('getGlobal')->with('AppStripsHtml')->will($this->returnValue(false)); $this->assertEquals('html', $field->invoke($dc, 'html')); }
/** * Boosts results that match the the current hub category and wiki title * @see \Wikia\Search\QueryService\Select\Video::getBoostQueryString() * @return string */ public function getBoostQueryString() { $service = $this->getService(); return sprintf('%s^150 AND (%s)^250 AND (html_media_extras_txt:(%s))^300', Utilities::valueForField('categories', $service->getHubForWikiId($service->getWikiId())), $this->getConfig()->getQuery()->getSolrQuery(), $this->getTopicsAsQuery()); }
/** * @group Slow * @slowExecutionTime 0.08589 ms * @covers Wikia\Search\IndexService\CrossWikiCore::getVisualizationInfo */ public function testGetVisualizationInfoNoDesc() { $service = $this->getMockBuilder('Wikia\\Search\\IndexService\\CrossWikiCore')->disableOriginalConstructor()->setMethods(['getService', 'getWikiId'])->getMock(); $mwService = $this->getMock('Wikia\\Search\\MediaWikiService', ['getVisualizationInfoForWikiId', 'getSimpleMessage', 'getGlobal']); $desc = 'this is my description'; $headline = 'this is my headline'; $service->expects($this->once())->method('getService')->will($this->returnValue($mwService)); $service->expects($this->once())->method('getWikiId')->will($this->returnValue(123)); $mwService->expects($this->once())->method('getVisualizationInfoForWikiId')->with(123)->will($this->returnValue(['image' => 'foo.jpg', 'flags' => ['hot' => 0], 'headline' => $headline])); $mwService->expects($this->once())->method('getGlobal')->with('Sitename')->will($this->returnValue('sitename')); $mwService->expects($this->once())->method('getSimpleMessage')->with('wikiasearch2-crosswiki-description', ['sitename'])->will($this->returnValue($desc)); $get = new ReflectionMethod($service, 'getVisualizationInfo'); $get->setAccessible(true); $expected = ['image_s' => 'foo.jpg', 'description_txt' => $desc, Utilities::field('description') => $desc, 'hot_b' => 'false', 'headline_txt' => $headline]; $this->assertEquals($expected, $get->invoke($service)); }
/** * Configures result snippet highlighting * @param Solarium_Query_Select $query * @return AbstractSelect */ protected function registerHighlighting(Solarium_Query_Select $query) { $highlighting = $query->getHighlighting(); $highlighting->addField(Utilities::field($this->highlightingField))->setSnippets(1)->setRequireFieldMatch(true)->setFragSize(self::HL_FRAG_SIZE)->setSimplePrefix(self::HL_MATCH_PREFIX)->setSimplePostfix(self::HL_MATCH_POSTFIX)->setAlternateField('nolang_txt')->setMaxAlternateFieldLength(100); return $this; }
/** * @group Slow * @slowExecutionTime 0.10144 ms * @covers Wikia\Search\QueryService\Select\AbstractSelect::getRequestedFields */ public function testGetRequestedFields() { $mockService = $this->getMockBuilder('Wikia\\Search\\QueryService\\Select\\AbstractSelect')->disableOriginalConstructor()->setMethods(['getConfig'])->getMockForAbstractClass(); $mockConfig = $this->getMock('Wikia\\Search\\Config', ['getRequestedFields']); $attr = new ReflectionProperty($mockService, 'requestedFields'); $attr->setAccessible(true); $attr->setValue($mockService, ['html']); $get = new ReflectionMethod($mockService, 'getRequestedFields'); $get->setAccessible(true); $mockService->expects($this->once())->method('getConfig')->will($this->returnValue($mockConfig)); $mockConfig->expects($this->once())->method('getRequestedFields')->will($this->returnValue(['title'])); $this->assertEquals([Utilities::field('html'), Utilities::field('title')], $get->invoke($mockService)); }
/** * Constructor method * @param array $params */ public function __construct(array $params = []) { $dynamicFilterCodes = [self::FILTER_CAT_VIDEOGAMES => Utilities::valueForField('categories', 'Games', ['quote' => '"']), self::FILTER_CAT_ENTERTAINMENT => Utilities::valueForField('categories', 'Entertainment'), self::FILTER_CAT_LIFESTYLE => Utilities::valueForField('categories', 'Lifestyle')]; $this->filterCodes = array_merge($this->filterCodes, $dynamicFilterCodes); $this->configureByArray($params); }
/** * Builds the string used with filter queries based on search config * @return string */ protected function getFilterQueryString() { $namespaces = []; foreach ($this->config->getNamespaces() as $ns) { $namespaces[] = Utilities::valueForField('ns', $ns); } $minArticleQuality = $this->config->getMinArticleQuality(); $filters = [sprintf('(%s)', implode(' OR ', $namespaces)), Utilities::valueForField('wid', $this->config->getCityId())]; if ($minArticleQuality) { $filters[] = Utilities::rangeIntValueField('article_quality_i', $minArticleQuality); } return implode(' AND ', $filters); }
protected function generateArrayQuery($queryArray, $name, $values) { $q = ''; if (!empty($values)) { if (!is_array($values)) { $values = [$values]; } foreach ($values as $item) { if (!$item) { continue; } $q .= ($q ? ' OR ' : '') . Utilities::valueForField($name, $item); } if ($q) { $queryArray[] = ' ( ' . $q . ' ) '; } } return $queryArray; }
/** * @group Slow * @slowExecutionTime 0.09938 ms * @covers Wikia\Search\Utilities::field */ public function testField() { $this->assertEquals(Utils::field('html', 'fr'), (string) new \Wikia\Search\Field\Field('html', 'fr')); }
protected function getTopArticles($wikiId, $lang) { return \WikiaDataAccess::cache(wfSharedMemcKey("CombinedSearchService", $wikiId, $lang), self::TOP_ARTICLES_CACHE_TIME, function () use($wikiId, $lang) { $timer = Time::start(["CombinedSearchService", "getTopArticles"]); $requestedFields = ["title", "url", "id", "score", "pageid", "lang", "wid", "article_quality_i", Utilities::field('html', $lang)]; $topArticlesMap = \DataMartService::getTopArticlesByPageview($wikiId, null, [NS_MAIN], false, self::TOP_ARTICLES_PER_WIKI + 1); $query = " +(" . Utilities::valueForField("wid", $wikiId) . ") "; $query .= " +( " . implode(" OR ", array_map(function ($x) { return Utilities::valueForField("pageid", $x); }, array_keys($topArticlesMap))) . ") "; $query .= " +(is_main_page:false) "; $searchConfig = new Config(); $searchConfig->setLimit(self::TOP_ARTICLES_PER_WIKI)->setQuery($query)->setPage(1)->setRequestedFields($requestedFields)->setDirectLuceneQuery(true)->setWikiId($wikiId); $resultSet = (new Factory())->getFromConfig($searchConfig)->search(); $currentResults = $resultSet->toArray($requestedFields); $articles = []; foreach ($currentResults as $article) { $articles[$article['pageid']] = $this->processArticle($article); if (sizeof($articles) >= self::TOP_ARTICLES_PER_WIKI) { break; } } $result = []; foreach ($topArticlesMap as $id => $a) { if (isset($articles[$id])) { $result[] = $articles[$id]; } } $timer->stop(); return $result; }); }
/** * @group Slow * @slowExecutionTime 0.10235 ms * @covers Wikia\Search\QueryService\Select\Dismax\VideoEmbedTool::getBoostQueryString */ public function testGetBoostQueryString() { $service = $this->getMockBuilder(self::CLASSNAME)->disableOriginalConstructor()->setMethods(['getConfig', 'getTopicsAsQuery', 'getService'])->getMock(); $mwService = $this->getMock('Wikia\\Search\\MediaWikiService', ['getHubForWikiId', 'getWikiId']); $config = $this->getMock('Wikia\\Search\\Config', ['getQuery']); $query = $this->getMock('Wikia\\Search\\Query\\Select', ['getSolrQuery'], ['foo']); $service->expects($this->once())->method('getService')->will($this->returnValue($mwService)); $mwService->expects($this->once())->method('getWikiId')->will($this->returnValue(123)); $mwService->expects($this->once())->method('getHubForWikiId')->with(123)->will($this->returnValue('Entertainment')); $service->expects($this->once())->method('getConfig')->will($this->returnValue($config)); $config->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $query->expects($this->once())->method('getSolrQuery')->will($this->returnValue('solrquery')); $service->expects($this->once())->method('getTopicsAsQuery')->will($this->returnValue('topicquery')); $this->assertEquals(sprintf('%s^150 AND (%s)^250 AND (html_media_extras_txt:(%s))^300', Utilities::valueForField('categories', 'Entertainment'), 'solrquery', 'topicquery'), $service->getBoostQueryString()); }
/** * Allows us to overload parent offsetGet with getTitle(), getText(), etc. * This is good when using $result->toArray() * @see Solarium_Document_ReadOnly::offsetGet() */ public function offsetGet($key) { $value = null; $keyParts = explode('_', $key); $nolangKey = reset($keyParts); switch ($nolangKey) { case 'title': $value = $this->getTitle(); break; case 'text': $value = $this->getText(); break; case 'videoViews': $value = $this->getVideoViews(); break; default: $value = parent::offsetGet(Utilities::field($nolangKey)); // e.g. infoboxes_txt if (empty($value)) { $value = parent::offsetGet($key); } } return $value; }
/** * @group Slow * @slowExecutionTime 0.10045 ms * @covers Wikia\Search\Result::getTitle * @covers Wikia\Search\Result::setTitle */ public function testTitleFieldMethods() { global $wgLanguageCode; $wgLanguageCode = 'fr'; $fieldsCopy = $this->defaultFields; unset($fieldsCopy['title']); unset($fieldsCopy[Utilities::field('title')]); $result = new Result($fieldsCopy); $this->assertEquals('', $result->getTitle(), 'A result with no title or language title field should return an empty string during getTitle().'); $title = 'Foo'; $result['title'] = $title; $this->assertEquals($title, $result->getTitle(), 'A result with no language title field should return a normal title field during getTitle() if it exists.'); $languageTitle = 'LangFoo'; $result[Utilities::field('title')] = $languageTitle; $this->assertEquals($languageTitle, $result->getTitle(), 'A result should return the language title field during getTitle() if it exists.'); $languageTitleWithJunk = '.../**$#(FooEnglish...</span>…'; $this->assertEquals($result, $result->setTitle($languageTitleWithJunk), 'Wikia\\Search\\Result::setTitle should provide a fluent interface'); $method = new ReflectionMethod('Wikia\\Search\\Result', 'fixSnippeting'); $method->setAccessible(true); $this->assertEquals($method->invoke($result, $languageTitleWithJunk), $result->getTitle(), 'A title set with Wikia\\Search\\Result::setTitle() should be filtered with Wikia\\Search\\Utilities::fixSnippeting before storage.'); unset($result[Utilities::field('title')]); unset($result['title']); $result[Utilities::field('title', 'en')] = $languageTitle; global $wgLanguageCode; $oldCode = $wgLanguageCode; $wgLanguageCode = 'fr'; $result = new Result(array('title_en' => $languageTitle)); $this->assertEquals($languageTitle, $result->getTitle(), 'A result should return the english language title field during getTitle() if it exists, but the non-english field doesn\'t (video support).'); $wgLanguageCode = $oldCode; }
/** * @group Slow * @slowExecutionTime 0.08515 ms * @covers \Wikia\Search\IndexService\Redirects::execute */ public function testRedirectsService() { $mwservice = $this->service->setMethods(array('getGlobal', 'getRedirectTitlesForPageId'))->getMock(); $mwservice->expects($this->at(0))->method('getGlobal')->with('AppStripsHtml')->will($this->returnValue(true)); $mwservice->expects($this->at(1))->method('getRedirectTitlesForPageId')->with($this->pageId)->will($this->returnValue(array('foo', 'bar'))); $service = $this->getMockBuilder('\\Wikia\\Search\\IndexService\\Redirects')->disableOriginalConstructor()->setMethods(null)->getMock(); $this->injectService($service, $mwservice); $service->setPageId($this->pageId); $this->assertEquals(array(\Wikia\Search\Utilities::field('redirect_titles') => array('foo', 'bar'), 'redirect_titles_mv_em' => array('foo', 'bar')), $service->execute()); }
/** * Retrieves all categories and top categories. * @return array */ protected function getCategories() { $categories = []; $dbr = wfGetDB(DB_SLAVE); $sql = "SELECT cat_title FROM category WHERE cat_hidden = 0 ORDER BY cat_pages DESC"; $query = $dbr->select('category', 'cat_title', 'cat_hidden = 0', __METHOD__, ['LIMIT' => 50, 'ORDER BY' => 'cat_pages DESC']); while ($result = $dbr->fetchObject($query)) { $categories[] = str_replace('_', ' ', $result->cat_title); } $topCategories = array_slice($categories, 0, 20); $cats = Utilities::field('categories'); $cats = $cats == 'categories' ? 'categories_txt' : $cats; $topsCats = Utilities::field('top_categories'); $topsCats = $topsCats == 'top_categories' ? 'top_categories_txt' : $topsCats; return array($cats => $categories, 'categories_txt' => $categories, 'top_categories_txt' => $topCategories, $topsCats => $topCategories); }
/** * Given a set of page IDs, deletes by query * @param array $documentIds * @return bool true */ public function deleteBatch(array $documentIds = array()) { $updateHandler = $this->getClient()->createUpdate(); foreach ($documentIds as $id) { $updateHandler->addDeleteQuery(Utilities::valueForField('id', $id)); } $updateHandler->addCommit(); try { $this->getClient()->update($updateHandler); } catch (\Exception $e) { $this->getLogger()->log(__METHOD__, implode(',', $documentIds), $e); } return true; }
/** * @group Slow * @slowExecutionTime 0.12031 ms * @covers \Wikia\Search\ResultSet\Base::addResult */ public function testAddResult() { $this->prepareMocks(array(), array(), array('getHighlighting')); $mockSearchResult = $this->getMockBuilder('Wikia\\Search\\Result')->disableOriginalConstructor()->setMethods(array('offsetGet', 'setText', 'setVar', 'getVar'))->getMock(); $mockHighlighting = $this->getMockBuilder('Solarium_Result_Select_Highlighting')->disableOriginalConstructor()->setMethods(array('getResult'))->getMock(); $mockHighlightingResult = $this->getMockBuilder('Solarium_Result_Select_Highlighting_Result')->disableOriginalConstructor()->setMethods(array('getField'))->getMock(); $mockService = $this->getMockBuilder('Wikia\\Search\\MediaWikiService')->disableOriginalConstructor()->setMethods(array('getMediaWikiFormattedTimestamp'))->getMock(); $mockArrayIterator = $this->getMock('\\ArrayIterator'); $mockId = "123_456"; $mockHlSnippet = "I can't believe it's not <em>butter</em>!"; $mockTimestamp = date('Y-m-d') . 'T00:00:00Z'; // always today $wfts = wfTimestamp(TS_MW, $mockTimestamp); $mockSearchResult->expects($this->at(0))->method('offsetGet')->with('id')->will($this->returnValue($mockId)); $this->searchResult->expects($this->at(0))->method('getHighlighting')->will($this->returnValue($mockHighlighting)); $mockHighlighting->expects($this->at(0))->method('getResult')->with($mockId)->will($this->returnValue($mockHighlightingResult)); $mockHighlightingResult->expects($this->at(0))->method('getField')->with(Wikia\Search\Utilities::field('html'))->will($this->returnValue(array($mockHlSnippet))); $mockSearchResult->expects($this->at(1))->method('setText')->with($mockHlSnippet); $mockSearchResult->expects($this->at(2))->method('offsetGet')->with('created')->will($this->returnValue($mockTimestamp)); $mockSearchResult->expects($this->at(3))->method('offsetGet')->with('created')->will($this->returnValue($mockTimestamp)); $mockService->expects($this->once())->method('getMediaWikiFormattedTimestamp')->with($mockTimestamp)->will($this->returnValue('Today')); $mockSearchResult->expects($this->at(4))->method('setVar')->with('fmt_timestamp', 'Today')->will($this->returnValue($mockSearchResult)); $mockSearchResult->expects($this->at(5))->method('offsetGet')->with('created')->will($this->returnValue($mockTimestamp)); $mockSearchResult->expects($this->at(6))->method('setVar')->with('created_30daysago', false)->will($this->returnValue($mockSearchResult)); $mockSearchResult->expects($this->at(7))->method('offsetGet')->with('wikiarticles')->will($this->returnValue(12345)); $mockSearchResult->expects($this->at(8))->method('setVar')->with('cityArticlesNum', 12345)->will($this->returnValue($mockSearchResult)); $mockSearchResult->expects($this->at(9))->method('offsetGet')->with(Wikia\Search\Utilities::field('wikititle'))->will($this->returnValue("My Wiki")); $mockSearchResult->expects($this->at(10))->method('setVar')->with('wikititle', "My Wiki")->will($this->returnValue($mockSearchResult)); $mockArrayIterator->expects($this->once())->method('offsetSet')->with($mockId, $mockSearchResult); $intRefl = new ReflectionProperty('Wikia\\Search\\ResultSet\\Base', 'service'); $intRefl->setAccessible(true); $intRefl->setValue($this->resultSet, $mockService); $global = new ReflectionProperty('\\Wikia\\Search\\ResultSet\\Base', 'results'); $global->setAccessible(true); $global->setValue($this->resultSet, $mockArrayIterator); $addResult = new ReflectionMethod('\\Wikia\\Search\\ResultSet\\Base', 'addResult'); $addResult->setAccessible(true); $this->assertEquals($this->resultSet, $addResult->invoke($this->resultSet, $mockSearchResult), '\\Wikia\\Search\\ResultSet\\Base::addResult should provide a fluent interface'); }