Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParsoidMapFrame
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 1
 sourceToDom
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace Kartographer\Tag;
4
5use DOMException;
6use Kartographer\ParsoidUtils;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Title\Title;
9use Wikimedia\Parsoid\DOM\DocumentFragment;
10use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
11
12/**
13 * @license MIT
14 */
15class ParsoidMapFrame extends ParsoidTagHandler {
16
17    public const TAG = 'mapframe';
18
19    /**
20     * @param ParsoidExtensionAPI $extApi
21     * @param string $src
22     * @param array $extArgs
23     * @return DocumentFragment
24     * @throws DOMException
25     */
26    public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
27        [ $status, $args, $geometries, $srcOffsets ] = $this->parseTag( $extApi, $src, $extArgs );
28
29        if ( !$status->isGood() ) {
30            return $this->reportErrors( $extApi, self::TAG, $status );
31        }
32
33        // TODO if fullwidth, we really should use interactive mode..
34        // BUT not possible to use both modes at the same time right now. T248023
35        // Should be fixed, especially considering VE in page editing etc...
36        $config = MediaWikiServices::getInstance()->getMainConfig();
37        $staticMode = $config->get( 'KartographerStaticMapframe' );
38
39        $serverMayRenderOverlays = !$extApi->isPreview();
40        if ( $staticMode && !$serverMayRenderOverlays ) {
41            $extApi->getMetadata()->setJsConfigVar( 'wgKartographerStaticMapframePreview', 1 );
42        }
43
44        $extApi->getMetadata()->addModules( [ $staticMode && $serverMayRenderOverlays
45            ? 'ext.kartographer.staticframe'
46            : 'ext.kartographer.frame' ] );
47
48        $gen = new MapFrameAttributeGenerator( $args, $config );
49        $attrs = $gen->prepareAttrs();
50
51        $linkTarget = $extApi->getPageConfig()->getLinkTarget();
52        $pageTitle = Title::newFromLinkTarget( $linkTarget )->getPrefixedText();
53        $revisionId = $extApi->getPageConfig()->getRevisionId();
54        $imgAttrs = $gen->prepareImgAttrs( $serverMayRenderOverlays, $pageTitle, $revisionId );
55
56        $doc = $extApi->getTopLevelDoc();
57        $dom = $doc->createDocumentFragment();
58
59        $thumbnail = $doc->createElement( 'img' );
60
61        ParsoidUtils::addAttributesToNode( $imgAttrs, $thumbnail );
62        ParsoidUtils::createLangAttribute( $thumbnail, 'alt', 'kartographer-static-mapframe-alt', [], $extApi,
63            null );
64
65        if ( !$serverMayRenderOverlays ) {
66            $noscript = $doc->createElement( 'noscript' );
67            $noscript->appendChild( $thumbnail );
68            $thumbnail = $noscript;
69            if ( $args->usesAutoPosition() ) {
70                // Impossible to render .png thumbnails that depend on unsaved ExternalData. Preview
71                // will replace this with a dynamic map anyway when JavaScript is available.
72                $thumbnail = $doc->createTextNode( '' );
73            }
74        }
75
76        $a = $doc->createElement( 'a' );
77        $dataKart = [
78            'groupId' => $args->groupId,
79            'showGroups' => $args->showGroups,
80            'geometries' => $geometries
81        ];
82        $a->setAttribute( 'data-kart', json_encode( $dataKart ) );
83        ParsoidUtils::addAttributesToNode( $attrs, $a );
84
85        $a->appendChild( $thumbnail );
86
87        if ( $args->frameless ) {
88            $dom->appendChild( $a );
89            return $dom;
90        }
91        $thumbinner = $doc->createElement( 'div' );
92
93        $thumbinner->setAttribute( 'class', 'thumbinner' );
94        $thumbinner->setAttribute( 'style', "width: $gen->cssWidth;" );
95        $thumbinner->appendChild( $a );
96        $caption = (string)$args->text;
97        if ( $caption !== '' ) {
98            $parsedCaption = $extApi->wikitextToDOM( $caption, [
99                'parseOpts' => [
100                    'extTag' => 'mapframe',
101                    'context' => 'inline',
102                ],
103                'srcOffsets' => $srcOffsets['text']->value ?? null,
104            ], false );
105            $div = $doc->createElement( 'div' );
106            $div->setAttribute( 'class', 'thumbcaption' );
107            $div->appendChild( $parsedCaption );
108            $thumbinner->appendChild( $div );
109        }
110        $container = $doc->createElement( 'div' );
111        $containerClasses = $gen->getThumbClasses();
112        $container->setAttribute( 'class', implode( ' ', $containerClasses ) );
113        $container->appendChild( $thumbinner );
114        $dom->appendChild( $container );
115        return $dom;
116    }
117
118}