Skip to content

Commit

Permalink
REST: Implement Patch Item Aliases happy path
Browse files Browse the repository at this point in the history
Bug: T337371
Change-Id: I4a4bae90f15385fef05cdba650b5d6932413e351
  • Loading branch information
outdooracorn committed Oct 12, 2023
1 parent fbfdb7c commit c15382f
Show file tree
Hide file tree
Showing 16 changed files with 591 additions and 1 deletion.
8 changes: 7 additions & 1 deletion repo/rest-api/routes.dev.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
[ ]
[
{
"path": "/wikibase/v0/entities/items/{item_id}/aliases",
"method": "PATCH",
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\PatchItemAliasesRouteHandler::factory"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabels\DeserializedGetPropertyLabelsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\DeserializedGetPropertyStatementRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\DeserializedGetPropertyStatementsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemAliases\DeserializedPatchItemAliasesRequest;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemDescriptions\DeserializedPatchItemDescriptionsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemLabels\DeserializedPatchItemLabelsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemStatement\DeserializedPatchItemStatementRequest;
Expand Down Expand Up @@ -65,6 +66,7 @@ class DeserializedRequestAdapter implements
DeserializedGetPropertyStatementsRequest,
DeserializedPatchItemLabelsRequest,
DeserializedPatchItemDescriptionsRequest,
DeserializedPatchItemAliasesRequest,
DeserializedPatchItemStatementRequest,
DeserializedPatchPropertyStatementRequest,
DeserializedRemoveItemStatementRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare( strict_types=1 );

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

use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedEditMetadataRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedItemIdRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedPatchRequest;

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

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

use Wikibase\Repo\RestApi\Application\Serialization\AliasesDeserializer;
use Wikibase\Repo\RestApi\Application\Serialization\AliasesSerializer;
use Wikibase\Repo\RestApi\Application\UseCases\ItemRedirect;
use Wikibase\Repo\RestApi\Application\UseCases\PatchJson;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Domain\Model\AliasesEditSummary;
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata;
use Wikibase\Repo\RestApi\Domain\Services\ItemAliasesRetriever;
use Wikibase\Repo\RestApi\Domain\Services\ItemRetriever;
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater;

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

private PatchItemAliasesValidator $useCaseValidator;
private ItemAliasesRetriever $aliasesRetriever;
private AliasesSerializer $aliasesSerializer;
private PatchJson $patcher;
private ItemRetriever $itemRetriever;
private AliasesDeserializer $aliasesDeserializer;
private ItemUpdater $itemUpdater;

public function __construct(
PatchItemAliasesValidator $useCaseValidator,
ItemAliasesRetriever $aliasesRetriever,
AliasesSerializer $aliasesSerializer,
PatchJson $patcher,
ItemRetriever $itemRetriever,
AliasesDeserializer $aliasesDeserializer,
ItemUpdater $itemUpdater
) {
$this->useCaseValidator = $useCaseValidator;
$this->aliasesRetriever = $aliasesRetriever;
$this->aliasesSerializer = $aliasesSerializer;
$this->patcher = $patcher;
$this->itemRetriever = $itemRetriever;
$this->aliasesDeserializer = $aliasesDeserializer;
$this->itemUpdater = $itemUpdater;
}

/**
* @throws ItemRedirect
* @throws UseCaseError
*/
public function execute( PatchItemAliasesRequest $request ): PatchItemAliasesResponse {
$deserializedRequest = $this->useCaseValidator->validateAndDeserialize( $request );

$aliases = $this->aliasesRetriever->getAliases( $deserializedRequest->getItemId() );
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
$serialization = $this->aliasesSerializer->serialize( $aliases );

$patchedAliases = $this->patcher->execute( iterator_to_array( $serialization ), $deserializedRequest->getPatch() );

$item = $this->itemRetriever->getItem( $deserializedRequest->getItemId() );
$modifiedAliases = $this->aliasesDeserializer->deserialize( $patchedAliases );
$item->getFingerprint()->setAliasGroups( $modifiedAliases );

$editMetadata = new EditMetadata(
$deserializedRequest->getEditMetadata()->getTags(),
$deserializedRequest->getEditMetadata()->isBot(),
AliasesEditSummary::newPatchSummary( $deserializedRequest->getEditMetadata()->getComment() )
);
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
$revision = $this->itemUpdater->update( $item, $editMetadata );

return new PatchItemAliasesResponse(
$revision->getItem()->getAliases(),
$revision->getLastModified(),
$revision->getRevisionId()
);
}

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

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

use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\EditMetadataRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\ItemIdRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PatchRequest;
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest;

/**
* @license GPL-2.0-or-later
*/
class PatchItemAliasesRequest implements UseCaseRequest, ItemIdRequest, PatchRequest, EditMetadataRequest {

private string $itemId;
private array $patch;
private array $editTags;
private bool $isBot;
private ?string $comment;
private ?string $username;

public function __construct(
string $itemId,
array $patch,
array $editTags,
bool $isBot,
?string $comment,
?string $username
) {
$this->itemId = $itemId;
$this->patch = $patch;
$this->editTags = $editTags;
$this->isBot = $isBot;
$this->comment = $comment;
$this->username = $username;
}

public function getItemId(): string {
return $this->itemId;
}

public function getPatch(): array {
return $this->patch;
}

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,34 @@
<?php declare( strict_types=1 );

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

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

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

private Aliases $aliases;
private string $lastModified;
private int $revisionId;

public function __construct( Aliases $aliases, string $lastModified, int $revisionId ) {
$this->aliases = $aliases;
$this->lastModified = $lastModified;
$this->revisionId = $revisionId;
}

public function getAliases(): aliases {
return $this->aliases;
}

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

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

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

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

use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;

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

/**
* @throws UseCaseError
*/
public function validateAndDeserialize( PatchItemAliasesRequest $request ): DeserializedPatchItemAliasesRequest;

}
29 changes: 29 additions & 0 deletions repo/rest-api/src/Domain/Model/AliasesEditSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Domain\Model;

/**
* @license GPL-2.0-or-later
*/
class AliasesEditSummary implements EditSummary {

private string $editAction;
private ?string $userComment;

public static function newPatchSummary( ?string $userComment ): self {
$summary = new self();
$summary->editAction = self::PATCH_ACTION;
$summary->userComment = $userComment;

return $summary;
}

public function getEditAction(): string {
return $this->editAction;
}

public function getUserComment(): ?string {
return $this->userComment;
}

}
3 changes: 3 additions & 0 deletions repo/rest-api/src/Infrastructure/EditSummaryFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LogicException;
use Wikibase\Lib\FormatableSummary;
use Wikibase\Lib\Summary;
use Wikibase\Repo\RestApi\Domain\Model\AliasesEditSummary;
use Wikibase\Repo\RestApi\Domain\Model\DescriptionEditSummary;
use Wikibase\Repo\RestApi\Domain\Model\DescriptionsEditSummary;
use Wikibase\Repo\RestApi\Domain\Model\EditSummary;
Expand Down Expand Up @@ -54,6 +55,8 @@ private function convertToFormattableSummary( EditSummary $editSummary ): Format
case EditSummary::REPLACE_ACTION:
return $this->newSummaryForDescriptionEdit( $editSummary, 'set' );
}
} elseif ( $editSummary instanceof AliasesEditSummary ) {
return new Summary();
} elseif ( $editSummary instanceof StatementEditSummary ) {
switch ( $editSummary->getEditAction() ) {
case EditSummary::ADD_ACTION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\GetPropertyStatementValidator;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\GetPropertyStatementsValidator;
use Wikibase\Repo\RestApi\Application\UseCases\GetStatement\GetStatementValidator;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemAliases\PatchItemAliasesValidator;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemDescriptions\PatchItemDescriptionsValidator;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemLabels\PatchItemLabelsValidator;
use Wikibase\Repo\RestApi\Application\UseCases\PatchItemStatement\PatchItemStatementValidator;
Expand Down Expand Up @@ -78,6 +79,7 @@ class ValidatingRequestDeserializer implements
GetStatementValidator,
PatchItemLabelsValidator,
PatchItemDescriptionsValidator,
PatchItemAliasesValidator,
PatchItemStatementValidator,
PatchPropertyStatementValidator,
PatchStatementValidator,
Expand Down
Loading

0 comments on commit c15382f

Please sign in to comment.