-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST: Implement Patch Item Aliases happy path
Bug: T337371 Change-Id: I4a4bae90f15385fef05cdba650b5d6932413e351
- Loading branch information
1 parent
fbfdb7c
commit c15382f
Showing
16 changed files
with
591 additions
and
1 deletion.
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 +1,7 @@ | ||
[ ] | ||
[ | ||
{ | ||
"path": "/wikibase/v0/entities/items/{item_id}/aliases", | ||
"method": "PATCH", | ||
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\PatchItemAliasesRouteHandler::factory" | ||
} | ||
] |
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
13 changes: 13 additions & 0 deletions
13
...est-api/src/Application/UseCases/PatchItemAliases/DeserializedPatchItemAliasesRequest.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
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 { | ||
} |
79 changes: 79 additions & 0 deletions
79
repo/rest-api/src/Application/UseCases/PatchItemAliases/PatchItemAliases.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
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() | ||
); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
repo/rest-api/src/Application/UseCases/PatchItemAliases/PatchItemAliasesRequest.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
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; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
repo/rest-api/src/Application/UseCases/PatchItemAliases/PatchItemAliasesResponse.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
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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
repo/rest-api/src/Application/UseCases/PatchItemAliases/PatchItemAliasesValidator.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
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; | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |
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
Oops, something went wrong.