Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
IdMap | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getInstance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getNewQid | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
addQid | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
addWikiText | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MathSearch\StackExchange; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use Wikibase\DataModel\Entity\Item; |
7 | use Wikibase\Repo\WikibaseRepo; |
8 | |
9 | class IdMap { |
10 | |
11 | /** @var \Wikimedia\Rdbms\IDatabase */ |
12 | private $dbw; |
13 | /** @var Item */ |
14 | private $item; |
15 | /** @var \Wikibase\Lib\Store\EntityStore */ |
16 | private $store; |
17 | /** |
18 | * @var \HashBagOStuff |
19 | */ |
20 | private $cache; |
21 | /** @var self|null */ |
22 | private static $instance; |
23 | |
24 | private function __construct() { |
25 | $this->dbw = MediaWikiServices::getInstance() |
26 | ->getConnectionProvider() |
27 | ->getPrimaryDatabase(); |
28 | |
29 | $this->item = new Item(); |
30 | $this->store = WikibaseRepo::getEntityStore(); |
31 | // don't store too many keys |
32 | $this->cache = new \HashBagOStuff( [ 'maxKeys' => 10000 ] ); |
33 | } |
34 | |
35 | /** |
36 | * @return IdMap |
37 | */ |
38 | public static function getInstance() { |
39 | if ( !self::$instance ) { |
40 | self::$instance = new IdMap(); |
41 | } |
42 | |
43 | return self::$instance; |
44 | } |
45 | |
46 | private function getNewQid() { |
47 | $this->item->setId( null ); |
48 | $this->store->assignFreshId( $this->item ); |
49 | |
50 | return $this->item->getId(); |
51 | } |
52 | |
53 | public function addQid( int $extId, int $extIdType ) { |
54 | $cacheKey = "$extIdType.$extId"; |
55 | if ( $this->cache->hasKey( $cacheKey ) ) { |
56 | return $this->cache->get( $cacheKey ); |
57 | } |
58 | $fields = [ |
59 | 'math_external_id' => $extId, |
60 | 'math_external_id_type' => $extIdType, |
61 | ]; |
62 | |
63 | $qid = $this->dbw->selectField( 'math_wbs_entity_map', 'math_local_qid', $fields ); |
64 | if ( !$qid ) { |
65 | $qid = $this->getNewQid()->getNumericId(); |
66 | $fields['math_local_qid'] = $qid; |
67 | $this->dbw->insert( 'math_wbs_entity_map', $fields ); |
68 | } |
69 | $this->cache->set( $cacheKey, $qid ); |
70 | |
71 | return $qid; |
72 | } |
73 | |
74 | public function addWikiText( int $qid, $parent, int $type, string $text ) { |
75 | $cond = [ |
76 | 'math_local_qid' => $qid, |
77 | ]; |
78 | $table = 'math_wbs_text_store'; |
79 | $entryExists = $this->dbw->selectRowCount( $table, 'math_local_qid', $cond ); |
80 | |
81 | $fields = [ |
82 | 'math_local_qid' => $qid, |
83 | 'math_reply_to' => $parent, |
84 | 'math_post_type' => $type, |
85 | 'math_body' => $text, |
86 | ]; |
87 | |
88 | if ( $entryExists ) { |
89 | $this->dbw->update( $table, $fields, $cond ); |
90 | } else { |
91 | $this->dbw->insert( $table, $fields ); |
92 | } |
93 | } |
94 | } |