Skip to content

Commit 6dbdcff

Browse files
JeroenDeDauwclaude
andcommitted
Treat piped item types as alternatives instead of one invalid id
`haswbfacet:P1=Q1|Q2` on the item type property was handed to `ItemId` whole, which rejected it, leaving the query with no item type: the search applied no type filter, the sidebar showed no facets, and `action=wbfacetsearch` failed with `wbfs-item-type-required`. `|` already means OR in a facet value filter, and repeated item type tokens already union, so the value is now split on `|`, valid ids added and invalid segments skipped as a single invalid id already was. Piped and repeated forms now select the same items and the same facets, which remain those of the first item type in the expression. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent f41af94 commit 6dbdcff

3 files changed

Lines changed: 93 additions & 10 deletions

File tree

‎src/Application/QueryStringParser.php‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function parse( string $queryString ): Query {
2323

2424
foreach ( $this->splitQueryString( $queryString ) as $part ) {
2525
if ( $this->isInstanceOfPart( $part ) ) {
26-
$itemTypes = $this->extractItemTypes( $part, $itemTypes );
26+
$itemTypes = [ ...$itemTypes, ...$this->extractItemTypes( $part ) ];
2727
} elseif ( $this->isFacetPart( $part ) ) {
2828
$constraints = $constraints->withConstraint( $this->handleFacetPart( $part, $constraints ) );
2929
}
@@ -60,23 +60,30 @@ private function isFacetPart( string $part ): bool {
6060
}
6161

6262
/**
63-
* @param ItemId[] $itemTypes
6463
* @return ItemId[]
6564
*/
66-
private function extractItemTypes( string $part, array &$itemTypes ): array {
67-
$itemTypeStr = substr( $part, strlen( 'haswbfacet:' . $this->itemTypeProperty->getSerialization() . '=' ) );
65+
private function extractItemTypes( string $part ): array {
66+
$value = substr( $part, strlen( 'haswbfacet:' . $this->itemTypeProperty->getSerialization() . '=' ) );
6867

69-
if ( $itemTypeStr === '' ) {
70-
return $itemTypes;
68+
$itemTypes = [];
69+
70+
foreach ( explode( '|', $value ) as $itemTypeString ) {
71+
$itemType = $this->newItemId( $itemTypeString );
72+
73+
if ( $itemType !== null ) {
74+
$itemTypes[] = $itemType;
75+
}
7176
}
7277

78+
return $itemTypes;
79+
}
80+
81+
private function newItemId( string $serialization ): ?ItemId {
7382
try {
74-
$itemTypes[] = new ItemId( $itemTypeStr );
83+
return new ItemId( $serialization );
7584
} catch ( InvalidArgumentException ) {
76-
return $itemTypes;
85+
return null;
7786
}
78-
79-
return $itemTypes;
8087
}
8188

8289
private function handleFacetPart( string $part, PropertyConstraintsList $constraintsList ): PropertyConstraints {

‎tests/phpunit/Application/QueryStringParserTest.php‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,63 @@ public function testIgnoresInvalidItemTypes(): void {
103103
$this->assertEquals( $itemTypes, $query->getItemTypes() );
104104
}
105105

106+
public function testParsesOrValuesAsMultipleItemTypes(): void {
107+
$parser = $this->newQueryStringParser( itemTypeProperty: 'P32' );
108+
$query = $parser->parse( 'unrelated haswbfacet:P32=Q68|Q67|Q69 unrelated' );
109+
110+
$itemTypes = [
111+
new ItemId( 'Q68' ),
112+
new ItemId( 'Q67' ),
113+
new ItemId( 'Q69' ),
114+
];
115+
116+
$this->assertEquals( $itemTypes, $query->getItemTypes() );
117+
}
118+
119+
public function testIgnoresInvalidOrValueItemTypes(): void {
120+
$parser = $this->newQueryStringParser( itemTypeProperty: 'P32' );
121+
$query = $parser->parse( 'haswbfacet:P32=Q68|invalid|Q69' );
122+
123+
$itemTypes = [
124+
new ItemId( 'Q68' ),
125+
new ItemId( 'Q69' ),
126+
];
127+
128+
$this->assertEquals( $itemTypes, $query->getItemTypes() );
129+
}
130+
131+
public function testIgnoresEmptyOrValueItemTypes(): void {
132+
$parser = $this->newQueryStringParser( itemTypeProperty: 'P32' );
133+
$query = $parser->parse( 'haswbfacet:P32=|Q68|' );
134+
135+
$itemTypes = [
136+
new ItemId( 'Q68' ),
137+
];
138+
139+
$this->assertEquals( $itemTypes, $query->getItemTypes() );
140+
}
141+
142+
public function testHasNoItemTypesWhenTheValueIsEmpty(): void {
143+
$parser = $this->newQueryStringParser( itemTypeProperty: 'P32' );
144+
$query = $parser->parse( 'haswbfacet:P32=' );
145+
146+
$this->assertSame( [], $query->getItemTypes() );
147+
}
148+
149+
public function testCombinesOrValueAndRepeatedItemTypes(): void {
150+
$parser = $this->newQueryStringParser( itemTypeProperty: 'P32' );
151+
$query = $parser->parse( 'haswbfacet:P32=Q68|Q67|Q66 haswbfacet:P32=Q69' );
152+
153+
$itemTypes = [
154+
new ItemId( 'Q68' ),
155+
new ItemId( 'Q67' ),
156+
new ItemId( 'Q66' ),
157+
new ItemId( 'Q69' ),
158+
];
159+
160+
$this->assertEquals( $itemTypes, $query->getItemTypes() );
161+
}
162+
106163
public function testParsesAndValues(): void {
107164
$parser = $this->newQueryStringParser();
108165
$query = $parser->parse( 'haswbfacet:P42=foo haswbfacet:P42=bar' );

‎tests/phpunit/Persistence/Search/Query/HasWbFacetFeatureTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace ProfessionalWiki\WikibaseFacetedSearch\Tests\Persistence\Search\Query;
66

77
use CirrusSearch\Query\KeywordFeatureAssertions;
8+
use CirrusSearch\Search\SearchContext;
9+
use CirrusSearch\SearchConfig;
10+
use Elastica\Query\Terms;
811
use ProfessionalWiki\WikibaseFacetedSearch\Application\Config;
912
use ProfessionalWiki\WikibaseFacetedSearch\Application\QueryStringParser;
1013
use ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query\DelegatingFacetQueryBuilder;
@@ -43,6 +46,22 @@ public static function noDataProvider() {
4346
];
4447
}
4548

49+
public function testFiltersOnEveryItemTypeOfAnOrValue(): void {
50+
$term = 'haswbfacet:P42=Q68|Q67|Q69';
51+
$context = $this->newSearchContext( $term );
52+
53+
$this->newHasWbFacetFeature()->apply( $context, $term );
54+
55+
$this->assertEquals( [ new Terms( 'wbfs_P42', [ 'Q68', 'Q67', 'Q69' ] ) ], $context->getFilters() );
56+
}
57+
58+
// KeywordFeatureAssertions::assertFilter is not used: it requires a FilterQueryFeature and never stubs getOriginalSearchTerm().
59+
private function newSearchContext( string $term ): SearchContext {
60+
$context = new SearchContext( new SearchConfig() );
61+
$context->setOriginalSearchTerm( $term );
62+
return $context;
63+
}
64+
4665
private function newHasWbFacetFeature(): HasWbFacetFeature {
4766
return new HasWbFacetFeature(
4867
new Config(),

0 commit comments

Comments
 (0)