MediaWiki REL1_35
listVariants.php
Go to the documentation of this file.
1<?php
27
28require_once dirname( __DIR__ ) . '/Maintenance.php';
29
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;
75require_once RUN_MAINTENANCE_IF_MAIN;
const 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.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
$maintClass
if(!isset( $args[0])) $lang