Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
3.28% |
2 / 61 |
|
16.67% |
2 / 12 |
CRAP | |
0.00% |
0 / 1 |
Row | |
3.28% |
2 / 61 |
|
16.67% |
2 / 12 |
501.65 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
getLog | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIgnoredFieldCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createWbItem | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
getItem | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getQid | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStatementList | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
processBody | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
getQIdFromField | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
addField | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MathSearch\StackExchange; |
4 | |
5 | use Exception; |
6 | use MediaWiki\Logger\LoggerFactory; |
7 | use SimpleXMLElement; |
8 | use User; |
9 | use Wikibase\DataModel\Entity\Item; |
10 | use Wikibase\DataModel\Entity\ItemId; |
11 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
12 | use Wikibase\DataModel\Statement\StatementList; |
13 | use Wikibase\Repo\WikibaseRepo; |
14 | |
15 | class Row { |
16 | |
17 | /** @var Field[] */ |
18 | private $fields = []; |
19 | /** @var int */ |
20 | private $ignoredFieldCount = 0; |
21 | /** @var string */ |
22 | private $normFileName; |
23 | |
24 | public function __construct( $line, $fileName ) { |
25 | $this->normFileName = $fileName; |
26 | set_error_handler( static function ( $errno, $errstr, $errfile, $errline ) { |
27 | throw new Exception( $errstr, $errno ); |
28 | } ); |
29 | if ( is_array( $line ) ) { |
30 | foreach ( $line as $key => $value ) { |
31 | $this->addField( $fileName, $key, $value ); |
32 | } |
33 | } else { |
34 | $row = new SimpleXMLElement( $line ); |
35 | restore_error_handler(); |
36 | foreach ( $row->attributes() as $key => $value ) { |
37 | $this->addField( $fileName, $key, (string)$value ); |
38 | } |
39 | } |
40 | } |
41 | |
42 | private static function getLog() { |
43 | return LoggerFactory::getInstance( 'MathSearch' ); |
44 | } |
45 | |
46 | /** |
47 | * @return Field[] |
48 | */ |
49 | public function getFields(): array { |
50 | return $this->fields; |
51 | } |
52 | |
53 | /** |
54 | * @return int |
55 | */ |
56 | public function getIgnoredFieldCount(): int { |
57 | return $this->ignoredFieldCount; |
58 | } |
59 | |
60 | public function createWbItem() { |
61 | $store = WikibaseRepo::getEntityStore(); |
62 | $user = User::newFromName( 'Maintenance script' ); |
63 | $item = $this->getItem(); |
64 | $id = $this->fields['Id']->getContent(); |
65 | $item->setLabel( 'en', "{$this->normFileName} {$id}" ); |
66 | if ( array_key_exists( 'Title', $this->fields ) ) { |
67 | $item->setDescription( 'en', $this->fields['Title']->getContent() ); |
68 | } |
69 | $item->setStatements( $this->getStatementList() ); |
70 | $store->saveEntity( $item, "Imported item from StackExchange.", $user ); |
71 | } |
72 | |
73 | public function getItem() { |
74 | $qid = $this->getQid(); |
75 | $item = new Item(); |
76 | $item->setId( ItemId::newFromNumber( $qid ) ); |
77 | |
78 | return $item; |
79 | } |
80 | |
81 | /** |
82 | * @return int|string |
83 | */ |
84 | public function getQid() { |
85 | $id = $this->getField( 'Id' ); |
86 | return IdMap::getInstance()->addQid( $id->getContent(), $id->getExternalIdType() ); |
87 | } |
88 | |
89 | public function getField( $name ): Field { |
90 | return $this->fields[$name]; |
91 | } |
92 | |
93 | public function getStatementList() { |
94 | $guidGenerator = new GuidGenerator(); |
95 | $statements = new StatementList(); |
96 | foreach ( $this->fields as $field ) { |
97 | if ( !$field->isExcludedFromWb() ) { |
98 | $snaks = $field->getSnaks(); |
99 | foreach ( $snaks as $snak ) { |
100 | $guid = $guidGenerator->newGuid( $this->getItem()->getId() ); |
101 | $statements->addNewStatement( $snak, null, null, $guid ); |
102 | } |
103 | } |
104 | } |
105 | |
106 | return $statements; |
107 | } |
108 | |
109 | public function processBody() { |
110 | $body = $this->getField( 'Body' )->getContent(); |
111 | $wtGen = new WikitextGenerator(); |
112 | $qid = $this->getQid(); |
113 | try { |
114 | $wt = $wtGen->toWikitext( $body, $qid ); |
115 | foreach ( $wtGen->getFormulae() as $f ) { |
116 | // $f->createWbItem(); |
117 | $f->updateSearchIndex(); |
118 | } |
119 | } catch ( \Throwable $e ) { |
120 | $wt = $body; |
121 | self::getLog()->error( "Problem while concerting {body} to wikitext: {e}", [ |
122 | 'body' => $body, |
123 | 'e' => $e->getMessage(), |
124 | ] ); |
125 | } |
126 | $type = $this->getQIdFromField( 'PostTypeId' ); |
127 | $parent = $this->getQIdFromField( 'ParentId' ); |
128 | IdMap::getInstance()->addWikiText( $qid, $parent, $type, $wt ); |
129 | } |
130 | |
131 | public function getQIdFromField( $fieldName ) { |
132 | if ( array_key_exists( $fieldName, $this->fields ) ) { |
133 | return $this->getField( $fieldName )->getContent(); |
134 | } else { |
135 | return null; |
136 | } |
137 | } |
138 | |
139 | /** |
140 | * @param string $fileName |
141 | * @param string $key |
142 | * @param string $value |
143 | */ |
144 | private function addField( $fileName, $key, $value ): void { |
145 | $field = new Field( $key, $value, $fileName ); |
146 | if ( $field->isKnown() ) { |
147 | $this->fields[$key] = $field; |
148 | } else { |
149 | $this->ignoredFieldCount++; |
150 | } |
151 | } |
152 | } |