MediaWiki master
languageNameIndexer.php
Go to the documentation of this file.
1<?php
19// @codeCoverageIgnoreStart
20require_once __DIR__ . '/Maintenance.php';
21// @codeCoverageIgnoreEnd
22
23use MediaWiki\Extension\CLDR\LanguageNames;
30use Wikimedia\LanguageData\LanguageUtil;
31
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Script to create language names index.' );
36
37 $extensionRegistry = ExtensionRegistry::getInstance();
38 if ( !$extensionRegistry->isLoaded( 'cldr' ) ) {
39 $this->requireExtension( 'cldr' );
40 }
41 }
42
43 public function execute() {
44 // Avoid local configuration leaking to this script
45 if ( $this->getConfig()->get( MainConfigNames::ExtraLanguageNames ) !== [] ) {
46 $this->fatalError( 'You have entries in $wgExtraLanguageNames. Needs to be empty for this script.' );
47 }
48
49 $languageNames = [];
50 // Add languages from language-data
51 $languageNames[ 'autonyms' ] = LanguageUtil::get()->getAutonyms();
52
53 // Languages and their names in different languages from Names.php and the cldr extension
54 // This comes after $ulsLanguages so that for example the als/gsw mixup is using the code
55 // used in the Wikimedia world.
56 $mwLanguages = $this->getServiceContainer()->getLanguageNameUtils()
57 ->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::ALL );
58 foreach ( array_keys( $mwLanguages ) as $languageCode ) {
59 // This method is in the CLDR extension
60 // @phan-suppress-next-line PhanUndeclaredClassMethod
61 $languageNames[ $languageCode ] = LanguageNames::getNames( $languageCode, 0, 2 );
62 }
63
64 $buckets = [];
65 foreach ( $languageNames as $translations ) {
66 foreach ( $translations as $targetLanguage => $translation ) {
67 $translation = mb_strtolower( $translation );
68 $translation = trim( $translation );
69
70 // Clean up "gjermanishte zvicerane (dialekti i alpeve)" to "gjermanishte zvicerane".
71 // The original name is still shown, but avoid us creating entries such as
72 // "(dialekti" or "alpeve)".
73 $basicForm = preg_replace( '/\‍(.+\‍)$/', '', $translation );
74 $words = preg_split( '/[\s]+/u', $basicForm, -1, PREG_SPLIT_NO_EMPTY );
75
76 foreach ( $words as $index => $word ) {
77 $bucket = LanguageNameSearch::getIndex( $word );
78
79 $type = 'prefix';
80 $display = $translation;
81 if ( $index > 0 ) {
82 // Avoid creating infix entries for short strings like punctuation, articles, prepositions...
83 if ( mb_strlen( $word ) < 3 ) {
84 continue;
85 }
86
87 $type = 'infix';
88 $display = "$word — $translation";
89 }
90 $buckets[$bucket][$type][$display] = $targetLanguage;
91 }
92 }
93 }
94
95 // Some languages don't have a conveniently searchable name in CLDR.
96 // For example, the name of Western Punjabi doesn't start with
97 // the string "punjabi" in any language, so it cannot be found
98 // by people who search in English.
99 // To resolve this, some languages are added here locally.
100 $specialLanguages = [
101 // Abron / Brong / Bono (T369464)
102 'abr' => [ 'bono', 'brong' ],
103 // Acholi (T376060)
104 'ach' => [ 'leb acoli' ],
105 // Hadhrami Arabic (T397355)
106 'ayh' => [ 'حضرمية' ],
107 // Catalan, sometimes searched as "Valencià"
108 'ca' => [ 'valencia' ],
109 // Compatibility with the old name and other Chinese varieties
110 'cdo' => [ 'chinese min dong' ],
111 // Alternate names for Anufo in linguistic literature
112 'cko' => [ 'chakosi', 'chokosi', 'tchokossi' ],
113 // Dolgan (T395396)
114 'dlg' => [ 'һака' ],
115 // Older name, see T375891
116 'dtp' => [ 'bundu-liwan, dusun' ],
117 // Spanish, the transliteration of the autonym is often used for searching
118 'es' => [ 'castellano' ],
119 // Armenian, the transliteration of the autonym is often used for searching
120 'hy' => [ 'hayeren' ],
121 // Japanese, the transliteration of the autonym is often used for searching
122 'ja' => [ 'nihongo', 'にほんご' ],
123 // Javanese (T393746)
124 'jv-java' => [ 'jawa hanacaraka' ],
125 // Georgian, the transliteration of the autonym is often used for searching
126 'ka' => [ 'kartuli', 'qartuli' ],
127 // Lango (Uganda; T376054).
128 // The second alias help avoid ambiguity with
129 // other languages named "Lango" and also
130 // with "Langi"
131 'laj' => [ 'leb lango', 'lango, leb' ],
132 // Chiluvale (T368856)
133 'lue' => [ 'luvale, chi-' ],
134 // Shan (T377856)
135 'shn' => [ 'ၽႃႇသႃႇတႆး', 'လိၵ်ႈတႆး' ],
136 // Tigrinya: variant names in Hebrew,
137 // to ensure they can be found in different spellings
138 'ti' => [
139 'טגריניה',
140 'טגרינית',
141 'טיגריניה',
142 'טיגרינית',
143 'תגריניה',
144 'תגרינית',
145 'תיגריניה',
146 ],
147 // Tigre: variant names in Hebrew,
148 // to ensure they can be found in different spellings
149 'tig' => [
150 'טגרה',
151 'טגרית',
152 'טיגרה',
153 'תגרה',
154 'תגרית',
155 'תיגרה',
156 'תיגרית',
157 ],
158 // Mon, renamed in core MediaWiki's Names.php (T352776)
159 'mnw' => [ 'ဘာသာ မန်' ],
160 // Palembang, also known as "Musi".
161 // Writing this as two words ensures that it has a unique key,
162 // so that Moore (mos), which is known as "musi" in one of the languages,
163 // can also be found
164 'mui' => [ 'musi palembang' ],
165 // Western Punjabi, doesn't start with the word "Punjabi" in any language
166 'pnb' => [ 'punjabi western' ],
167 // Tai Nuea (T367377)
168 'tdd' => [ 'ᥖᥭᥰᥖᥬᥳᥑᥨᥒᥰ' ],
169 // Waale (T368046) - support alternate spellings of the name
170 'wlx' => [ 'waali', 'waalii' ],
171 // Simplified and Traditional Chinese, because zh-hans and zh-hant
172 // are not mapped to any English name
173 'zh-hans' => [ 'chinese simplified' ],
174 'zh-hant' => [ 'chinese traditional' ],
175 // Compatibility with the old name and other Chinese varieties
176 'zh-min-nan' => [ 'chinese min nan' ],
177 ];
178
179 foreach ( $specialLanguages as $targetLanguage => $translations ) {
180 foreach ( $translations as $translation ) {
181 $bucket = LanguageNameSearch::getIndex( $translation );
182 $buckets[$bucket]['prefix'][$translation] = $targetLanguage;
183 }
184 }
185
186 $lengths = [];
187 // Sorting the bucket contents gives two benefits:
188 // - more consistent output across environments
189 // - shortest matches appear first, especially exact matches
190 // Sort buckets by index
191 ksort( $buckets );
192 foreach ( $buckets as &$bucketTypes ) {
193 $lengths[] = array_sum( array_map( 'count', $bucketTypes ) );
194 // Ensure 'prefix' is before 'infix';
195 krsort( $bucketTypes );
196 // Ensure each bucket has entries sorted
197 foreach ( $bucketTypes as &$bucket ) {
198 ksort( $bucket );
199 }
200 }
201
202 $count = count( $buckets );
203 $min = min( $lengths );
204 $max = max( $lengths );
205 $median = $lengths[ceil( $count / 2 )];
206 $avg = array_sum( $lengths ) / $count;
207 $this->output( "Bucket stats:\n - $count buckets\n - smallest has $min entries\n" );
208 $this->output( " - largest has $max entries\n - median size is $median entries\n" );
209 $this->output( " - average size is $avg entries\n" );
210
211 $this->generateFile( $buckets );
212 }
213
217 private function generateFile( array $buckets ) {
218 // Add metadata to indicate this is a generated file
219 $data = [
220 '_comment' => 'This file is generated by a script!',
221 '_generator' => 'maintenance/languageNameIndexer.php',
222 'buckets' => $buckets
223 ];
224 $json = FormatJson::encode( $data, "\t", FormatJson::ALL_OK );
225 file_put_contents( __DIR__ . '/../languages/data/LanguageNameSearchData.json', $json . "\n" );
226 }
227}
228
229// @codeCoverageIgnoreStart
230$maintClass = LanguageNameIndexer::class;
231require_once RUN_MAINTENANCE_IF_MAIN;
232// @codeCoverageIgnoreEnd
execute()
Do the actual work.
__construct()
Default constructor.
JSON formatter wrapper class.
Cross-Language Language name search.
A service that provides utilities to do with language names and codes.
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Load JSON files, and uses a Processor to extract information.