-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite item link with page sitelink (#24)
- Loading branch information
Showing
7 changed files
with
232 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,13 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: '#^Call to method addHTML\(\) on an unknown class OutputPage\.$#' | ||
identifier: class.notFound | ||
message: '#^Method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:rewriteLinkForNonEntityResult\(\) never assigns null to &\$titleSnippet so it can be removed from the by\-ref type\.$#' | ||
identifier: parameterByRef.unusedType | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Call to method addModuleStyles\(\) on an unknown class OutputPage\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Call to method getNamespace\(\) on an unknown class Title\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Call to static method element\(\) on an unknown class Html\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Constant WB_NS_ITEM not found\.$#' | ||
identifier: constant.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Parameter \$output of method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:onSpecialSearchResultsAppend\(\) has invalid type OutputPage\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Parameter \$specialSearch of method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:onShowSearchHitTitle\(\) has invalid type SpecialSearch\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Parameter \$specialSearch of method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:onSpecialSearchResultsAppend\(\) has invalid type SpecialSearch\.$#' | ||
identifier: class.notFound | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php | ||
|
||
- | ||
message: '#^Parameter \$title of method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:onShowSearchHitTitle\(\) has invalid type Title\.$#' | ||
identifier: class.notFound | ||
message: '#^Method ProfessionalWiki\\WikibaseFacetedSearch\\EntryPoints\\WikibaseFacetedSearchHooks\:\:rewriteLinkForNonEntityResult\(\) never assigns string to &\$titleSnippet so it can be removed from the by\-ref type\.$#' | ||
identifier: parameterByRef.unusedType | ||
count: 1 | ||
path: src/EntryPoints/WikibaseFacetedSearchHooks.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence; | ||
|
||
use Title; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
|
||
interface ItemPageLookup { | ||
|
||
public function getPageTitle( ItemId $itemId ): ?Title; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence; | ||
|
||
use Title; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\Lib\Store\SiteLinkLookup; | ||
|
||
class SiteLinkItemPageLookup implements ItemPageLookup { | ||
|
||
public function __construct( | ||
private readonly SiteLinkLookup $siteLinksStore, | ||
private readonly string $siteLinkSiteId | ||
) { | ||
} | ||
|
||
public function getPageTitle( ItemId $itemId ): ?Title { | ||
$siteLink = array_filter( | ||
$this->siteLinksStore->getSiteLinksForItem( $itemId ), | ||
fn( $siteLink ) => $siteLink->getSiteId() === $this->siteLinkSiteId | ||
)[0] ?? null; | ||
|
||
if ( $siteLink === null ) { | ||
return null; | ||
} | ||
|
||
return Title::newFromText( $siteLink->getPageName() ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Tests\Persistence; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Persistence\SiteLinkItemPageLookup; | ||
use Title; | ||
use Wikibase\DataModel\Entity\Item; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\Lib\Store\HashSiteLinkStore; | ||
|
||
/** | ||
* @covers \ProfessionalWiki\WikibaseFacetedSearch\Persistence\SiteLinkItemPageLookup | ||
*/ | ||
class SiteLinkItemPageLookupTest extends TestCase { | ||
|
||
private const SITE_ID = 'testSiteId'; | ||
|
||
private HashSiteLinkStore $siteLinkStore; | ||
private SiteLinkItemPageLookup $lookup; | ||
|
||
protected function setUp(): void { | ||
$this->siteLinkStore = new HashSiteLinkStore(); | ||
$this->lookup = new SiteLinkItemPageLookup( | ||
$this->siteLinkStore, | ||
self::SITE_ID | ||
); | ||
} | ||
|
||
public function testReturnsPageWhenSiteLinkExists(): void { | ||
$this->createItemWithSiteLink( 'Q42', self::SITE_ID, 'Page for Q42' ); | ||
|
||
$this->assertItemPageHasTitle( 'Q42', 'Page for Q42' ); | ||
} | ||
|
||
private function createItemWithSiteLink( string $itemId, string $siteId, string $pageName ): void { | ||
$item = new Item( new ItemId( $itemId ) ); | ||
$item->getSiteLinkList()->addNewSiteLink( $siteId, $pageName ); | ||
$this->siteLinkStore->saveLinksOfItem( $item ); | ||
} | ||
|
||
private function assertItemPageHasTitle( string $itemId, $pageTitle ): void { | ||
$this->assertEquals( | ||
Title::newFromText( $pageTitle ), | ||
$this->lookup->getPageTitle( new ItemId( $itemId ) ) | ||
); | ||
} | ||
|
||
public function testReturnsNullWhenNoSiteLinksExist(): void { | ||
$this->assertNull( $this->lookup->getPageTitle( new ItemId( 'Q404' ) ) ); | ||
} | ||
|
||
public function testReturnsNullWhenOnlyOtherSiteLinksExist(): void { | ||
$this->createItemWithSiteLink( 'Q100', 'otherSiteId', 'Other page' ); | ||
$this->createItemWithSiteLink( 'Q200', 'anotherSiteId', 'Another page' ); | ||
|
||
$this->assertNull( $this->lookup->getPageTitle( new ItemId( 'Q42' ) ) ); | ||
} | ||
|
||
public function testReturnsPageWhenManySiteLinksExist(): void { | ||
$this->createItemWithSiteLink( 'Q42', self::SITE_ID, 'Page for Q42' ); | ||
$this->createItemWithSiteLink( 'Q100', 'otherSiteId', 'Other page' ); | ||
$this->createItemWithSiteLink( 'Q200', 'anotherSiteId', 'Another page' ); | ||
|
||
$this->assertItemPageHasTitle( 'Q42', 'Page for Q42' ); | ||
} | ||
|
||
} |