phpcs: More require/include is not a function
[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__ . '/../../includes/normal/UtfNormalUtil.php';
25
26 require_once __DIR__ . '/../Maintenance.php';
27
28 /**
29 * Generates normalizer data files for Arabic and Malayalam.
30 * For NFC see includes/normal.
31 *
32 * @ingroup MaintenanceLanguage
33 */
34 class GenerateNormalizerData extends Maintenance {
35 public $dataFile;
36
37 public function __construct() {
38 parent::__construct();
39 $this->addOption( 'unicode-data-file', 'The local location of the data file ' .
40 'from http://unicode.org/Public/UNIDATA/UnicodeData.txt', false, true );
41 }
42
43 public function execute() {
44 if ( !$this->hasOption( 'unicode-data-file' ) ) {
45 $this->dataFile = 'UnicodeData.txt';
46 if ( !file_exists( $this->dataFile ) ) {
47 $this->error( "Unable to find UnicodeData.txt. Please specify its location with --unicode-data-file=<FILE>" );
48 exit( 1 );
49 }
50 } else {
51 $this->dataFile = $this->getOption( 'unicode-data-file' );
52 if ( !file_exists( $this->dataFile ) ) {
53 $this->error( 'Unable to find the specified data file.' );
54 exit( 1 );
55 }
56 }
57
58 $this->generateArabic();
59 $this->generateMalayalam();
60 }
61
62 function generateArabic() {
63 $file = fopen( $this->dataFile, 'r' );
64 if ( !$file ) {
65 $this->error( 'Unable to open the data file.' );
66 exit( 1 );
67 }
68
69 // For the file format, see http://www.unicode.org/reports/tr44/
70 $fieldNames = array(
71 'Code',
72 'Name',
73 'General_Category',
74 'Canonical_Combining_Class',
75 'Bidi_Class',
76 'Decomposition_Type_Mapping',
77 'Numeric_Type_Value',
78 'Bidi_Mirrored',
79 'Unicode_1_Name',
80 'ISO_Comment',
81 'Simple_Uppercase_Mapping',
82 'Simple_Lowercase_Mapping',
83 'Simple_Titlecase_Mapping'
84 );
85
86 $pairs = array();
87
88 $lineNum = 0;
89 while ( false !== ( $line = fgets( $file ) ) ) {
90 ++$lineNum;
91
92 # Strip comments
93 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
94 if ( $line === '' ) {
95 continue;
96 }
97
98 # Split fields
99 $numberedData = explode( ';', $line );
100 $data = array();
101 foreach ( $fieldNames as $number => $name ) {
102 $data[$name] = $numberedData[$number];
103 }
104
105 $code = base_convert( $data['Code'], 16, 10 );
106 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
107 || ( $code >= 0xFE70 && $code <= 0xFEFF ) ) # Arabic presentation forms B
108 {
109 if ( $data['Decomposition_Type_Mapping'] === '' ) {
110 // No decomposition
111 continue;
112 }
113 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
114 $data['Decomposition_Type_Mapping'], $m ) )
115 {
116 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
117 $this->error( $line );
118 continue;
119 }
120
121 $source = hexSequenceToUtf8( $data['Code'] );
122 $dest = hexSequenceToUtf8( $m[2] );
123 $pairs[$source] = $dest;
124 }
125 }
126
127 global $IP;
128 file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
129 echo "ar: " . count( $pairs ) . " pairs written.\n";
130 }
131
132 function generateMalayalam() {
133 $hexPairs = array(
134 # From http://unicode.org/versions/Unicode5.1.0/#Malayalam_Chillu_Characters
135 '0D23 0D4D 200D' => '0D7A',
136 '0D28 0D4D 200D' => '0D7B',
137 '0D30 0D4D 200D' => '0D7C',
138 '0D32 0D4D 200D' => '0D7D',
139 '0D33 0D4D 200D' => '0D7E',
140
141 # From http://permalink.gmane.org/gmane.science.linguistics.wikipedia.technical/46413
142 '0D15 0D4D 200D' => '0D7F',
143 );
144
145 $pairs = array();
146 foreach ( $hexPairs as $hexSource => $hexDest ) {
147 $source = hexSequenceToUtf8( $hexSource );
148 $dest = hexSequenceToUtf8( $hexDest );
149 $pairs[$source] = $dest;
150 }
151
152 global $IP;
153 file_put_contents( "$IP/serialized/normalize-ml.ser", serialize( $pairs ) );
154 echo "ml: " . count( $pairs ) . " pairs written.\n";
155 }
156 }
157
158 $maintClass = 'GenerateNormalizerData';
159 require_once RUN_MAINTENANCE_IF_MAIN;