/** * @dataProvider termProvider */ public function testRebuildSearchKey($languageCode, $termText, $searchText, $matches) { /* @var TermSqlIndex $termCache */ $termCache = WikibaseRepo::getDefaultInstance()->getStore()->getTermIndex(); // make term in item $item = new Item(new ItemId('Q42')); $item->setLabel($languageCode, $termText); // save term $termCache->clear(); $termCache->saveTermsOfEntity($item); // remove search key $dbw = wfGetDB(DB_MASTER); $dbw->update($termCache->getTableName(), array('term_search_key' => ''), array(), __METHOD__); // rebuild search key $builder = new TermSearchKeyBuilder($termCache); $builder->setRebuildAll(true); $builder->rebuildSearchKey(); // remove search key $term = new TermIndexEntry(); $term->setLanguage($languageCode); $term->setText($searchText); $options = array('caseSensitive' => false); $obtainedTerms = $termCache->getMatchingTerms(array($term), TermIndexEntry::TYPE_LABEL, Item::ENTITY_TYPE, $options); $this->assertEquals($matches ? 1 : 0, count($obtainedTerms)); if ($matches) { $obtainedTerm = array_shift($obtainedTerms); $this->assertEquals($termText, $obtainedTerm->getText()); } }
/** * @dataProvider termProvider */ public function testGetMatchingTerms2($languageCode, $termText, $searchText, $matches) { $termIndex = $this->getTermIndex(); $termIndex->clear(); $item = new Item(new ItemId('Q42')); $item->setLabel($languageCode, $termText); $termIndex->saveTermsOfEntity($item); $term = new TermIndexEntry(); $term->setLanguage($languageCode); $term->setText($searchText); $options = array('caseSensitive' => false); //FIXME: test with arrays for term types and entity types! $obtainedTerms = $termIndex->getMatchingTerms(array($term), TermIndexEntry::TYPE_LABEL, Item::ENTITY_TYPE, $options); $this->assertEquals($matches ? 1 : 0, count($obtainedTerms)); if ($matches) { $obtainedTerm = array_shift($obtainedTerms); $this->assertEquals($termText, $obtainedTerm->getText()); } }
public function getTermKey(TermIndexEntry $term) { $key = ''; if ($term->getEntityId() !== null) { $key .= $term->getEntityId()->getSerialization(); } $key .= '/'; if ($term->getType() !== null) { $key .= $term->getType(); } $key .= '.'; if ($term->getLanguage() !== null) { $key .= $term->getLanguage(); } $key .= ':'; if ($term->getText() !== null) { $key .= $term->getText(); } return $key; }
/** * @param TermIndexEntry $termIndexEntry * * @return TermSearchResult */ private function convertToSearchResult(TermIndexEntry $termIndexEntry) { $entityId = $termIndexEntry->getEntityId(); return new TermSearchResult($termIndexEntry->getTerm(), $termIndexEntry->getType(), $entityId, $this->getLabelDisplayTerm($entityId), $this->getDescriptionDisplayTerm($entityId)); }
/** * @dataProvider provideTermIndexEntryData */ public function testGetTerm_throwsException($termIndexEntryData) { $termIndexEntry = new TermIndexEntry($termIndexEntryData); $this->setExpectedException('MWException', 'Can not construct Term from partial TermIndexEntry'); $termIndexEntry->getTerm(); }
/** * @param array $entry * @param TermIndexEntry $term */ private function checkAndSetAlias(array &$entry, TermIndexEntry $term) { // Do not add more than one matching alias to the "aliases" field. if (!empty($entry['aliases'])) { return; } if (preg_match($this->searchPattern, $term->getText())) { if (!isset($entry['aliases'])) { $entry['aliases'] = array(); ApiResult::setIndexedTagName($entry['aliases'], 'alias'); } $entry['aliases'][] = $term->getText(); } }
/** * @param TermIndexEntry $term * @param TermIndexEntry[] $templates * @param array $options * * @return bool */ private function termMatchesTemplates(TermIndexEntry $term, array $templates, array $options = array()) { foreach ($templates as $template) { if ($template->getType() !== null && $template->getType() != $term->getType()) { continue; } if ($template->getEntityType() !== null && $template->getEntityType() != $term->getEntityType()) { continue; } if ($template->getLanguage() !== null && $template->getLanguage() != $term->getLanguage()) { continue; } if ($template->getText() !== null && !$this->textMatches($template->getText(), $term->getText(), $options)) { continue; } if ($template->getEntityId() !== null && !$template->getEntityId()->equals($term->getEntityType())) { continue; } return true; } return false; }