Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[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 * Example usage:
32 *
33 * curl 'https://unicode.org/Public/6.0.0/ucd/UnicodeData.txt' > /tmp/UnicodeData.txt
34 * php generateNormalizerDataAr.php --unicode-data-file /tmp/UnicodeData.txt
35 *
36 * @ingroup MaintenanceLanguage
37 */
38 class GenerateNormalizerDataAr extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Generate the normalizer data file for Arabic' );
42 $this->addOption( 'unicode-data-file', 'The local location of the data file ' .
43 'from https://unicode.org/Public/6.0.0/ucd/UnicodeData.txt', false, true );
44 }
45
46 public function getDbType() {
47 return Maintenance::DB_NONE;
48 }
49
50 public function execute() {
51 if ( !$this->hasOption( 'unicode-data-file' ) ) {
52 $dataFile = 'UnicodeData.txt';
53 if ( !file_exists( $dataFile ) ) {
54 $this->fatalError( "Unable to find UnicodeData.txt. Please specify " .
55 "its location with --unicode-data-file=<FILE>" );
56 }
57 } else {
58 $dataFile = $this->getOption( 'unicode-data-file' );
59 if ( !file_exists( $dataFile ) ) {
60 $this->fatalError( 'Unable to find the specified data file.' );
61 }
62 }
63
64 $file = fopen( $dataFile, 'r' );
65 if ( !$file ) {
66 $this->fatalError( 'Unable to open the data file.' );
67 }
68
69 // For the file format, see https://www.unicode.org/reports/tr44/
70 $fieldNames = [
71 'Code',
72 'Name',
73 'General_Category',
74 'Canonical_Combining_Class',
75 'Bidi_Class',
76 'Decomposition_Type_Mapping',
77 'Numeric_Type_Value_6',
78 'Numeric_Type_Value_7',
79 'Numeric_Type_Value_8',
80 'Bidi_Mirrored',
81 'Unicode_1_Name',
82 'ISO_Comment',
83 'Simple_Uppercase_Mapping',
84 'Simple_Lowercase_Mapping',
85 'Simple_Titlecase_Mapping'
86 ];
87
88 $pairs = [];
89
90 $lineNum = 0;
91 while ( false !== ( $line = fgets( $file ) ) ) {
92 ++$lineNum;
93
94 # Strip comments
95 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
96 if ( $line === '' ) {
97 continue;
98 }
99
100 # Split fields
101 $numberedData = explode( ';', $line );
102 $data = [];
103 foreach ( $fieldNames as $number => $name ) {
104 $data[$name] = $numberedData[$number];
105 }
106
107 $code = base_convert( $data['Code'], 16, 10 );
108 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
109 || ( $code >= 0xFE70 && $code <= 0xFEFF ) # Arabic presentation forms B
110 ) {
111 if ( $data['Decomposition_Type_Mapping'] === '' ) {
112 // No decomposition
113 continue;
114 }
115 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
116 $data['Decomposition_Type_Mapping'], $m )
117 ) {
118 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
119 $this->error( $line );
120 continue;
121 }
122
123 $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
124 $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
125 $pairs[$source] = $dest;
126 }
127 }
128
129 global $IP;
130 $writer = new StaticArrayWriter();
131 file_put_contents( "$IP/languages/data/normalize-ar.php", $writer->create(
132 $pairs,
133 'File created by generateNormalizerDataAr.php'
134 ) );
135
136 echo "ar: " . count( $pairs ) . " pairs written.\n";
137 }
138 }
139
140 $maintClass = GenerateNormalizerDataAr::class;
141 require_once RUN_MAINTENANCE_IF_MAIN;