MediaWiki master
listVariants.php
Go to the documentation of this file.
1<?php
28
29require_once dirname( __DIR__ ) . '/Maintenance.php';
30
35
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Outputs a list of language variants' );
39 $this->addOption( 'flat', 'Output variants in a flat list' );
40 $this->addOption( 'json', 'Output variants as JSON' );
41 }
42
43 public function execute() {
44 $services = $this->getServiceContainer();
45 $languageFactory = $services->getLanguageFactory();
46 $languageConverterFactory = $services->getLanguageConverterFactory();
47 $variantLangs = [];
48 $variants = [];
49 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
50 $lang = $languageFactory->getLanguage( $langCode );
51 $langConv = $languageConverterFactory->getLanguageConverter( $lang );
52 if ( $langConv->hasVariants() ) {
53 $variants += array_fill_keys( $langConv->getVariants(), true );
54 $variantLangs[$langCode] = $langConv->getVariants();
55 }
56 }
57 $variants = array_keys( $variants );
58 sort( $variants );
59 $result = $this->hasOption( 'flat' ) ? $variants : $variantLangs;
60
61 // Not using $this->output() because muting makes no sense here
62 if ( $this->hasOption( 'json' ) ) {
63 echo FormatJson::encode( $result, true ) . "\n";
64 } else {
65 foreach ( $result as $key => $value ) {
66 if ( is_array( $value ) ) {
67 echo "$key\n";
68 foreach ( $value as $variant ) {
69 echo " $variant\n";
70 }
71 } else {
72 echo "$value\n";
73 }
74 }
75 }
76 }
77}
78
79$maintClass = ListVariants::class;
80require_once RUN_MAINTENANCE_IF_MAIN;
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
JSON formatter wrapper class.
$maintClass