| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/../Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
use Wikimedia\StaticArrayWriter; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class GenerateNormalizerDataAr extends Maintenance { |
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
$this->addDescription( 'Generate the normalizer data file for Arabic' ); |
| 46 |
$this->addOption( 'unicode-data-file', 'The local location of the data file ' . |
| 47 |
'from https://unicode.org/Public/6.0.0/ucd/UnicodeData.txt', false, true ); |
| 48 |
} |
| 49 |
|
| 50 |
public function getDbType() { |
| 51 |
return Maintenance::DB_NONE; |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
if ( !$this->hasOption( 'unicode-data-file' ) ) { |
| 56 |
$dataFile = 'UnicodeData.txt'; |
| 57 |
if ( !file_exists( $dataFile ) ) { |
| 58 |
$this->fatalError( "Unable to find UnicodeData.txt. Please specify " . |
| 59 |
"its location with --unicode-data-file=<FILE>" ); |
| 60 |
} |
| 61 |
} else { |
| 62 |
$dataFile = $this->getOption( 'unicode-data-file' ); |
| 63 |
if ( !file_exists( $dataFile ) ) { |
| 64 |
$this->fatalError( 'Unable to find the specified data file.' ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$file = fopen( $dataFile, 'r' ); |
| 69 |
if ( !$file ) { |
| 70 |
$this->fatalError( 'Unable to open the data file.' ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
$fieldNames = [ |
| 75 |
'Code', |
| 76 |
'Name', |
| 77 |
'General_Category', |
| 78 |
'Canonical_Combining_Class', |
| 79 |
'Bidi_Class', |
| 80 |
'Decomposition_Type_Mapping', |
| 81 |
'Numeric_Type_Value_6', |
| 82 |
'Numeric_Type_Value_7', |
| 83 |
'Numeric_Type_Value_8', |
| 84 |
'Bidi_Mirrored', |
| 85 |
'Unicode_1_Name', |
| 86 |
'ISO_Comment', |
| 87 |
'Simple_Uppercase_Mapping', |
| 88 |
'Simple_Lowercase_Mapping', |
| 89 |
'Simple_Titlecase_Mapping' |
| 90 |
]; |
| 91 |
|
| 92 |
$pairs = []; |
| 93 |
|
| 94 |
$lineNum = 0; |
| 95 |
|
| 96 |
while ( ( $line = fgets( $file ) ) !== false ) { |
| 97 |
++$lineNum; |
| 98 |
|
| 99 |
|
| 100 |
$line = trim( substr( $line, 0, strcspn( $line, '#' ) ) ); |
| 101 |
if ( $line === '' ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
$numberedData = explode( ';', $line ); |
| 107 |
$data = []; |
| 108 |
foreach ( $fieldNames as $number => $name ) { |
| 109 |
$data[$name] = $numberedData[$number]; |
| 110 |
} |
| 111 |
|
| 112 |
$code = base_convert( $data['Code'], 16, 10 ); |
| 113 |
if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) |
| 114 |
|| ( $code >= 0xFE70 && $code <= 0xFEFF ) |
| 115 |
) { |
| 116 |
if ( $data['Decomposition_Type_Mapping'] === '' ) { |
| 117 |
|
| 118 |
continue; |
| 119 |
} |
| 120 |
if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/', |
| 121 |
$data['Decomposition_Type_Mapping'], $m ) |
| 122 |
) { |
| 123 |
$this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" ); |
| 124 |
$this->error( $line ); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] ); |
| 129 |
$dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] ); |
| 130 |
$pairs[$source] = $dest; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
global $IP; |
| 135 |
$writer = new StaticArrayWriter(); |
| 136 |
file_put_contents( "$IP/includes/languages/data/NormalizeAr.php", $writer->writeClass( |
| 137 |
$pairs, |
| 138 |
[ |
| 139 |
'header' => 'Generated by generateNormalizerDataAr.php. Do not modify!', |
| 140 |
'namespace' => 'MediaWiki\\Languages\\Data', |
| 141 |
'class' => 'NormalizeAr', |
| 142 |
'const' => 'PAIRS', |
| 143 |
] |
| 144 |
) ); |
| 145 |
|
| 146 |
echo "ar: " . count( $pairs ) . " pairs written.\n"; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
$maintClass = GenerateNormalizerDataAr::class; |
| 152 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 153 |
|