Skip to content

Commit 6e4e036

Browse files
committed
Add NewStatement::withSubject()
This method is meant to be used in combination with `withSomeGuid()` and sets the prefix of the Statement ID. `withSomeGuid()` otherwise uses the Statement Property as the GUID prefix, which does not really make sense given that the Statement GUID is supposed to contain the *subject* ID. Change-Id: I579b7960d675871ea4d182c5df60d5e180e545c2
1 parent c3b7620 commit 6e4e036

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

lib/packages/wikibase/data-model/tests/unit/NewStatement.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class NewStatement {
4949
/** @var string|bool|null */
5050
private $guid;
5151

52+
private ?string $subjectId = null;
53+
5254
/**
5355
* @var Snak[]
5456
*/
@@ -152,10 +154,7 @@ public function withGuid( $guid ) {
152154
return $result;
153155
}
154156

155-
/**
156-
* @return static
157-
*/
158-
public function withSomeGuid() {
157+
public function withSomeGuid(): static {
159158
$result = clone $this;
160159
if ( $result->guid !== null ) {
161160
throw new LogicException( 'Cannot redefine GUID' );
@@ -166,6 +165,17 @@ public function withSomeGuid() {
166165
return $result;
167166
}
168167

168+
public function withSubject( EntityId|string $subject ): static {
169+
$result = clone $this;
170+
if ( $result->subjectId !== null ) {
171+
throw new LogicException( 'Cannot redefine subject' );
172+
}
173+
174+
$result->subjectId = (string)$subject;
175+
176+
return $result;
177+
}
178+
169179
/**
170180
* @param string|NumericPropertyId $propertyId
171181
* @param DataValue|EntityId|string $value If not a DataValue object, the builder tries to
@@ -230,8 +240,10 @@ public function build() {
230240
$result->setRank( $this->rank );
231241

232242
if ( $this->guid === self::GENERATE_GUID ) {
243+
// defaulting to the Property ID here doesn't really make sense. withSomeGuid() should be used in combination with withSubject()
244+
$subject = $this->subjectId ?? $this->propertyId;
233245
$result->setGuid(
234-
$this->propertyId->getSerialization() . '$' . $this->generateUuidV4()
246+
"$subject\$" . $this->generateUuidV4()
235247
);
236248
} elseif ( $this->guid ) {
237249
$result->setGuid( $this->guid );

repo/domains/reuse/tests/phpunit/Domain/Services/StatementReadModelConverterTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Wikibase\Repo\Tests\Domains\Reuse\Domain\Services;
44

55
use PHPUnit\Framework\TestCase;
6-
use Wikibase\DataModel\Entity\ItemId;
76
use Wikibase\DataModel\Entity\NumericPropertyId;
87
use Wikibase\DataModel\Services\Lookup\InMemoryDataTypeLookup;
9-
use Wikibase\DataModel\Statement\StatementGuid;
108
use Wikibase\DataModel\Tests\NewStatement;
119
use Wikibase\Repo\Domains\Reuse\Domain\Services\StatementReadModelConverter;
1210
use Wikibase\Repo\WikibaseRepo;
@@ -23,14 +21,14 @@ class StatementReadModelConverterTest extends TestCase {
2321
private const STRING_PROPERTY = 'P123';
2422

2523
public function testConvert_simpleStatement(): void {
26-
$id = new StatementGuid( new ItemId( 'Q123' ), 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE' );
2724
$statementWriteModel = NewStatement::noValueFor( 'P123' )
28-
->withGuid( (string)$id )
25+
->withSubject( 'Q123' )
26+
->withSomeGuid()
2927
->build();
3028

3129
$readModel = $this->newConverter()->convert( $statementWriteModel );
3230

33-
$this->assertEquals( $id, $readModel->id );
31+
$this->assertEquals( $statementWriteModel->getGuid(), $readModel->id );
3432
$this->assertSame( $statementWriteModel->getPropertyId(), $readModel->property->id );
3533
$this->assertSame( 'string', $readModel->property->dataType );
3634
}

repo/domains/reuse/tests/phpunit/Infrastructure/DataAccess/EntityLookupItemsBatchRetrieverTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ public function testGetItemsWithAliasesAndSitelinks(): void {
112112
}
113113

114114
public function testGetItemsWithStatements(): void {
115-
$item1StatementGuid = "$this->item1Id\$bed933b7-4207-d679-7571-3630cfb49d7f";
116115
$item1StatementPropertyId = 'P1';
117116
$item1StatementQualifierPropertyId = new NumericPropertyId( 'P42' );
118117
$item1Statement = NewStatement::noValueFor( $item1StatementPropertyId )
119-
->withGuid( $item1StatementGuid )
118+
->withSubject( $this->item1Id )
119+
->withSomeGuid()
120120
->withRank( 0 )
121121
->withQualifier( $item1StatementQualifierPropertyId, new StringValue( 'stringValue' ) )
122122
->build();
@@ -179,11 +179,11 @@ public function testGetItemsWithStatements(): void {
179179
}
180180

181181
public function testGetItemsWithStatementsWithReferences(): void {
182-
$item1StatementGuid = "$this->item1Id\$bed933b7-4207-d679-7571-3630cfb49d7f";
183182
$item1StatementPropertyId = 'P1';
184183
$item1StatementReferencePropertyId = new NumericPropertyId( 'P42' );
185184
$item1Statement = NewStatement::noValueFor( $item1StatementPropertyId )
186-
->withGuid( $item1StatementGuid )
185+
->withSubject( $this->item1Id )
186+
->withSomeGuid()
187187
->withReference( new Reference( [ new PropertySomeValueSnak( $item1StatementReferencePropertyId ) ] ) )
188188
->build();
189189

@@ -226,17 +226,17 @@ public function testGetItemsWithStatementsWithReferences(): void {
226226
}
227227

228228
public function testGetItemWithStatementsWithValue(): void {
229-
$item1StatementGuid = "$this->item1Id\$bed933b7-4207-d679-7571-3630cfb49d7f";
230-
$item1Statement2Guid = "$this->item1Id\$bed933b7-4207-d679-7571-3630cfb49d8f";
231229
$item1StatementPropertyId = 'P1';
232230
$item1Statement2PropertyId = 'P3';
233231
$itemValueItemId = 'Q6';
234232
$item1Statement = NewStatement::forProperty( $item1StatementPropertyId )
235-
->withGuid( $item1StatementGuid )
233+
->withSubject( $this->item1Id )
234+
->withSomeGuid()
236235
->withValue( 'stringValue' )
237236
->build();
238237
$item1Statement2 = NewStatement::forProperty( $item1Statement2PropertyId )
239-
->withGuid( $item1Statement2Guid )
238+
->withSubject( $this->item1Id )
239+
->withSomeGuid()
240240
->withValue( new ItemId( $itemValueItemId ) )
241241
->build();
242242

repo/domains/reuse/tests/phpunit/Infrastructure/GraphQL/GraphQLServiceTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,30 @@ public function queryProvider(): Generator {
152152
$qualifierStringValue = 'qualifierStringValue';
153153
$statementStringValue = 'statementStringValue';
154154
$statementWithStringValue = NewStatement::forProperty( ( $statementWithStringValuePropertyId ) )
155-
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d7f" )
155+
->withSubject( $itemId )
156+
->withSomeGuid()
156157
->withRank( 1 )
157158
->withQualifier( new NumericPropertyId( $qualifierPropertyId ), new StringValue( $qualifierStringValue ) )
158159
->withReference( new Reference( [ new PropertySomeValueSnak( new NumericPropertyId( $statementReferencePropertyId ) ) ] ) )
159160
->withValue( $statementStringValue )
160161
->build();
161162

162163
$statementWithItemValue = NewStatement::forProperty( ( $statementWithItemValuePropertyId ) )
163-
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d8f" )
164+
->withSubject( $itemId )
165+
->withSomeGuid()
164166
->withValue( new ItemId( $itemValueItemId ) )
165167
->withQualifier( $statementWithItemValueQualifierPropertyId, self::$qualifierValueItem->getId() )
166168
->build();
167169
$globeCoordinateValue = new GlobeCoordinateValue( new LatLongValue( 52.516, 13.383 ) );
168170
$statementWithGlobeCoordinateValue = NewStatement::forProperty( $statementWithGlobeCoordinateValuePropertyId )
169-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
171+
->withSubject( $itemId )
172+
->withSomeGuid()
170173
->withValue( $globeCoordinateValue )
171174
->build();
172175
$monolingualTextValue = new MonolingualTextValue( 'en', 'potato' );
173176
$statementWithMonolingualTextValue = NewStatement::forProperty( $statementWithMonolingualTextValuePropertyId )
174-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
177+
->withSubject( $itemId )
178+
->withSomeGuid()
175179
->withValue( $monolingualTextValue )
176180
->build();
177181
$quantityValue = new QuantityValue(
@@ -185,11 +189,13 @@ public function queryProvider(): Generator {
185189
'https://wikibase.example/wiki/Q321',
186190
);
187191
$statementWithQuantityValue = NewStatement::forProperty( $statementWithQuantityValuePropertyId )
188-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
192+
->withSubject( $itemId )
193+
->withSomeGuid()
189194
->withValue( $quantityValue )
190195
->build();
191196
$statementWithUnboundedQuantityValue = NewStatement::forProperty( $statementWithQuantityValuePropertyId )
192-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
197+
->withSubject( $itemId )
198+
->withSomeGuid()
193199
->withValue( $unboundedQuantityValue )
194200
->build();
195201
$timeValue = new TimeValue(
@@ -201,7 +207,8 @@ public function queryProvider(): Generator {
201207
calendarModel: 'http://www.wikidata.org/entity/Q1985727',
202208
);
203209
$statementWithTimeValue = NewStatement::forProperty( $statementWithTimeValuePropertyId )
204-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
210+
->withSubject( $itemId )
211+
->withSomeGuid()
205212
->withValue( $timeValue )
206213
->build();
207214
self::$propertyUsedAsValue = new Property(
@@ -210,15 +217,18 @@ public function queryProvider(): Generator {
210217
'string',
211218
);
212219
$statementWithPropertyValue = NewStatement::forProperty( $statementWithPropertyValuePropertyId )
213-
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
220+
->withSubject( $itemId )
221+
->withSomeGuid()
214222
->withValue( new EntityIdValue( self::$propertyUsedAsValue->getId() ) )
215223
->build();
216224

217225
$statementWithNoValue = NewStatement::noValueFor( ( $statementWithNoValuePropertyId ) )
218-
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d9f" )
226+
->withSubject( $itemId )
227+
->withSomeGuid()
219228
->build();
220229
$statementWithSomeValue = NewStatement::someValueFor( ( $statementWithSomeValuePropertyId ) )
221-
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d6f" )
230+
->withSubject( $itemId )
231+
->withSomeGuid()
222232
->build();
223233

224234
self::$sitelinkSite = new MediaWikiSite();

0 commit comments

Comments
 (0)