/** * @covers WikiaSearchResult::getTitle * @covers WikiaSearchResult::setTitle */ public function testTitleFieldMethods() { $fieldsCopy = $this->defaultFields; unset($fieldsCopy['title']); unset($fieldsCopy[WikiaSearch::field('title')]); $result = F::build('WikiaSearchResult', array($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[WikiaSearch::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), 'WikiaSearchResult::setTitle should provide a fluent interface'); $method = new ReflectionMethod('WikiaSearchResult', 'fixSnippeting'); $method->setAccessible(true); $this->assertEquals($method->invoke($result, $languageTitleWithJunk), $result->getTitle(), 'A title set with WikiaSearch::setTitle() should be filtered with WikiaSearch::fixSnippeting before storage.'); }
/** * @covers WikiaSearch::getQueryFieldsString */ public function testGetQueryFieldsString() { $this->mockGlobalVariable('wgLanguageCode', 'en'); $this->mockApp(); $mockClient = $this->getMock('Solarium_Client'); $wikiaSearch = F::build('WikiaSearch', array($mockClient)); $searchConfig = F::build('WikiaSearchConfig'); /** @var WikiaSearchConfig $searchConfig **/ $method = new ReflectionMethod('WikiaSearch', 'getQueryFieldsString'); $defaultString = sprintf('%s^5 %s^1.5 %s^4 %s^1', WikiaSearch::field('title'), WikiaSearch::field('html'), WikiaSearch::field('redirect_titles'), WikiaSearch::field('categories')); $interwikiString = $defaultString . sprintf(' %s^7', WikiaSearch::field('wikititle')); $method->setAccessible(true); $searchConfig->setQuery('foo'); $this->assertEquals($defaultString, $method->invoke($wikiaSearch, $searchConfig), 'WikiaSearch should query against the dynamic title, html, and redirect titles fields by default.'); $searchConfig->setInterWiki(true); $this->assertEquals($interwikiString, $method->invoke($wikiaSearch, $searchConfig), 'WikiaSearch should add wikititle as a query field if we are performing an interwiki search.'); $searchConfig->setIsInterWiki(false)->setVideoSearch(true); $this->assertEquals($defaultString, $method->invoke($wikiaSearch, $searchConfig), 'WikiaSearch should use the default query fields if the global language code is english.'); $this->mockGlobalVariable('wgLanguageCode', 'fr'); $this->mockApp(); global $wgLanguageCode; $wgLanguageCode = 'fr'; $frVideoString = sprintf('%s^5 %s^1.5 %s^4 %s^1 %s^5 %s^1.5 %s^4', WikiaSearch::field('title', 'fr'), WikiaSearch::field('html', 'fr'), WikiaSearch::field('redirect_titles', 'fr'), WikiaSearch::field('categories', 'fr'), WikiaSearch::field('title', 'en'), WikiaSearch::field('html', 'en'), WikiaSearch::field('redirect_titles', 'en')); $this->assertEquals($frVideoString, $method->invoke($wikiaSearch, $searchConfig), 'WikiaSearch should append english query fields if the global language is not english and we\'re doing premium video search.'); }
/** * Does a little prep on a result oject, applies highlighting if exists, and adds to result array. * @param WikiaSearchResult $result * @throws WikiaException * @return WikiaSearchResultSet provides fluent interface */ private function addResult(WikiaSearchResult $result) { if ($this->isValidResult($result)) { $id = $result['id']; if ($this->highlightingObject !== null && ($hlResult = $this->highlightingObject->getResult($id)) && ($field = $hlResult->getField(WikiaSearch::field('html')))) { $result->setText($field[0]); } if ($result['created'] !== null && $this->wg->Lang) { $result->setVar('created', $result['created'])->setVar('fmt_timestamp', $this->wg->Lang->date(wfTimestamp(TS_MW, $result['created']))); if ($result->getVar('fmt_timestamp')) { $result->setVar('created_30daysago', time() - strtotime($result['created']) > 2592000); } } $result->setVar('categories', $result[WikiaSearch::field('categories')] ?: 'NONE')->setVar('cityArticlesNum', $result['wikiarticles'])->setVar('wikititle', $result[WikiaSearch::field('wikititle')]); $this->results[$id] = $result; } else { throw new WikiaException('Invalid result in set'); } return $this; }
/** * Allows you to set the title for the search result. * @see WikiaSearchResultTest::testTitleFieldMethods * @param string $value * @return WikiaSearchResult provides fluent interface */ public function setTitle($value) { $this->_fields[WikiaSearch::field('title')] = $this->fixSnippeting($value); return $this; }
/** * Provides the requested fields with respect to dynamic language fields * @return array */ public function getRequestedFields() { $fieldsPrepped = array(); foreach ($this['requestedFields'] as $field) { $fieldsPrepped[] = WikiaSearch::field($field); } return $fieldsPrepped; }
/** * Generates a Solr document from a page ID * @param int $pageId * @return Solarium_Document_ReadWrite */ public function getSolrDocument($pageId) { $pageData = $this->getPage($pageId); $html = $pageData['html']; $regexes = array('\\+s' => ' ', '<span[^>]*editsection[^>]*>.*?<\\/span>' => '', '<img[^>]*>' => '', '<\\/img>' => '', '<noscript>.*?<\\/noscript>' => '', '<div[^>]*picture-attribution[^>]*>.*?<\\/div>' => '', '<ol[^>]*references[^>]*>.*?<\\/ol>' => '', '<sup[^>]*reference[^>]*>.*?<\\/sup>' => '', '<script .*?<\\/script>' => '', '<style .*?<\\/style>' => '', '\\+s' => ' '); foreach ($regexes as $re => $repl) { $html = preg_replace("/{$re}/mU", $repl, $html); } $pageData['html'] = strip_tags($html); foreach (WikiaSearch::$languageFields as $field) { if (isset($pageData[$field])) { $pageData[WikiaSearch::field($field)] = $pageData[$field]; } } return F::build('Solarium_Document_ReadWrite', array($pageData)); }