MediaWiki  1.32.0
generateNormalizerDataAr.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/../Maintenance.php';
25 
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 
141 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
GenerateNormalizerDataAr\__construct
__construct()
Default constructor.
Definition: generateNormalizerDataAr.php:39
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$maintClass
$maintClass
Definition: generateNormalizerDataAr.php:140
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
$IP
$IP
Definition: update.php:3
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:813
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
$line
$line
Definition: cdb.php:59
Maintenance\DB_NONE
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:65
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$source
$source
Definition: mwdoc-filter.php:46
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:442
GenerateNormalizerDataAr\execute
execute()
Do the actual work.
Definition: generateNormalizerDataAr.php:50
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
GenerateNormalizerDataAr
Generates the normalizer data file for Arabic.
Definition: generateNormalizerDataAr.php:38
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
GenerateNormalizerDataAr\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: generateNormalizerDataAr.php:46