From ba554e322ac1afb0d34a1ac5dc4d6f151fd49d0e Mon Sep 17 00:00:00 2001 From: Savagedlight Date: Tue, 31 Dec 2013 16:45:44 +0100 Subject: [PATCH 1/2] ITEMS_MODULE: Added optional CIDB support Now supports querying the CIDB by v1.1 of the specifications. Now has a setting, 'items_database', which supports the following options: - local: Use local database as before. - central: Use CIDB via http://cidb.botsharp.net/ - Anything else: Assumed to be an URL to some CIDB. --- .../ITEMS_MODULE/ItemsController.class.php | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/modules/ITEMS_MODULE/ItemsController.class.php b/modules/ITEMS_MODULE/ItemsController.class.php index 39798d314..3fbf84731 100644 --- a/modules/ITEMS_MODULE/ItemsController.class.php +++ b/modules/ITEMS_MODULE/ItemsController.class.php @@ -63,10 +63,18 @@ class ItemsController { * @Options("30;40;50;60") */ public $defaultMaxitems = "40"; - + /** @Setup */ public function setup() { + /* TODO: Only load this if using local database. + * This will require some hook for when the setting + * '$database' is altered, so that it can be loaded + * when changing from remote to local database. + */ $this->db->loadSQLFile($this->moduleName, "aodb"); + + // Initialize settings - Demoder + $this->settingManager->add($this->moduleName, "items_database", "Use database", "edit", "text", 'local', 'local;central'); } /** @@ -91,7 +99,7 @@ public function itemsCommand($message, $channel, $sender, $sendto, $args) { } $search = htmlspecialchars_decode($search); - $msg = $this->find_items_from_local($search, $ql); + $msg = $this->find_items($search, $ql); $sendto->reply($msg); } @@ -199,6 +207,47 @@ public function download_newest_itemsdb() { return $msg; } + public function find_items($search, $ql) { + $db = $this->settingManager->get('items_database'); + echo "Database set to: " . $db . "\r\n"; + switch($db) { + case 'central': + return $this->find_items_from_remote($search, $ql, "http://cidb.botsharp.net/"); + case 'local': + return $this->find_items_from_local($search, $ql); + default: + return $this->find_items_from_remote($search, $ql, $db); + } + } + + public function find_items_from_remote($search, $ql, $server) { + + $icons=true; + /*$color_header = 'DFDF00'; + $color_highlight = '97BE37'; + $color_normal = 'CCF0AD';*/ + $max = $this->defaultMaxItems; + + $parameters = array( + "bot=BudaBot", + "output=aoml", + "max=".$max, + "search=".urlencode($search), + "icons=".$icons, + "color_header=".$color_header, + "color_highlight=".$color_highlight, + "color_normal=".$color_normal); + if ($ql>0) { + $parameters[]="ql=".$ql; + } + $url=$server . '?' . implode('&', $parameters); + $msg = file_get_contents($url); + if (empty($msg)) { + $msg = "Unable to query Central Items Database."; + } + return $msg; + } + public function find_items_from_local($search, $ql) { $tmp = explode(" ", $search); list($query, $params) = $this->util->generateQueryFromParams($tmp, 'name'); From 6f69cfc7d8bcd703223470c9ed11d8d38b6062c8 Mon Sep 17 00:00:00 2001 From: Savagedlight Date: Tue, 31 Dec 2013 16:55:38 +0100 Subject: [PATCH 2/2] ITEMS_MODULE: Cleaned up previous commit Removed unnecessary comments and commented-out code which were introduced in the previous commit. --- .../ITEMS_MODULE/ItemsController.class.php | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/modules/ITEMS_MODULE/ItemsController.class.php b/modules/ITEMS_MODULE/ItemsController.class.php index 3fbf84731..24d33c319 100644 --- a/modules/ITEMS_MODULE/ItemsController.class.php +++ b/modules/ITEMS_MODULE/ItemsController.class.php @@ -66,11 +66,6 @@ class ItemsController { /** @Setup */ public function setup() { - /* TODO: Only load this if using local database. - * This will require some hook for when the setting - * '$database' is altered, so that it can be loaded - * when changing from remote to local database. - */ $this->db->loadSQLFile($this->moduleName, "aodb"); // Initialize settings - Demoder @@ -208,38 +203,41 @@ public function download_newest_itemsdb() { } public function find_items($search, $ql) { + // Figure out which database to query - Demoder $db = $this->settingManager->get('items_database'); echo "Database set to: " . $db . "\r\n"; switch($db) { - case 'central': - return $this->find_items_from_remote($search, $ql, "http://cidb.botsharp.net/"); case 'local': + // Local database return $this->find_items_from_local($search, $ql); + case 'central': + // Default CIDB + return $this->find_items_from_remote($search, $ql, "http://cidb.botsharp.net/"); default: + // Specified CIDB return $this->find_items_from_remote($search, $ql, $db); } } - public function find_items_from_remote($search, $ql, $server) { - + /* + * Method to query the Central Items Database - Demoder + */ + public function find_items_from_remote($search, $ql, $server) { $icons=true; - /*$color_header = 'DFDF00'; - $color_highlight = '97BE37'; - $color_normal = 'CCF0AD';*/ $max = $this->defaultMaxItems; - + // Store parameters as an array, for easy assembly later. $parameters = array( + // Should always specify which bot software is querying. "bot=BudaBot", "output=aoml", "max=".$max, "search=".urlencode($search), - "icons=".$icons, - "color_header=".$color_header, - "color_highlight=".$color_highlight, - "color_normal=".$color_normal); + "icons=".$icons); + // Don't include QL in the query unless the user specified it. if ($ql>0) { $parameters[]="ql=".$ql; - } + } + // Assemble query URL and retrieve results. $url=$server . '?' . implode('&', $parameters); $msg = file_get_contents($url); if (empty($msg)) {