Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListVariants
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Lists all language variants
4 *
5 * Copyright © 2014 MediaWiki developers
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27use MediaWiki\Json\FormatJson;
28use MediaWiki\Language\LanguageConverter;
29
30// @codeCoverageIgnoreStart
31require_once dirname( __DIR__ ) . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
34/**
35 * @since 1.24
36 */
37class ListVariants extends Maintenance {
38
39    public function __construct() {
40        parent::__construct();
41        $this->addDescription( 'Outputs a list of language variants' );
42        $this->addOption( 'flat', 'Output variants in a flat list' );
43        $this->addOption( 'json', 'Output variants as JSON' );
44    }
45
46    public function execute() {
47        $services = $this->getServiceContainer();
48        $languageFactory = $services->getLanguageFactory();
49        $languageConverterFactory = $services->getLanguageConverterFactory();
50        $variantLangs = [];
51        $variants = [];
52        foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
53            $lang = $languageFactory->getLanguage( $langCode );
54            $langConv = $languageConverterFactory->getLanguageConverter( $lang );
55            if ( $langConv->hasVariants() ) {
56                $variants += array_fill_keys( $langConv->getVariants(), true );
57                $variantLangs[$langCode] = $langConv->getVariants();
58            }
59        }
60        $variants = array_keys( $variants );
61        sort( $variants );
62        $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
63
64        // Not using $this->output() because muting makes no sense here
65        if ( $this->hasOption( 'json' ) ) {
66            echo FormatJson::encode( $result, true ) . "\n";
67        } else {
68            foreach ( $result as $key => $value ) {
69                if ( is_array( $value ) ) {
70                    echo "$key\n";
71                    foreach ( $value as $variant ) {
72                        echo "   $variant\n";
73                    }
74                } else {
75                    echo "$value\n";
76                }
77            }
78        }
79    }
80}
81
82// @codeCoverageIgnoreStart
83$maintClass = ListVariants::class;
84require_once RUN_MAINTENANCE_IF_MAIN;
85// @codeCoverageIgnoreEnd