Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / maintenance / language / generateNormalizerDataAr.php
1 <?php
2 /**
3 * Generates the normalizer data file for Arabic.
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 /**
27 * Generates the normalizer data file for Arabic.
28 *
29 * This data file is used after normalizing to NFC.
30 *
31 * @ingroup MaintenanceLanguage
32 */
33 class GenerateNormalizerDataAr extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Generate the normalizer data file for Arabic' );
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 getDbType() {
42 return Maintenance::DB_NONE;
43 }
44
45 public function execute() {
46 if ( !$this->hasOption( 'unicode-data-file' ) ) {
47 $dataFile = 'UnicodeData.txt';
48 if ( !file_exists( $dataFile ) ) {
49 $this->fatalError( "Unable to find UnicodeData.txt. Please specify " .
50 "its location with --unicode-data-file=<FILE>" );
51 }
52 } else {
53 $dataFile = $this->getOption( 'unicode-data-file' );
54 if ( !file_exists( $dataFile ) ) {
55 $this->fatalError( 'Unable to find the specified data file.' );
56 }
57 }
58
59 $file = fopen( $dataFile, 'r' );
60 if ( !$file ) {
61 $this->fatalError( 'Unable to open the data file.' );
62 }
63
64 // For the file format, see http://www.unicode.org/reports/tr44/
65 $fieldNames = [
66 'Code',
67 'Name',
68 'General_Category',
69 'Canonical_Combining_Class',
70 'Bidi_Class',
71 'Decomposition_Type_Mapping',
72 'Numeric_Type_Value_6',
73 'Numeric_Type_Value_7',
74 'Numeric_Type_Value_8',
75 'Bidi_Mirrored',
76 'Unicode_1_Name',
77 'ISO_Comment',
78 'Simple_Uppercase_Mapping',
79 'Simple_Lowercase_Mapping',
80 'Simple_Titlecase_Mapping'
81 ];
82
83 $pairs = [];
84
85 $lineNum = 0;
86 while ( false !== ( $line = fgets( $file ) ) ) {
87 ++$lineNum;
88
89 # Strip comments
90 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
91 if ( $line === '' ) {
92 continue;
93 }
94
95 # Split fields
96 $numberedData = explode( ';', $line );
97 $data = [];
98 foreach ( $fieldNames as $number => $name ) {
99 $data[$name] = $numberedData[$number];
100 }
101
102 $code = base_convert( $data['Code'], 16, 10 );
103 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
104 || ( $code >= 0xFE70 && $code <= 0xFEFF ) # Arabic presentation forms B
105 ) {
106 if ( $data['Decomposition_Type_Mapping'] === '' ) {
107 // No decomposition
108 continue;
109 }
110 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
111 $data['Decomposition_Type_Mapping'], $m )
112 ) {
113 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
114 $this->error( $line );
115 continue;
116 }
117
118 $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
119 $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
120 $pairs[$source] = $dest;
121 }
122 }
123
124 global $IP;
125 file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
126 echo "ar: " . count( $pairs ) . " pairs written.\n";
127 }
128 }
129
130 $maintClass = GenerateNormalizerDataAr::class;
131 require_once RUN_MAINTENANCE_IF_MAIN;