MediaWiki  master
listVariants.php
Go to the documentation of this file.
1 <?php
27 
28 require_once dirname( __DIR__ ) . '/Maintenance.php';
29 
33 class ListVariants extends Maintenance {
34 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Outputs a list of language variants' );
38  $this->addOption( 'flat', 'Output variants in a flat list' );
39  $this->addOption( 'json', 'Output variants as JSON' );
40  }
41 
42  public function execute() {
43  $services = MediaWikiServices::getInstance();
44  $languageFactory = $services->getLanguageFactory();
45  $languageConverterFactory = $services->getLanguageConverterFactory();
46  $variantLangs = [];
47  $variants = [];
48  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
49  $lang = $languageFactory->getLanguage( $langCode );
50  $langConv = $languageConverterFactory->getLanguageConverter( $lang );
51  if ( $langConv->hasVariants() ) {
52  $variants += array_fill_keys( $langConv->getVariants(), true );
53  $variantLangs[$langCode] = $langConv->getVariants();
54  }
55  }
56  $variants = array_keys( $variants );
57  sort( $variants );
58  $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
59 
60  // Not using $this->output() because muting makes no sense here
61  if ( $this->hasOption( 'json' ) ) {
62  echo FormatJson::encode( $result, true ) . "\n";
63  } else {
64  foreach ( $result as $key => $value ) {
65  if ( is_array( $value ) ) {
66  echo "$key\n";
67  foreach ( $value as $variant ) {
68  echo " $variant\n";
69  }
70  } else {
71  echo "$value\n";
72  }
73  }
74  }
75  }
76 }
77 
78 $maintClass = ListVariants::class;
79 require_once RUN_MAINTENANCE_IF_MAIN;
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
static array $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.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Service locator for MediaWiki core services.
$maintClass
if(!isset( $args[0])) $lang