Merge "docs: Fix typo"
[mediawiki.git] / maintenance / language / listVariants.php
blob0c14a9006733c261449a8569292b5a85bb9f4699
1 <?php
2 /**
3 * Lists all language variants
5 * Copyright © 2014 MediaWiki developers
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Maintenance
27 use MediaWiki\Json\FormatJson;
28 use MediaWiki\Language\LanguageConverter;
30 // @codeCoverageIgnoreStart
31 require_once dirname( __DIR__ ) . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * @since 1.24
37 class ListVariants extends Maintenance {
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' );
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();
60 $variants = array_keys( $variants );
61 sort( $variants );
62 $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
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";
74 } else {
75 echo "$value\n";
82 // @codeCoverageIgnoreStart
83 $maintClass = ListVariants::class;
84 require_once RUN_MAINTENANCE_IF_MAIN;
85 // @codeCoverageIgnoreEnd