Skip to content

Commit

Permalink
Merge "REST: Create SetPropertyLabel happy path"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Oct 12, 2023
2 parents ad4d0dd + 0a2ca8d commit dd0e463
Show file tree
Hide file tree
Showing 23 changed files with 829 additions and 11 deletions.
5 changes: 5 additions & 0 deletions repo/rest-api/routes.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"method": "PATCH",
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\PatchItemAliasesRouteHandler::factory"
},
{
"path": "/wikibase/v0/entities/properties/{property_id}/labels/{language_code}",
"method": "PUT",
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\SetPropertyLabelRouteHandler::factory"
},
{
"path": "/wikibase/v0/entities/properties/{property_id}/aliases",
"method": "PATCH",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* @license GPL-2.0-or-later
*/
interface DeserializedItemLabelEditRequest extends DeserializedItemIdRequest {
public function getLabel(): Term;
public function getItemLabel(): Term;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCaseRequestValidation;

use Wikibase\DataModel\Term\Term;

/**
* @license GPL-2.0-or-later
*/
interface DeserializedPropertyLabelEditRequest extends DeserializedPropertyIdRequest {
public function getPropertyLabel(): Term;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\SetItemDescription\DeserializedSetItemDescriptionRequest;
use Wikibase\Repo\RestApi\Application\UseCases\SetItemLabel\DeserializedSetItemLabelRequest;
use Wikibase\Repo\RestApi\Application\UseCases\SetPropertyDescription\DeserializedSetPropertyDescriptionRequest;
use Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel\DeserializedSetPropertyLabelRequest;
use Wikibase\Repo\RestApi\Domain\Model\UserProvidedEditMetadata;

/**
Expand Down Expand Up @@ -80,7 +81,8 @@ class DeserializedRequestAdapter implements
DeserializedGetPropertyLabelRequest,
DeserializedGetPropertyDescriptionRequest,
DeserializedSetPropertyDescriptionRequest,
DeserializedPatchPropertyAliasesRequest
DeserializedPatchPropertyAliasesRequest,
DeserializedSetPropertyLabelRequest
{
private array $deserializedRequest;

Expand Down Expand Up @@ -128,14 +130,18 @@ public function getPatch(): array {
return $this->getRequestField( PatchRequest::class );
}

public function getLabel(): Term {
public function getItemLabel(): Term {
return $this->getRequestField( ItemLabelEditRequest::class );
}

public function getItemDescription(): Term {
return $this->getRequestField( ItemDescriptionEditRequest::class );
}

public function getPropertyLabel(): Term {
return $this->getRequestField( PropertyLabelEditRequest::class );
}

public function getPropertyDescription(): Term {
return $this->getRequestField( PropertyDescriptionEditRequest::class );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCaseRequestValidation;

/**
* @license GPL-2.0-or-later
*/
interface PropertyLabelEditRequest extends PropertyIdRequest, LanguageCodeRequest {
public function getLabel(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCaseRequestValidation;

use Wikibase\DataModel\Term\Term;

/**
* @license GPL-2.0-or-later
*/
class PropertyLabelEditRequestValidatingDeserializer {

public function validateAndDeserialize( PropertyLabelEditRequest $request ): Term {
// TODO: validation
return new Term( $request->getLanguageCode(), $request->getLabel() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
public function execute( SetItemLabelRequest $request ): SetItemLabelResponse {
$deserializedRequest = $this->validator->validateAndDeserialize( $request );
$itemId = $deserializedRequest->getItemId();
$label = $deserializedRequest->getLabel();
$label = $deserializedRequest->getItemLabel();

$this->assertItemExists->execute( $itemId );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel;

use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedEditMetadataRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedPropertyLabelEditRequest;

/**
* @license GPL-2.0-or-later
*/
interface DeserializedSetPropertyLabelRequest extends DeserializedPropertyLabelEditRequest, DeserializedEditMetadataRequest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel;

use Wikibase\Repo\RestApi\Domain\Model\EditMetadata;
use Wikibase\Repo\RestApi\Domain\Model\LabelEditSummary;
use Wikibase\Repo\RestApi\Domain\Services\PropertyRetriever;
use Wikibase\Repo\RestApi\Domain\Services\PropertyUpdater;

/**
* @license GPL-2.0-or-later
*/
class SetPropertyLabel {

private PropertyRetriever $propertyRetriever;
private PropertyUpdater $propertyUpdater;
private SetPropertyLabelValidator $validator;

public function __construct(
SetPropertyLabelValidator $validator,
PropertyRetriever $propertyRetriever,
PropertyUpdater $propertyUpdater
) {
$this->validator = $validator;
$this->propertyRetriever = $propertyRetriever;
$this->propertyUpdater = $propertyUpdater;
}

public function execute( SetPropertyLabelRequest $request ): SetPropertyLabelResponse {
$deserializedRequest = $this->validator->validateAndDeserialize( $request );
$propertyId = $deserializedRequest->getPropertyId();
$label = $deserializedRequest->getPropertyLabel();
$editMetadata = $deserializedRequest->getEditMetadata();

$property = $this->propertyRetriever->getProperty( $propertyId );
$labelExists = $property->getLabels()->hasTermForLanguage( $label->getLanguageCode() );
$property->getLabels()->setTerm( $label );

$editSummary = $labelExists
? LabelEditSummary::newReplaceSummary( $editMetadata->getComment(), $label )
: LabelEditSummary::newAddSummary( $editMetadata->getComment(), $label );

$newRevision = $this->propertyUpdater->update(
$property, // @phan-suppress-current-line PhanTypeMismatchArgumentNullable
new EditMetadata( $editMetadata->getTags(), $editMetadata->isBot(), $editSummary )
);

return new SetPropertyLabelResponse(
$newRevision->getProperty()->getLabels()[$label->getLanguageCode()],
$newRevision->getLastModified(),
$newRevision->getRevisionId(),
$labelExists
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel;

use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\EditMetadataRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyLabelEditRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest;

/**
* @license GPL-2.0-or-later
*/
class SetPropertyLabelRequest implements UseCaseRequest, PropertyLabelEditRequest, EditMetadataRequest {

private string $propertyId;
private string $languageCode;
private string $label;
private array $editTags;
private bool $isBot;
private ?string $comment;
private ?string $username;

public function __construct(
string $propertyId,
string $languageCode,
string $label,
array $editTags,
bool $isBot,
?string $comment,
?string $username
) {
$this->propertyId = $propertyId;
$this->languageCode = $languageCode;
$this->label = trim( $label );
$this->editTags = $editTags;
$this->isBot = $isBot;
$this->comment = $comment;
$this->username = $username;
}

public function getPropertyId(): string {
return $this->propertyId;
}

public function getLanguageCode(): string {
return $this->languageCode;
}

public function getLabel(): string {
return $this->label;
}

public function getEditTags(): array {
return $this->editTags;
}

public function isBot(): bool {
return $this->isBot;
}

public function getComment(): ?string {
return $this->comment;
}

public function getUsername(): ?string {
return $this->username;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel;

use Wikibase\Repo\RestApi\Domain\ReadModel\Label;

/**
* @license GPL-2.0-or-later
*/
class SetPropertyLabelResponse {

private Label $label;
private string $lastModified;
private int $revisionId;
private bool $replaced;

public function __construct( Label $label, string $lastModified, int $revisionId, bool $replaced ) {
$this->label = $label;
$this->lastModified = $lastModified;
$this->revisionId = $revisionId;
$this->replaced = $replaced;
}

public function getLabel(): Label {
return $this->label;
}

public function getLastModified(): string {
return $this->lastModified;
}

public function getRevisionId(): int {
return $this->revisionId;
}

public function wasReplaced(): bool {
return $this->replaced;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel;

/**
* @license GPL-2.0-or-later
*/
interface SetPropertyLabelValidator {

public function validateAndDeserialize( SetPropertyLabelRequest $request ): DeserializedSetPropertyLabelRequest;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyFieldsRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyIdFilterRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyIdRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyLabelEditRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\StatementIdRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\StatementSerializationRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest;
Expand Down Expand Up @@ -55,6 +56,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\SetItemDescription\SetItemDescriptionValidator;
use Wikibase\Repo\RestApi\Application\UseCases\SetItemLabel\SetItemLabelValidator;
use Wikibase\Repo\RestApi\Application\UseCases\SetPropertyDescription\SetPropertyDescriptionValidator;
use Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel\SetPropertyLabelValidator;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;

/**
Expand Down Expand Up @@ -97,7 +99,8 @@ class ValidatingRequestDeserializer implements
GetPropertyLabelValidator,
GetPropertyDescriptionValidator,
SetPropertyDescriptionValidator,
PatchPropertyAliasesValidator
PatchPropertyAliasesValidator,
SetPropertyLabelValidator
{
private const PREFIX = 'WbRestApi.RequestValidation.';
public const ITEM_ID_REQUEST_VALIDATING_DESERIALIZER = self::PREFIX . 'ItemIdRequestValidatingDeserializer';
Expand All @@ -115,6 +118,7 @@ class ValidatingRequestDeserializer implements
public const ITEM_DESCRIPTION_EDIT_REQUEST_VALIDATING_DESERIALIZER = self::PREFIX . 'ItemDescriptionEditRequestValidatingDeserializer';
public const PROPERTY_DESCRIPTION_EDIT_REQUEST_VALIDATING_DESERIALIZER =
self::PREFIX . 'PropertyDescriptionEditRequestValidatingDeserializer';
public const PROPERTY_LABEL_EDIT_REQUEST_VALIDATING_DESERIALIZER = self::PREFIX . 'PropertyLabelEditRequestValidatingDeserializer';

private ContainerInterface $serviceContainer;
private array $validRequestResults = [];
Expand Down Expand Up @@ -149,6 +153,7 @@ public function validateAndDeserialize( UseCaseRequest $request ): DeserializedR
PatchRequest::class => self::PATCH_REQUEST_VALIDATING_DESERIALIZER,
ItemLabelEditRequest::class => self::ITEM_LABEL_EDIT_REQUEST_VALIDATING_DESERIALIZER,
ItemDescriptionEditRequest::class => self::ITEM_DESCRIPTION_EDIT_REQUEST_VALIDATING_DESERIALIZER,
PropertyLabelEditRequest::class => self::PROPERTY_LABEL_EDIT_REQUEST_VALIDATING_DESERIALIZER,
PropertyDescriptionEditRequest::class => self::PROPERTY_DESCRIPTION_EDIT_REQUEST_VALIDATING_DESERIALIZER,
];
$result = [];
Expand Down
Loading

0 comments on commit dd0e463

Please sign in to comment.