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
15 * @package Phergie_Plugin_Lart
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_Plugin_Lart
23 * Accepts terms and corresponding definitions for storage to a local data
24 * source and performs and returns the result of lookups for term definitions
25 * as they are requested.
28 * @package Phergie_Plugin_Lart
29 * @author Phergie Development Team <team@phergie.org>
30 * @license http://phergie.org/license New BSD License
31 * @link http://pear.phergie.org/package/Phergie_Plugin_Lart
32 * @uses Phergie_Plugin_Command pear.phergie.org
34 * @uses extension pdo_sqlite
36 class Phergie_Plugin_Lart extends Phergie_Plugin_Abstract
39 * PDO instance for the database
46 * Prepared statement for inserting a new definition
53 * Prepared statement for deleting the definition for a given term
60 * Prepared statement for searching for a definition for which the term
61 * matches as a regular expression against a given search string
68 * Prepared statement for searching for a definition by its exact term
75 * Checks for dependencies and initializes the database.
79 public function onLoad()
81 if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
82 $this->fail('PDO and pdo_sqlite extensions must be installed');
85 $this->plugins->getPlugin('Command');
87 $dir = dirname(__FILE__) . '/' . $this->getName();
88 $path = $dir . '/lart.db';
89 $exists = file_exists($path);
95 $this->db = new PDO('sqlite:' . $path);
96 } catch (PDO_Exception $e) {
97 throw new Phergie_Plugin_Exception($e->getMessage());
100 $this->db->sqliteCreateFunction('preg_match', 'preg_match');
107 hostmask VARCHAR(50),
112 CREATE UNIQUE INDEX lart_name ON lart (name)
116 $this->save = $this->db->prepare('
117 REPLACE INTO lart (name, definition, hostmask, tstamp)
118 VALUES (:name, :definition, :hostmask, :tstamp)
121 $this->process = $this->db->prepare('
124 WHERE preg_match(name, :name)
127 $this->select = $this->db->prepare('
133 $this->delete = $this->db->prepare('
140 * Retrieves the definition for a given term if it exists.
142 * @param string $term Term to search for
144 * @return mixed String containing the definition or FALSE if no definition
147 protected function getLart($term)
149 $this->process->execute(array(':name' => $term));
150 $row = $this->process->fetchObject();
151 if ($row === false) {
154 preg_match($row->name, $term, $match);
155 $definition = preg_replace(
156 "/(?:\\\\|\\$)([0-9]+)/e",
160 $event = $this->getEvent();
161 $definition = str_replace(
162 array('$source', '$nick'),
163 array($event->getSource(), $event->getNick()),
170 * Deletes a given definition.
172 * @param string $term Term for which the definition should be deleted
174 * @return boolean TRUE if the definition was found and deleted, FALSE
177 protected function deleteLart($term)
179 $this->delete->execute(array(':name' => $term));
180 return ($this->delete->rowCount() > 0);
184 * Saves a given definition.
186 * @param string $term Term to trigger a response containing the
187 * corresponding definition, may be a regular expression
188 * @param string $definition Definition corresponding to the term
190 * @return boolean TRUE if the definition was saved successfully, FALSE
193 protected function saveLart($term, $definition)
197 ':definition' => $definition,
198 ':hostmask' => (string) $this->getEvent()->getHostmask(),
201 $this->save->execute($data);
202 return ($this->save->rowCount() > 0);
206 * Returns information about a definition.
208 * @param string $term Term about which to return information
212 public function onCommandLartinfo($term)
214 $this->select->execute(array(':name' => $term));
215 $row = $this->select->fetchObject();
216 $msg = $this->getEvent()->getNick() . ': ';
218 $msg .= 'Lart not found';
220 $msg .= 'Term: ' . $row->name
221 . ', Definition: ' . $row->definition
222 . ', User: ' . $row->hostmask
223 . ', Added: ' . date('n/j/y g:i A', $row->tstamp);
225 $this->doNotice($this->getEvent()->getSource(), $msg);
229 * Creates a new definition.
231 * @param string $term Term to add
232 * @param string $definition Definition to add
236 public function onCommandAddlart($term, $definition)
238 $result = $this->saveLart($term, $definition);
240 $msg = 'Lart saved successfully';
242 $msg = 'Lart could not be saved';
244 $this->doNotice($this->getEvent()->getSource(), $msg);
248 * Removes an existing definition.
250 * @param string $term Term for which the definition should be removed
254 public function onCommandDeletelart($term)
256 $source = $this->getEvent()->getSource();
257 if ($this->deleteLart($term)) {
258 $msg = 'Lart deleted successfully';
260 $msg = 'Lart not found';
262 $this->doNotice($source, $msg);
266 * Processes definition triggers in the text of the current event.
270 protected function processLart()
272 $lart = $this->getLart($this->getEvent()->getText());
274 if (strpos($lart, '/me') === 0) {
275 $lart = substr($lart, 4);
276 $method = 'doAction';
278 $method = 'doPrivmsg';
280 $this->$method($this->getEvent()->getSource(), $lart);
285 * Processes definition triggers in messages.
289 public function onPrivmsg()
291 $this->processLart();
295 * Processes definition triggers in CTCP actions.
299 public function onAction()
301 $this->processLart();