]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Serve.php
Revert "Merged in Phergie changes"
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Serve.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_Serve
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_Serve
20  */
21
22 /**
23  * Processes requests to serve a user something from a database.
24  *
25  * @category Phergie
26  * @package  Phergie_Plugin_Serve
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie_Plugin_Serve
30  * @uses     extension pdo
31  * @uses     extension pdo_sqlite
32  */
33 class Phergie_Plugin_Serve extends Phergie_Plugin_Abstract
34 {
35     /**
36      * Checks for dependencies.
37      *
38      * @return void
39      */
40     public function onLoad()
41     {
42         if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
43             $this->fail('PDO and pdo_sqlite extensions must be installed');
44         }
45     }
46
47     /**
48      * Retrieves a random item from the database table.
49      *
50      * @param string $database Path to the SQLite database file
51      * @param string $table    Name of the database table
52      * @param array  $request  Parsed request
53      *
54      * @return object Retrieved item
55      */
56     protected function getItem($database, $table, array $request)
57     {
58         $db = new PDO('sqlite:' . $database);
59         if (!empty($request['suggestion'])) {
60             $query = 'SELECT * FROM ' . $table . ' WHERE name LIKE ? ORDER BY RANDOM() LIMIT 1';
61             $stmt = $db->prepare($query);
62             $stmt->execute(array('%' . $request['suggestion'] . '%'));
63             $item = $stmt->fetchObject();
64             if (!$item) {
65                 $item = new stdClass;
66                 $item->name = $request['suggestion'];
67                 $item->link = null;
68             }
69         } else {
70             $query = 'SELECT * FROM ' . $table . ' ORDER BY RANDOM() LIMIT 1';
71             $stmt = $db->query($query);
72             $item = $stmt->fetchObject();
73         }
74         return $item;
75     }
76
77     /**
78      * Processes a request to serve a user something.
79      *
80      * @param string $database Path to the SQLite database file
81      * @param string $table    Name of the database table
82      * @param string $format   Format of the response where %target%,
83      *        %item%, %article%', and %link will be replaced with their
84      *        respective data
85      * @param string $request  Request string including the target and an
86      *        optional suggestion of the item to fetch
87      * @param boolean $censor  TRUE to integrate with the Censor plugin,
88      *        defaults to FALSE
89      *
90      * @return boolean TRUE if the request was processed successfully, FALSE
91      *         otherwise
92      */
93     public function serve($database, $table, $format, $request, $censor = false)
94     {
95         // Parse the request
96         $result = preg_match(
97             '/(?P<target>[^\s]+)(\s+an?\s+)?(?P<suggestion>.*)?/',
98             $request,
99             $match
100         );
101
102         if (!$result) {
103             return false;
104         }
105
106         // Resolve the target
107         $target = $match['target'];
108         if ($target == 'me') {
109             $target = $this->event->getNick();
110         }
111
112         // Process the request
113         $item = $this->getItem($database, $table, $match);
114
115         // Reprocess the request for censorship if required
116         $attempts = 0;
117         while ($censor && $attempts < 3) {
118             $plugin = $this->plugins->getPlugin('Censor');
119             $clean = $plugin->cleanString($item->name);
120             if ($item->name != $clean) {
121                 $attempts++;
122                 $item = $this->getItem($database, $table, $match);
123             } else {
124                 $censor = false;
125             }
126         }
127
128         if ($censor && $attempts == 3) {
129             $this->doAction($this->event->getSource(), 'shrugs.');
130         }
131
132         // Derive the proper article for the item
133         if (preg_match('/^[aeiou]/i', $item->name)) {
134             $article = 'an';
135         } else {
136             $article = 'a';
137         }
138
139         // Format the message
140         $replacements = array(
141             'target' => $target,
142             'item' => $item->name,
143             'link' => $item->link,
144             'article' => $article
145         );
146
147         $msg = $format;
148         foreach ($replacements as $placeholder => $value) {
149             $msg = str_replace(
150                 '%' . $placeholder . '%',
151                 $value,
152                 $msg
153             );
154         }
155
156         // Send the message
157         $this->doAction($this->event->getSource(), $msg);
158     }
159 }