MediaWiki master
listVariants.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once dirname( __DIR__ ) . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Outputs a list of language variants' );
43 $this->addOption( 'flat', 'Output variants in a flat list' );
44 $this->addOption( 'json', 'Output variants as JSON' );
45 }
46
47 public function execute() {
48 $services = $this->getServiceContainer();
49 $languageFactory = $services->getLanguageFactory();
50 $languageConverterFactory = $services->getLanguageConverterFactory();
51 $variantLangs = [];
52 $variants = [];
53 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
54 $lang = $languageFactory->getLanguage( $langCode );
55 $langConv = $languageConverterFactory->getLanguageConverter( $lang );
56 if ( $langConv->hasVariants() ) {
57 $variants += array_fill_keys( $langConv->getVariants(), true );
58 $variantLangs[$langCode] = $langConv->getVariants();
59 }
60 }
61 $variants = array_keys( $variants );
62 sort( $variants );
63 $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
64
65 // Not using $this->output() because muting makes no sense here
66 if ( $this->hasOption( 'json' ) ) {
67 echo FormatJson::encode( $result, true ) . "\n";
68 } else {
69 foreach ( $result as $key => $value ) {
70 if ( is_array( $value ) ) {
71 echo "$key\n";
72 foreach ( $value as $variant ) {
73 echo " $variant\n";
74 }
75 } else {
76 echo "$value\n";
77 }
78 }
79 }
80 }
81}
82
83// @codeCoverageIgnoreStart
84$maintClass = ListVariants::class;
85require_once RUN_MAINTENANCE_IF_MAIN;
86// @codeCoverageIgnoreEnd
__construct()
Default constructor.
execute()
Do the actual work.
JSON formatter wrapper class.
Base class for multi-variant language conversion.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
$maintClass