Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
45 / 50 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RestoreViewAction | |
90.00% |
45 / 50 |
|
50.00% |
2 / 4 |
7.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
3.43 | |||
| showRestorePreview | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
| getDiffFromRevision | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\MediaWiki\Actions; |
| 6 | |
| 7 | use Diff\DiffOp\Diff\Diff; |
| 8 | use EntitySchema\MediaWiki\Content\EntitySchemaContent; |
| 9 | use EntitySchema\MediaWiki\Content\EntitySchemaSlotDiffRenderer; |
| 10 | use EntitySchema\Presentation\ConfirmationFormRenderer; |
| 11 | use EntitySchema\Presentation\DiffRenderer; |
| 12 | use EntitySchema\Services\Converter\EntitySchemaConverter; |
| 13 | use EntitySchema\Services\Diff\EntitySchemaDiffer; |
| 14 | use MediaWiki\Context\IContextSource; |
| 15 | use MediaWiki\MediaWikiServices; |
| 16 | use MediaWiki\Page\Article; |
| 17 | use MediaWiki\Revision\RevisionRecord; |
| 18 | use MediaWiki\Revision\SlotRecord; |
| 19 | use MediaWiki\Status\Status; |
| 20 | |
| 21 | /** |
| 22 | * @license GPL-2.0-or-later |
| 23 | */ |
| 24 | final class RestoreViewAction extends AbstractRestoreAction { |
| 25 | |
| 26 | private EntitySchemaSlotDiffRenderer $slotDiffRenderer; |
| 27 | |
| 28 | public function __construct( |
| 29 | Article $article, |
| 30 | IContextSource $context, |
| 31 | EntitySchemaSlotDiffRenderer $slotDiffRenderer |
| 32 | ) { |
| 33 | parent::__construct( $article, $context ); |
| 34 | $this->slotDiffRenderer = $slotDiffRenderer; |
| 35 | } |
| 36 | |
| 37 | public function show(): void { |
| 38 | $this->checkPermissions(); |
| 39 | |
| 40 | $req = $this->getContext()->getRequest(); |
| 41 | $revStatus = $this->getRevisionFromRequest( $req ); |
| 42 | if ( !$revStatus->isOK() ) { |
| 43 | $this->showRestoreErrorPage( $revStatus ); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $diffStatus = $this->getDiffFromRevision( $revStatus->getValue() ); |
| 48 | if ( !$diffStatus->isOK() ) { |
| 49 | $this->showRestoreErrorPage( $diffStatus ); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $this->showRestorePreview( $diffStatus->getValue(), $req->getInt( 'restore' ) ); |
| 54 | } |
| 55 | |
| 56 | private function showRestorePreview( Diff $diff, int $restoredRevID ): void { |
| 57 | $this->getOutput()->enableOOUI(); |
| 58 | $this->getOutput()->setPageTitleMsg( |
| 59 | $this->msg( |
| 60 | 'entityschema-restore-heading', |
| 61 | $this->getTitle()->getTitleValue()->getText() |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | $diffRenderer = new DiffRenderer( $this, $this->slotDiffRenderer ); |
| 66 | $diffHTML = $diffRenderer->renderSchemaDiffTable( |
| 67 | $this->slotDiffRenderer->renderSchemaDiffRows( $diff ), |
| 68 | $this->msg( 'currentrev' ) |
| 69 | ); |
| 70 | |
| 71 | $this->getOutput()->addHTML( $diffHTML ); |
| 72 | $this->getOutput()->addModuleStyles( 'mediawiki.diff.styles' ); |
| 73 | |
| 74 | $confFormRenderer = new ConfirmationFormRenderer( $this ); |
| 75 | $confFormHTML = $confFormRenderer->showUndoRestoreConfirmationForm( |
| 76 | [ |
| 77 | 'restore' => $restoredRevID, |
| 78 | ], |
| 79 | 'restore', |
| 80 | $this->getTitle(), |
| 81 | $this->getUser() |
| 82 | ); |
| 83 | |
| 84 | $this->getOutput()->addHTML( $confFormHTML ); |
| 85 | } |
| 86 | |
| 87 | /** @return Status<Diff> */ |
| 88 | private function getDiffFromRevision( RevisionRecord $revToRestore ): Status { |
| 89 | |
| 90 | /** @var EntitySchemaContent $contentToRestore */ |
| 91 | $contentToRestore = $revToRestore->getContent( SlotRecord::MAIN ); |
| 92 | |
| 93 | /** @var EntitySchemaContent $baseContent */ |
| 94 | $baseContent = MediaWikiServices::getInstance()->getRevisionStore() |
| 95 | ->getRevisionById( $this->getTitle()->getLatestRevID() ) |
| 96 | ->getContent( SlotRecord::MAIN ); |
| 97 | |
| 98 | $differ = new EntitySchemaDiffer(); |
| 99 | $converter = new EntitySchemaConverter(); |
| 100 | $diff = $differ->diffSchemas( |
| 101 | // @phan-suppress-next-line PhanUndeclaredMethod |
| 102 | $converter->getFullArraySchemaData( $baseContent->getText() ), |
| 103 | // @phan-suppress-next-line PhanUndeclaredMethod |
| 104 | $converter->getFullArraySchemaData( $contentToRestore->getText() ) |
| 105 | ); |
| 106 | |
| 107 | if ( $diff->isEmpty() ) { |
| 108 | return Status::newFatal( 'entityschema-restore-empty' ); |
| 109 | } |
| 110 | |
| 111 | return Status::newGood( $diff ); |
| 112 | } |
| 113 | |
| 114 | } |