Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
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;
28
29require_once dirname( __DIR__ ) . '/Maintenance.php';
30
31/**
32 * @since 1.24
33 */
34class ListVariants extends Maintenance {
35
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription( 'Outputs a list of language variants' );
39        $this->addOption( 'flat', 'Output variants in a flat list' );
40        $this->addOption( 'json', 'Output variants as JSON' );
41    }
42
43    public function execute() {
44        $services = $this->getServiceContainer();
45        $languageFactory = $services->getLanguageFactory();
46        $languageConverterFactory = $services->getLanguageConverterFactory();
47        $variantLangs = [];
48        $variants = [];
49        foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
50            $lang = $languageFactory->getLanguage( $langCode );
51            $langConv = $languageConverterFactory->getLanguageConverter( $lang );
52            if ( $langConv->hasVariants() ) {
53                $variants += array_fill_keys( $langConv->getVariants(), true );
54                $variantLangs[$langCode] = $langConv->getVariants();
55            }
56        }
57        $variants = array_keys( $variants );
58        sort( $variants );
59        $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
60
61        // Not using $this->output() because muting makes no sense here
62        if ( $this->hasOption( 'json' ) ) {
63            echo FormatJson::encode( $result, true ) . "\n";
64        } else {
65            foreach ( $result as $key => $value ) {
66                if ( is_array( $value ) ) {
67                    echo "$key\n";
68                    foreach ( $value as $variant ) {
69                        echo "   $variant\n";
70                    }
71                } else {
72                    echo "$value\n";
73                }
74            }
75        }
76    }
77}
78
79$maintClass = ListVariants::class;
80require_once RUN_MAINTENANCE_IF_MAIN;