Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/219169
Change subject: Use SearchInteractor in SpecialItemDisambiguation ...................................................................... Use SearchInteractor in SpecialItemDisambiguation Bug: T90692 Change-Id: I24bc570df0ef3d0c31a888814c304e416684c7a9 --- M repo/includes/ItemDisambiguation.php M repo/includes/specials/SpecialItemDisambiguation.php M repo/tests/phpunit/includes/ItemDisambiguationTest.php M repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php 4 files changed, 247 insertions(+), 410 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/69/219169/1 diff --git a/repo/includes/ItemDisambiguation.php b/repo/includes/ItemDisambiguation.php index 4f045ba..aa428c0 100644 --- a/repo/includes/ItemDisambiguation.php +++ b/repo/includes/ItemDisambiguation.php @@ -3,9 +3,10 @@ namespace Wikibase; use Html; -use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Term\Term; use Wikibase\Lib\EntityIdFormatter; use Wikibase\Lib\LanguageNameLookup; +use Wikibase\Repo\Interactors\TermSearchInteractor; /** * Class representing the disambiguation of a list of WikibaseItems. @@ -17,8 +18,14 @@ * @author jeblad * @author Jeroen De Dauw < jeroended...@gmail.com > * @author Daniel Kinzler + * @authro Adam Shorland */ class ItemDisambiguation { + + /** + * @var EntityIdFormatter + */ + private $linkFormatter; /** * @var LanguageNameLookup @@ -28,37 +35,23 @@ /** * @var string */ - private $searchLangCode; - - /** - * @var string - */ - private $userLangCode; - - /** - * @var EntityIdFormatter - */ - private $linkFormatter; + private $displayLanguageCode; /** * @since 0.5 * - * @param string $searchLangCode The language the search was performed for. - * @param string $userLangCode The user's interface language. - * @param LanguageNameLookup $languageNameLookup * @param EntityIdFormatter $linkFormatter A formatter for generating HTML links for a given EntityId. + * @param LanguageNameLookup $languageNameLookup + * @param string $displayLanguageCode */ public function __construct( - $searchLangCode, - $userLangCode, + EntityIdFormatter $linkFormatter, LanguageNameLookup $languageNameLookup, - EntityIdFormatter $linkFormatter + $displayLanguageCode ) { - $this->searchLangCode = $searchLangCode; - $this->userLangCode = $userLangCode; - $this->linkFormatter = $linkFormatter; $this->languageNameLookup = $languageNameLookup; + $this->displayLanguageCode = $displayLanguageCode; } /** @@ -66,69 +59,70 @@ * * @since 0.5 * - * @param Item[] $items + * @param array[] $searchResults as returned by TermSearchInteractor * * @return string HTML */ - public function getHTML( array $items ) { + public function getHTML( array $searchResults ) { return '<ul class="wikibase-disambiguation">' . implode( '', array_map( - array( $this, 'getItemHtml' ), - $items + array( $this, 'getResultHtml' ), + $searchResults ) ). '</ul>'; } /** - * @param Item $item + * @param array[] $searchResult * * @return string HTML */ - public function getItemHtml( Item $item ) { - $userLang = $this->userLangCode; - $searchLang = $this->searchLangCode; - - $result = $this->linkFormatter->formatEntityId( $item->getId() ); - - // Display the label in the searched language in case it is different than in - // the user language. - if ( $userLang !== $searchLang - && $item->getLabel( $userLang ) !== $item->getLabel( $searchLang ) ) { - $result .= $this->getLabelHtml( $item, $searchLang ); - }; - - $result .= $this->getDescriptionHtml( $item, $userLang ); - + public function getResultHtml( array $searchResult ) { + $result = $this->linkFormatter->formatEntityId( $searchResult['entityId'] ); + $result .= $this->getLabelHtml( + $searchResult[TermSearchInteractor::DISPLAYTERMS_KEY], + $searchResult[TermSearchInteractor::MATCHEDTERM_KEY] + ); + $result .= $this->getDescriptionHtml( + $searchResult[TermSearchInteractor::DISPLAYTERMS_KEY], + $searchResult[TermSearchInteractor::ENTITYID_KEY] + ); $result = Html::rawElement( 'li', array( 'class' => 'wikibase-disambiguation' ), $result ); - return $result; } /** - * Returns HTML representing the label in the given language. + * Returns HTML representing the label in the search language. * The result will include the language's name in the user language. * - * @param Item $item - * @param string $language + * If the label is the same as the label already displayed by the formatted + * ItemID link then no additional label will be displayed + * + * @param Term[] $displayTerms + * @param Term $matchedTerm * * @return string HTML */ - private function getLabelHtml( Item $item, $language ) { - $label = $item->getLabel( $language ); - + private function getLabelHtml( $displayTerms, $matchedTerm ) { + if( array_key_exists( TermIndexEntry::TYPE_LABEL, $displayTerms ) ) { + $displayLabel = $displayTerms[TermIndexEntry::TYPE_LABEL]; + } + if( isset( $displayLabel ) && $displayLabel->getText() == $matchedTerm->getText() ) { + return ''; + } + $label = $matchedTerm->getText(); + $language = $matchedTerm->getLanguageCode(); $labelElement = Html::element( 'span', array( 'class' => 'wb-itemlink-query-lang', 'lang' => $language ), $label ); - $msg = wfMessage( 'wikibase-itemlink-userlang-wrapper' ) ->rawParams( - $this->languageNameLookup->getName( $language, $this->userLangCode ), + $this->languageNameLookup->getName( $language, $this->displayLanguageCode ), $labelElement ); - return $msg->parse(); } @@ -139,34 +133,26 @@ * returns an empty string, because the entity ID was already used as * a label. * - * @param Item $item - * @param string $language + * @param Term[] $displayTerms + * @param string $entityId * * @return string HTML */ - private function getDescriptionHtml( Item $item, $language ) { - - // display the description in the user's language - $description = $item->getDescription( $language ); - if ( $description === false || $description === '' ) { - // Display the ID if no description is available - // do not display it if the ID was already displayed, i.e. if it was used instead of the label previously - $userLabel = $item->getLabel( $language ); - $idLabel = $item->getId()->getSerialization(); - - $html = $userLabel ? ' ' . $idLabel : ''; - } else { + private function getDescriptionHtml( $displayTerms, $entityId ) { + if( array_key_exists( TermIndexEntry::TYPE_DESCRIPTION, $displayTerms ) ) { $descriptionElement = Html::element( 'span', array( 'class' => 'wb-itemlink-description' ), - $description + $displayTerms[TermIndexEntry::TYPE_DESCRIPTION]->getText() ); - - $html = htmlspecialchars( wfMessage( 'colon-separator' )->plain() ) - . $descriptionElement; + return htmlspecialchars( wfMessage( 'colon-separator' )->plain() ) . $descriptionElement; + } else { + if( array_key_exists( TermIndexEntry::TYPE_LABEL, $displayTerms ) ) { + $entityIdElement = Html::element( 'span', array(), $entityId ); + return htmlspecialchars( wfMessage( 'colon-separator' )->plain() ) . $entityIdElement; + } + return ''; } - - return $html; } } diff --git a/repo/includes/specials/SpecialItemDisambiguation.php b/repo/includes/specials/SpecialItemDisambiguation.php index 272eab3..e58e86a 100644 --- a/repo/includes/specials/SpecialItemDisambiguation.php +++ b/repo/includes/specials/SpecialItemDisambiguation.php @@ -4,17 +4,14 @@ use Html; use Language; -use Wikibase\DataModel\Entity\Item; use Wikibase\ItemDisambiguation; use Wikibase\Lib\EntityIdHtmlLinkFormatter; use Wikibase\Lib\LanguageNameLookup; -use Wikibase\Lib\Store\EntityLookup; use Wikibase\Lib\Store\EntityRetrievingTermLookup; -use Wikibase\Lib\Store\EntityTitleLookup; use Wikibase\Lib\Store\LanguageLabelDescriptionLookup; +use Wikibase\Repo\Interactors\TermIndexSearchInteractor; use Wikibase\Repo\WikibaseRepo; use Wikibase\TermIndexEntry; -use Wikibase\TermIndex; /** * Enables accessing items by providing the label of the item and the language of the label. @@ -25,28 +22,19 @@ * @author John Erling Blad < jeb...@gmail.com > * @author Jeroen De Dauw < jeroended...@gmail.com > * @author Daniel Kinzler + * @author Adam Shorland */ class SpecialItemDisambiguation extends SpecialWikibasePage { /** - * @var TermIndex + * @var ItemDisambiguation DO NOT ACCESS DIRECTLY use this->getItemDisambiguation */ - private $termIndex; + private $itemDisambiguation; /** - * @var EntityLookup + * @var TermIndexSearchInteractor|null DO NOT ACCESS DIRECTLY use this->getSearchInteractor */ - private $entityLookup; - - /** - * @var EntityTitleLookup - */ - private $entityTitleLookup; - - /** - * @var LanguageNameLookup - */ - private $languageNameLookup; + private $searchInteractor = null; /** * @var int @@ -60,14 +48,6 @@ */ public function __construct() { parent::__construct( 'ItemDisambiguation', '', true ); - - $this->initServices( - WikibaseRepo::getDefaultInstance()->getStore()->getTermIndex(), - WikibaseRepo::getDefaultInstance()->getEntityLookup(), - WikibaseRepo::getDefaultInstance()->getEntityTitleLookup(), - new LanguageNameLookup() - ); - //@todo: make this configurable $this->limit = 100; } @@ -75,17 +55,51 @@ /** * Set service objects to use. Unit tests may call this to substitute mock * services. + * + * @param ItemDisambiguation $itemDisambiguation + * @param TermIndexSearchInteractor|null $searchInteractor */ public function initServices( - TermIndex $termIndex, - EntityLookup $entityLookup, - EntityTitleLookup $entityTitleLookup, - LanguageNameLookup $languageNameLookup + ItemDisambiguation $itemDisambiguation, + TermIndexSearchInteractor $searchInteractor ) { - $this->termIndex = $termIndex; - $this->entityLookup = $entityLookup; - $this->entityTitleLookup = $entityTitleLookup; - $this->languageNameLookup = $languageNameLookup; + $this->itemDisambiguation = $itemDisambiguation; + $this->searchInteractor = $searchInteractor; + } + + /** + * @param string $displayLanguageCode Only used if the service does not already exist + * + * @return TermIndexSearchInteractor + */ + private function getSearchInteractor( $displayLanguageCode ) { + if( $this->searchInteractor === null ) { + $interactor = WikibaseRepo::getDefaultInstance()->newTermSearchInteractor( $displayLanguageCode ); + $this->searchInteractor = $interactor; + } + return $this->searchInteractor; + } + + /** + * @return ItemDisambiguation + */ + private function getItemDisambiguation() { + if( $this->itemDisambiguation === null ) { + $itemDisambiguation = new ItemDisambiguation( + new EntityIdHtmlLinkFormatter( + new LanguageLabelDescriptionLookup( + new EntityRetrievingTermLookup( WikibaseRepo::getDefaultInstance()->getEntityLookup() ), + $this->getLanguage()->getCode() + ), + WikibaseRepo::getDefaultInstance()->getEntityTitleLookup(), + new LanguageNameLookup() + ), + new LanguageNameLookup(), + $this->getLanguage()->getCode() + ); + $this->itemDisambiguation = $itemDisambiguation; + } + return $this->itemDisambiguation; } /** @@ -102,7 +116,6 @@ $request = $this->getRequest(); $parts = $subPage === '' ? array() : explode( '/', $subPage, 2 ); $languageCode = $request->getVal( 'language', isset( $parts[0] ) ? $parts[0] : '' ); - if ( $languageCode === '' ) { $languageCode = $this->getLanguage()->getCode(); } @@ -118,15 +131,22 @@ // Display the result set if ( isset( $languageCode ) && isset( $label ) && $label !== '' ) { - $items = $this->findLabelUsage( - $languageCode, - $label + $searchInteractor = $this->getSearchInteractor( $languageCode ); + $searchInteractor->setLimit( $this->limit ); + $searchInteractor->setIsCaseSensitive( true ); + $searchInteractor->setIsPrefixSearch( false ); + $searchInteractor->setUseLanguageFallback( false ); + $searchResult = $searchInteractor->searchForTerms( + $label, + array( $languageCode ), + 'item', + array( TermIndexEntry::TYPE_LABEL ) ); //@todo: show a message if count( $items ) > $this->limit. - if ( 0 < count( $items ) ) { + if ( 0 < count( $searchResult ) ) { $this->getOutput()->setPageTitle( $this->msg( 'wikibase-disambiguation-title', $label )->escaped() ); - $this->displayDisambiguationPage( $items, $languageCode ); + $this->displayDisambiguationPage( $searchResult ); } else { $this->showNothingFound( $languageCode, $label ); } @@ -165,33 +185,11 @@ /** * Display disambiguation page. * - * @param Item[] $items - * @param string $languageCode + * @param array[] $searchResult */ - private function displayDisambiguationPage( array $items, $languageCode ) { - // @fixme it is confusing to have so many $langCodes here, coming from - // different places and maybe not necessary to be this way. - - // @fixme inject this! - $labelDescriptionLookup = new LanguageLabelDescriptionLookup( - new EntityRetrievingTermLookup( $this->entityLookup ), - $this->getLanguage()->getCode() - ); - - $linkFormatter = new EntityIdHtmlLinkFormatter( - $labelDescriptionLookup, - $this->entityTitleLookup, - $this->languageNameLookup - ); - - $disambiguationList = new ItemDisambiguation( - $languageCode, - $this->getContext()->getLanguage()->getCode(), - $this->languageNameLookup, - $linkFormatter - ); - - $html = $disambiguationList->getHTML( $items ); + private function displayDisambiguationPage( array $searchResult ) { + $itemDisambiguation = $this->getItemDisambiguation(); + $html = $itemDisambiguation->getHTML( $searchResult ); $this->getOutput()->addHTML( $html ); } @@ -264,56 +262,6 @@ . Html::closeElement( 'fieldset' ) . Html::closeElement( 'form' ) ); - } - - /** - * Finds items that use the given label in the given language. - * - * @todo: Make this use an EntityInfoBuilder or similar instead of loading full entities. - * @todo: Should search over aliases as well, not just labels! Needs smart display though... - * - * @param string $languageCode - * @param string $label - * - * @return Item[] - */ - private function findLabelUsage( $languageCode, $label ) { - //@todo: optionally - $protoTerms = array( - new TermIndexEntry( array( - 'termType' => TermIndexEntry::TYPE_LABEL, - 'termLanguage' => $languageCode, - 'termText' => $label - ) ), - ); - - //@todo: expose options - $options = array( - 'caseSensitive' => true, - 'prefixSearch' => false, - 'LIMIT' => $this->limit - ); - - $entityIds = $this->termIndex->getMatchingIDs( $protoTerms, Item::ENTITY_TYPE, $options ); - $entities = array(); - - $count = 0; - - foreach ( $entityIds as $entityId ) { - $entity = $this->entityLookup->getEntity( $entityId ); - - if ( $entity !== null ) { - $entities[] = $entity; - } - - $count++; - - if ( $count >= $this->limit ) { - break; - } - } - - return $entities; } } diff --git a/repo/tests/phpunit/includes/ItemDisambiguationTest.php b/repo/tests/phpunit/includes/ItemDisambiguationTest.php index 348f86a..33e9193 100644 --- a/repo/tests/phpunit/includes/ItemDisambiguationTest.php +++ b/repo/tests/phpunit/includes/ItemDisambiguationTest.php @@ -3,10 +3,12 @@ namespace Wikibase\Test; use MediaWikiTestCase; -use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Term\Term; use Wikibase\ItemDisambiguation; +use Wikibase\Lib\EntityIdFormatter; use Wikibase\Lib\LanguageNameLookup; +use Wikibase\TermIndexEntry; /** * @covers Wikibase\ItemDisambiguation @@ -19,101 +21,123 @@ * * @licence GNU GPL v2+ * @author Daniel Kinzler + * @author Adam Shorland */ class ItemDisambiguationTest extends \MediaWikiTestCase { /** - * @param string $searchLanguageCode - * @param string $userLanguageCode - * - * @return ItemDisambiguation + * @return EntityIdFormatter */ - private function newItemDisambiguation( $searchLanguageCode, $userLanguageCode ) { + private function getMockEntityIdFormatter() { $entityIdFormatter = $this->getMock( 'Wikibase\Lib\EntityIdFormatter' ); - $entityIdFormatter->expects( $this->any() ) ->method( 'formatEntityId' ) ->will( $this->returnCallback( function( ItemId $itemId ) { return $itemId->getSerialization(); } ) ); + return $entityIdFormatter; + } + /** + * @return ItemDisambiguation + */ + private function newItemDisambiguation() { return new ItemDisambiguation( - $searchLanguageCode, - $userLanguageCode, + $this->getMockEntityIdFormatter(), new LanguageNameLookup(), - $entityIdFormatter + 'en' ); } public function getHTMLProvider() { - $one = new Item( new ItemId( 'Q1' ) ); - $one->setLabel( 'en', 'one' ); - $one->setLabel( 'de', 'eins' ); - $one->setDescription( 'en', 'number' ); - $one->setDescription( 'de', 'Zahl' ); - - $oneone = new Item( new ItemId( 'Q11' ) ); - $oneone->setLabel( 'en', 'oneone' ); - $oneone->setLabel( 'de', 'einseins' ); - $cases = array(); $matchers = array(); + // No results $matchers['matches'] = array( 'tag' => 'ul', 'content' => '', 'attributes' => array( 'class' => 'wikibase-disambiguation' ), ); + $cases['No Results'] = array( array(), $matchers ); - $cases['empty'] = array( 'en', 'en', array(), $matchers ); + // One Normal Result + $matchers['matches'] = array( + 'tag' => 'ul', + 'children' => array( 'count' => 1 ), + 'attributes' => array( 'class' => 'wikibase-disambiguation' ), + ); + $matchers['one'] = array( + 'tag' => 'li', + 'content' => 'regexp:/^Q1[^1]/s', + ); + $matchers['one/desc'] = array( + 'tag' => 'span', + 'content' => 'DisplayDescription', + 'attributes' => array( 'class' => 'wb-itemlink-description' ), + ); + $cases['One Normal Result'] = array( + array( + array( + 'entityId' => new ItemId( 'Q1' ), + 'matchedTerm' => new Term( 'de', 'Foo' ), + 'displayTerms' => array( + TermIndexEntry::TYPE_LABEL => new Term( 'en', 'DisplayLabel' ), + TermIndexEntry::TYPE_DESCRIPTION => new Term( 'en', 'DisplayDescription' ), + ), + ), + ), + $matchers + ); - // en/one + // Two Results - (1 - No Label in display Language, 2 - No Description) $matchers['matches'] = array( 'tag' => 'ul', 'children' => array( 'count' => 2 ), 'attributes' => array( 'class' => 'wikibase-disambiguation' ), ); - $matchers['one'] = array( 'tag' => 'li', - 'content' => 'regexp:/^Q1[^1]/s', + 'content' => 'regexp:/^Q2[^1]/s', ); - + $matchers['one/label'] = array( + 'tag' => 'span', + 'content' => 'Foo', + 'attributes' => array( 'class' => 'wb-itemlink-query-lang', 'lang' => 'de' ), + ); $matchers['one/desc'] = array( 'tag' => 'span', - 'content' => 'number', + 'content' => 'DisplayDescription', 'attributes' => array( 'class' => 'wb-itemlink-description' ), ); - - $matchers['oneone'] = array( + $matchers['two'] = array( 'tag' => 'li', - 'content' => 'regexp:/^Q11/s', + 'content' => 'regexp:/^Q3[^1]/s', ); - - $matchers['oneone/desc'] = array( + $matchers['two/desc'] = array( 'tag' => 'span', - //'content' => 'Q11', - 'attributes' => array( 'class' => 'wb-itemlink-description' ), + 'content' => 'Q3', + 'attributes' => array( ), ); - - $cases['en/one'] = array( 'en', 'en', array( $one, $oneone ), $matchers ); - - // de/eins - $matchers['one/de'] = array( - 'tag' => 'span', - 'parent' => array( 'tag' => 'li' ), - 'content' => 'eins', - 'attributes' => array( 'lang' => 'de' ), + $cases['Two Results'] = array( + array( + array( + 'entityId' => new ItemId( 'Q2' ), + 'matchedTerm' => new Term( 'de', 'Foo' ), + 'displayTerms' => array( + TermIndexEntry::TYPE_DESCRIPTION => new Term( 'en', 'DisplayDescription' ), + ), + ), + array( + 'entityId' => new ItemId( 'Q3' ), + 'matchedTerm' => new Term( 'de', 'Foo' ), + 'displayTerms' => array( + TermIndexEntry::TYPE_LABEL => new Term( 'en', 'DisplayLabel' ), + ), + ), + ), + $matchers ); - - $matchers['oneone/de'] = array( - 'tag' => 'span', - //'parent' => array( 'tag' => 'li' ), // PHPUnit's assertTag doesnt like this here - 'content' => 'einseins', - 'attributes' => array( 'lang' => 'de' ), - ); - - $cases['de/eins'] = array( 'de', 'en', array( $one, $oneone ), $matchers ); return $cases; } @@ -121,10 +145,10 @@ /** * @dataProvider getHTMLProvider */ - public function testGetHTML( $searchLanguageCode, $userLanguageCode, array $items, array $matchers ) { - $disambig = $this->newItemDisambiguation( $searchLanguageCode, $userLanguageCode ); + public function testGetHTML( array $searchResults, array $matchers ) { + $disambig = $this->newItemDisambiguation(); - $html = $disambig->getHTML( $items ); + $html = $disambig->getHTML( $searchResults ); foreach ( $matchers as $key => $matcher ) { MediaWikiTestCase::assertTag( $matcher, $html, "Failed to match HTML output with tag '{$key}'" ); diff --git a/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php b/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php index 124d3e0..648aadf 100644 --- a/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php @@ -3,14 +3,12 @@ namespace Wikibase\Test; use FauxRequest; -use Title; -use Wikibase\DataModel\Entity\EntityId; -use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\Lib\Store\EntityLookup; -use Wikibase\Lib\Store\EntityTitleLookup; +use Wikibase\DataModel\Term\Term; +use Wikibase\ItemDisambiguation; +use Wikibase\Repo\Interactors\TermIndexSearchInteractor; use Wikibase\Repo\Specials\SpecialItemDisambiguation; -use Wikibase\TermIndex; +use Wikibase\TermIndexEntry; /** * @covers Wikibase\Repo\Specials\SpecialItemDisambiguation @@ -25,97 +23,66 @@ * * @licence GNU GPL v2+ * @author Daniel Kinzler + * @author Adam Shorland */ class SpecialItemDisambiguationTest extends SpecialPageTestBase { /** - * @return EntityTitleLookup + * @return ItemDisambiguation */ - private function getEntityTitleLookup() { - $mock = $this->getMock( 'Wikibase\Lib\Store\EntityTitleLookup' ); + private function getMockItemDisambiguation( ) { + $mock = $this->getMockBuilder( 'Wikibase\ItemDisambiguation' ) + ->disableOriginalConstructor() + ->getMock(); $mock->expects( $this->any() ) - ->method( 'getTitleForId' ) - ->will( $this->returnCallback( - function ( EntityId $id ) { - return Title::makeTitle( NS_MAIN, $id->getSerialization() ); - } - ) ); - + ->method( 'getHTML' ) + ->will( $this->returnCallback( function ( $searchResult ) { + return '<span class="mock-span" >ItemDisambiguationHTML-' . count( $searchResult ) . '</span>'; + } ) ); return $mock; } /** - * @return TermIndex + * @return TermIndexSearchInteractor */ - private function getTermIndex() { - // Matches TermIndex::getEntityIdsForLabel shall return. - // Array keys are derived from the function parameters - $matches = array( - 'one,en,item,' => array( - new ItemId( 'Q1' ), - new ItemId( 'Q11' ), + private function getMockSearchInteractor() { + $returnResults = array( + array( + 'entityId' => new ItemId( 'Q2' ), + 'matchedTerm' => new Term( 'fr', 'Foo' ), + 'displayTerms' => array( + TermIndexEntry::TYPE_DESCRIPTION => new Term( 'en', 'DisplayDescription' ), + ), ), - 'eins,de,item,' => array( - new ItemId( 'Q1' ), - new ItemId( 'Q11' ), + array( + 'entityId' => new ItemId( 'Q3' ), + 'matchedTerm' => new Term( 'fr', 'Foo' ), + 'displayTerms' => array( + TermIndexEntry::TYPE_LABEL => new Term( 'en', 'DisplayLabel' ), + ), ), ); - - $mock = $this->getMock( 'Wikibase\TermIndex' ); + $mock = $this->getMockBuilder( 'Wikibase\Repo\Interactors\TermIndexSearchInteractor' ) + ->disableOriginalConstructor() + ->getMock(); $mock->expects( $this->any() ) - ->method( 'getMatchingIDs' ) - ->will( $this->returnCallback( - function ( array $terms, $entityType, array $options = array() ) - use ( $matches ) - { - $term = reset( $terms ); - $fuzzySearch = isset( $options['caseSensitive'] ) && !$options['caseSensitive']; - $key = $term->getText() . ',' . $term->getLanguage() . ',' . $entityType . ',' . $fuzzySearch; - return isset( $matches[$key] ) ? $matches[$key] : array(); - } - ) ); - + ->method( 'searchForTerms' ) + ->with( + $this->equalTo( 'Foo' ), + $this->equalTo( array( 'fr' ) ), + $this->equalTo( 'item' ), + $this->equalTo( array( TermIndexEntry::TYPE_LABEL ) ) + ) + ->will( $this->returnValue( $returnResults ) ); return $mock; - } - - /** - * @return EntityLookup - */ - private function getEntityLookup() { - $repo = new MockRepository(); - - $one = new Item( new ItemId( 'Q1' ) ); - $one->setLabel( 'en', 'one' ); - $one->setLabel( 'de', 'eins' ); - $one->setDescription( 'en', 'number' ); - $one->setDescription( 'de', 'Zahl' ); - - $repo->putEntity( $one ); - - $oneone = new Item( new ItemId( 'Q11' ) ); - $oneone->setLabel( 'en', 'oneone' ); - $oneone->setLabel( 'de', 'einseins' ); - - $repo->putEntity( $oneone ); - - return $repo; } protected function newSpecialPage() { $page = new SpecialItemDisambiguation(); - - $languageNameLookup = $this->getMock( 'Wikibase\Lib\LanguageNameLookup' ); - $languageNameLookup->expects( $this->any() ) - ->method( 'getName' ) - ->will( $this->returnValue( 'LANGUAGE NAME' ) ); - $page->initServices( - $this->getTermIndex(), - $this->getEntityLookup(), - $this->getEntityTitleLookup(), - $languageNameLookup + $this->getMockItemDisambiguation(), + $this->getMockSearchInteractor() ); - return $page; } @@ -148,103 +115,15 @@ $cases['empty'] = array( '', array(), null, $matchers ); - // en/one - $matchers['language']['attributes']['value'] = 'en'; - $matchers['label']['attributes']['value'] = 'one'; - + // fr/Foo + $matchers['language']['attributes']['value'] = 'fr'; + $matchers['label']['attributes']['value'] = 'Foo'; $matchers['matches'] = array( - 'tag' => 'ul', - 'children' => array( 'count' => 2 ), - 'attributes' => array( 'class' => 'wikibase-disambiguation' ), - ); - - $matchers['one'] = array( - 'tag' => 'a', - 'parent' => array( 'tag' => 'li' ), - 'content' => 'one', - 'attributes' => array( 'title' => 'Q1' ), - ); - - $matchers['one/desc'] = array( 'tag' => 'span', - 'content' => 'number', - 'attributes' => array( 'class' => 'wb-itemlink-description' ), + 'content' => 'ItemDisambiguationHTML-2', + 'attributes' => array( 'class' => 'mock-span' ), ); - - $matchers['oneone'] = array( - 'tag' => 'a', - //'parent' => array( 'tag' => 'li' ), // PHPUnit's assertTag doesnt like this here - 'content' => 'oneone', - 'attributes' => array( 'title' => 'Q11' ), - ); - - $matchers['oneone/desc'] = array( - 'tag' => 'span', - //'content' => 'Q11', - 'attributes' => array( 'class' => 'wb-itemlink-description' ), - ); - - $cases['en/one'] = array( 'en/one', array(), 'en', $matchers ); - - // de/eins - $matchers['language']['attributes']['value'] = 'de'; - $matchers['label']['attributes']['value'] = 'eins'; - - $matchers['one'] = array( - 'tag' => 'a', - 'parent' => array( 'tag' => 'li' ), - 'content' => 'one', - 'attributes' => array( 'title' => 'Q1' ), - ); - - $matchers['oneone'] = array( - 'tag' => 'a', - //'parent' => array( 'tag' => 'li' ), // PHPUnit's assertTag doesnt like this here - 'content' => 'oneone', - 'attributes' => array( 'title' => 'Q11' ), - ); - - $matchers['one/de'] = array( - 'tag' => 'span', - 'parent' => array( 'tag' => 'li' ), - 'content' => 'eins', - 'attributes' => array( 'lang' => 'de' ), - ); - - $matchers['oneone/de'] = array( - 'tag' => 'span', - //'parent' => array( 'tag' => 'li' ), // PHPUnit's assertTag doesnt like this here - 'content' => 'einseins', - 'attributes' => array( 'lang' => 'de' ), - ); - - $cases['de/eins'] = array( 'de/eins', array(), 'en', $matchers ); - - // en/unknown - $matchers['language']['attributes']['value'] = 'en'; - $matchers['label']['attributes']['value'] = 'unknown'; - - unset( $matchers['matches'] ); - unset( $matchers['one'] ); - unset( $matchers['one/desc'] ); - unset( $matchers['oneone'] ); - unset( $matchers['one/de'] ); - unset( $matchers['oneone/de'] ); - unset( $matchers['oneone/desc'] ); - - $matchers['sorry'] = array( - 'tag' => 'p', - 'content' => 'regexp:/^Sorry.*found/' - ); - - $cases['en/unknown'] = array( 'en/unknown', array(), 'en', $matchers ); - - // invalid/unknown - $matchers['language']['attributes']['value'] = 'invalid'; - $matchers['label']['attributes']['value'] = 'unknown'; - $matchers['sorry']['content'] = 'regexp:/^Sorry.*language/'; - - $cases['invalid/unknown'] = array( 'invalid/unknown', array(), 'en', $matchers ); + $cases['en/Foo'] = array( 'fr/Foo', array(), 'en', $matchers ); return $cases; } -- To view, visit https://gerrit.wikimedia.org/r/219169 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I24bc570df0ef3d0c31a888814c304e416684c7a9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits