]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Lart.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Lart.php
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_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
20  */
21
22 /**
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.
26  *
27  * @category Phergie
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
33  * @uses     extension PDO
34  * @uses     extension pdo_sqlite
35  */
36 class Phergie_Plugin_Lart extends Phergie_Plugin_Abstract
37 {
38     /**
39      * PDO instance for the database
40      *
41      * @var PDO
42      */
43     protected $db;
44
45     /**
46      * Prepared statement for inserting a new definition
47      *
48      * @var PDOStatement
49      */
50     protected $save;
51
52     /**
53      * Prepared statement for deleting the definition for a given term
54      *
55      * @var PDOStatement
56      */
57     protected $delete;
58
59     /**
60      * Prepared statement for searching for a definition for which the term
61      * matches as a regular expression against a given search string
62      *
63      * @var PDOStatement
64      */
65     protected $process;
66
67     /**
68      * Prepared statement for searching for a definition by its exact term
69      *
70      * @var PDOStatement
71      */
72     protected $select;
73
74     /**
75      * Checks for dependencies and initializes the database.
76      *
77      * @return void
78      */
79     public function onLoad()
80     {
81         if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
82             $this->fail('PDO and pdo_sqlite extensions must be installed');
83         }
84
85         $this->plugins->getPlugin('Command');
86
87         $dir = dirname(__FILE__) . '/' . $this->getName();
88         $path = $dir . '/lart.db';
89         $exists = file_exists($path);
90         if (!$exists) {
91             mkdir($dir);
92         }
93
94         try {
95             $this->db = new PDO('sqlite:' . $path);
96         } catch (PDO_Exception $e) {
97             throw new Phergie_Plugin_Exception($e->getMessage());
98         }
99
100         $this->db->sqliteCreateFunction('preg_match', 'preg_match');
101
102         if (!$exists) {
103             $this->db->exec('
104                 CREATE TABLE lart (
105                     name VARCHAR(255),
106                     definition TEXT,
107                     hostmask VARCHAR(50),
108                     tstamp VARCHAR(19)
109                 )
110             ');
111             $this->db->exec('
112                 CREATE UNIQUE INDEX lart_name ON lart (name)
113             ');
114         }
115
116         $this->save = $this->db->prepare('
117             REPLACE INTO lart (name, definition, hostmask, tstamp)
118             VALUES (:name, :definition, :hostmask, :tstamp)
119         ');
120
121         $this->process = $this->db->prepare('
122             SELECT *
123             FROM lart
124             WHERE preg_match(name, :name)
125         ');
126
127         $this->select = $this->db->prepare('
128             SELECT *
129             FROM lart
130             WHERE name = :name
131         ');
132
133         $this->delete = $this->db->prepare('
134             DELETE FROM lart
135             WHERE name = :name
136         ');
137     }
138
139     /**
140      * Retrieves the definition for a given term if it exists.
141      *
142      * @param string $term Term to search for
143      *
144      * @return mixed String containing the definition or FALSE if no definition
145      *               exists
146      */
147     protected function getLart($term)
148     {
149         $this->process->execute(array(':name' => $term));
150         $row = $this->process->fetchObject();
151         if ($row === false) {
152             return false;
153         }
154         preg_match($row->name, $term, $match);
155         $definition = preg_replace(
156             "/(?:\\\\|\\$)([0-9]+)/e",
157             '$match[\1]',
158             $row->definition
159         );
160         $event = $this->getEvent();
161         $definition = str_replace(
162             array('$source', '$nick'),
163             array($event->getSource(), $event->getNick()),
164             $definition
165         );
166         return $definition;
167     }
168
169     /**
170      * Deletes a given definition.
171      *
172      * @param string $term Term for which the definition should be deleted
173      *
174      * @return boolean TRUE if the definition was found and deleted, FALSE
175      *         otherwise
176      */
177     protected function deleteLart($term)
178     {
179         $this->delete->execute(array(':name' => $term));
180         return ($this->delete->rowCount() > 0);
181     }
182
183     /**
184      * Saves a given definition.
185      *
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
189      *
190      * @return boolean TRUE if the definition was saved successfully, FALSE
191      *         otherwise
192      */
193     protected function saveLart($term, $definition)
194     {
195         $data = array(
196             ':name' => $term,
197             ':definition' => $definition,
198             ':hostmask' => (string) $this->getEvent()->getHostmask(),
199             ':tstamp' => time()
200         );
201         $this->save->execute($data);
202         return ($this->save->rowCount() > 0);
203     }
204
205     /**
206      * Returns information about a definition.
207      *
208      * @param string $term Term about which to return information
209      *
210      * @return void
211      */
212     public function onCommandLartinfo($term)
213     {
214         $this->select->execute(array(':name' => $term));
215         $row = $this->select->fetchObject();
216         $msg = $this->getEvent()->getNick() . ': ';
217         if (!$row) {
218             $msg .= 'Lart not found';
219         } else {
220             $msg .= 'Term: ' . $row->name
221                 . ', Definition: ' . $row->definition
222                 . ', User: ' . $row->hostmask
223                 . ', Added: ' . date('n/j/y g:i A', $row->tstamp);
224         }
225         $this->doNotice($this->getEvent()->getSource(), $msg);
226     }
227
228     /**
229      * Creates a new definition.
230      *
231      * @param string $term       Term to add
232      * @param string $definition Definition to add
233      *
234      * @return void
235      */
236     public function onCommandAddlart($term, $definition)
237     {
238         $result = $this->saveLart($term, $definition);
239         if ($result) {
240             $msg = 'Lart saved successfully';
241         } else {
242             $msg = 'Lart could not be saved';
243         }
244         $this->doNotice($this->getEvent()->getSource(), $msg);
245     }
246
247     /**
248      * Removes an existing definition.
249      *
250      * @param string $term Term for which the definition should be removed
251      *
252      * @return void
253      */
254     public function onCommandDeletelart($term)
255     {
256         $source = $this->getEvent()->getSource();
257         if ($this->deleteLart($term)) {
258             $msg = 'Lart deleted successfully';
259         } else {
260             $msg = 'Lart not found';
261         }
262         $this->doNotice($source, $msg);
263     }
264
265     /**
266      * Processes definition triggers in the text of the current event.
267      *
268      * @return void
269      */
270     protected function processLart()
271     {
272         $lart = $this->getLart($this->getEvent()->getText());
273         if ($lart) {
274             if (strpos($lart, '/me') === 0) {
275                 $lart = substr($lart, 4);
276                 $method = 'doAction';
277             } else {
278                 $method = 'doPrivmsg';
279             }
280             $this->$method($this->getEvent()->getSource(), $lart);
281         }
282     }
283
284     /**
285      * Processes definition triggers in messages.
286      *
287      * @return void
288      */
289     public function onPrivmsg()
290     {
291         $this->processLart();
292     }
293
294     /**
295      * Processes definition triggers in CTCP actions.
296      *
297      * @return void
298      */
299     public function onAction()
300     {
301         $this->processLart();
302     }
303 }