/** * Delivers a JSON response with keywords from the page ID */ public function getKeywords() { $id = $this->getVal('id'); if (empty($id)) { throw new Exception('Please provide an ID'); } $searchConfig = F::build('WikiaSearchConfig'); $searchConfig->setPageId($id); $responseData = $this->wikiaSearch->getKeywords($searchConfig); $this->response->setData($responseData); $this->response->setFormat('json'); }
/** * @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; }
/** * Prevents XSS and escapes characters used in Lucene query syntax. * Any query string transformations before sending to backend should be placed here. * @see WikiaSearchTest::testSanitizeQuery * @param string $query * @return string */ public static function sanitizeQuery($query) { wfProfileIn(__METHOD__); if (self::$queryHelper === null) { self::$queryHelper = new Solarium_Query_Helper(); } // non-indexed number-string phrases issue workaround (RT #24790) $query = preg_replace('/(\\d+)([a-zA-Z]+)/i', '$1 $2', $query); // escape all lucene special characters: + - && || ! ( ) { } [ ] ^ " ~ * ? : \ (RT #25482) // added html entity decoding now that we're doing extra work to prevent xss $query = self::$queryHelper->escapeTerm(html_entity_decode($query, ENT_COMPAT, 'UTF-8')); wfProfileOut(__METHOD__); return $query; }
/** * 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; }
/** * Given a set of page IDs, deletes by query * @param array $documentIds * @return bool true */ public function deleteBatch(array $documentIds = array(), $verbosity = self::REINDEX_DEFAULT) { $updateHandler = $this->client->createUpdate(); foreach ($documentIds as $id) { $updateHandler->addDeleteQuery(WikiaSearch::valueForField('id', $id)); } $updateHandler->addCommit(); try { $this->client->update($updateHandler); $confirmationString = implode(' ', $documentIds) . ' ' . count($documentIds) . " document(s) deleted\n"; if ($verbosity == self::REINDEX_VERBOSE) { echo $confirmationString; } } catch (Exception $e) { $id = rand(1000, 9999); Wikia::Log(__METHOD__, $id, $e); if ($verbosity == self::REINDEX_VERBOSE) { echo "There was an error deleting from the index. Please search for {$id} in the logs.\n"; } } return true; }