mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-26 17:29:21 +02:00
Move items from database to Cache_PHP
This commit is contained in:
parent
fd91d1fb06
commit
bc8f24afad
@ -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'));
|
||||
|
@ -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,
|
||||
|
@ -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
3
system/migrations/24.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
$db->exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'items`;');
|
Loading…
x
Reference in New Issue
Block a user