Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EntityIdFixingRevisionLookup
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityRevision
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getLatestRevisionId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\MediaInfo\DataAccess\Store;
4
5use Psr\Log\LoggerInterface;
6use Wikibase\DataModel\Entity\EntityId;
7use Wikibase\Lib\Store\DivergingEntityIdException;
8use Wikibase\Lib\Store\EntityRevisionLookup;
9use Wikibase\Lib\Store\LookupConstants;
10
11/**
12 * This is an EntityRevisionLookup which works around that fact that assumptions
13 * that were made during the creation of MediaInfo, "the entity IDs numeric
14 * part matches page_id of the page" (see MediaInfoEntityQuery), are occasionally
15 * not true if a page was deleted and then restored.
16 * The "new" page however contains a copy of the entity with its previous id and
17 * we can modify it to have the old data but new id.
18 * We may be able to remove this once https://phabricator.wikimedia.org/T193690
19 * is implemented and old pages are somehow cleaned up.
20 *
21 * @license GPL-2.0-or-later
22 */
23class EntityIdFixingRevisionLookup implements EntityRevisionLookup {
24
25    public function __construct(
26        private readonly EntityRevisionLookup $lookup,
27        private readonly LoggerInterface $logger,
28    ) {
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public function getEntityRevision(
35        EntityId $entityId,
36        $revisionId = 0,
37        $mode = LookupConstants::LATEST_FROM_REPLICA
38    ) {
39        try {
40            $entityRevision = $this->lookup->getEntityRevision( $entityId, $revisionId, $mode );
41        } catch ( DivergingEntityIdException $exception ) {
42            // TODO "correct" log level?
43            $this->logger->warning( $exception->getNormalizedMessage(), $exception->getMessageContext() );
44
45            $entityRevision = $exception->getEntityRevision();
46            $entityRevision->getEntity()->setId( $entityId );
47        }
48        return $entityRevision;
49    }
50
51    /**
52     * @inheritDoc
53     */
54    public function getLatestRevisionId( EntityId $entityId, $mode = LookupConstants::LATEST_FROM_REPLICA ) {
55        return $this->lookup->getLatestRevisionId( $entityId, $mode );
56    }
57
58}