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  $variantLangs = [];
44  $variants = [];
45  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
46  $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $langCode );
47  if ( $lang->hasVariants() ) {
48  $variants += array_flip( $lang->getVariants() );
49  $variantLangs[$langCode] = $lang->getVariants();
50  }
51  }
52  $variants = array_keys( $variants );
53  sort( $variants );
54  $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
55 
56  // Not using $this->output() because muting makes no sense here
57  if ( $this->hasOption( 'json' ) ) {
58  echo FormatJson::encode( $result, true ) . "\n";
59  } else {
60  foreach ( $result as $key => $value ) {
61  if ( is_array( $value ) ) {
62  echo "$key\n";
63  foreach ( $value as $variant ) {
64  echo " $variant\n";
65  }
66  } else {
67  echo "$value\n";
68  }
69  }
70  }
71  }
72 }
73 
74 $maintClass = ListVariants::class;
75 require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$maintClass
if(!isset( $args[0])) $lang
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
hasOption( $name)
Checks to see if a particular option exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static array $languagesWithVariants
languages supporting variants