MediaWiki master
generateNormalizerDataAr.php
Go to the documentation of this file.
1<?php
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/../Maintenance.php';
12// @codeCoverageIgnoreEnd
13
16
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Generate the normalizer data file for Arabic' );
33 $this->addOption( 'unicode-data-file', 'The local location of the data file ' .
34 'from https://unicode.org/Public/6.0.0/ucd/UnicodeData.txt', false, true );
35 }
36
38 public function getDbType() {
39 return Maintenance::DB_NONE;
40 }
41
42 public function execute() {
43 if ( !$this->hasOption( 'unicode-data-file' ) ) {
44 $dataFile = 'UnicodeData.txt';
45 if ( !file_exists( $dataFile ) ) {
46 $this->fatalError( "Unable to find UnicodeData.txt. Please specify " .
47 "its location with --unicode-data-file=<FILE>" );
48 }
49 } else {
50 $dataFile = $this->getOption( 'unicode-data-file' );
51 if ( !file_exists( $dataFile ) ) {
52 $this->fatalError( 'Unable to find the specified data file.' );
53 }
54 }
55
56 $file = fopen( $dataFile, 'r' );
57 if ( !$file ) {
58 $this->fatalError( 'Unable to open the data file.' );
59 }
60
61 // For the file format, see https://www.unicode.org/reports/tr44/
62 $fieldNames = [
63 'Code',
64 'Name',
65 'General_Category',
66 'Canonical_Combining_Class',
67 'Bidi_Class',
68 'Decomposition_Type_Mapping',
69 'Numeric_Type_Value_6',
70 'Numeric_Type_Value_7',
71 'Numeric_Type_Value_8',
72 'Bidi_Mirrored',
73 'Unicode_1_Name',
74 'ISO_Comment',
75 'Simple_Uppercase_Mapping',
76 'Simple_Lowercase_Mapping',
77 'Simple_Titlecase_Mapping'
78 ];
79
80 $pairs = [];
81
82 $lineNum = 0;
83 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
84 while ( ( $line = fgets( $file ) ) !== false ) {
85 ++$lineNum;
86
87 # Strip comments
88 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
89 if ( $line === '' ) {
90 continue;
91 }
92
93 # Split fields
94 $numberedData = explode( ';', $line );
95 $data = [];
96 foreach ( $fieldNames as $number => $name ) {
97 $data[$name] = $numberedData[$number];
98 }
99
100 $code = base_convert( $data['Code'], 16, 10 );
101 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
102 || ( $code >= 0xFE70 && $code <= 0xFEFF ) # Arabic presentation forms B
103 ) {
104 if ( $data['Decomposition_Type_Mapping'] === '' ) {
105 // No decomposition
106 continue;
107 }
108 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
109 $data['Decomposition_Type_Mapping'], $m )
110 ) {
111 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
112 $this->error( $line );
113 continue;
114 }
115
116 $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
117 $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
118 $pairs[$source] = $dest;
119 }
120 }
121
122 global $IP;
123 $writer = new StaticArrayWriter();
124 file_put_contents( "$IP/includes/Languages/Data/NormalizeAr.php", $writer->writeClass(
125 $pairs,
126 [
127 'header' => 'Generated by generateNormalizerDataAr.php. Do not modify!',
128 'namespace' => 'MediaWiki\\Languages\\Data',
129 'class' => 'NormalizeAr',
130 'const' => 'PAIRS',
131 ]
132 ) );
133
134 echo "ar: " . count( $pairs ) . " pairs written.\n";
135 }
136}
137
138// @codeCoverageIgnoreStart
139$maintClass = GenerateNormalizerDataAr::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
141// @codeCoverageIgnoreEnd
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
Generates the normalizer data file for Arabic.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
addDescription( $text)
Set the description text.
Format a static PHP array to be written to a file.
$source