PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Phergie/Plugin/Tld/db.php

https://github.com/markizano/phergie
PHP | 87 lines | 59 code | 9 blank | 19 comment | 3 complexity | d295168f7b901195d45e862689a2d774 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://phergie.org/license
  13. *
  14. * @category Phergie
  15. * @package Phergie
  16. * @author Phergie Development Team <team@phergie.org>
  17. * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  18. * @license http://phergie.org/license New BSD License
  19. * @link http://pear.phergie.org/package/Phergie
  20. */
  21. $dbFile = 'tld.db';
  22. if (file_exists($dbFile)) {
  23. exit;
  24. }
  25. $db = new PDO('sqlite:' . dirname(__FILE__) . '/' . $dbFile);
  26. $query = '
  27. CREATE TABLE tld (
  28. tld VARCHAR(20),
  29. type VARCHAR(20),
  30. description VARCHAR(255)
  31. )
  32. ';
  33. $db->exec($query);
  34. $insert = $db->prepare(
  35. 'INSERT INTO tld (tld, type, description)
  36. VALUES (:tld, :type, :description)'
  37. );
  38. $contents = file_get_contents(
  39. 'http://www.iana.org/domains/root/db/'
  40. );
  41. libxml_use_internal_errors(true);
  42. $doc = new DOMDocument;
  43. $doc->loadHTML($contents);
  44. libxml_clear_errors();
  45. $descriptions = array(
  46. 'com' => 'Commercial',
  47. 'info' => 'Information',
  48. 'net' => 'Network',
  49. 'org' => 'Organization',
  50. 'edu' => 'Educational',
  51. 'name' => 'Individuals, by name'
  52. );
  53. $xpath = new DOMXPath($doc);
  54. $rows = $xpath->query('//tr[contains(@class, "iana-group")]');
  55. foreach (range(0, $rows->length - 1) as $index) {
  56. $row = $rows->item($index);
  57. $tld = strtolower(ltrim($row->childNodes->item(0)->textContent, '.'));
  58. $type = $row->childNodes->item(1)->nodeValue;
  59. if (isset($descriptions[$tld])) {
  60. $description = $descriptions[$tld];
  61. } else {
  62. $description = $row->childNodes->item(2)->textContent;
  63. $regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
  64. . '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
  65. . '\s*|\s*as advised.*$)}i';
  66. $description = preg_replace($regex, '', $description);
  67. $description = ucfirst(trim($description));
  68. }
  69. $data = array_map(
  70. 'html_entity_decode',
  71. array(
  72. 'tld' => $tld,
  73. 'type' => $type,
  74. 'description' => $description
  75. )
  76. );
  77. $insert->execute($data);
  78. }