2011-01-17 14:02:22 +00:00
|
|
|
|
<?php
|
2012-05-21 19:56:04 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
|
*
|
|
|
|
|
|
* @file
|
|
|
|
|
|
*/
|
2011-01-17 14:02:22 +00:00
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
2011-01-17 14:02:22 +00:00
|
|
|
|
class IcuCollation extends Collation {
|
2013-10-12 22:21:01 +00:00
|
|
|
|
const FIRST_LETTER_VERSION = 2;
|
2013-03-13 03:53:20 +00:00
|
|
|
|
|
2014-05-12 14:42:51 +00:00
|
|
|
|
/** @var Collator */
|
|
|
|
|
|
private $primaryCollator;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var Collator */
|
|
|
|
|
|
private $mainCollator;
|
|
|
|
|
|
|
2014-07-24 17:42:24 +00:00
|
|
|
|
/** @var string */
|
2014-05-12 14:42:51 +00:00
|
|
|
|
private $locale;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var Language */
|
|
|
|
|
|
protected $digitTransformLanguage;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
|
private $firstLetterData;
|
2011-01-17 14:02:22 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Unified CJK blocks.
|
|
|
|
|
|
*
|
2012-10-10 18:13:40 +00:00
|
|
|
|
* The same definition of a CJK block must be used for both Collation and
|
|
|
|
|
|
* generateCollationData.php. These blocks are omitted from the first
|
|
|
|
|
|
* letter data, as an optimisation measure and because the default UCA table
|
|
|
|
|
|
* is pretty useless for sorting Chinese text anyway. Japanese and Korean
|
2011-01-17 14:02:22 +00:00
|
|
|
|
* blocks are not included here, because they are smaller and more useful.
|
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
|
private static $cjkBlocks = [
|
|
|
|
|
|
[ 0x2E80, 0x2EFF ], // CJK Radicals Supplement
|
|
|
|
|
|
[ 0x2F00, 0x2FDF ], // Kangxi Radicals
|
|
|
|
|
|
[ 0x2FF0, 0x2FFF ], // Ideographic Description Characters
|
|
|
|
|
|
[ 0x3000, 0x303F ], // CJK Symbols and Punctuation
|
|
|
|
|
|
[ 0x31C0, 0x31EF ], // CJK Strokes
|
|
|
|
|
|
[ 0x3200, 0x32FF ], // Enclosed CJK Letters and Months
|
|
|
|
|
|
[ 0x3300, 0x33FF ], // CJK Compatibility
|
|
|
|
|
|
[ 0x3400, 0x4DBF ], // CJK Unified Ideographs Extension A
|
|
|
|
|
|
[ 0x4E00, 0x9FFF ], // CJK Unified Ideographs
|
|
|
|
|
|
[ 0xF900, 0xFAFF ], // CJK Compatibility Ideographs
|
|
|
|
|
|
[ 0xFE30, 0xFE4F ], // CJK Compatibility Forms
|
|
|
|
|
|
[ 0x20000, 0x2A6DF ], // CJK Unified Ideographs Extension B
|
|
|
|
|
|
[ 0x2A700, 0x2B73F ], // CJK Unified Ideographs Extension C
|
|
|
|
|
|
[ 0x2B740, 0x2B81F ], // CJK Unified Ideographs Extension D
|
|
|
|
|
|
[ 0x2F800, 0x2FA1F ], // CJK Compatibility Ideographs Supplement
|
|
|
|
|
|
];
|
2011-01-17 14:02:22 +00:00
|
|
|
|
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
/**
|
2013-02-28 15:44:26 +00:00
|
|
|
|
* Additional characters (or character groups) to be considered separate
|
2013-03-11 21:24:09 +00:00
|
|
|
|
* letters for given languages, or to be removed from the list of such
|
|
|
|
|
|
* letters (denoted by keys starting with '-').
|
|
|
|
|
|
*
|
|
|
|
|
|
* These are additions to (or subtractions from) the data stored in the
|
2013-02-28 15:44:26 +00:00
|
|
|
|
* first-letters-root.ser file (which among others includes full basic latin,
|
|
|
|
|
|
* cyrillic and greek alphabets).
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
*
|
2013-02-28 15:44:26 +00:00
|
|
|
|
* "Separate letter" is a letter that would have a separate heading/section
|
|
|
|
|
|
* for it in a dictionary or a phone book in this language. This data isn't
|
|
|
|
|
|
* used for sorting (the ICU library handles that), only for deciding which
|
|
|
|
|
|
* characters (or character groups) to use as headings.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Initially generated based on the primary level of Unicode collation
|
|
|
|
|
|
* tailorings available at http://developer.mimer.com/charts/tailorings.htm ,
|
|
|
|
|
|
* later modified.
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
*
|
|
|
|
|
|
* Empty arrays are intended; this signifies that the data for the language is
|
|
|
|
|
|
* available and that there are, in fact, no additional letters to consider.
|
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
|
private static $tailoringFirstLetters = [
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
// Verified by native speakers
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'be' => [ "Ё" ],
|
|
|
|
|
|
'be-tarask' => [ "Ё" ],
|
|
|
|
|
|
'cy' => [ "Ch", "Dd", "Ff", "Ng", "Ll", "Ph", "Rh", "Th" ],
|
|
|
|
|
|
'en' => [],
|
2016-07-20 13:05:12 +00:00
|
|
|
|
// RTL, let's put each letter on a new line
|
|
|
|
|
|
'fa' => [
|
|
|
|
|
|
"آ",
|
|
|
|
|
|
"ء",
|
|
|
|
|
|
"ه",
|
|
|
|
|
|
"ا",
|
|
|
|
|
|
"و"
|
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'fi' => [ "Å", "Ä", "Ö" ],
|
|
|
|
|
|
'fr' => [],
|
|
|
|
|
|
'hu' => [ "Cs", "Dz", "Dzs", "Gy", "Ly", "Ny", "Ö", "Sz", "Ty", "Ü", "Zs" ],
|
|
|
|
|
|
'is' => [ "Á", "Ð", "É", "Í", "Ó", "Ú", "Ý", "Þ", "Æ", "Ö", "Å" ],
|
|
|
|
|
|
'it' => [],
|
|
|
|
|
|
'lv' => [ "Č", "Ģ", "Ķ", "Ļ", "Ņ", "Š", "Ž" ],
|
|
|
|
|
|
'pl' => [ "Ą", "Ć", "Ę", "Ł", "Ń", "Ó", "Ś", "Ź", "Ż" ],
|
|
|
|
|
|
'pt' => [],
|
|
|
|
|
|
'ru' => [],
|
|
|
|
|
|
'sv' => [ "Å", "Ä", "Ö" ],
|
|
|
|
|
|
'sv@collation=standard' => [ "Å", "Ä", "Ö" ],
|
|
|
|
|
|
'uk' => [ "Ґ", "Ь" ],
|
|
|
|
|
|
'vi' => [ "Ă", "Â", "Đ", "Ê", "Ô", "Ơ", "Ư" ],
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
// Not verified, but likely correct
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'af' => [],
|
|
|
|
|
|
'ast' => [ "Ch", "Ll", "Ñ" ],
|
|
|
|
|
|
'az' => [ "Ç", "Ə", "Ğ", "İ", "Ö", "Ş", "Ü" ],
|
|
|
|
|
|
'bg' => [],
|
|
|
|
|
|
'br' => [ "Ch", "C'h" ],
|
|
|
|
|
|
'bs' => [ "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ],
|
|
|
|
|
|
'ca' => [],
|
|
|
|
|
|
'co' => [],
|
|
|
|
|
|
'cs' => [ "Č", "Ch", "Ř", "Š", "Ž" ],
|
|
|
|
|
|
'da' => [ "Æ", "Ø", "Å" ],
|
|
|
|
|
|
'de' => [],
|
|
|
|
|
|
'dsb' => [ "Č", "Ć", "Dź", "Ě", "Ch", "Ł", "Ń", "Ŕ", "Š", "Ś", "Ž", "Ź" ],
|
|
|
|
|
|
'el' => [],
|
|
|
|
|
|
'eo' => [ "Ĉ", "Ĝ", "Ĥ", "Ĵ", "Ŝ", "Ŭ" ],
|
|
|
|
|
|
'es' => [ "Ñ" ],
|
|
|
|
|
|
'et' => [ "Š", "Ž", "Õ", "Ä", "Ö", "Ü", "W" ], // added W for CollationEt (xx-uca-et)
|
|
|
|
|
|
'eu' => [ "Ñ" ],
|
|
|
|
|
|
'fo' => [ "Á", "Ð", "Í", "Ó", "Ú", "Ý", "Æ", "Ø", "Å" ],
|
|
|
|
|
|
'fur' => [ "À", "Á", "Â", "È", "Ì", "Ò", "Ù" ],
|
|
|
|
|
|
'fy' => [],
|
|
|
|
|
|
'ga' => [],
|
|
|
|
|
|
'gd' => [],
|
|
|
|
|
|
'gl' => [ "Ch", "Ll", "Ñ" ],
|
|
|
|
|
|
'hr' => [ "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ],
|
|
|
|
|
|
'hsb' => [ "Č", "Dź", "Ě", "Ch", "Ł", "Ń", "Ř", "Š", "Ć", "Ž" ],
|
|
|
|
|
|
'kk' => [ "Ү", "І" ],
|
|
|
|
|
|
'kl' => [ "Æ", "Ø", "Å" ],
|
|
|
|
|
|
'ku' => [ "Ç", "Ê", "Î", "Ş", "Û" ],
|
|
|
|
|
|
'ky' => [ "Ё" ],
|
|
|
|
|
|
'la' => [],
|
|
|
|
|
|
'lb' => [],
|
|
|
|
|
|
'lt' => [ "Č", "Š", "Ž" ],
|
|
|
|
|
|
'mk' => [],
|
|
|
|
|
|
'mo' => [ "Ă", "Â", "Î", "Ş", "Ţ" ],
|
|
|
|
|
|
'mt' => [ "Ċ", "Ġ", "Għ", "Ħ", "Ż" ],
|
|
|
|
|
|
'nl' => [],
|
|
|
|
|
|
'no' => [ "Æ", "Ø", "Å" ],
|
|
|
|
|
|
'oc' => [],
|
|
|
|
|
|
'rm' => [],
|
|
|
|
|
|
'ro' => [ "Ă", "Â", "Î", "Ş", "Ţ" ],
|
|
|
|
|
|
'rup' => [ "Ă", "Â", "Î", "Ľ", "Ń", "Ş", "Ţ" ],
|
|
|
|
|
|
'sco' => [],
|
|
|
|
|
|
'sk' => [ "Ä", "Č", "Ch", "Ô", "Š", "Ž" ],
|
|
|
|
|
|
'sl' => [ "Č", "Š", "Ž" ],
|
|
|
|
|
|
'smn' => [ "Á", "Č", "Đ", "Ŋ", "Š", "Ŧ", "Ž", "Æ", "Ø", "Å", "Ä", "Ö" ],
|
|
|
|
|
|
'sq' => [ "Ç", "Dh", "Ë", "Gj", "Ll", "Nj", "Rr", "Sh", "Th", "Xh", "Zh" ],
|
|
|
|
|
|
'sr' => [],
|
2016-05-25 14:36:12 +00:00
|
|
|
|
'ta' => [
|
|
|
|
|
|
"\xE0\xAE\x82", "ஃ", "க்ஷ", "க்", "ங்", "ச்", "ஞ்", "ட்", "ண்", "த்", "ந்",
|
|
|
|
|
|
"ப்", "ம்", "ய்", "ர்", "ல்", "வ்", "ழ்", "ள்", "ற்", "ன்", "ஜ்", "ஶ்", "ஷ்",
|
|
|
|
|
|
"ஸ்", "ஹ்", "க்ஷ்"
|
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'tk' => [ "Ç", "Ä", "Ž", "Ň", "Ö", "Ş", "Ü", "Ý" ],
|
|
|
|
|
|
'tl' => [ "Ñ", "Ng" ],
|
|
|
|
|
|
'tr' => [ "Ç", "Ğ", "İ", "Ö", "Ş", "Ü" ],
|
|
|
|
|
|
'tt' => [ "Ә", "Ө", "Ү", "Җ", "Ң", "Һ" ],
|
|
|
|
|
|
'uz' => [ "Ch", "G'", "Ng", "O'", "Sh" ],
|
|
|
|
|
|
];
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
2011-01-17 14:02:22 +00:00
|
|
|
|
const RECORD_LENGTH = 14;
|
|
|
|
|
|
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function __construct( $locale ) {
|
2011-01-17 14:02:22 +00:00
|
|
|
|
if ( !extension_loaded( 'intl' ) ) {
|
2012-10-10 18:13:40 +00:00
|
|
|
|
throw new MWException( 'An ICU collation was requested, ' .
|
2011-01-17 14:02:22 +00:00
|
|
|
|
'but the intl extension is not available.' );
|
|
|
|
|
|
}
|
2013-10-12 22:21:01 +00:00
|
|
|
|
|
2011-01-17 14:02:22 +00:00
|
|
|
|
$this->locale = $locale;
|
2013-10-12 22:21:01 +00:00
|
|
|
|
// Drop everything after the '@' in locale's name
|
|
|
|
|
|
$localeParts = explode( '@', $locale );
|
|
|
|
|
|
$this->digitTransformLanguage = Language::factory( $locale === 'root' ? 'en' : $localeParts[0] );
|
|
|
|
|
|
|
2011-01-17 14:02:22 +00:00
|
|
|
|
$this->mainCollator = Collator::create( $locale );
|
|
|
|
|
|
if ( !$this->mainCollator ) {
|
|
|
|
|
|
throw new MWException( "Invalid ICU locale specified for collation: $locale" );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->primaryCollator = Collator::create( $locale );
|
|
|
|
|
|
$this->primaryCollator->setStrength( Collator::PRIMARY );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getSortKey( $string ) {
|
2016-05-23 22:43:16 +00:00
|
|
|
|
return $this->mainCollator->getSortKey( $string );
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getPrimarySortKey( $string ) {
|
2016-05-23 22:43:16 +00:00
|
|
|
|
return $this->primaryCollator->getSortKey( $string );
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getFirstLetter( $string ) {
|
2011-01-17 14:02:22 +00:00
|
|
|
|
$string = strval( $string );
|
|
|
|
|
|
if ( $string === '' ) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check for CJK
|
|
|
|
|
|
$firstChar = mb_substr( $string, 0, 1, 'UTF-8' );
|
2015-03-07 09:27:42 +00:00
|
|
|
|
if ( ord( $firstChar ) > 0x7f && self::isCjk( UtfNormal\Utils::utf8ToCodepoint( $firstChar ) ) ) {
|
2011-01-17 14:02:22 +00:00
|
|
|
|
return $firstChar;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sortKey = $this->getPrimarySortKey( $string );
|
|
|
|
|
|
|
|
|
|
|
|
// Do a binary search to find the correct letter to sort under
|
2012-10-18 09:33:15 +00:00
|
|
|
|
$min = ArrayUtils::findLowerBound(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ $this, 'getSortKeyByLetterIndex' ],
|
2011-01-17 14:02:22 +00:00
|
|
|
|
$this->getFirstLetterCount(),
|
|
|
|
|
|
'strcmp',
|
|
|
|
|
|
$sortKey );
|
|
|
|
|
|
|
|
|
|
|
|
if ( $min === false ) {
|
|
|
|
|
|
// Before the first letter
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
return $this->getLetterByIndex( $min );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
2016-04-19 21:27:22 +00:00
|
|
|
|
* @return array
|
2016-04-03 08:36:49 +00:00
|
|
|
|
*/
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getFirstLetterData() {
|
2016-04-19 21:27:22 +00:00
|
|
|
|
if ( $this->firstLetterData === null ) {
|
|
|
|
|
|
$cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
|
|
|
|
|
|
$cacheKey = $cache->makeKey(
|
|
|
|
|
|
'first-letters',
|
|
|
|
|
|
$this->locale,
|
|
|
|
|
|
$this->digitTransformLanguage->getCode(),
|
|
|
|
|
|
self::getICUVersion(),
|
|
|
|
|
|
self::FIRST_LETTER_VERSION
|
|
|
|
|
|
);
|
|
|
|
|
|
$this->firstLetterData = $cache->getWithSetCallback( $cacheKey, $cache::TTL_WEEK, function () {
|
|
|
|
|
|
return $this->fetchFirstLetterData();
|
|
|
|
|
|
} );
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
2016-04-19 21:27:22 +00:00
|
|
|
|
return $this->firstLetterData;
|
|
|
|
|
|
}
|
2011-01-17 14:02:22 +00:00
|
|
|
|
|
2016-04-19 21:27:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws MWException
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function fetchFirstLetterData() {
|
2011-01-17 14:02:22 +00:00
|
|
|
|
// Generate data from serialized data file
|
2013-04-26 12:18:06 +00:00
|
|
|
|
if ( isset( self::$tailoringFirstLetters[$this->locale] ) ) {
|
2016-04-19 21:27:22 +00:00
|
|
|
|
$letters = wfGetPrecompiledData( 'first-letters-root.ser' );
|
2013-03-11 21:24:09 +00:00
|
|
|
|
// Append additional characters
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
$letters = array_merge( $letters, self::$tailoringFirstLetters[$this->locale] );
|
2013-03-11 21:24:09 +00:00
|
|
|
|
// Remove unnecessary ones, if any
|
2013-03-24 10:01:51 +00:00
|
|
|
|
if ( isset( self::$tailoringFirstLetters['-' . $this->locale] ) ) {
|
|
|
|
|
|
$letters = array_diff( $letters, self::$tailoringFirstLetters['-' . $this->locale] );
|
2013-03-11 21:24:09 +00:00
|
|
|
|
}
|
2013-10-12 22:21:01 +00:00
|
|
|
|
// Apply digit transforms
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
|
2013-10-12 22:21:01 +00:00
|
|
|
|
$letters = array_diff( $letters, $digits );
|
|
|
|
|
|
foreach ( $digits as $digit ) {
|
|
|
|
|
|
$letters[] = $this->digitTransformLanguage->formatNum( $digit, true );
|
|
|
|
|
|
}
|
(bug 43799) create language-specific collations for category sorting
This allows one to *finally* get articles to be correctly sorted on
category pages for 67 languages based in latin, greek and cyrillic
alphabets.
Fixes bug 29788, bug 41040, and bug 42412 (implementing collations for
Swedish, Polish, Ukrainian).
Full list of language codes this adds support for: af, ast, az, be,
bg, br, bs, ca, co, cs, cy, da, de, dsb, el, en, eo, es, et, eu, fi,
fo, fr, fur, fy, ga, gd, gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la,
lb, lt, lv, mk, mo, mt, nl, no, oc, pl, pt, rm, ro, ru, rup, sco, sk,
sl, smn, sq, sr, sv, tk, tl, tr, tt, uk, uz, vi.
* Include data about first-letter characters for 67 language
tailorings. This data was generated from based on
http://developer.mimer.com/charts/tailorings.htm by a Ruby script
(https://www.mediawiki.org/wiki/User:Matma_Rex/generateCollationTailoringData.rb),
then adjusted by hand (removed duplicate definitions for Spanish and
German, changed code fil -> tl (Filipino -> Tagalog).
* Mark languages verified by native speakers (currently only pl
(Polish) I verified by myself and fi (Finnish) checked by Niklas).
* Allow for collations named like 'uca-<langcode>', mapping them to
IcuCollation with appropriate parameter. The code doesn't check if
we actually have data for given language, as it's checked after the
IcuCollation class instance is constructed.
* Add the tailoring data to the default first-letter file (for root
collation) before it's cached for given locale.
Change-Id: I838484b9aaf23945fe7880fef2e3da5f5c06877f
2013-02-18 21:09:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
$letters = wfGetPrecompiledData( "first-letters-{$this->locale}.ser" );
|
|
|
|
|
|
if ( $letters === false ) {
|
|
|
|
|
|
throw new MWException( "MediaWiki does not support ICU locale " .
|
|
|
|
|
|
"\"{$this->locale}\"" );
|
|
|
|
|
|
}
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-14 07:40:50 +00:00
|
|
|
|
/* Sort the letters.
|
|
|
|
|
|
*
|
|
|
|
|
|
* It's impossible to have the precompiled data file properly sorted,
|
|
|
|
|
|
* because the sort order changes depending on ICU version. If the
|
|
|
|
|
|
* array is not properly sorted, the binary search will return random
|
|
|
|
|
|
* results.
|
|
|
|
|
|
*
|
|
|
|
|
|
* We also take this opportunity to remove primary collisions.
|
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$letterMap = [];
|
2011-01-17 14:02:22 +00:00
|
|
|
|
foreach ( $letters as $letter ) {
|
|
|
|
|
|
$key = $this->getPrimarySortKey( $letter );
|
|
|
|
|
|
if ( isset( $letterMap[$key] ) ) {
|
|
|
|
|
|
// Primary collision
|
|
|
|
|
|
// Keep whichever one sorts first in the main collator
|
|
|
|
|
|
if ( $this->mainCollator->compare( $letter, $letterMap[$key] ) < 0 ) {
|
|
|
|
|
|
$letterMap[$key] = $letter;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$letterMap[$key] = $letter;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ksort( $letterMap, SORT_STRING );
|
2015-10-14 07:40:50 +00:00
|
|
|
|
|
|
|
|
|
|
/* Remove duplicate prefixes. Basically if something has a sortkey
|
|
|
|
|
|
* which is a prefix of some other sortkey, then it is an
|
|
|
|
|
|
* expansion and probably should not be considered a section
|
|
|
|
|
|
* header.
|
|
|
|
|
|
*
|
|
|
|
|
|
* For example 'þ' is sometimes sorted as if it is the letters
|
|
|
|
|
|
* 'th'. Other times it is its own primary element. Another
|
|
|
|
|
|
* example is '₨'. Sometimes its a currency symbol. Sometimes it
|
|
|
|
|
|
* is an 'R' followed by an 's'.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Additionally an expanded element should always sort directly
|
|
|
|
|
|
* after its first element due to they way sortkeys work.
|
|
|
|
|
|
*
|
|
|
|
|
|
* UCA sortkey elements are of variable length but no collation
|
|
|
|
|
|
* element should be a prefix of some other element, so I think
|
|
|
|
|
|
* this is safe. See:
|
|
|
|
|
|
* - https://ssl.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm
|
|
|
|
|
|
* - http://site.icu-project.org/design/collation/uca-weight-allocation
|
|
|
|
|
|
*
|
|
|
|
|
|
* Additionally, there is something called primary compression to
|
|
|
|
|
|
* worry about. Basically, if you have two primary elements that
|
|
|
|
|
|
* are more than one byte and both start with the same byte then
|
|
|
|
|
|
* the first byte is dropped on the second primary. Additionally
|
|
|
|
|
|
* either \x03 or \xFF may be added to mean that the next primary
|
|
|
|
|
|
* does not start with the first byte of the first primary.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This shouldn't matter much, as the first primary is not
|
|
|
|
|
|
* changed, and that is what we are comparing against.
|
|
|
|
|
|
*
|
|
|
|
|
|
* tl;dr: This makes some assumptions about how icu implements
|
|
|
|
|
|
* collations. It seems incredibly unlikely these assumptions
|
|
|
|
|
|
* will change, but nonetheless they are assumptions.
|
|
|
|
|
|
*/
|
2013-03-24 03:09:43 +00:00
|
|
|
|
|
|
|
|
|
|
$prev = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$duplicatePrefixes = [];
|
2013-04-20 22:49:30 +00:00
|
|
|
|
foreach ( $letterMap as $key => $value ) {
|
2013-03-24 03:09:43 +00:00
|
|
|
|
// Remove terminator byte. Otherwise the prefix
|
|
|
|
|
|
// comparison will get hung up on that.
|
|
|
|
|
|
$trimmedKey = rtrim( $key, "\0" );
|
|
|
|
|
|
if ( $prev === false || $prev === '' ) {
|
|
|
|
|
|
$prev = $trimmedKey;
|
|
|
|
|
|
// We don't yet have a collation element
|
|
|
|
|
|
// to compare against, so continue.
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Due to the fact the array is sorted, we only have
|
|
|
|
|
|
// to compare with the element directly previous
|
|
|
|
|
|
// to the current element (skipping expansions).
|
|
|
|
|
|
// An element "X" will always sort directly
|
|
|
|
|
|
// before "XZ" (Unless we have "XY", but we
|
|
|
|
|
|
// do not update $prev in that case).
|
|
|
|
|
|
if ( substr( $trimmedKey, 0, strlen( $prev ) ) === $prev ) {
|
|
|
|
|
|
$duplicatePrefixes[] = $key;
|
|
|
|
|
|
// If this is an expansion, we don't want to
|
|
|
|
|
|
// compare the next element to this element,
|
|
|
|
|
|
// but to what is currently $prev
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
$prev = $trimmedKey;
|
|
|
|
|
|
}
|
2013-04-20 22:49:30 +00:00
|
|
|
|
foreach ( $duplicatePrefixes as $badKey ) {
|
2014-03-29 10:52:07 +00:00
|
|
|
|
wfDebug( "Removing '{$letterMap[$badKey]}' from first letters.\n" );
|
2013-03-24 03:09:43 +00:00
|
|
|
|
unset( $letterMap[$badKey] );
|
|
|
|
|
|
// This code assumes that unsetting does not change sort order.
|
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$data = [
|
2011-01-17 14:02:22 +00:00
|
|
|
|
'chars' => array_values( $letterMap ),
|
2013-03-13 03:53:20 +00:00
|
|
|
|
'keys' => array_keys( $letterMap ),
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2011-01-17 14:02:22 +00:00
|
|
|
|
|
|
|
|
|
|
// Reduce memory usage before caching
|
|
|
|
|
|
unset( $letterMap );
|
|
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getLetterByIndex( $index ) {
|
2016-04-19 21:27:22 +00:00
|
|
|
|
return $this->getFirstLetterData()['chars'][$index];
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getSortKeyByLetterIndex( $index ) {
|
2016-04-19 21:27:22 +00:00
|
|
|
|
return $this->getFirstLetterData()['keys'][$index];
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
2016-04-03 08:23:20 +00:00
|
|
|
|
public function getFirstLetterCount() {
|
2016-04-19 21:27:22 +00:00
|
|
|
|
return count( $this->getFirstLetterData()['chars'] );
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-03 08:36:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @since 1.16.3
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static function isCjk( $codepoint ) {
|
2011-01-17 14:02:22 +00:00
|
|
|
|
foreach ( self::$cjkBlocks as $block ) {
|
|
|
|
|
|
if ( $codepoint >= $block[0] && $codepoint <= $block[1] ) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2013-01-18 21:36:32 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Return the version of ICU library used by PHP's intl extension,
|
|
|
|
|
|
* or false when the extension is not installed of the version
|
|
|
|
|
|
* can't be determined.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The constant INTL_ICU_VERSION this function refers to isn't really
|
|
|
|
|
|
* documented. It is available since PHP 5.3.7 (see PHP bug 54561).
|
|
|
|
|
|
* This function will return false on older PHPs.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @since 1.21
|
2014-04-23 11:39:49 +00:00
|
|
|
|
* @return string|bool
|
2013-01-18 21:36:32 +00:00
|
|
|
|
*/
|
|
|
|
|
|
static function getICUVersion() {
|
|
|
|
|
|
return defined( 'INTL_ICU_VERSION' ) ? INTL_ICU_VERSION : false;
|
|
|
|
|
|
}
|
2013-01-18 21:36:32 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Return the version of Unicode appropriate for the version of ICU library
|
|
|
|
|
|
* currently in use, or false when it can't be determined.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @since 1.21
|
2014-04-23 11:39:49 +00:00
|
|
|
|
* @return string|bool
|
2013-01-18 21:36:32 +00:00
|
|
|
|
*/
|
|
|
|
|
|
static function getUnicodeVersionForICU() {
|
|
|
|
|
|
$icuVersion = IcuCollation::getICUVersion();
|
|
|
|
|
|
if ( !$icuVersion ) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$versionPrefix = substr( $icuVersion, 0, 3 );
|
|
|
|
|
|
// Source: http://site.icu-project.org/download
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$map = [
|
2016-07-20 19:47:22 +00:00
|
|
|
|
'57.' => '8.0',
|
|
|
|
|
|
'56.' => '8.0',
|
|
|
|
|
|
'55.' => '7.0',
|
|
|
|
|
|
'54.' => '7.0',
|
|
|
|
|
|
'53.' => '6.3',
|
|
|
|
|
|
'52.' => '6.3',
|
|
|
|
|
|
'51.' => '6.2',
|
2013-01-18 21:36:32 +00:00
|
|
|
|
'50.' => '6.2',
|
|
|
|
|
|
'49.' => '6.1',
|
|
|
|
|
|
'4.8' => '6.0',
|
|
|
|
|
|
'4.6' => '6.0',
|
|
|
|
|
|
'4.4' => '5.2',
|
|
|
|
|
|
'4.2' => '5.1',
|
|
|
|
|
|
'4.0' => '5.1',
|
|
|
|
|
|
'3.8' => '5.0',
|
|
|
|
|
|
'3.6' => '5.0',
|
|
|
|
|
|
'3.4' => '4.1',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2013-01-18 21:36:32 +00:00
|
|
|
|
|
|
|
|
|
|
if ( isset( $map[$versionPrefix] ) ) {
|
|
|
|
|
|
return $map[$versionPrefix];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-01-17 14:02:22 +00:00
|
|
|
|
}
|