Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
MwsDumpWriter | |
0.00% |
0 / 36 |
|
0.00% |
0 / 12 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
InitializeHeader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
generateIndexString | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getHead | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFooter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMwsExpression | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
addMwsExpression | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addRevision | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getMwsns | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setMwsns | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addFromMathIdGenerator | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | use MediaWiki\Extension\Math\MathLaTeXML; |
4 | |
5 | class MwsDumpWriter { |
6 | |
7 | /** @var string */ |
8 | private $mwsns = 'mws:'; |
9 | /** @var string */ |
10 | private $XMLHead; |
11 | /** @var string */ |
12 | private $XMLFooter; |
13 | /** @var string */ |
14 | private $outBuffer = ''; |
15 | |
16 | public function __construct( $ns = 'mws:' ) { |
17 | $this->setMwsns( $ns ); |
18 | $this->InitializeHeader(); |
19 | } |
20 | |
21 | public function InitializeHeader() { |
22 | $ns = $this->mwsns; |
23 | $this->XMLHead = "<?xml version=\"1.0\"?>\n<" . $ns . |
24 | 'harvest xmlns:mws="http://search.mathweb.org/ns" xmlns:m="http://www.w3.org/1998/Math/MathML">'; |
25 | $this->XMLFooter = '</' . $ns . 'harvest>'; |
26 | } |
27 | |
28 | /** |
29 | * @param stdClass $row |
30 | * |
31 | * @return string |
32 | */ |
33 | public function generateIndexString( $row ) { |
34 | $xml = simplexml_load_string( $row->math_mathml ); |
35 | if ( !$xml ) { |
36 | echo "ERROR while converting:\n " . var_export( $row->math_mathml, true ) . "\n"; |
37 | foreach ( libxml_get_errors() as $error ) { |
38 | echo "\t", $error->message; |
39 | } |
40 | libxml_clear_errors(); |
41 | return ''; |
42 | } |
43 | return $this->getMwsExpression( $row->math_mathml, |
44 | $row->mathindex_revision_id, $row->mathindex_anchor |
45 | ); |
46 | } |
47 | |
48 | public function getHead() { |
49 | return $this->XMLHead; |
50 | } |
51 | |
52 | public function getFooter() { |
53 | return $this->XMLFooter; |
54 | } |
55 | |
56 | /** |
57 | * @param string $mathML |
58 | * @param int $revId |
59 | * @param string $eId |
60 | * @return string |
61 | */ |
62 | public function getMwsExpression( $mathML, $revId, $eId ) { |
63 | $out = "\n<" . $this->mwsns . "expr url=\"{$revId}#{$eId}\">\n\t"; |
64 | $out .= $mathML; |
65 | $out .= "\n</" . $this->mwsns . "expr>\n"; |
66 | return $out; |
67 | } |
68 | |
69 | /** |
70 | * @param string $mathML |
71 | * @param int $revId |
72 | * @param string $eId |
73 | * @return true |
74 | */ |
75 | public function addMwsExpression( $mathML, $revId, $eId ) { |
76 | $this->outBuffer .= $this->getMwsExpression( $mathML, $revId, $eId ); |
77 | return true; |
78 | } |
79 | |
80 | public function addRevision( $revId ) { |
81 | $mathId = MathIdGenerator::newFromRevisionId( $revId ); |
82 | $this->addFromMathIdGenerator( $mathId ); |
83 | } |
84 | |
85 | /** |
86 | * @return string |
87 | */ |
88 | public function getMwsns() { |
89 | return $this->mwsns; |
90 | } |
91 | |
92 | /** |
93 | * @param string $mwsns |
94 | */ |
95 | public function setMwsns( $mwsns ) { |
96 | $this->mwsns = $mwsns; |
97 | } |
98 | |
99 | public function getOutput() { |
100 | return $this->getHead() . $this->outBuffer . $this->getFooter(); |
101 | } |
102 | |
103 | /** |
104 | * @param MathIdGenerator $generator |
105 | */ |
106 | public function addFromMathIdGenerator( MathIdGenerator $generator ) { |
107 | foreach ( $generator->getMathTags() as $key => $tag ) { |
108 | $mml = new MathLaTeXML( $tag[MathIdGenerator::CONTENT_POS], |
109 | $tag[MathIdGenerator::ATTRIB_POS] ); |
110 | $mml->render(); |
111 | $this->outBuffer .= $this->getMwsExpression( $mml->getMathml(), |
112 | $generator->getRevisionId(), |
113 | $generator->parserKey2fId( $key ) ); |
114 | } |
115 | } |
116 | } |