1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\PackagePrivate; |
4
|
|
|
|
5
|
|
|
class InMemoryTermIdsAcquirer implements TermIdsAcquirer, TermIdsResolver, TermCleaner { |
6
|
|
|
|
7
|
|
|
private $terms = []; |
8
|
|
|
private $lastId = 0; |
9
|
|
|
|
10
|
|
|
public function acquireTermIds( array $termsArray ): array { |
11
|
|
|
$ids = []; |
12
|
|
|
|
13
|
|
|
foreach ( $termsArray as $type => $termsOfType ) { |
14
|
|
|
foreach ( $termsOfType as $lang => $terms ) { |
15
|
|
|
$terms = (array)$terms; |
16
|
|
|
|
17
|
|
|
foreach ( $terms as $term ) { |
18
|
|
|
if ( !isset( $this->terms[$type][$lang][$term] ) ) { |
19
|
|
|
$this->terms[$type][$lang][$term] = ++$this->lastId; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$ids[] = $this->terms[$type][$lang][$term]; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $ids; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function resolveTermIds( array $termIds ): array { |
31
|
|
|
$terms = []; |
32
|
|
|
foreach ( $this->terms as $type => $termsOfType ) { |
33
|
|
|
foreach ( $termsOfType as $lang => $termsOfLang ) { |
34
|
|
|
foreach ( $termsOfLang as $term => $id ) { |
35
|
|
|
if ( in_array( $id, $termIds ) ) { |
36
|
|
|
$terms[$type][$lang][] = $term; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
return $terms; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function cleanTerms( array $termInLangIds ) { |
45
|
|
|
$termInLangIdsAsKeys = array_flip( $termInLangIds ); |
46
|
|
|
foreach ( $this->terms as $type => &$termsOfType ) { |
47
|
|
|
foreach ( $termsOfType as $lang => &$termsOfLang ) { |
48
|
|
|
foreach ( $termsOfLang as $term => $id ) { |
49
|
|
|
if ( array_key_exists( $id, $termInLangIdsAsKeys ) ) { |
50
|
|
|
unset( $termsOfLang[$term] ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
if ( $termsOfLang === [] ) { |
54
|
|
|
unset( $termsOfType[$lang] ); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
if ( $termsOfType === [] ) { |
58
|
|
|
unset( $this->terms[$type] ); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|