Assembla home | Assembla project page
 

Changeset 183

Show
Ignore:
Timestamp:
03/21/08 22:46:34 (8 months ago)
Author:
Slynderdale
Message:

Added a couple new static functions to TLD that allow modules able to retrieve a list of TLDs and their definitions as well as added a couple bug fixes.

Files:

Legend:

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

    r89 r183  
    2323 
    2424    /** 
    25     * PDO resource for a SQLite database containing the TLDs  
     25    * PDO resource for a SQLite database containing the TLDs 
    2626    * 
    2727    * @var resource 
    2828    */ 
    29     protected $db = null; 
     29    protected static $db = null; 
    3030 
    3131    /** 
     
    3434    * @var PDOStatement 
    3535    */ 
    36     protected $select; 
     36    protected static $select; 
     37    protected static $selectAll; 
    3738 
    3839    /** 
     
    4546        try { 
    4647            // Initialize the database connection 
    47             $this->db = new PDO('sqlite:' . $this->dir . 'tld.db'); 
     48            self::$db = new PDO('sqlite:' . $this->dir . 'tld.db'); 
    4849 
    4950            // 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')) 
     51            $table = self::$db 
     52                ->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . self::$db->quote('tld')) 
    5253                ->fetchColumn(); 
    5354 
    5455            if ($table) { 
    55                 $table = $this->db 
     56                $table = self::$db 
    5657                    ->query('SELECT COUNT(*) FROM tld') 
    5758                    ->fetchColumn(); 
     
    6061            // Create and populate the table if needed 
    6162            if (!$table) { 
    62                 $this->db->exec('CREATE TABLE tld (name VARCHAR(8), description VARCHAR(255))'); 
     63                $this->debug('Creating the database schema'); 
     64                self::$db->exec('CREATE TABLE tld (name VARCHAR(8), description VARCHAR(255))'); 
    6365 
    64                 $insert = $this->db->prepare('INSERT INTO tld (name, description) VALUES (:name, :description)'); 
     66                $insert = self::$db->prepare('INSERT INTO tld (name, description) VALUES (:name, :description)'); 
    6567 
    6668                $doc = new DomDocument(); 
    6769                @$doc->loadHtmlFile('http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'); 
    68                  
     70 
    6971                $xpath = new DomXPath($doc); 
    7072                $rows = $xpath->query('//table[@class="wikitable"]/tr'); 
     
    7476                    if (isset($row->td)) { 
    7577                        $name = substr($row->td[0]->a, 1); 
     78                        if (empty($name)) { 
     79                                continue; 
     80                        } 
     81 
    7682                        if (isset($row->td[1]->a)) { 
    7783                            $description = $row->td[1]->a; 
     
    9399                        } 
    94100                        $data = array_map('html_entity_decode', array( 
    95                             'name' => $name,  
     101                            'name' => $name, 
    96102                            'description' => strip_tags($description) 
    97103                        )); 
     104 
     105                        $this->debug('Inserted TLD: ' . $name . ' = ' . $description); 
    98106                        unset($name, $description); 
     107 
    99108                        $insert->execute($data); 
    100109                    } 
     
    106115 
    107116            // Create a prepared statement for retrieving TLDs 
    108             $this->select = $this->db->prepare('SELECT description FROM tld WHERE name = :name'); 
     117            self::$select = self::$db->prepare('SELECT description FROM tld WHERE LOWER(name) = LOWER(:name)'); 
     118            self::$selectAll = self::$db->prepare('SELECT name, description FROM tld'); 
    109119        } catch (PDOException $e) { } 
     120    } 
     121 
     122    /** 
     123    * Returns whether or not the plugin's dependencies are met. 
     124    * 
     125    * @param Phergie_Driver_Abstract $client Client instance 
     126    * @param array $plugins List of short names for plugins that the 
     127    *                       bootstrap file intends to instantiate 
     128    * @see Phergie_Plugin_Abstract_Base::checkDependencies() 
     129    * @return bool TRUE if dependencies are met, FALSE otherwise 
     130    */ 
     131    public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins) 
     132    { 
     133        if (!extension_loaded('PDO') 
     134            || !extension_loaded('pdo_sqlite')) { 
     135            return false; 
     136        } 
     137 
     138        return true; 
    110139    } 
    111140 
     
    118147    public function onPrivmsg() 
    119148    { 
    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);  
    125             } else { 
    126                 $this->doPrivmsg($this->event->getSource(), 'Unknown TLD'); 
     149        if (preg_match('#^(?:'.preg_quote($this->getIni('nick')).'\s*:?\s+)?\.([a-z]{2,6})$#i', $this->event->getArgument(1), $m) && 
     150            $description = self::getTld($m[1])) { 
     151            $this->doPrivmsg($this->event->getSource(), $m[0] . ' -> ' . ($description ? ucfirst($description) : 'Unknown TLD')); 
     152        } 
     153    } 
     154 
     155    /** 
     156    * Retrieves the definition for a given TLD if it exists 
     157    * 
     158    * @param string $tld TLD to search for 
     159    * @return string Defination of the given TLD 
     160    */ 
     161    public static function getTld($tld) { 
     162        $tld = trim(strtolower($tld)); 
     163        if (self::$db && self::$select->execute(array('name' => $tld))) { 
     164            return self::$select->fetchColumn(); 
     165        } 
     166        return false; 
     167    } 
     168 
     169    /** 
     170    * Retrieves a list of all the TLDs and their definations 
     171    * 
     172    * @return array Array of all the TLDs and their definations 
     173    */ 
     174    public static function getTlds() { 
     175        if (self::$db && self::$selectAll->execute()) { 
     176            $tlds = self::$selectAll->fetchAll(); 
     177            if (is_array($tlds)) { 
     178                $tldinfo = array(); 
     179                foreach($tlds as $key => $tld) { 
     180                        if (!empty($tld['name'])) { 
     181                            $tldinfo[$tld['name']] = $tld['description']; 
     182                    } 
     183                } 
     184                unset($tlds); 
     185                return $tldinfo; 
    127186            } 
    128187        } 
     188        return false; 
    129189    } 
    130190}