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