MediaWiki  1.34.0
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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
ListVariants\execute
execute()
Do the actual work.
Definition: listVariants.php:40
$maintClass
$maintClass
Definition: listVariants.php:72
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
ListVariants\__construct
__construct()
Default constructor.
Definition: listVariants.php:33
ListVariants
Definition: listVariants.php:31
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288