MediaWiki  REL1_31
generateNormalizerDataAr.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/../Maintenance.php';
25 
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 );
39  }
40 
41  public function getDbType() {
42  return Maintenance::DB_NONE;
43  }
44 
45  public function execute() {
46  if ( !$this->hasOption( 'unicode-data-file' ) ) {
47  $dataFile = 'UnicodeData.txt';
48  if ( !file_exists( $dataFile ) ) {
49  $this->fatalError( "Unable to find UnicodeData.txt. Please specify " .
50  "its location with --unicode-data-file=<FILE>" );
51  }
52  } else {
53  $dataFile = $this->getOption( 'unicode-data-file' );
54  if ( !file_exists( $dataFile ) ) {
55  $this->fatalError( 'Unable to find the specified data file.' );
56  }
57  }
58 
59  $file = fopen( $dataFile, 'r' );
60  if ( !$file ) {
61  $this->fatalError( 'Unable to open the data file.' );
62  }
63 
64  // For the file format, see http://www.unicode.org/reports/tr44/
65  $fieldNames = [
66  'Code',
67  'Name',
68  'General_Category',
69  'Canonical_Combining_Class',
70  'Bidi_Class',
71  'Decomposition_Type_Mapping',
72  'Numeric_Type_Value_6',
73  'Numeric_Type_Value_7',
74  'Numeric_Type_Value_8',
75  'Bidi_Mirrored',
76  'Unicode_1_Name',
77  'ISO_Comment',
78  'Simple_Uppercase_Mapping',
79  'Simple_Lowercase_Mapping',
80  'Simple_Titlecase_Mapping'
81  ];
82 
83  $pairs = [];
84 
85  $lineNum = 0;
86  while ( false !== ( $line = fgets( $file ) ) ) {
87  ++$lineNum;
88 
89  # Strip comments
90  $line = trim( substr( $line, 0, strcspn( $line, '#' ) ) );
91  if ( $line === '' ) {
92  continue;
93  }
94 
95  # Split fields
96  $numberedData = explode( ';', $line );
97  $data = [];
98  foreach ( $fieldNames as $number => $name ) {
99  $data[$name] = $numberedData[$number];
100  }
101 
102  $code = base_convert( $data['Code'], 16, 10 );
103  if ( ( $code >= 0xFB50 && $code <= 0xFDFF ) # Arabic presentation forms A
104  || ( $code >= 0xFE70 && $code <= 0xFEFF ) # Arabic presentation forms B
105  ) {
106  if ( $data['Decomposition_Type_Mapping'] === '' ) {
107  // No decomposition
108  continue;
109  }
110  if ( !preg_match( '/^ *(<\w*>) +([0-9A-F ]*)$/',
111  $data['Decomposition_Type_Mapping'], $m )
112  ) {
113  $this->error( "Can't parse Decomposition_Type/Mapping on line $lineNum" );
114  $this->error( $line );
115  continue;
116  }
117 
118  $source = UtfNormal\Utils::hexSequenceToUtf8( $data['Code'] );
119  $dest = UtfNormal\Utils::hexSequenceToUtf8( $m[2] );
120  $pairs[$source] = $dest;
121  }
122  }
123 
124  global $IP;
125  file_put_contents( "$IP/serialized/normalize-ar.ser", serialize( $pairs ) );
126  echo "ar: " . count( $pairs ) . " pairs written.\n";
127  }
128 }
129 
131 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:439
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
GenerateNormalizerDataAr\__construct
__construct()
Default constructor.
Definition: generateNormalizerDataAr.php:34
serialize
serialize()
Definition: ApiMessage.php:184
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:37
$maintClass
$maintClass
Definition: generateNormalizerDataAr.php:130
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
$line
$line
Definition: cdb.php:59
Maintenance\DB_NONE
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:66
hexSequenceToUtf8
hexSequenceToUtf8( $sequence)
Take a series of space-separated hexadecimal numbers representing Unicode code points and return a UT...
Definition: UtfNormalUtil.php:55
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:254
$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:865
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:22
$source
$source
Definition: mwdoc-filter.php:46
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:416
GenerateNormalizerDataAr\execute
execute()
Do the actual work.
Definition: generateNormalizerDataAr.php:45
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:56
GenerateNormalizerDataAr
Generates the normalizer data file for Arabic.
Definition: generateNormalizerDataAr.php:33
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:240
GenerateNormalizerDataAr\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: generateNormalizerDataAr.php:41
$IP
$IP
Definition: WebStart.php:52