Move items from database to Cache_PHP

This commit is contained in:
tobi132 2019-07-16 19:06:51 +02:00
parent fd91d1fb06
commit bc8f24afad
4 changed files with 29 additions and 40 deletions

View File

@ -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'));

View File

@ -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,

View File

@ -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;
}
}
}

3
system/migrations/24.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$db->exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'items`;');