Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
36.00% covered (danger)
36.00%
18 / 50
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DtdFormat
36.73% covered (danger)
36.73%
18 / 49
20.00% covered (danger)
20.00%
1 / 5
29.51
0.00% covered (danger)
0.00%
0 / 1
 getFileExtensions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 readFromVariable
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 writeReal
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
 doHeader
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 doAuthors
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\FileFormatSupport;
5
6use MediaWiki\Extension\Translate\MessageLoading\Message;
7use MediaWiki\Extension\Translate\MessageLoading\MessageCollection;
8use MediaWiki\Extension\Translate\Utilities\Utilities;
9
10/**
11 * File format support for DTD.
12 * @author Guillaume Duhamel
13 * @author Niklas Laxström
14 * @author Siebrand Mazeland
15 * @copyright Copyright © 2009-2010, Guillaume Duhamel, Niklas Laxström, Siebrand Mazeland
16 * @license GPL-2.0-or-later
17 * @ingroup FileFormatSupport
18 */
19class DtdFormat extends SimpleFormat {
20    public function getFileExtensions(): array {
21        return [ '.dtd' ];
22    }
23
24    public function readFromVariable( string $data ): array {
25        preg_match_all( ',# Author: ([^\n]+)\n,', $data, $matches );
26        $authors = $matches[1];
27
28        preg_match_all( ',<!ENTITY[ ]+([^ ]+)\s+"([^"]+)"[^>]*>,', $data, $matches );
29        [ , $keys, $messages ] = $matches;
30        $messages = array_combine(
31            $keys,
32            array_map(
33                static function ( $message ) {
34                    return html_entity_decode( $message, ENT_QUOTES );
35                },
36                $messages
37            )
38        );
39
40        $messages = $this->group->getMangler()->mangleArray( $messages );
41
42        return [
43            'AUTHORS' => $authors,
44            'MESSAGES' => $messages,
45        ];
46    }
47
48    protected function writeReal( MessageCollection $collection ): string {
49        $collection->loadTranslations();
50
51        $header = "<!--\n";
52        $header .= $this->doHeader( $collection );
53        $header .= $this->doAuthors( $collection );
54        $header .= "-->\n";
55
56        $output = '';
57        $mangler = $this->group->getMangler();
58
59        /** @var Message $message */
60        foreach ( $collection as $key => $message ) {
61            $key = $mangler->unmangle( $key );
62            $trans = $message->translation() ?? '';
63            if ( $trans === '' ) {
64                continue;
65            }
66            $trans = str_replace( TRANSLATE_FUZZY, '', $trans );
67
68            $trans = str_replace( '"', '&quot;', $trans );
69            $output .= "<!ENTITY $key \"$trans\">\n";
70        }
71
72        if ( $output ) {
73            return $header . $output;
74        }
75
76        return '';
77    }
78
79    private function doHeader( MessageCollection $collection ): string {
80        global $wgSitename;
81
82        $code = $collection->code;
83        $name = Utilities::getLanguageName( $code );
84        $native = Utilities::getLanguageName( $code, $code );
85
86        $output = "# Messages for $name ($native)\n";
87        $output .= "# Exported from $wgSitename\n\n";
88
89        return $output;
90    }
91
92    private function doAuthors( MessageCollection $collection ): string {
93        $output = '';
94        $authors = $collection->getAuthors();
95        $authors = $this->filterAuthors( $authors, $collection->code );
96
97        foreach ( $authors as $author ) {
98            $output .= "# Author: $author\n";
99        }
100
101        return $output;
102    }
103}
104
105class_alias( DtdFormat::class, 'DtdFFS' );