MediaWiki master
generateNormalizerDataAr.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/../Maintenance.php';
26// @codeCoverageIgnoreEnd
27
30
44 public function __construct() {
45 parent::__construct();
46 $this->addDescription( 'Generate the normalizer data file for Arabic' );
47 $this->addOption( 'unicode-data-file', 'The local location of the data file ' .
48 'from https://unicode.org/Public/6.0.0/ucd/UnicodeData.txt', false, true );
49 }
50
52 public function getDbType() {
53 return Maintenance::DB_NONE;
54 }
55
56 public function execute() {
57 if ( !$this->hasOption( 'unicode-data-file' ) ) {
58 $dataFile = 'UnicodeData.txt';
59 if ( !file_exists( $dataFile ) ) {
60 $this->fatalError( "Unable to find UnicodeData.txt. Please specify " .
61 "its location with --unicode-data-file=<FILE>" );
62 }
63 } else {
64 $dataFile = $this->getOption( 'unicode-data-file' );
65 if ( !file_exists( $dataFile ) ) {
66 $this->fatalError( 'Unable to find the specified data file.' );
67 }
68 }
69
70 $file = fopen( $dataFile, 'r' );
71 if ( !$file ) {
72 $this->fatalError( 'Unable to open the data file.' );
73 }
74
75 // For the file format, see https://www.unicode.org/reports/tr44/
76 $fieldNames = [
77 'Code',
78 'Name',
79 'General_Category',
80 'Canonical_Combining_Class',
81 'Bidi_Class',
82 'Decomposition_Type_Mapping',
83 'Numeric_Type_Value_6',
84 'Numeric_Type_Value_7',
85 'Numeric_Type_Value_8',
86 'Bidi_Mirrored',
87 'Unicode_1_Name',
88 'ISO_Comment',
89 'Simple_Uppercase_Mapping',
90 'Simple_Lowercase_Mapping',
91 'Simple_Titlecase_Mapping'
92 ];
93
94 $pairs = [];
95
96 $lineNum = 0;
97 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
98 while ( ( $line = fgets( $file ) ) !== false ) {
99 ++$lineNum;
100
101 # Strip comments
102 $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
103 if ( $line === '' ) {
104 continue;
105 }
106
107 # Split fields
108 $numberedData = explode( ';', $line );
109 $data = [];
110 foreach ( $fieldNames as $number => $name ) {
111 $data[$name] = $numberedData[$number];
112 }
113
114 $code = base_convert( $data['Code'], 16, 10 );
115 if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
116 || ( $code >= 0xFE70 && $code <= 0xFEFF ) # Arabic presentation forms B
117 ) {
118 if ( $data['Decomposition_Type_Mapping'] === '' ) {
119 // No decomposition
120 continue;
121 }
122 if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
123 $data['Decomposition_Type_Mapping'], $m )
124 ) {
125 $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
126 $this->error( $line );
127 continue;
128 }
129
130 $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
131 $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
132 $pairs[$source] = $dest;
133 }
134 }
135
136 global $IP;
137 $writer = new StaticArrayWriter();
138 file_put_contents( "$IP/includes/languages/data/NormalizeAr.php", $writer->writeClass(
139 $pairs,
140 [
141 'header' => 'Generated by generateNormalizerDataAr.php. Do not modify!',
142 'namespace' => 'MediaWiki\\Languages\\Data',
143 'class' => 'NormalizeAr',
144 'const' => 'PAIRS',
145 ]
146 ) );
147
148 echo "ar: " . count( $pairs ) . " pairs written.\n";
149 }
150}
151
152// @codeCoverageIgnoreStart
153$maintClass = GenerateNormalizerDataAr::class;
154require_once RUN_MAINTENANCE_IF_MAIN;
155// @codeCoverageIgnoreEnd
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:103
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