Merge "Adding ResourceLoader module "jquery.jStorage""
[lhc/web/wiklou.git] / maintenance / language / generateNormalizerData.php
1 <?php
2 /**
3 * Generates normalizer data files for Arabic and Malayalam.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup MaintenanceLanguage
22 */
23
24 require_once( __DIR__ . '/../Maintenance.php' );
25
26 require_once( __DIR__ . '/../../includes/normal/UtfNormalUtil.php' );
27
28 /**
29 * Generates normalizer data files for Arabic and Malayalam.
30 * For NFC see includes/normal.
31 */
32 class GenerateNormalizerData extends Maintenance {
33 var $dataFile;
34
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 );
39 }
40
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>" );
46 exit( 1 );
47 }
48 } else {
49 $this->dataFile = $this->getOption( 'unicode-data-file' );
50 if ( !file_exists( $this->dataFile ) ) {
51 $this->error( 'Unable to find the specified data file.' );
52 exit( 1 );
53 }
54 }
55
56 $this->generateArabic();
57 $this->generateMalayalam();
58 }
59
60 function generateArabic() {
61 $file = fopen( $this->dataFile, 'r' );
62 if ( !$file ) {
63 $this->error( 'Unable to open the data file.' );
64 exit( 1 );
65 }
66
67 // For the file format, see http://www.unicode.org/reports/tr44/
68 $fieldNames = array(
69 'Code',
70 'Name',
71 'General_Category',
72 'Canonical_Combining_Class',
73 'Bidi_Class',
74 'Decomposition_Type_Mapping',
75 'Numeric_Type_Value',
76 'Bidi_Mirrored',
77 'Unicode_1_Name',
78 'ISO_Comment',
79 'Simple_Uppercase_Mapping',
80 'Simple_Lowercase_Mapping',
81 'Simple_Titlecase_Mapping'
82 );
83
84 $pairs = array();
85
86 $lineNum = 0;
87 while ( false !== ( $line = fgets( $file ) ) ) {
88 ++$lineNum;
89
90 # Strip comments
91 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
92 if ( $line === '' ) {
93 continue;
94 }
95
96 # Split fields
97 $numberedData = explode( ';', $line );
98 $data = array();
99 foreach ( $fieldNames as $number => $name ) {
100 $data[$name] = $numberedData[$number];
101 }
102
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
106 {
107 if ( $data['Decomposition_Type_Mapping'] === '' ) {
108 // No decomposition
109 continue;
110 }
111 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
112 $data['Decomposition_Type_Mapping'], $m ) )
113 {
114 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
115 $this->error( $line );
116 continue;
117 }
118
119 $source = hexSequenceToUtf8( $data['Code'] );
120 $dest = hexSequenceToUtf8( $m[2] );
121 $pairs[$source] = $dest;
122 }
123 }
124
125 global $IP;
126 file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
127 echo "ar: " . count( $pairs ) . " pairs written.\n";
128 }
129
130 function generateMalayalam() {
131 $hexPairs = array(
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',
138
139 # From http://permalink.gmane.org/gmane.science.linguistics.wikipedia.technical/46413
140 '0D15 0D4D 200D' => '0D7F',
141 );
142
143 $pairs = array();
144 foreach ( $hexPairs as $hexSource => $hexDest ) {
145 $source = hexSequenceToUtf8( $hexSource );
146 $dest = hexSequenceToUtf8( $hexDest );
147 $pairs[$source] = $dest;
148 }
149
150 global $IP;
151 file_put_contents( "$IP/serialized/normalize-ml.ser", serialize( $pairs ) );
152 echo "ml: " . count( $pairs ) . " pairs written.\n";
153 }
154 }
155
156 $maintClass = 'GenerateNormalizerData';
157 require_once( RUN_MAINTENANCE_IF_MAIN );