MediaWiki  master
listVariants.php
Go to the documentation of this file.
1 <?php
26 require_once dirname( __DIR__ ) . '/Maintenance.php';
27 
31 class ListVariants extends Maintenance {
32 
33  public function __construct() {
34  parent::__construct();
35  $this->addDescription( 'Outputs a list of language variants' );
36  $this->addOption( 'flat', 'Output variants in a flat list' );
37  $this->addOption( 'json', 'Output variants as JSON' );
38  }
39 
40  public function execute() {
41  $variantLangs = [];
42  $variants = [];
43  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
44  $lang = Language::factory( $langCode );
45  if ( $lang->hasVariants() ) {
46  $variants += array_flip( $lang->getVariants() );
47  $variantLangs[$langCode] = $lang->getVariants();
48  }
49  }
50  $variants = array_keys( $variants );
51  sort( $variants );
52  $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
53 
54  // Not using $this->output() because muting makes no sense here
55  if ( $this->hasOption( 'json' ) ) {
56  echo FormatJson::encode( $result, true ) . "\n";
57  } else {
58  foreach ( $result as $key => $value ) {
59  if ( is_array( $value ) ) {
60  echo "$key\n";
61  foreach ( $value as $variant ) {
62  echo " $variant\n";
63  }
64  } else {
65  echo "$value\n";
66  }
67  }
68  }
69  }
70 }
71 
72 $maintClass = ListVariants::class;
73 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:82
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.
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:218
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static array $languagesWithVariants
languages supporting variants