Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaWikiPageSubEntityMetaDataAccessor
97.14% covered (success)
97.14%
34 / 35
75.00% covered (warning)
75.00%
3 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadRevisionInformation
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 loadRevisionInformationByRevisionId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadLatestRevisionIds
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2
3namespace Wikibase\Lexeme\DataAccess\Store;
4
5use LogicException;
6use stdClass;
7use Wikibase\DataModel\Entity\EntityId;
8use Wikibase\Lexeme\Domain\Model\LexemeSubEntityId;
9use Wikibase\Lib\Store\LookupConstants;
10use Wikibase\Lib\Store\Sql\WikiPageEntityMetaDataAccessor;
11
12/**
13 * An Accessor for SubEntities that uses the supplied Accessor to look up
14 * the Lexeme's MetaData. Only loadLatestRevisionIds and loadRevisionInformation
15 * are currently implemented
16 *
17 * @license GPL-2.0-or-later
18 */
19class MediaWikiPageSubEntityMetaDataAccessor implements WikiPageEntityMetaDataAccessor {
20
21    private WikiPageEntityMetaDataAccessor $wikiPageEntityMetaDataLookup;
22
23    public function __construct( WikiPageEntityMetaDataAccessor $entityMetaDataAccessor ) {
24        $this->wikiPageEntityMetaDataLookup = $entityMetaDataAccessor;
25    }
26
27    /**
28     * Loads revision information of entities that given ids of sub-entities (forms or senses)
29     * belong.
30     *
31     * @param EntityId[] $entityIds values must be instances of {@link LexemeSubEntityId}
32     * @param string $mode (LookupConstants::LATEST_FROM_REPLICA,
33     *     LookupConstants::LATEST_FROM_REPLICA_WITH_FALLBACK or
34     *     LookupConstants::LATEST_FROM_MASTER)
35     *
36     * @return (stdClass|bool)[] Array mapping entity ID serializations to either objects
37     * or false if an entity could not be found.
38     *
39     * @throws LogicException if some entity id in $entityIds is not instance of
40     * {@link LexemeSubEntityId}
41     */
42    public function loadRevisionInformation( array $entityIds, $mode ) {
43        $subEntityIds = [];
44        foreach ( $entityIds as $key => $entityId ) {
45            if ( $entityId instanceof LexemeSubEntityId ) {
46                $subEntityIds[] = $entityId;
47                $entityIds[$key] = $entityId->getLexemeId();
48            } else {
49                throw new LogicException(
50                    $entityId->getSerialization() . ' is not instance of ' . LexemeSubEntityId::class );
51            }
52        }
53
54        $entitiesRevisionInfo = $this->wikiPageEntityMetaDataLookup->loadRevisionInformation(
55            $entityIds,
56            $mode
57        );
58
59        $subEntitiesRevisionInformation = [];
60        /** @var LexemeSubEntityId $subEntityId */
61        foreach ( $subEntityIds as $subEntityId ) {
62            $subEntityIdString = $subEntityId->getSerialization();
63            $lexemeIdString = $subEntityId->getLexemeId()->getSerialization();
64            $subEntitiesRevisionInformation[ $subEntityIdString ] = $entitiesRevisionInfo[ $lexemeIdString ];
65        }
66
67        return $subEntitiesRevisionInformation;
68    }
69
70    /**
71     * Not implemented
72     *
73     * @param EntityId $entityId
74     * @param int $revisionId Revision id to fetch data about, must be an integer greater than 0.
75     * @param string $mode (LookupConstants::LATEST_FROM_REPLICA,
76     *     LookupConstants::LATEST_FROM_REPLICA_WITH_FALLBACK or
77     *     LookupConstants::LATEST_FROM_MASTER).
78     *
79     * @return never
80     */
81    public function loadRevisionInformationByRevisionId( EntityId $entityId,
82        $revisionId,
83        $mode = LookupConstants::LATEST_FROM_MASTER ) {
84        throw new LogicException( 'Not Implemented' );
85    }
86
87    /**
88     * Looks up the latest revision ID(s) for the given entityId(s).
89     * Returns an array of integer revision IDs using the wrapped
90     * WikiPageEntityMetaDataLookup
91     *
92     * If passed a LexemeSubEntityId then the look up if done on the lexeme
93     * and the SubEntity row is reinserted into the lookup afterwards.
94     *
95     * @param EntityId[] $entityIds
96     * @param string $mode (LookupConstants::LATEST_FROM_REPLICA,
97     *     LookupConstants::LATEST_FROM_REPLICA_WITH_FALLBACK or
98     *     LookupConstants::LATEST_FROM_MASTER)
99     *
100     * @return (int|bool)[] Array mapping entity ID serializations to either revision IDs
101     * or false if an entity could not be found (including if the page is a redirect).
102     */
103    public function loadLatestRevisionIds( array $entityIds, $mode ): array {
104        $subEntityIds = [];
105        foreach ( $entityIds as $key => $entityId ) {
106            if ( $entityId instanceof LexemeSubEntityId ) {
107                $subEntityIds[] = $entityId;
108                $entityIds[$key] = $entityId->getLexemeId();
109            } else {
110                throw new LogicException();
111            }
112        }
113
114        $entityRevisionIds = $this->wikiPageEntityMetaDataLookup->loadLatestRevisionIds(
115            $entityIds,
116            $mode
117        );
118
119        $subEntitiesRevisionIds = [];
120        /** @var LexemeSubEntityId $subEntityId */
121        foreach ( $subEntityIds as $subEntityId ) {
122            $subEntityIdString = $subEntityId->getSerialization();
123            $lexemeIdString = $subEntityId->getLexemeId()->getSerialization();
124            $subEntitiesRevisionIds[ $subEntityIdString ] = $entityRevisionIds[ $lexemeIdString ];
125        }
126
127        return $subEntitiesRevisionIds;
128    }
129
130}