Changeset 183
- Timestamp:
- 03/21/08 22:46:34 (8 months ago)
- Files:
-
- trunk/Phergie/Plugin/Tld.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Plugin/Tld.php
r89 r183 23 23 24 24 /** 25 * PDO resource for a SQLite database containing the TLDs 25 * PDO resource for a SQLite database containing the TLDs 26 26 * 27 27 * @var resource 28 28 */ 29 protected $db = null;29 protected static $db = null; 30 30 31 31 /** … … 34 34 * @var PDOStatement 35 35 */ 36 protected $select; 36 protected static $select; 37 protected static $selectAll; 37 38 38 39 /** … … 45 46 try { 46 47 // Initialize the database connection 47 $this->db = new PDO('sqlite:' . $this->dir . 'tld.db');48 self::$db = new PDO('sqlite:' . $this->dir . 'tld.db'); 48 49 49 50 // Check to see if the TLD table needs to be created 50 $table = $this->db51 ->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')) 52 53 ->fetchColumn(); 53 54 54 55 if ($table) { 55 $table = $this->db56 $table = self::$db 56 57 ->query('SELECT COUNT(*) FROM tld') 57 58 ->fetchColumn(); … … 60 61 // Create and populate the table if needed 61 62 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))'); 63 65 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)'); 65 67 66 68 $doc = new DomDocument(); 67 69 @$doc->loadHtmlFile('http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'); 68 70 69 71 $xpath = new DomXPath($doc); 70 72 $rows = $xpath->query('//table[@class="wikitable"]/tr'); … … 74 76 if (isset($row->td)) { 75 77 $name = substr($row->td[0]->a, 1); 78 if (empty($name)) { 79 continue; 80 } 81 76 82 if (isset($row->td[1]->a)) { 77 83 $description = $row->td[1]->a; … … 93 99 } 94 100 $data = array_map('html_entity_decode', array( 95 'name' => $name, 101 'name' => $name, 96 102 'description' => strip_tags($description) 97 103 )); 104 105 $this->debug('Inserted TLD: ' . $name . ' = ' . $description); 98 106 unset($name, $description); 107 99 108 $insert->execute($data); 100 109 } … … 106 115 107 116 // 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'); 109 119 } 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; 110 139 } 111 140 … … 118 147 public function onPrivmsg() 119 148 { 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; 127 186 } 128 187 } 188 return false; 129 189 } 130 190 }