Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
JCMapDataContentView
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 valueToHtml
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
156
 getDefault
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace JsonConfig;
3
4use ExtensionRegistry;
5use FormatJson;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Page\PageReference;
8use MediaWiki\Parser\ParserOutput;
9use ParserOptions;
10
11/**
12 * @package JsonConfig
13 */
14
15class JCMapDataContentView extends JCContentView {
16
17    /**
18     * Render JCContent object as HTML
19     * Called from an override of AbstractContent::fillParserOutput()
20     *
21     * Render JCContent object as HTML - replaces valueToHtml()
22     * @param JCContent|JCDataContent $content
23     * @param PageReference $page Context title for parsing
24     * @param int|null $revId Revision ID (for {{REVISIONID}})
25     * @param ParserOptions $options Parser options
26     * @param bool $generateHtml Whether or not to generate HTML
27     * @param ParserOutput &$output The output object to fill (reference).
28     * @return string
29     */
30    public function valueToHtml(
31        JCContent $content, PageReference $page, $revId, ParserOptions $options,
32        $generateHtml, ParserOutput &$output
33    ) {
34        $parser = MediaWikiServices::getInstance()->getParserFactory()->getInstance();
35
36        $localizedData = $content->getLocalizedData( $options->getUserLangObj() );
37        if ( $localizedData ) {
38            $extReg = ExtensionRegistry::getInstance();
39
40            // Test both because for some reason getTags() is empty during preview
41            if ( in_array( 'mapframe', $parser->getTags(), true ) ||
42                $extReg->isLoaded( 'Kartographer' )
43            ) {
44                $zoom = $content->getField( 'zoom' );
45                $lat = $content->getField( 'latitude' );
46                $lon = $content->getField( 'longitude' );
47                if ( $zoom && $lat && $lon && !$zoom->error() && !$lat->error() &&
48                    !$lon->error()
49                ) {
50                    $zoom = $zoom->getValue();
51                    $lat = $lat->getValue();
52                    $lon = $lon->getValue();
53                } else {
54                    $zoom = 3;
55                    $lat = $lon = 0;
56                }
57
58                $jsonText = FormatJson::encode( $localizedData->data, false, FormatJson::UTF8_OK );
59                $text = <<<EOT
60<mapframe width="100%" height="600" latitude="$lat" longitude="$lon" zoom="$zoom">
61$jsonText
62</mapframe>
63EOT;
64            } else {
65                $jsonText = FormatJson::encode( $localizedData->data, true, FormatJson::UTF8_OK );
66                if ( in_array( 'syntaxhighlight', $parser->getTags(), true ) ||
67                    $extReg->isLoaded( 'SyntaxHighlight' )
68                ) {
69                    $text = "<syntaxhighlight lang=json>\n$jsonText\n</syntaxhighlight>";
70                } else {
71                    $text = "<pre>\n$jsonText\n</pre>";
72                }
73            }
74            $output = $parser->parse( $text, $page, $options, true, true, $revId );
75        }
76
77        return $content->renderDescription( $options->getUserLangObj() ) . '<br>' .
78            $output->getRawText() . '<br clear=all>' .
79            $content->renderSources( $parser, $page, $revId, $options ) .
80            $content->renderLicense();
81    }
82
83    /**
84     * Returns default content for this object.
85     * The returned valued does not have to be valid JSON
86     * @param string $modelId
87     * @return string
88     */
89    public function getDefault( $modelId ) {
90        $licenseIntro = JCContentView::getLicenseIntro();
91
92        return <<<EOT
93{
94    // !!!!! All comments will be automatically deleted on save !!!!!
95
96    // Optional "description" field to describe this map
97    "description": {"en": "map description"},
98
99    // Optional "sources" field to describe the sources of the map.  Can use Wiki Markup
100    "sources": "Copied from [http://example.com Example Map Source]",
101
102    $licenseIntro
103
104    "zoom": 3,
105    "latitude": 0,
106    "longitude": 0,
107    "data": {
108
109        ... GeoJSON ...
110
111    }
112}
113EOT;
114    }
115}