Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.78% covered (warning)
83.78%
31 / 37
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LegacyMapFrame
83.78% covered (warning)
83.78%
31 / 37
66.67% covered (warning)
66.67%
2 / 3
12.61
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 saveData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 render
80.00% covered (warning)
80.00%
24 / 30
0.00% covered (danger)
0.00%
0 / 1
10.80
1<?php
2
3namespace Kartographer\Tag;
4
5use Kartographer\PartialWikitextParser;
6use Kartographer\State;
7use MediaWiki\Config\Config;
8use MediaWiki\Html\Html;
9use MediaWiki\Languages\LanguageNameUtils;
10use MediaWiki\Title\TitleFormatter;
11
12/**
13 * The <mapframe> tag inserts a map into wiki page
14 *
15 * @license MIT
16 */
17class LegacyMapFrame extends LegacyTagHandler {
18    public const TAG = 'mapframe';
19
20    private TitleFormatter $titleFormatter;
21
22    public function __construct(
23        Config $config,
24        LanguageNameUtils $languageCodeValidator,
25        TitleFormatter $titleFormatter
26    ) {
27        parent::__construct(
28            $config,
29            $languageCodeValidator
30        );
31        $this->titleFormatter = $titleFormatter;
32    }
33
34    protected function saveData( State $state, array $geometries ): void {
35        parent::saveData( $state, $geometries );
36        // Must be after the parent call because that possibly added a private group hash
37        $state->addInteractiveGroups( $this->args->showGroups );
38    }
39
40    /** @inheritDoc */
41    protected function render( PartialWikitextParser $parser, bool $serverMayRenderOverlays ): string {
42        // TODO if fullwidth, we really should use interactive mode..
43        // BUT not possible to use both modes at the same time right now. T248023
44        // Should be fixed, especially considering VE in page editing etc...
45        $staticMode = $this->config->get( 'KartographerStaticMapframe' );
46        if ( $staticMode && !$serverMayRenderOverlays ) {
47            $this->getOutput()->setJsConfigVar( 'wgKartographerStaticMapframePreview', 1 );
48        }
49        $this->getOutput()->addModules( [ $staticMode && $serverMayRenderOverlays
50            ? 'ext.kartographer.staticframe'
51            : 'ext.kartographer.frame' ] );
52
53        $gen = new MapFrameAttributeGenerator( $this->args, $this->config );
54        $attrs = $gen->prepareAttrs();
55
56        $page = $this->parser->getPage();
57        $pageTitle = $page ? $this->titleFormatter->getPrefixedText( $page ) : '';
58        $revisionId = $this->parser->getRevisionId();
59        $imgAttrs = $gen->prepareImgAttrs( $serverMayRenderOverlays, $pageTitle, $revisionId );
60        $imgAttrs['alt'] = wfMessage( 'kartographer-static-mapframe-alt' )->text();
61
62        $thumbnail = Html::element( 'img', $imgAttrs );
63        if ( !$serverMayRenderOverlays ) {
64            $thumbnail = Html::rawElement( 'noscript', [], $thumbnail );
65            if ( $this->args->usesAutoPosition() ) {
66                // Impossible to render .png thumbnails that depend on unsaved ExternalData. Preview
67                // will replace this with a dynamic map anyway when JavaScript is available.
68                $thumbnail = '';
69            }
70        }
71
72        $html = Html::rawElement( 'a', $attrs, $thumbnail );
73        if ( $this->args->frameless ) {
74            return $html;
75        }
76
77        $caption = (string)$this->args->text;
78        if ( $caption !== '' ) {
79            $html .= Html::rawElement( 'div', [ 'class' => 'thumbcaption' ],
80                $parser->halfParseWikitext( $caption ) );
81        }
82
83        return Html::rawElement( 'div', [ 'class' => $gen->getThumbClasses() ],
84            Html::rawElement( 'div', [
85                    'class' => 'thumbinner',
86                    'style' => "width: $gen->cssWidth;",
87                ], $html ) );
88    }
89
90}