Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / language / generateNormalizerDataAr.php
blob34903de1dbe76f89520d546b0f6520c7373a107d
1 <?php
2 /**
3 * Generates the normalizer data file for Arabic.
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
20 * @file
21 * @ingroup MaintenanceLanguage
24 require_once __DIR__ . '/../Maintenance.php';
26 /**
27 * Generates the normalizer data file for Arabic.
29 * This data file is used after normalizing to NFC.
31 * @ingroup MaintenanceLanguage
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 );
41 public function getDbType() {
42 return Maintenance::DB_NONE;
45 public function execute() {
46 if ( !$this->hasOption( 'unicode-data-file' ) ) {
47 $dataFile = 'UnicodeData.txt';
48 if ( !file_exists( $dataFile ) ) {
49 $this->error( "Unable to find UnicodeData.txt. Please specify " .
50 "its location with --unicode-data-file=<FILE>" );
51 exit( 1 );
53 } else {
54 $dataFile = $this->getOption( 'unicode-data-file' );
55 if ( !file_exists( $dataFile ) ) {
56 $this->error( 'Unable to find the specified data file.' );
57 exit( 1 );
61 $file = fopen( $dataFile, 'r' );
62 if ( !$file ) {
63 $this->error( 'Unable to open the data file.' );
64 exit( 1 );
67 // For the file format, see http://www.unicode.org/reports/tr44/
68 $fieldNames = [
69 'Code',
70 'Name',
71 'General_Category',
72 'Canonical_Combining_Class',
73 'Bidi_Class',
74 'Decomposition_Type_Mapping',
75 'Numeric_Type_Value_6',
76 'Numeric_Type_Value_7',
77 'Numeric_Type_Value_8',
78 'Bidi_Mirrored',
79 'Unicode_1_Name',
80 'ISO_Comment',
81 'Simple_Uppercase_Mapping',
82 'Simple_Lowercase_Mapping',
83 'Simple_Titlecase_Mapping'
86 $pairs = [];
88 $lineNum = 0;
89 while ( false !== ( $line = fgets( $file ) ) ) {
90 ++$lineNum;
92 # Strip comments
93 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
94 if ( $line === '' ) {
95 continue;
98 # Split fields
99 $numberedData = explode( ';', $line );
100 $data = [];
101 foreach ( $fieldNames as $number => $name ) {
102 $data[$name] = $numberedData[$number];
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
109 if ( $data['Decomposition_Type_Mapping'] === '' ) {
110 // No decomposition
111 continue;
113 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
114 $data['Decomposition_Type_Mapping'], $m )
116 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
117 $this->error( $line );
118 continue;
121 $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
122 $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
123 $pairs[$source] = $dest;
127 global $IP;
128 file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
129 echo "ar: " . count( $pairs ) . " pairs written.\n";
133 $maintClass = 'GenerateNormalizerDataAr';
134 require_once RUN_MAINTENANCE_IF_MAIN;