summaryrefslogtreecommitdiff
blob: 71a8cb01c825fa69caae38d23e4d574feb76ecb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
 * txt2php: Converts the text file of ISO codes to a PHP static array definition.
 *
 * Usage: php txt2php.php
 */

 use Wikimedia\StaticArrayWriter;

$dir = __DIR__;
$IP = "$dir/../..";

require_once "$IP/maintenance/commandLine.inc";

$dir = __DIR__;

$names = [];
$codes = [];
$fr = fopen( "$dir/codes.txt", 'r' );

while ( true ) {
	$line = fgets( $fr );
	if ( !$line ) {
		break;
	}

	// Format is code1 code2 "language name"
	$line = explode( ' ', $line, 3 );
	$iso1 = trim( $line[0] );
	$iso3 = trim( $line[1] );
	// Strip quotes
	$name = substr( trim( $line[2] ), 1, -1 );
	if ( $iso1 !== '-' ) {
		$codes[ $iso1 ] = $iso1;
		if ( $iso3 !== '-' ) {
			$codes[ $iso3 ] = $iso1;
		}
		$names[ $iso1 ] = $name;
		$names[ $iso3 ] = $name;
	} elseif ( $iso3 !== '-' ) {
		$codes[ $iso3 ] = $iso3;
		$names[ $iso3 ] = $name;
	}
}

fclose( $fr );

$writer = new StaticArrayWriter();
$header = 'This file is generated by txt2php.php. Do not edit it directly.';
$code = $writer->create( $names, $header );
file_put_contents( "$dir/names.php", $code );

$code = $writer->create( $codes, $header );
file_put_contents( "$dir/codes.php", $code );