3 * Generates normalizer data files for Arabic and Malayalam.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup MaintenanceLanguage
24 require_once( dirname( __FILE__
) . '/../Maintenance.php' );
26 require_once( dirname( __FILE__
) . '/../../includes/normal/UtfNormalUtil.php' );
29 * Generates normalizer data files for Arabic and Malayalam.
30 * For NFC see includes/normal.
32 class GenerateNormalizerData
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addOption( 'unicode-data-file', 'The local location of the data file ' .
38 'from http://unicode.org/Public/UNIDATA/UnicodeData.txt', false, true );
41 public function execute() {
42 if ( !$this->hasOption( 'unicode-data-file' ) ) {
43 $this->dataFile
= 'UnicodeData.txt';
44 if ( !file_exists( $this->dataFile
) ) {
45 $this->error( "Unable to find UnicodeData.txt. Please specify its location with --unicode-data-file=<FILE>" );
49 $this->dataFile
= $this->getOption( 'unicode-data-file' );
50 if ( !file_exists( $this->dataFile
) ) {
51 $this->error( 'Unable to find the specified data file.' );
56 $this->generateArabic();
57 $this->generateMalayalam();
60 function generateArabic() {
61 $file = fopen( $this->dataFile
, 'r' );
63 $this->error( 'Unable to open the data file.' );
67 // For the file format, see http://www.unicode.org/reports/tr44/
72 'Canonical_Combining_Class',
74 'Decomposition_Type_Mapping',
79 'Simple_Uppercase_Mapping',
80 'Simple_Lowercase_Mapping',
81 'Simple_Titlecase_Mapping'
87 while ( false !== ( $line = fgets( $file ) ) ) {
91 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
97 $numberedData = explode( ';', $line );
99 foreach ( $fieldNames as $number => $name ) {
100 $data[$name] = $numberedData[$number];
103 $code = base_convert( $data['Code'], 16, 10 );
104 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
105 ||
( $code >= 0xFE70 && $code <= 0xFEFF ) ) # Arabic presentation forms B
107 if ( $data['Decomposition_Type_Mapping'] === '' ) {
111 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
112 $data['Decomposition_Type_Mapping'], $m ) )
114 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
115 $this->error( $line );
119 $source = hexSequenceToUtf8( $data['Code'] );
120 $dest = hexSequenceToUtf8( $m[2] );
121 $pairs[$source] = $dest;
126 file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
127 echo "ar: " . count( $pairs ) . " pairs written.\n";
130 function generateMalayalam() {
132 # From http://unicode.org/versions/Unicode5.1.0/#Malayalam_Chillu_Characters
133 '0D23 0D4D 200D' => '0D7A',
134 '0D28 0D4D 200D' => '0D7B',
135 '0D30 0D4D 200D' => '0D7C',
136 '0D32 0D4D 200D' => '0D7D',
137 '0D33 0D4D 200D' => '0D7E',
139 # From http://permalink.gmane.org/gmane.science.linguistics.wikipedia.technical/46413
140 '0D15 0D4D 200D' => '0D7F',
144 foreach ( $hexPairs as $hexSource => $hexDest ) {
145 $source = hexSequenceToUtf8( $hexSource );
146 $dest = hexSequenceToUtf8( $hexDest );
147 $pairs[$source] = $dest;
151 file_put_contents( "$IP/serialized/normalize-ml.ser", serialize( $pairs ) );
152 echo "ml: " . count( $pairs ) . " pairs written.\n";
156 $maintClass = 'GenerateNormalizerData';
157 require_once( RUN_MAINTENANCE_IF_MAIN
);