Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
EntitySchemaUpdateGuard | |
100.00% |
32 / 32 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
guardSchemaUpdate | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
cleanupData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
array2persistence | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\DataAccess; |
6 | |
7 | use Diff\Patcher\PatcherException; |
8 | use EntitySchema\Services\Converter\EntitySchemaConverter; |
9 | use EntitySchema\Services\Converter\FullArrayEntitySchemaData; |
10 | use EntitySchema\Services\Converter\PersistenceEntitySchemaData; |
11 | use EntitySchema\Services\Diff\EntitySchemaDiffer; |
12 | use EntitySchema\Services\Diff\EntitySchemaPatcher; |
13 | use MediaWiki\Revision\RevisionRecord; |
14 | use MediaWiki\Revision\SlotRecord; |
15 | |
16 | /** |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class EntitySchemaUpdateGuard { |
20 | |
21 | private EntitySchemaConverter $schemaConverter; |
22 | private EntitySchemaDiffer $schemaDiffer; |
23 | private EntitySchemaPatcher $schemaPatcher; |
24 | |
25 | public function __construct() { |
26 | $this->schemaConverter = new EntitySchemaConverter(); |
27 | $this->schemaDiffer = new EntitySchemaDiffer(); |
28 | $this->schemaPatcher = new EntitySchemaPatcher(); |
29 | } |
30 | |
31 | /** |
32 | * @param RevisionRecord $baseRevision The revision that the user’s data is based on. |
33 | * @param RevisionRecord $parentRevision The parent revision returned by the PageUpdater. |
34 | * @param callable $schemaUpdate Function accepting a FullArraySchemaData object, |
35 | * updating it with the user’s data. |
36 | * @return PersistenceEntitySchemaData|null The data that should be stored, |
37 | * or null if there is nothing to do. |
38 | * @throws PatcherException If there is an edit conflict and the update cannot be done. |
39 | */ |
40 | public function guardSchemaUpdate( |
41 | RevisionRecord $baseRevision, |
42 | RevisionRecord $parentRevision, |
43 | callable $schemaUpdate |
44 | ): ?PersistenceEntitySchemaData { |
45 | $baseData = $this->schemaConverter->getFullArraySchemaData( |
46 | // @phan-suppress-next-line PhanUndeclaredMethod |
47 | $baseRevision->getContent( SlotRecord::MAIN )->getText() |
48 | ); |
49 | |
50 | $updateData = clone $baseData; |
51 | $schemaUpdate( $updateData ); |
52 | |
53 | $this->cleanupData( $baseData ); |
54 | $this->cleanupData( $updateData ); |
55 | $diff = $this->schemaDiffer->diffSchemas( $baseData, $updateData ); |
56 | |
57 | if ( $diff->isEmpty() ) { |
58 | return null; |
59 | } |
60 | |
61 | if ( $baseRevision->getId() === $parentRevision->getId() ) { |
62 | return $this->array2persistence( $updateData ); |
63 | } |
64 | |
65 | $parentData = $this->schemaConverter->getFullArraySchemaData( |
66 | // @phan-suppress-next-line PhanUndeclaredMethod |
67 | $parentRevision->getContent( SlotRecord::MAIN )->getText() |
68 | ); |
69 | |
70 | // may throw PatcherException |
71 | $patchedData = $this->schemaPatcher->patchSchema( $parentData, $diff ); |
72 | |
73 | return $this->array2persistence( $patchedData ); |
74 | } |
75 | |
76 | private function cleanupData( FullArrayEntitySchemaData $data ): void { |
77 | EntitySchemaCleaner::cleanupParameters( |
78 | $data->data['labels'], |
79 | $data->data['descriptions'], |
80 | $data->data['aliases'], |
81 | $data->data['schemaText'] |
82 | ); |
83 | } |
84 | |
85 | // TODO this is very silly |
86 | private function array2persistence( FullArrayEntitySchemaData $arrayData ): PersistenceEntitySchemaData { |
87 | $persistenceData = new PersistenceEntitySchemaData(); |
88 | $persistenceData->labels = $arrayData->data['labels']; |
89 | $persistenceData->descriptions = $arrayData->data['descriptions']; |
90 | $persistenceData->aliases = $arrayData->data['aliases']; |
91 | $persistenceData->schemaText = $arrayData->data['schemaText']; |
92 | return $persistenceData; |
93 | } |
94 | |
95 | } |