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 );
115 if ( !str_contains(
'LNPS', $category )
116 && $data[
'cp'] !==
'0020'
120 $cp = (int)hexdec( $data[
'cp'] );
124 if ( IcuCollation::isCjk( $cp ) ) {
130 if ( $data[
'block'] ==
'Hangul Syllables' ) {
136 if ( $data[
'dm'] !==
'#' && !str_contains( $data[
'dm'],
' ' ) &&
137 !isset( $this->weights[ hexdec( $data[
'dm'] ) ] )
143 $a = 0xFBC0 + ( $cp >> 15 );
144 $b = ( $cp & 0x7fff ) | 0x8000;
146 $this->weights[$cp] = sprintf(
".%04X.%04X", $a, $b );
148 if ( $data[
'dm'] !==
'#' ) {
149 $this->mappedChars[$cp] =
true;
152 if ( $cp % 4096 == 0 ) {
153 print
"{$data['cp']}\n";
157 private function generateFirstChars() {
158 $file = fopen(
"{$this->dataDir}/allkeys.txt",
'r' );
160 $this->
fatalError(
"Unable to open allkeys.txt" );
163 $goodTertiaryChars = [];
169 while ( ( $line = fgets( $file ) ) !==
false ) {
171 $line = trim( $line );
172 if ( !preg_match(
'/^([0-9A-F]+)\s*;\s*([^#]*)/', $line, $m ) ) {
176 $cp = (int)hexdec( $m[1] );
177 $allWeights = trim( $m[2] );
181 if ( !isset( $this->weights[$cp] ) ) {
185 foreach ( StringUtils::explode(
'[', $allWeights ) as $weightStr ) {
186 if ( preg_match_all(
'/[*.]([0-9A-F]+)/', $weightStr, $m ) ) {
187 if ( $m[1][0] !==
'0000' ) {
188 $primary .=
'.' . $m[1][0];
190 if ( $m[1][2] !==
'0000' ) {
191 $tertiary .=
'.' . $m[1][2];
195 $this->weights[$cp] = $primary;
196 if ( $tertiary ===
'.0008'
197 || $tertiary ===
'.000E'
199 $goodTertiaryChars[$cp] =
true;
206 asort( $this->weights, SORT_STRING );
207 $prevWeight = reset( $this->weights );
209 foreach ( $this->weights as $cp => $weight ) {
210 if ( $weight !== $prevWeight ) {
211 $this->groups[$prevWeight] = $group;
212 $prevWeight = $weight;
213 $group = $this->groups[$weight] ?? [];
218 $this->groups[$prevWeight] = $group;
227 foreach ( $this->groups as $weight => $group ) {
228 if ( preg_match(
'/(\.[0-9A-F]*)\./', $weight, $m ) ) {
229 if ( isset( $this->groups[$m[1]] ) ) {
230 unset( $this->groups[$weight] );
235 ksort( $this->groups, SORT_STRING );
240 $tertiaryCollator =
new Collator(
'root' );
241 $primaryCollator =
new Collator(
'root' );
242 $primaryCollator->setStrength( Collator::PRIMARY );
244 foreach ( $this->groups as $weight => $group ) {
245 $uncomposedChars = [];
247 foreach ( $group as $cp ) {
248 if ( isset( $goodTertiaryChars[$cp] ) ) {
251 if ( !isset( $this->mappedChars[$cp] ) ) {
252 $uncomposedChars[] = $cp;
255 $x = array_intersect( $goodChars, $uncomposedChars );
257 $x = $uncomposedChars;
264 $tertiaryCollator->sort( $x );
267 $char = UtfNormal\Utils::codepointToUtf8( $cp );
268 $headerChars[] = $char;
269 if ( $primaryCollator->compare( $char, $prevChar ) <= 0 ) {
274 if ( $this->debugOutFile ) {
275 fwrite( $this->debugOutFile, sprintf(
"%05X %s %s (%s)\n", $cp, $weight, $char,
276 implode(
' ', array_map( [ UtfNormal\Utils::class,
'codepointToUtf8' ], $group ) ) ) );
280 print
"Out of order: $numOutOfOrder / " . count( $headerChars ) .
"\n";
284 MW_INSTALL_PATH .
'/languages/data/first-letters-root.php',
287 "File created by maintenance/language/generateCollationData.php\n"
288 .
"@codeCoverageIgnore"
291 echo
"first-letters-root: file written.\n";