46 parent::__construct();
47 $this->
addOption(
'data-dir',
'A directory on the local filesystem ' .
48 'containing allkeys.txt and ucd.all.grouped.xml from unicode.org',
50 $this->
addOption(
'debug-output',
'Filename for sending debug output to',
55 $this->dataDir = $this->
getOption(
'data-dir',
'.' );
57 $allkeysPresent = file_exists(
"{$this->dataDir}/allkeys.txt" );
58 $ucdallPresent = file_exists(
"{$this->dataDir}/ucd.all.grouped.xml" );
60 if ( !$allkeysPresent || !$ucdallPresent ) {
61 $icuVersion = INTL_ICU_VERSION;
62 $unicodeVersion = implode(
'.', array_slice( IntlChar::getUnicodeVersion(), 0, 3 ) );
66 if ( !$allkeysPresent ) {
67 $error .=
"Unable to find allkeys.txt. "
68 .
"Download it and specify its location with --data-dir=<DIR>. "
71 if ( !$ucdallPresent ) {
72 $error .=
"Unable to find ucd.all.grouped.xml. "
73 .
"Download it, unzip, and specify its location with --data-dir=<DIR>. "
77 $error .=
"You are using ICU $icuVersion, intended for Unicode $unicodeVersion. "
78 .
"Appropriate file(s) should be available at:\n";
80 $allkeysURL =
"https://www.unicode.org/Public/UCA/$unicodeVersion/allkeys.txt";
81 $ucdallURL =
"https://www.unicode.org/Public/$unicodeVersion/ucdxml/ucd.all.grouped.zip";
83 if ( !$allkeysPresent ) {
84 $error .=
"* $allkeysURL\n";
86 if ( !$ucdallPresent ) {
87 $error .=
"* $ucdallURL\n";
93 $debugOutFileName = $this->
getOption(
'debug-output' );
94 if ( $debugOutFileName ) {
95 $this->debugOutFile = fopen( $debugOutFileName,
'w' );
96 if ( !$this->debugOutFile ) {
97 $this->
fatalError(
"Unable to open debug output file for writing" );
101 $this->generateFirstChars();
104 private function loadUcd() {
105 $uxr =
new UcdXmlReader(
"{$this->dataDir}/ucd.all.grouped.xml" );
106 $uxr->readChars( $this->charCallback( ... ) );
109 private function charCallback( array $data ) {
113 $category = substr( $data[
'gc'], 0, 1 );
114 if ( !str_contains(
'LNPS', $category )
115 && $data[
'cp'] !==
'0020'
119 $cp = hexdec( $data[
'cp'] );
123 if ( IcuCollation::isCjk( $cp ) ) {
129 if ( $data[
'block'] ==
'Hangul Syllables' ) {
135 if ( $data[
'dm'] !==
'#' && !str_contains( $data[
'dm'],
' ' ) &&
136 !isset( $this->weights[ hexdec( $data[
'dm'] ) ] )
142 $a = 0xFBC0 + ( $cp >> 15 );
143 $b = ( $cp & 0x7fff ) | 0x8000;
145 $this->weights[$cp] = sprintf(
".%04X.%04X", $a, $b );
147 if ( $data[
'dm'] !==
'#' ) {
148 $this->mappedChars[$cp] =
true;
151 if ( $cp % 4096 == 0 ) {
152 print
"{$data['cp']}\n";
156 private function generateFirstChars() {
157 $file = fopen(
"{$this->dataDir}/allkeys.txt",
'r' );
159 $this->
fatalError(
"Unable to open allkeys.txt" );
162 $goodTertiaryChars = [];
168 while ( ( $line = fgets( $file ) ) !==
false ) {
170 $line = trim( $line );
171 if ( !preg_match(
'/^([0-9A-F]+)\s*;\s*([^#]*)/', $line, $m ) ) {
175 $cp = hexdec( $m[1] );
176 $allWeights = trim( $m[2] );
180 if ( !isset( $this->weights[$cp] ) ) {
184 foreach ( StringUtils::explode(
'[', $allWeights ) as $weightStr ) {
185 if ( preg_match_all(
'/[*.]([0-9A-F]+)/', $weightStr, $m ) ) {
186 if ( $m[1][0] !==
'0000' ) {
187 $primary .=
'.' . $m[1][0];
189 if ( $m[1][2] !==
'0000' ) {
190 $tertiary .=
'.' . $m[1][2];
194 $this->weights[$cp] = $primary;
195 if ( $tertiary ===
'.0008'
196 || $tertiary ===
'.000E'
198 $goodTertiaryChars[$cp] =
true;
205 asort( $this->weights, SORT_STRING );
206 $prevWeight = reset( $this->weights );
208 foreach ( $this->weights as $cp => $weight ) {
209 if ( $weight !== $prevWeight ) {
210 $this->groups[$prevWeight] = $group;
211 $prevWeight = $weight;
212 $group = $this->groups[$weight] ?? [];
217 $this->groups[$prevWeight] = $group;
226 foreach ( $this->groups as $weight => $group ) {
227 if ( preg_match(
'/(\.[0-9A-F]*)\./', $weight, $m ) ) {
228 if ( isset( $this->groups[$m[1]] ) ) {
229 unset( $this->groups[$weight] );
234 ksort( $this->groups, SORT_STRING );
239 $tertiaryCollator =
new Collator(
'root' );
240 $primaryCollator =
new Collator(
'root' );
241 $primaryCollator->setStrength( Collator::PRIMARY );
243 foreach ( $this->groups as $weight => $group ) {
244 $uncomposedChars = [];
246 foreach ( $group as $cp ) {
247 if ( isset( $goodTertiaryChars[$cp] ) ) {
250 if ( !isset( $this->mappedChars[$cp] ) ) {
251 $uncomposedChars[] = $cp;
254 $x = array_intersect( $goodChars, $uncomposedChars );
256 $x = $uncomposedChars;
263 $tertiaryCollator->sort( $x );
266 $char = UtfNormal\Utils::codepointToUtf8( $cp );
267 $headerChars[] = $char;
268 if ( $primaryCollator->compare( $char, $prevChar ) <= 0 ) {
273 if ( $this->debugOutFile ) {
274 fwrite( $this->debugOutFile, sprintf(
"%05X %s %s (%s)\n", $cp, $weight, $char,
275 implode(
' ', array_map( [ UtfNormal\Utils::class,
'codepointToUtf8' ], $group ) ) ) );
279 print
"Out of order: $numOutOfOrder / " . count( $headerChars ) .
"\n";
283 MW_INSTALL_PATH .
'/languages/data/first-letters-root.php',
286 "File created by maintenance/language/generateCollationData.php\n"
287 .
"@codeCoverageIgnore"
290 echo
"first-letters-root: file written.\n";
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.