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 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 | private $id; |
17 | private $qid; |
18 | private $text; |
19 | private $postQId; |
20 | |
21 | /** |
22 | * @param int $id |
23 | * @param int $qid |
24 | * @param string $text |
25 | * @param int $postQId |
26 | */ |
27 | public function __construct( $id, $qid, $text, $postQId ) { |
28 | $this->id = $id; |
29 | $this->qid = $qid; |
30 | $this->text = $text; |
31 | $this->postQId = $postQId; |
32 | } |
33 | |
34 | public function updateSearchIndex() { |
35 | $renderer = new MathMathML( $this->text, [ 'display' => 'block' ] ); |
36 | $hash = $renderer->getInputHash(); |
37 | $renderer->writeToCache(); |
38 | // TODO: Fix fake revision ID |
39 | \MathSearchHooks::writeMathIndex( 1, $this->id, $hash, $this->text ); |
40 | } |
41 | |
42 | public function createWbItem() { |
43 | $sf = WikibaseRepo::getSnakFactory(); |
44 | $store = WikibaseRepo::getEntityStore(); |
45 | $user = User::newFromName( 'Maintenance script' ); |
46 | $item = new Item(); |
47 | $item->setId( ItemId::newFromNumber( $this->qid ) ); |
48 | $item->setLabel( 'en', "Formula {$this->id}" ); |
49 | $guidGenerator = new GuidGenerator(); |
50 | $statements = new StatementList(); |
51 | $guid = $guidGenerator->newGuid( $item->getId() ); |
52 | // TODO: get from settings |
53 | $snak = $sf->newSnak( new NumericPropertyId( 'P1' ), 'value', $this->text ); |
54 | $statements->addNewStatement( $snak, null, null, $guid ); |
55 | $guid = $guidGenerator->newGuid( $item->getId() ); |
56 | $snak = $sf->newSnak( new NumericPropertyId( 'P8' ), 'value', (string)$this->id ); |
57 | $statements->addNewStatement( $snak, null, null, $guid ); |
58 | $guid = $guidGenerator->newGuid( $item->getId() ); |
59 | $snak = |
60 | $sf->newSnak( new NumericPropertyId( 'P16' ), 'value', |
61 | [ 'entity-type' => 'item', 'numeric-id' => (int)$this->postQId ] ); |
62 | $statements->addNewStatement( $snak, null, null, $guid ); |
63 | $item->setStatements( $statements ); |
64 | $store->saveEntity( $item, "Imported formula from StackExchange.", $user ); |
65 | } |
66 | } |