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 | private static $instance; |
22 | |
23 | private function __construct() { |
24 | $this->dbw = MediaWikiServices::getInstance() |
25 | ->getConnectionProvider() |
26 | ->getPrimaryDatabase(); |
27 | |
28 | $this->item = new Item(); |
29 | $this->store = WikibaseRepo::getEntityStore(); |
30 | // don't store too many keys |
31 | $this->cache = new \HashBagOStuff( [ 'maxKeys' => 10000 ] ); |
32 | } |
33 | |
34 | /** |
35 | * @return IdMap |
36 | */ |
37 | public static function getInstance() { |
38 | if ( !self::$instance ) { |
39 | self::$instance = new IdMap(); |
40 | } |
41 | |
42 | return self::$instance; |
43 | } |
44 | |
45 | private function getNewQid() { |
46 | $this->item->setId( null ); |
47 | $this->store->assignFreshId( $this->item ); |
48 | |
49 | return $this->item->getId(); |
50 | } |
51 | |
52 | public function addQid( int $extId, int $extIdType ) { |
53 | $cacheKey = "$extIdType.$extId"; |
54 | if ( $this->cache->hasKey( $cacheKey ) ) { |
55 | return $this->cache->get( $cacheKey ); |
56 | } |
57 | $fields = [ |
58 | 'math_external_id' => $extId, |
59 | 'math_external_id_type' => $extIdType, |
60 | ]; |
61 | |
62 | $qid = $this->dbw->selectField( 'math_wbs_entity_map', 'math_local_qid', $fields ); |
63 | if ( !$qid ) { |
64 | $qid = $this->getNewQid()->getNumericId(); |
65 | $fields['math_local_qid'] = $qid; |
66 | $this->dbw->insert( 'math_wbs_entity_map', $fields ); |
67 | } |
68 | $this->cache->set( $cacheKey, $qid ); |
69 | |
70 | return $qid; |
71 | } |
72 | |
73 | public function addWikiText( int $qid, $parent, int $type, string $text ) { |
74 | $cond = [ |
75 | 'math_local_qid' => $qid, |
76 | ]; |
77 | $table = 'math_wbs_text_store'; |
78 | $entryExists = $this->dbw->selectRowCount( $table, 'math_local_qid', $cond ); |
79 | |
80 | $fields = [ |
81 | 'math_local_qid' => $qid, |
82 | 'math_reply_to' => $parent, |
83 | 'math_post_type' => $type, |
84 | 'math_body' => $text, |
85 | ]; |
86 | |
87 | if ( $entryExists ) { |
88 | $this->dbw->update( $table, $fields, $cond ); |
89 | } else { |
90 | $this->dbw->insert( $table, $fields ); |
91 | } |
92 | } |
93 | } |