summaryrefslogtreecommitdiff
blob: 317acc7177f3ef8a25652c1b21ef61e9e66a6173 (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
<?php
/**
 * txt2cdb: Converts the text file of ISO codes to a constant database.
 *
 * Usage: php txt2cdb.php
 */

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

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

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

try {
	$names = Cdb\Writer::open( $names );
	$codes = Cdb\Writer::open( $codes );

	while ( $line = fgets( $fr ) ) {
		// 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->set( $iso1, $iso1 );
			if ( $iso3 !== '-' ) {
				$codes->set( $iso3, $iso1 );
			}
			$names->set( $iso1, $name );
			$names->set( $iso3, $name );
		} elseif ( $iso3 !== '-' ) {
			$codes->set( $iso3, $iso3 );
			$names->set( $iso3, $name );
		}
	}
} catch ( Cdb\Exception $e ) {
	throw new Exception( $e->getMessage() );
}

fclose( $fr );