Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
Formula | |
0.00% |
0 / 29 |
|
0.00% |
0 / 3 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
updateSearchIndex | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
createWbItem | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MathSearch\StackExchange; |
4 | |
5 | use MediaWiki\Extension\Math\MathMathML; |
6 | use MediaWiki\User\User; |
7 | use Wikibase\DataModel\Entity\Item; |
8 | use Wikibase\DataModel\Entity\ItemId; |
9 | use Wikibase\DataModel\Entity\NumericPropertyId; |
10 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
11 | use Wikibase\DataModel\Statement\StatementList; |
12 | use Wikibase\Repo\WikibaseRepo; |
13 | |
14 | class Formula { |
15 | |
16 | /** @var int */ |
17 | private $id; |
18 | /** @var int */ |
19 | private $qid; |
20 | /** @var string */ |
21 | private $text; |
22 | /** @var int */ |
23 | private $postQId; |
24 | |
25 | /** |
26 | * @param int $id |
27 | * @param int $qid |
28 | * @param string $text |
29 | * @param int $postQId |
30 | */ |
31 | public function __construct( $id, $qid, $text, $postQId ) { |
32 | $this->id = $id; |
33 | $this->qid = $qid; |
34 | $this->text = $text; |
35 | $this->postQId = $postQId; |
36 | } |
37 | |
38 | public function updateSearchIndex() { |
39 | $renderer = new MathMathML( $this->text, [ 'display' => 'block' ] ); |
40 | $hash = $renderer->getInputHash(); |
41 | $renderer->writeToCache(); |
42 | // TODO: Fix fake revision ID |
43 | \MathSearchHooks::writeMathIndex( 1, $this->id, $hash, $this->text ); |
44 | } |
45 | |
46 | public function createWbItem() { |
47 | $sf = WikibaseRepo::getSnakFactory(); |
48 | $store = WikibaseRepo::getEntityStore(); |
49 | $user = User::newFromName( 'Maintenance script' ); |
50 | $item = new Item(); |
51 | $item->setId( ItemId::newFromNumber( $this->qid ) ); |
52 | $item->setLabel( 'en', "Formula {$this->id}" ); |
53 | $guidGenerator = new GuidGenerator(); |
54 | $statements = new StatementList(); |
55 | $guid = $guidGenerator->newGuid( $item->getId() ); |
56 | // TODO: get from settings |
57 | $snak = $sf->newSnak( new NumericPropertyId( 'P1' ), 'value', $this->text ); |
58 | $statements->addNewStatement( $snak, null, null, $guid ); |
59 | $guid = $guidGenerator->newGuid( $item->getId() ); |
60 | $snak = $sf->newSnak( new NumericPropertyId( 'P8' ), 'value', (string)$this->id ); |
61 | $statements->addNewStatement( $snak, null, null, $guid ); |
62 | $guid = $guidGenerator->newGuid( $item->getId() ); |
63 | $snak = |
64 | $sf->newSnak( new NumericPropertyId( 'P16' ), 'value', |
65 | [ 'entity-type' => 'item', 'numeric-id' => (int)$this->postQId ] ); |
66 | $statements->addNewStatement( $snak, null, null, $guid ); |
67 | $item->setStatements( $statements ); |
68 | $store->saveEntity( $item, "Imported formula from StackExchange.", $user ); |
69 | } |
70 | } |