MediaWiki master
listVariants.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once dirname( __DIR__ ) . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
25
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( 'Outputs a list of language variants' );
29 $this->addOption( 'flat', 'Output variants in a flat list' );
30 $this->addOption( 'json', 'Output variants as JSON' );
31 }
32
33 public function execute() {
34 $services = $this->getServiceContainer();
35 $languageFactory = $services->getLanguageFactory();
36 $languageConverterFactory = $services->getLanguageConverterFactory();
37 $variantLangs = [];
38 $variants = [];
39 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
40 $lang = $languageFactory->getLanguage( $langCode );
41 $langConv = $languageConverterFactory->getLanguageConverter( $lang );
42 if ( $langConv->hasVariants() ) {
43 $variants += array_fill_keys( $langConv->getVariants(), true );
44 $variantLangs[$langCode] = $langConv->getVariants();
45 }
46 }
47 $variants = array_keys( $variants );
48 sort( $variants );
49 $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
50
51 // Not using $this->output() because muting makes no sense here
52 if ( $this->hasOption( 'json' ) ) {
53 echo FormatJson::encode( $result, true ) . "\n";
54 } else {
55 foreach ( $result as $key => $value ) {
56 if ( is_array( $value ) ) {
57 echo "$key\n";
58 foreach ( $value as $variant ) {
59 echo " $variant\n";
60 }
61 } else {
62 echo "$value\n";
63 }
64 }
65 }
66 }
67}
68
69// @codeCoverageIgnoreStart
70$maintClass = ListVariants::class;
71require_once RUN_MAINTENANCE_IF_MAIN;
72// @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