Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.67% covered (danger)
26.67%
8 / 30
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScoreFormatter
26.67% covered (danger)
26.67%
8 / 30
40.00% covered (danger)
40.00%
2 / 5
58.72
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
5.07
 formatAsHtml
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 formatDetails
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/*
3  Score, a MediaWiki extension for rendering musical scores with LilyPond.
4  Copyright © 2011 Alexander Klauer
5
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21namespace MediaWiki\Extension\Score;
22
23use DataValues\StringValue;
24use InvalidArgumentException;
25use MediaWiki\Html\Html;
26use ValueFormatters\ValueFormatter;
27use Wikibase\Lib\Formatters\SnakFormatter;
28
29/**
30 * Formats Lilypond string based on the known formats
31 * - text/plain: used in the value input field of Wikidata
32 * - text/x-wiki: wikitext
33 * - text/html: used in Wikidata to display the value of properties
34 * Formats can look like this: "text/html; disposition=diff"
35 * or just "text/plain"
36 */
37class ScoreFormatter implements ValueFormatter {
38
39    /**
40     * Loads format to distinguish the type of formatting
41     *
42     * @param string $format One of the SnakFormatter::FORMAT_... constants.
43     */
44    public function __construct(
45        private readonly string $format,
46    ) {
47    }
48
49    /**
50     * @param StringValue $value
51     *
52     * @throws InvalidArgumentException if not called with a StringValue
53     * @return string
54     */
55    public function format( $value ) {
56        if ( !( $value instanceof StringValue ) ) {
57            throw new InvalidArgumentException( '$value must be a StringValue' );
58        }
59
60        $valueString = $value->getValue();
61
62        switch ( $this->format ) {
63            case SnakFormatter::FORMAT_PLAIN:
64                return $valueString;
65            case SnakFormatter::FORMAT_WIKI:
66                return "<score>$valueString</score>";
67            default:
68                return $this->formatAsHtml( $valueString );
69        }
70    }
71
72    /**
73     * @param string $valueString
74     * @return string
75     */
76    private function formatAsHtml( $valueString ) {
77        $args = [];
78
79        global $wgWikibaseMusicalNotationLineWidthInches;
80        $args['line_width_inches'] = $wgWikibaseMusicalNotationLineWidthInches;
81
82        try {
83            $valueHtml = Score::renderScore(
84                $valueString,
85                $args
86            );
87        } catch ( ScoreException $exception ) {
88            return (string)$exception;
89        }
90
91        if ( $this->format === SnakFormatter::FORMAT_HTML_DIFF ) {
92            $valueHtml = $this->formatDetails( $valueHtml, $valueString );
93        }
94
95        return $valueHtml;
96    }
97
98    /**
99     * Constructs a detailed HTML rendering for use in diff views.
100     *
101     * @param string $valueHtml
102     * @param string $valueString
103     *
104     * @return string HTML
105     */
106    private function formatDetails( $valueHtml, $valueString ) {
107        $detailsHtml = '';
108        $detailsHtml .= Html::rawElement( 'h4',
109            [ 'class' => 'wb-details wb-musical-notation-details wb-musical-notation-rendered' ],
110            $valueHtml
111        );
112
113        $detailsHtml .= Html::rawElement( 'div',
114            [ 'class' => 'wb-details wb-musical-notation-details' ],
115            Html::element( 'code', [], $valueString )
116        );
117
118        return $detailsHtml;
119    }
120
121    /**
122     * @return string One of the SnakFormatter::FORMAT_... constants.
123     */
124    public function getFormat() {
125        return $this->format;
126    }
127
128}