Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GettextDocumentationAid
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 1
 getData
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use FileBasedMessageGroup;
7use MediaWiki\Extension\Translate\FileFormatSupport\GettextFormat;
8use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException;
9use MediaWiki\MediaWikiServices;
10
11/**
12 * Translation aid that provides Gettext documentation.
13 * @ingroup TranslationAids
14 * @author Niklas Laxström
15 * @license GPL-2.0-or-later
16 * @since 2013-01-01
17 */
18class GettextDocumentationAid extends TranslationAid {
19    public function getData(): array {
20        // We need to get the primary group to get the correct file
21        // So $group can be different from $this->group
22        $group = $this->handle->getGroup();
23        if ( !$group instanceof FileBasedMessageGroup ) {
24            throw new TranslationHelperException( 'Not a FileBasedMessageGroup group' );
25        }
26
27        $fileFormat = $group->getFFS();
28        if ( !$fileFormat instanceof GettextFormat ) {
29            throw new TranslationHelperException( 'Group is not using GettextFormat' );
30        }
31
32        $cache = $group->getMessageGroupCache( $group->getSourceLanguage() );
33        if ( !$cache->exists() ) {
34            throw new TranslationHelperException( 'Definitions are not cached' );
35        }
36
37        $extra = $cache->getExtra();
38        $contLang = MediaWikiServices::getInstance()->getContentLanguage();
39        $messageKey = $contLang->lcfirst( $this->handle->getKey() );
40        $messageKey = str_replace( ' ', '_', $messageKey );
41
42        $help = $extra['TEMPLATE'][$messageKey]['comments'] ?? null;
43        if ( !$help ) {
44            throw new TranslationHelperException( "No comments found for key '$messageKey'" );
45        }
46
47        $conf = $group->getConfiguration();
48        if ( isset( $conf['BASIC']['codeBrowser'] ) ) {
49            $pattern = $conf['BASIC']['codeBrowser'];
50            $pattern = str_replace( '%FILE%', '\1', $pattern );
51            $pattern = str_replace( '%LINE%', '\2', $pattern );
52            $pattern = "[$pattern \\1:\\2]";
53        } else {
54            $pattern = "\\1:\\2";
55        }
56
57        $out = '';
58        foreach ( $help as $type => $lines ) {
59            if ( $type === ':' ) {
60                $files = '';
61                foreach ( $lines as $line ) {
62                    $files .= ' ' . preg_replace( '/([^ :]+):(\d+)/', $pattern, $line );
63                }
64                $out .= "<nowiki>#:</nowiki> $files<br />";
65            } else {
66                foreach ( $lines as $line ) {
67                    $out .= "<nowiki>#$type</nowiki> $line<br />";
68                }
69            }
70        }
71
72        $html = $this->context->getOutput()->parseAsContent( $out );
73
74        return [
75            'language' => $contLang->getCode(),
76            // @todo Provide raw data when possible
77            // 'value' => $help,
78            'html' => $html,
79        ];
80    }
81}