Assembla home | Assembla project page
 

Changeset 89

Show
Ignore:
Timestamp:
03/01/08 02:52:12 (9 months ago)
Author:
tobias382
Message:

Modified the Tld plugin to automatically create, populate, and retrieve from a SQLite database

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Tld.php

    r76 r89  
    1616{ 
    1717    /** 
    18     * TLD lookup table 
     18    * Indicates that a local directory is required for this plugin 
     19    * 
     20    * @var bool 
    1921    */ 
    20     protected $tld = array 
    21     ( 
    22         // Generic 
    23         'aero' => 'The air transport industry', 
    24         'asia' => 'Companies, organisations and individuals in the Asia-Pacific region', 
    25         'biz' => 'Business use', 
    26         'cat' => 'Catalan language/culture', 
    27         'com' => 'Commercial organizations, but unrestricted', 
    28         'coop' => 'Cooperatives', 
    29         'edu' => 'Post-secondary educational establishments', 
    30         'gov' => 'Governments and their agencies in the United States', 
    31         'info' => 'Informational sites, but unrestricted', 
    32         'int' => 'International organizations established by treaty', 
    33         'jobs' => 'Employment-related sites', 
    34         'mil' => 'The US military', 
    35         'mobi' => 'Sites catering to mobile devices', 
    36         'museum' => 'Museums', 
    37         'name' => 'Families and individuals', 
    38         'net' => 'Originally for network infrastructures, now unrestricted', 
    39         'org' => 'Originally for organizations not clearly falling within the other gTLDs, now unrestricted', 
    40         'pro' => 'Certain professions', 
    41         'tel' => 'Services involving connections between the telephone network and the Internet', 
    42         'travel' => 'Travel agents, airlines, hoteliers, tourism bureaus, etc.', 
    43         // Generic (proposals) 
    44         'post' => 'Postal services (proposal)', 
    45         'geo' => 'Geographically related sites (proposal)', 
    46         'cym' => 'Welsh language/culture (proposal)', 
    47         // Country Codes 
    48         'ac' => 'Ascension Island', 
    49         'ad' => 'Andorra', 
    50         'ae' => 'United Arab Emirates', 
    51         'af' => 'Afghanistan', 
    52         'ag' => 'Antigua and Barbuda', 
    53         'ai' => 'Anguilla', 
    54         'al' => 'Albania', 
    55         'am' => 'Armenia', 
    56         'an' => 'Netherlands Antilles', 
    57         'ao' => 'Angola', 
    58         'aq' => 'Antarctica', 
    59         'ar' => 'Argentina', 
    60         'as' => 'American Samoa', 
    61         'at' => 'Austria', 
    62         'au' => 'Australia', 
    63         'aw' => 'Aruba', 
    64         'ax' => 'Åland Islands', 
    65         'az' => 'Azerbaijan', 
    66         'ba' => 'Bosnia and Herzegovina', 
    67         'bb' => 'Barbados', 
    68         'bd' => 'Bangladesh', 
    69         'be' => 'Belgium', 
    70         'bf' => 'Burkina Faso', 
    71         'bg' => 'Bulgaria', 
    72         'bh' => 'Bahrain', 
    73         'bi' => 'Burundi', 
    74         'bj' => 'Benin', 
    75         'bm' => 'Bermuda', 
    76         'bn' => 'Brunei', 
    77         'bo' => 'Bolivia', 
    78         'br' => 'Brazil', 
    79         'bs' => 'Bahamas', 
    80         'bt' => 'Bhutan', 
    81         'bv' => 'Bouvet Island (not in use; no registrations)', 
    82         'bw' => 'Botswana', 
    83         'by' => 'Belarus', 
    84         'bz' => 'Belize', 
    85         'ca' => 'Canada', 
    86         'cc' => 'Cocos (Keeling) Islands', 
    87         'cd' => 'Democratic Republic of the Congo (formerly .zr - Zaire)', 
    88         'cf' => 'Central African Republic', 
    89         'cg' => 'Republic of the Congo', 
    90         'ch' => 'Switzerland', 
    91         'ci' => 'Côte d\'Ivoire (Ivory Coast)', 
    92         'ck' => 'Cook Islands', 
    93         'cl' => 'Chile', 
    94         'cm' => 'Cameroon', 
    95         'cn' => 'People\'s Republic of China', 
    96         'co' => 'Colombia', 
    97         'cr' => 'Costa Rica', 
    98         'cu' => 'Cuba', 
    99         'cv' => 'Cape Verde', 
    100         'cx' => 'Christmas Island', 
    101         'cy' => 'Cyprus', 
    102         'cz' => 'Czech Republic', 
    103         'de' => 'Germany', 
    104         'dj' => 'Djibouti', 
    105         'dk' => 'Denmark', 
    106         'dm' => 'Dominica', 
    107         'do' => 'Dominican Republic', 
    108         'dz' => 'Algeria', 
    109         'ec' => 'Ecuador', 
    110         'ee' => 'Estonia', 
    111         'eg' => 'Egypt', 
    112         'eh' => 'Western Sahara (not assigned; no DNS)', 
    113         'er' => 'Eritrea', 
    114         'es' => 'Spain', 
    115         'et' => 'Ethiopia', 
    116         'eu' => 'European Union (code "exceptionally reserved" by ISO 3166-1)', 
    117         'fi' => 'Finland', 
    118         'fj' => 'Fiji', 
    119         'fk' => 'Falkland Islands', 
    120         'fm' => 'Federated States of Micronesia', 
    121         'fo' => 'Faroe Islands', 
    122         'fr' => 'France', 
    123         'ga' => 'Gabon', 
    124         'gb' => 'United Kingdom (Reserved domain by IANA; deprecated - see .uk)', 
    125         'gd' => 'Grenada', 
    126         'ge' => 'Georgia', 
    127         'gf' => 'French Guiana', 
    128         'gg' => 'Guernsey', 
    129         'gh' => 'Ghana', 
    130         'gi' => 'Gibraltar', 
    131         'gl' => 'Greenland', 
    132         'gm' => 'Gambia', 
    133         'gn' => 'Guinea', 
    134         'gp' => 'Guadeloupe', 
    135         'gq' => 'Equatorial Guinea', 
    136         'gr' => 'Greece', 
    137         'gs' => 'South Georgia and the South Sandwich Islands', 
    138         'gt' => 'Guatemala', 
    139         'gu' => 'Guam', 
    140         'gw' => 'Guinea-Bissau', 
    141         'gy' => 'Guyana', 
    142         'hk' => 'Hong Kong', 
    143         'hm' => 'Heard Island and McDonald Islands', 
    144         'hn' => 'Honduras', 
    145         'hr' => 'Croatia', 
    146         'ht' => 'Haiti', 
    147         'hu' => 'Hungary', 
    148         'id' => 'Indonesia', 
    149         'ie' => 'Ireland', 
    150         'il' => 'Israel', 
    151         'im' => 'Isle of Man', 
    152         'in' => 'India', 
    153         'io' => 'British Indian Ocean Territory', 
    154         'iq' => 'Iraq', 
    155         'ir' => 'Iran', 
    156         'is' => 'Iceland', 
    157         'it' => 'Italy', 
    158         'je' => 'Jersey', 
    159         'jm' => 'Jamaica', 
    160         'jo' => 'Jordan', 
    161         'jp' => 'Japan', 
    162         'ke' => 'Kenya', 
    163         'kg' => 'Kyrgyzstan', 
    164         'kh' => 'Cambodia', 
    165         'ki' => 'Kiribati', 
    166         'km' => 'Comoros', 
    167         'kn' => 'Saint Kitts and Nevis', 
    168         'kp' => 'North Korea', 
    169         'kr' => 'South Korea', 
    170         'kw' => 'Kuwait', 
    171         'ky' => 'Cayman Islands', 
    172         'kz' => 'Kazakhstan', 
    173         'la' => 'Laos', 
    174         'lb' => 'Lebanon', 
    175         'lc' => 'Saint Lucia', 
    176         'li' => 'Liechtenstein', 
    177         'lk' => 'Sri Lanka', 
    178         'lr' => 'Liberia', 
    179         'ls' => 'Lesotho', 
    180         'lt' => 'Lithuania', 
    181         'lu' => 'Luxembourg', 
    182         'lv' => 'Latvia', 
    183         'ly' => 'Libya', 
    184         'ma' => 'Morocco', 
    185         'mc' => 'Monaco', 
    186         'md' => 'Moldova', 
    187         'me' => 'Montenegro', 
    188         'mg' => 'Madagascar', 
    189         'mh' => 'Marshall Islands', 
    190         'mk' => 'Republic of Macedonia', 
    191         'ml' => 'Mali', 
    192         'mm' => 'Myanmar', 
    193         'mn' => 'Mongolia', 
    194         'mo' => 'Macau', 
    195         'mp' => 'Northern Mariana Islands', 
    196         'mq' => 'Martinique', 
    197         'mr' => 'Mauritania', 
    198         'ms' => 'Montserrat', 
    199         'mt' => 'Malta', 
    200         'mu' => 'Mauritius', 
    201         'mv' => 'Maldives', 
    202         'mw' => 'Malawi', 
    203         'mx' => 'Mexico', 
    204         'my' => 'Malaysia', 
    205         'mz' => 'Mozambique', 
    206         'na' => 'Namibia', 
    207         'nc' => 'New Caledonia', 
    208         'ne' => 'Niger', 
    209         'nf' => 'Norfolk Island', 
    210         'ng' => 'Nigeria', 
    211         'ni' => 'Nicaragua', 
    212         'nl' => 'Netherlands', 
    213         'no' => 'Norway', 
    214         'np' => 'Nepal', 
    215         'nr' => 'Nauru', 
    216         'nu' => 'Niue', 
    217         'nz' => 'New Zealand', 
    218         'nc.tr' => 'North Cyprus', 
    219         'om' => 'Oman', 
    220         'pa' => 'Panama', 
    221         'pe' => 'Peru', 
    222         'pf' => 'French Polynesia', 
    223         'pg' => 'Papua New Guinea', 
    224         'ph' => 'Philippines', 
    225         'pk' => 'Pakistan', 
    226         'pl' => 'Poland', 
    227         'pm' => 'Saint Pierre and Miquelon', 
    228         'pn' => 'Pitcairn Islands', 
    229         'pr' => 'Puerto Rico', 
    230         'ps' => 'Palestinian Territory, Occupied', 
    231         'pt' => 'Portugal', 
    232         'pw' => 'Palau', 
    233         'py' => 'Paraguay', 
    234         'qa' => 'Qatar', 
    235         're' => 'Réunion', 
    236         'ro' => 'Romania', 
    237         'rs' => 'Serbia', 
    238         'ru' => 'Russia', 
    239         'rw' => 'Rwanda', 
    240         'sa' => 'Saudi Arabia', 
    241         'sb' => 'Solomon Islands', 
    242         'sc' => 'Seychelles', 
    243         'sd' => 'Sudan', 
    244         'se' => 'Sweden', 
    245         'sg' => 'Singapore', 
    246         'sh' => 'Saint Helena', 
    247         'si' => 'Slovenia', 
    248         'sj' => 'Svalbard and Jan Mayen islands (not in use; no registrations)', 
    249         'sk' => 'Slovakia', 
    250         'sl' => 'Sierra Leone', 
    251         'sm' => 'San Marino', 
    252         'sn' => 'Senegal', 
    253         'so' => 'Somalia (down, still is delegated to Monolith [ml.org] Philadelphia, an entity defunct since end-1998)', 
    254         'sr' => 'Suriname', 
    255         'st' => 'São Tomé and Príncipe', 
    256         'su' => 'Soviet Union (deprecated; being phased out; code "transitionally reserved" by ISO 3166-1)', 
    257         'sv' => 'El Salvador', 
    258         'sy' => 'Syria', 
    259         'sz' => 'Swaziland', 
    260         'tc' => 'Turks and Caicos Islands', 
    261         'td' => 'Chad', 
    262         'tf' => 'French Southern Territories', 
    263         'tg' => 'Togo', 
    264         'th' => 'Thailand', 
    265         'tj' => 'Tajikistan', 
    266         'tk' => 'Tokelau', 
    267         'tl' => 'East Timor (formerly .tp)', 
    268         'tm' => 'Turkmenistan', 
    269         'tn' => 'Tunisia', 
    270         'to' => 'Tonga', 
    271         'tp' => 'East Timor (deprecated - use .tl; code "transitionally reserved" by ISO 3166-1)', 
    272         'tr' => 'Turkey', 
    273         'tt' => 'Trinidad and Tobago', 
    274         'tv' => 'Tuvalu', 
    275         'tw' => 'Taiwan', 
    276         'tz' => 'Tanzania', 
    277         'ua' => 'Ukraine', 
    278         'ug' => 'Uganda', 
    279         'uk' => 'United Kingdom (code "exceptionally reserved" by ISO 3166-1) (see also .gb)', 
    280         'us' => 'United States', 
    281         'uy' => 'Uruguay', 
    282         'uz' => 'Uzbekistan', 
    283         'va' => 'Vatican City', 
    284         'vc' => 'Saint Vincent and the Grenadines', 
    285         've' => 'Venezuela', 
    286         'vg' => 'British Virgin Islands', 
    287         'vi' => 'United States Virgin Islands', 
    288         'vn' => 'Vietnam', 
    289         'vu' => 'Vanuatu', 
    290         'wf' => 'Wallis and Futuna', 
    291         'ws' => 'Samoa (formerly Western Samoa)', 
    292         'ye' => 'Yemen', 
    293         'yt' => 'Mayotte', 
    294         'yu' => 'Yugoslavia (subsequently renamed Serbia and Montenegro)', 
    295         'za' => 'South Africa', 
    296         'zm' => 'Zambia', 
    297         'zw' => 'Zimbabwe', 
    298         // Pranks 
    299         'spoon' => 'Don\'t you know ? There is no spoon !', 
    300         'poo' => 'Do you really think that\'s funny ?', 
    301     ); 
     22    protected $needsDir = true; 
     23 
     24    /** 
     25    * PDO resource for a SQLite database containing the TLDs  
     26    * 
     27    * @var resource 
     28    */ 
     29    protected $db = null; 
     30 
     31    /** 
     32    * Prepared statement to retrieve TLD records 
     33    * 
     34    * @var PDOStatement 
     35    */ 
     36    protected $select; 
     37 
     38    /** 
     39    * Connects to the database and populates tables where needed. 
     40    * 
     41    * @return void 
     42    */ 
     43    public function init() 
     44    { 
     45        try { 
     46            // Initialize the database connection 
     47            $this->db = new PDO('sqlite:' . $this->dir . 'tld.db'); 
     48 
     49            // Check to see if the TLD table needs to be created 
     50            $table = $this->db 
     51                ->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote('tld')) 
     52                ->fetchColumn(); 
     53 
     54            if ($table) { 
     55                $table = $this->db 
     56                    ->query('SELECT COUNT(*) FROM tld') 
     57                    ->fetchColumn(); 
     58            } 
     59 
     60            // Create and populate the table if needed 
     61            if (!$table) { 
     62                $this->db->exec('CREATE TABLE tld (name VARCHAR(8), description VARCHAR(255))'); 
     63 
     64                $insert = $this->db->prepare('INSERT INTO tld (name, description) VALUES (:name, :description)'); 
     65 
     66                $doc = new DomDocument(); 
     67                @$doc->loadHtmlFile('http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'); 
     68                 
     69                $xpath = new DomXPath($doc); 
     70                $rows = $xpath->query('//table[@class="wikitable"]/tr'); 
     71 
     72                for ($i = 0; $i < $rows->length; $i++) { 
     73                    $row = simplexml_import_dom($rows->item($i)); 
     74                    if (isset($row->td)) { 
     75                        $name = substr($row->td[0]->a, 1); 
     76                        if (isset($row->td[1]->a)) { 
     77                            $description = $row->td[1]->a; 
     78                        } elseif ($row->td[1] == 'N/A') { 
     79                            $description = $row->td[2]; 
     80                        } else { 
     81                            $description = $row->td[1]; 
     82                        } 
     83                        switch ($name) { 
     84                            case 'ax': 
     85                                $description = 'Aland'; 
     86                                break; 
     87                            case 'st': 
     88                                $description = 'Sao Tome and Principe'; 
     89                                break; 
     90                            case 're': 
     91                                $description = 'Reunion'; 
     92                                break; 
     93                        } 
     94                        $data = array_map('html_entity_decode', array( 
     95                            'name' => $name,  
     96                            'description' => strip_tags($description) 
     97                        )); 
     98                        unset($name, $description); 
     99                        $insert->execute($data); 
     100                    } 
     101                } 
     102 
     103                $insert->execute(array('name' => 'spoon', 'description' => 'Don\'t you know? There is no spoon!')); 
     104                $insert->execute(array('name' => 'poo', 'description' => 'Do you really think that\'s funny?')); 
     105            } 
     106 
     107            // Create a prepared statement for retrieving TLDs 
     108            $this->select = $this->db->prepare('SELECT description FROM tld WHERE name = :name'); 
     109        } catch (PDOException $e) { } 
     110    } 
    302111 
    303112    /** 
     
    309118    public function onPrivmsg() 
    310119    { 
    311         if (preg_match('#^\.([a-z]{2,6})$#i', $this->event->getArgument(1), $m)) { 
    312             if (isset($this->tld[$m[1]])) { 
    313                 $this->doPrivmsg($this->event->getSource(), $m[0].' -> '.$this->tld[$m[1]]); 
     120        if (preg_match('#^\.([a-z]{2,6})$#i', $this->event->getArgument(1), $m) 
     121            && $this->select->execute(array('name' => $m[1]))) { 
     122            $description = $this->select->fetchColumn(); 
     123            if ($description) { 
     124                $this->doPrivmsg($this->event->getSource(), $m[0] . ' -> ' . $description);  
    314125            } else { 
    315126                $this->doPrivmsg($this->event->getSource(), 'Unknown TLD');