diff --git a/common.php b/common.php index 84a807d8..37a85ec1 100644 --- a/common.php +++ b/common.php @@ -27,7 +27,7 @@ session_start(); define('MYAAC', true); define('MYAAC_VERSION', '0.8-dev'); -define('DATABASE_VERSION', 23); +define('DATABASE_VERSION', 24); define('TABLE_PREFIX', 'myaac_'); define('START_TIME', microtime(true)); define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX')); diff --git a/install/includes/schema.sql b/install/includes/schema.sql index 08339856..390757b5 100644 --- a/install/includes/schema.sql +++ b/install/includes/schema.sql @@ -131,16 +131,6 @@ CREATE TABLE `myaac_hooks` PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; -CREATE TABLE `myaac_items` -( - `id` INT(11) NOT NULL, - `article` VARCHAR(5) NOT NULL DEFAULT '', - `name` VARCHAR(50) NOT NULL DEFAULT '', - `plural` VARCHAR(50) NOT NULL DEFAULT '', - `attributes` VARCHAR(500) NOT NULL DEFAULT '', - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; - CREATE TABLE `myaac_menu` ( `id` INT(11) NOT NULL AUTO_INCREMENT, diff --git a/system/libs/items.php b/system/libs/items.php index 5bf1a645..6820a0fc 100644 --- a/system/libs/items.php +++ b/system/libs/items.php @@ -13,16 +13,10 @@ defined('MYAAC') or die('Direct access not allowed!'); class Items { private static $error = ''; + private static $items; public static function loadFromXML($show = false) { - global $db; - - try { - $db->exec("DELETE FROM `myaac_items`;"); - } catch (PDOException $error) { - } - $file_path = config('data_path') . 'items/items.xml'; if (!file_exists($file_path)) { self::$error = 'Cannot load file ' . $file_path; @@ -32,22 +26,26 @@ class Items $xml = new DOMDocument; $xml->load($file_path); + $items = array(); foreach ($xml->getElementsByTagName('item') as $item) { if ($item->getAttribute('fromid')) { for ($id = $item->getAttribute('fromid'); $id <= $item->getAttribute('toid'); $id++) { - self::parseNode($id, $item, $show); + $tmp = self::parseNode($id, $item, $show); + $items[$tmp['id']] = $tmp['content']; + } + } else { + $tmp = self::parseNode($item->getAttribute('id'), $item, $show); + $items[$tmp['id']] = $tmp['content']; } - } else - self::parseNode($item->getAttribute('id'), $item, $show); - } + require_once LIBS . 'cache_php.php'; + $cache_php = new Cache_PHP(config('cache_prefix'), CACHE); + $cache_php->set('items', $items); return true; } public static function parseNode($id, $node, $show = false) { - global $db; - $name = $node->getAttribute('name'); $article = $node->getAttribute('article'); $plural = $node->getAttribute('plural'); @@ -57,28 +55,26 @@ class Items $attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value'); } - $exist = $db->query('SELECT `id` FROM `' . TABLE_PREFIX . 'items` WHERE `id` = ' . $id); - if($exist->rowCount() > 0) { - if($show) { - warning('Duplicated item with id: ' . $id); - } - } - else { - $db->insert(TABLE_PREFIX . 'items', array('id' => $id, 'article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => json_encode($attributes))); - } + return array('id' => $id, 'content' => array('article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => $attributes)); } public static function getError() { return self::$error; } + public static function loadItems() { + if(self::$items) { + return; + } + + require_once LIBS . 'cache_php.php'; + $cache_php = new Cache_PHP(config('cache_prefix'), CACHE); + self::$items = $cache_php->get('items'); + } + public static function getItem($id) { - global $db; - - $item = $db->select(TABLE_PREFIX . 'items', array('id' => $id)); - $item['attributes'] = json_decode($item['attributes']); - - return $item; + self::loadItems(); + return self::$items[$id]; } public static function getDescription($id, $count = 1) { @@ -143,4 +139,4 @@ class Items } return $s; } -} \ No newline at end of file +} diff --git a/system/migrations/24.php b/system/migrations/24.php new file mode 100644 index 00000000..b7e93819 --- /dev/null +++ b/system/migrations/24.php @@ -0,0 +1,3 @@ +exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'items`;'); \ No newline at end of file