65 $this->dataDir = $this->
getOption(
'data-dir',
'.' );
67 $allkeysPresent = file_exists(
"{$this->dataDir}/allkeys.txt" );
68 $ucdallPresent = file_exists(
"{$this->dataDir}/ucd.all.grouped.xml" );
72 $allkeysURL =
"https://www.unicode.org/Public/UCA/<Unicode version>/allkeys.txt";
73 $ucdallURL =
"https://www.unicode.org/Public/<Unicode version>/ucdxml/ucd.all.grouped.zip";
75 if ( !$allkeysPresent || !$ucdallPresent ) {
76 $icuVersion = INTL_ICU_VERSION;
81 if ( !$allkeysPresent ) {
82 $error .=
"Unable to find allkeys.txt. "
83 .
"Download it and specify its location with --data-dir=<DIR>. "
86 if ( !$ucdallPresent ) {
87 $error .=
"Unable to find ucd.all.grouped.xml. "
88 .
"Download it, unzip, and specify its location with --data-dir=<DIR>. "
92 $versionKnown =
false;
93 if ( version_compare( $icuVersion,
"4.0",
"<" ) ) {
95 $error .=
"You are using outdated version of ICU ($icuVersion), intended for "
96 . ( $unicodeVersion ?
"Unicode $unicodeVersion" :
"an unknown version of Unicode" )
97 .
"; this file might not be avalaible for it, and it's not supported by MediaWiki. "
98 .
" You are on your own; consider upgrading PHP's intl extension or try "
99 .
"one of the files available at:";
100 } elseif ( version_compare( $icuVersion,
"51.0",
">=" ) ) {
102 $error .=
"You are using ICU $icuVersion, released after this script was last updated. "
103 .
"Check what is the Unicode version it is using at http://site.icu-project.org/download . "
104 .
"It can't be guaranteed everything will work, but appropriate file(s) should "
105 .
"be available at:";
108 $versionKnown =
true;
109 $error .=
"You are using ICU $icuVersion, intended for "
110 . ( $unicodeVersion ?
"Unicode $unicodeVersion" :
"an unknown version of Unicode" )
111 .
". Appropriate file(s) should be available at:";
115 if ( $versionKnown && $unicodeVersion ) {
116 $allkeysURL = str_replace(
"<Unicode version>",
"$unicodeVersion.0", $allkeysURL );
117 $ucdallURL = str_replace(
"<Unicode version>",
"$unicodeVersion.0", $ucdallURL );
120 if ( !$allkeysPresent ) {
121 $error .=
"* $allkeysURL\n";
123 if ( !$ucdallPresent ) {
124 $error .=
"* $ucdallURL\n";
130 $debugOutFileName = $this->
getOption(
'debug-output' );
131 if ( $debugOutFileName ) {
132 $this->debugOutFile = fopen( $debugOutFileName,
'w' );
133 if ( !$this->debugOutFile ) {
134 $this->
fatalError(
"Unable to open debug output file for writing" );
197 $file = fopen(
"{$this->dataDir}/allkeys.txt",
'r' );
199 $this->
fatalError(
"Unable to open allkeys.txt" );
202 $goodTertiaryChars = [];
207 while ( (
$line = fgets(
$file ) ) !==
false ) {
210 if ( !preg_match(
'/^([0-9A-F]+)\s*;\s*([^#]*)/',
$line, $m ) ) {
214 $cp = hexdec( $m[1] );
215 $allWeights = trim( $m[2] );
219 if ( !isset( $this->weights[$cp] ) ) {
223 foreach ( StringUtils::explode(
'[', $allWeights ) as $weightStr ) {
224 preg_match_all(
'/[*.]([0-9A-F]+)/', $weightStr, $m );
225 if ( !empty( $m[1] ) ) {
226 if ( $m[1][0] !==
'0000' ) {
227 $primary .=
'.' . $m[1][0];
229 if ( $m[1][2] !==
'0000' ) {
230 $tertiary .=
'.' . $m[1][2];
234 $this->weights[$cp] = $primary;
235 if ( $tertiary ===
'.0008'
236 || $tertiary ===
'.000E'
238 $goodTertiaryChars[$cp] =
true;
245 asort( $this->weights, SORT_STRING );
246 $prevWeight = reset( $this->weights );
248 foreach ( $this->weights as $cp => $weight ) {
249 if ( $weight !== $prevWeight ) {
250 $this->groups[$prevWeight] = $group;
251 $prevWeight = $weight;
252 $group = $this->groups[$weight] ?? [];
257 $this->groups[$prevWeight] = $group;
266 foreach ( $this->groups as $weight => $group ) {
267 if ( preg_match(
'/(\.[0-9A-F]*)\./', $weight, $m ) ) {
268 if ( isset( $this->groups[$m[1]] ) ) {
269 unset( $this->groups[$weight] );
274 ksort( $this->groups, SORT_STRING );
279 $tertiaryCollator =
new Collator(
'root' );
280 $primaryCollator =
new Collator(
'root' );
281 $primaryCollator->setStrength( Collator::PRIMARY );
283 foreach ( $this->groups as $weight => $group ) {
284 $uncomposedChars = [];
286 foreach ( $group as $cp ) {
287 if ( isset( $goodTertiaryChars[$cp] ) ) {
290 if ( !isset( $this->mappedChars[$cp] ) ) {
291 $uncomposedChars[] = $cp;
294 $x = array_intersect( $goodChars, $uncomposedChars );
296 $x = $uncomposedChars;
303 $tertiaryCollator->sort( $x );
306 $char = UtfNormal\Utils::codepointToUtf8( $cp );
307 $headerChars[] = $char;
308 if ( $primaryCollator->compare( $char, $prevChar ) <= 0 ) {
313 if ( $this->debugOutFile ) {
314 fwrite( $this->debugOutFile, sprintf(
"%05X %s %s (%s)\n", $cp, $weight, $char,
315 implode(
' ', array_map(
'UtfNormal\Utils::codepointToUtf8', $group ) ) ) );
319 print "Out of order: $numOutOfOrder / " . count( $headerChars ) .
"\n";
324 "$IP/includes/collation/data/first-letters-root.php",
325 $writer->create( $headerChars,
'File created by generateCollationData.php' )
327 echo
"first-letters-root: file written.\n";