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_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
23 * Processes requests to serve a user something from a database.
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
31 * @uses extension pdo_sqlite
33 class Phergie_Plugin_Serve extends Phergie_Plugin_Abstract
36 * Checks for dependencies.
40 public function onLoad()
42 if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
43 $this->fail('PDO and pdo_sqlite extensions must be installed');
48 * Retrieves a random item from the database table.
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
54 * @return object Retrieved item
56 protected function getItem($database, $table, array $request)
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();
66 $item->name = $request['suggestion'];
70 $query = 'SELECT * FROM ' . $table . ' ORDER BY RANDOM() LIMIT 1';
71 $stmt = $db->query($query);
72 $item = $stmt->fetchObject();
78 * Processes a request to serve a user something.
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
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,
90 * @return boolean TRUE if the request was processed successfully, FALSE
93 public function serve($database, $table, $format, $request, $censor = false)
97 '/(?P<target>[^\s]+)(\s+an?\s+)?(?P<suggestion>.*)?/',
106 // Resolve the target
107 $target = $match['target'];
108 if ($target == 'me') {
109 $target = $this->event->getNick();
112 // Process the request
113 $item = $this->getItem($database, $table, $match);
115 // Reprocess the request for censorship if required
116 if ($this->plugins->hasPlugin('Censor')) {
117 $plugin = $this->plugins->getPlugin('Censor');
119 while ($censor && $attempts < 3) {
120 $clean = $plugin->cleanString($item->name);
121 if ($item->name != $clean) {
123 $item = $this->getItem($database, $table, $match);
128 if ($censor && $attempts == 3) {
129 $this->doAction($this->event->getSource(), 'shrugs.');
133 // Derive the proper article for the item
134 if (preg_match('/^[aeiou]/i', $item->name)) {
140 // Format the message
141 $replacements = array(
143 'item' => $item->name,
144 'link' => $item->link,
145 'article' => $article
149 foreach ($replacements as $placeholder => $value) {
151 '%' . $placeholder . '%',
158 $this->doAction($this->event->getSource(), $msg);