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