Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.32% |
56 / 62 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
RestoreSubmitAction | |
90.32% |
56 / 62 |
|
71.43% |
5 / 7 |
13.15 | |
0.00% |
0 / 1 |
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
show | |
70.59% |
12 / 17 |
|
0.00% |
0 / 1 |
5.64 | |||
checkMethod | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
checkCurrentRevison | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
restore | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
storeRestoredSchema | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
createSummaryMessageForRestore | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\MediaWiki\Actions; |
6 | |
7 | use EntitySchema\DataAccess\EntitySchemaStatus; |
8 | use EntitySchema\DataAccess\MediaWikiRevisionEntitySchemaUpdater; |
9 | use EntitySchema\Domain\Model\EntitySchemaId; |
10 | use EntitySchema\MediaWiki\Content\EntitySchemaContent; |
11 | use EntitySchema\MediaWiki\EntitySchemaRedirectTrait; |
12 | use EntitySchema\Services\Converter\EntitySchemaConverter; |
13 | use EntitySchema\Services\Converter\PersistenceEntitySchemaData; |
14 | use MediaWiki\CommentStore\CommentStoreComment; |
15 | use MediaWiki\Revision\RevisionRecord; |
16 | use MediaWiki\Revision\SlotRecord; |
17 | use MediaWiki\Status\Status; |
18 | |
19 | /** |
20 | * @license GPL-2.0-or-later |
21 | */ |
22 | final class RestoreSubmitAction extends AbstractRestoreAction { |
23 | |
24 | use EntitySchemaRedirectTrait; |
25 | |
26 | public function getName(): string { |
27 | return 'submit'; |
28 | } |
29 | |
30 | public function show(): void { |
31 | $checkMethodStatus = $this->checkMethod(); |
32 | if ( !$checkMethodStatus->isOK() ) { |
33 | $this->showRestoreErrorPage( Status::newFatal( $checkMethodStatus ) ); |
34 | } |
35 | |
36 | $this->checkPermissions(); |
37 | |
38 | $currentRevStatus = $this->checkCurrentRevison(); |
39 | if ( !$currentRevStatus->isOK() ) { |
40 | $this->showRestoreErrorPage( $currentRevStatus ); |
41 | return; |
42 | } |
43 | |
44 | $revStatus = $this->getRevisionFromRequest( $this->getContext()->getRequest() ); |
45 | if ( !$revStatus->isOK() ) { |
46 | $this->showRestoreErrorPage( $revStatus ); |
47 | return; |
48 | } |
49 | |
50 | $restoreStatus = $this->restore( $revStatus->getValue() ); |
51 | if ( !$restoreStatus->isOK() ) { |
52 | $this->showRestoreErrorPage( $restoreStatus ); |
53 | return; |
54 | } |
55 | |
56 | $this->redirectToEntitySchema( $restoreStatus ); |
57 | } |
58 | |
59 | private function checkMethod(): Status { |
60 | if ( !$this->getContext()->getRequest()->wasPosted() ) { |
61 | return Status::newFatal( 'entityschema-error-not-post' ); |
62 | } |
63 | |
64 | return Status::newGood(); |
65 | } |
66 | |
67 | private function checkCurrentRevison(): Status { |
68 | $req = $this->getContext()->getRequest(); |
69 | |
70 | if ( $this->getTitle()->getLatestRevID() !== (int)$req->getText( 'wpBaseRev' ) ) { |
71 | return Status::newFatal( $this->msg( 'entityschema-restore-changed' ) ); |
72 | } |
73 | |
74 | return Status::newGood(); |
75 | } |
76 | |
77 | private function restore( RevisionRecord $revToRestore ): EntitySchemaStatus { |
78 | /** @var EntitySchemaContent $contentToRestore */ |
79 | $contentToRestore = $revToRestore->getContent( SlotRecord::MAIN ); |
80 | |
81 | $converter = new EntitySchemaConverter(); |
82 | |
83 | $summary = $this->createSummaryMessageForRestore( |
84 | $this->getContext()->getRequest()->getText( 'wpSummary' ), |
85 | $revToRestore |
86 | ); |
87 | |
88 | return $this->storeRestoredSchema( |
89 | $converter->getPersistenceSchemaData( |
90 | // @phan-suppress-next-line PhanUndeclaredMethod |
91 | $contentToRestore->getText() |
92 | ), |
93 | $this->getContext()->getRequest()->getInt( 'wpBaseRev' ), |
94 | $summary |
95 | ); |
96 | } |
97 | |
98 | private function storeRestoredSchema( |
99 | PersistenceEntitySchemaData $persistenceSchemaData, |
100 | int $baseRevId, |
101 | CommentStoreComment $summary |
102 | ): EntitySchemaStatus { |
103 | |
104 | $schemaUpdater = MediaWikiRevisionEntitySchemaUpdater::newFromContext( $this->getContext() ); |
105 | |
106 | return $schemaUpdater->overwriteWholeSchema( |
107 | new EntitySchemaId( $this->getTitle()->getTitleValue()->getText() ), |
108 | $persistenceSchemaData->labels, |
109 | $persistenceSchemaData->descriptions, |
110 | $persistenceSchemaData->aliases, |
111 | $persistenceSchemaData->schemaText, |
112 | $baseRevId, |
113 | $summary |
114 | ); |
115 | } |
116 | |
117 | private function createSummaryMessageForRestore( |
118 | string $userSummary, |
119 | RevisionRecord $revToBeRestored |
120 | ): CommentStoreComment { |
121 | $revId = $revToBeRestored->getId(); |
122 | $userName = $revToBeRestored->getUser()->getName(); |
123 | $autoComment = MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_RESTORE |
124 | . ':' . $revId |
125 | . ':' . $userName; |
126 | return CommentStoreComment::newUnsavedComment( |
127 | '/* ' . $autoComment . ' */' . $userSummary, |
128 | [ |
129 | 'key' => MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_RESTORE, |
130 | 'revId' => $revId, |
131 | 'userName' => $userName, |
132 | 'summary' => $userSummary, |
133 | ] |
134 | ); |
135 | } |
136 | |
137 | } |