]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Tld/db.php
Merge in Phergie changes
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Tld / db.php
1 <?php
2
3 $dbFile = 'tld.db';
4
5 if (file_exists($dbFile)) {
6     exit;
7 }
8
9 $db = new PDO('sqlite:' . dirname(__FILE__) . '/' . $dbFile);
10
11 $query = '
12     CREATE TABLE tld (
13         tld VARCHAR(20),
14         type VARCHAR(20),
15         description VARCHAR(255)
16     )
17 ';
18 $db->exec($query);
19
20 $insert = $db->prepare('
21     INSERT INTO tld (tld, type, description)
22     VALUES (:tld, :type, :description)
23 ');
24
25 $contents = file_get_contents(
26     'http://www.iana.org/domains/root/db/'
27 );
28
29 libxml_use_internal_errors(true);
30 $doc = new DOMDocument;
31 $doc->loadHTML($contents);
32 libxml_clear_errors();
33
34 $descriptions = array(
35     'com' => 'Commercial',
36     'info' => 'Information',
37     'net' => 'Network',
38     'org' => 'Organization',
39     'edu' => 'Educational',
40     'name' => 'Individuals, by name'
41 );
42
43 $xpath = new DOMXPath($doc);
44 $rows = $xpath->query('//tr[contains(@class, "iana-group")]');
45 foreach (range(0, $rows->length - 1) as $index) {
46     $row = $rows->item($index);
47     $tld = strtolower(ltrim($row->childNodes->item(0)->textContent, '.'));
48     $type = $row->childNodes->item(1)->nodeValue;
49     if (isset($descriptions[$tld])) {
50         $description = $descriptions[$tld];
51     } else {
52         $description = $row->childNodes->item(2)->textContent;
53         $regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
54          . '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
55          . '\s*|\s*as advised.*$)}i';
56         $description = preg_replace($regex, '', $description);
57         $description = ucfirst(trim($description));
58     }
59     $data = array_map(
60         'html_entity_decode',
61         array(
62             'tld' => $tld,
63             'type' => $type,
64             'description' => $description
65         )
66     );
67     $insert->execute($data);
68 }