mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-27 01:39:22 +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', true);
|
||||||
define('MYAAC_VERSION', '0.8-dev');
|
define('MYAAC_VERSION', '0.8-dev');
|
||||||
define('DATABASE_VERSION', 23);
|
define('DATABASE_VERSION', 24);
|
||||||
define('TABLE_PREFIX', 'myaac_');
|
define('TABLE_PREFIX', 'myaac_');
|
||||||
define('START_TIME', microtime(true));
|
define('START_TIME', microtime(true));
|
||||||
define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX'));
|
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`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
|
) 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`
|
CREATE TABLE `myaac_menu`
|
||||||
(
|
(
|
||||||
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
@ -13,16 +13,10 @@ defined('MYAAC') or die('Direct access not allowed!');
|
|||||||
class Items
|
class Items
|
||||||
{
|
{
|
||||||
private static $error = '';
|
private static $error = '';
|
||||||
|
private static $items;
|
||||||
|
|
||||||
public static function loadFromXML($show = false)
|
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';
|
$file_path = config('data_path') . 'items/items.xml';
|
||||||
if (!file_exists($file_path)) {
|
if (!file_exists($file_path)) {
|
||||||
self::$error = 'Cannot load file ' . $file_path;
|
self::$error = 'Cannot load file ' . $file_path;
|
||||||
@ -32,22 +26,26 @@ class Items
|
|||||||
$xml = new DOMDocument;
|
$xml = new DOMDocument;
|
||||||
$xml->load($file_path);
|
$xml->load($file_path);
|
||||||
|
|
||||||
|
$items = array();
|
||||||
foreach ($xml->getElementsByTagName('item') as $item) {
|
foreach ($xml->getElementsByTagName('item') as $item) {
|
||||||
if ($item->getAttribute('fromid')) {
|
if ($item->getAttribute('fromid')) {
|
||||||
for ($id = $item->getAttribute('fromid'); $id <= $item->getAttribute('toid'); $id++) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parseNode($id, $node, $show = false) {
|
public static function parseNode($id, $node, $show = false) {
|
||||||
global $db;
|
|
||||||
|
|
||||||
$name = $node->getAttribute('name');
|
$name = $node->getAttribute('name');
|
||||||
$article = $node->getAttribute('article');
|
$article = $node->getAttribute('article');
|
||||||
$plural = $node->getAttribute('plural');
|
$plural = $node->getAttribute('plural');
|
||||||
@ -57,28 +55,26 @@ class Items
|
|||||||
$attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value');
|
$attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value');
|
||||||
}
|
}
|
||||||
|
|
||||||
$exist = $db->query('SELECT `id` FROM `' . TABLE_PREFIX . 'items` WHERE `id` = ' . $id);
|
return array('id' => $id, 'content' => array('article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => $attributes));
|
||||||
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)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getError() {
|
public static function getError() {
|
||||||
return self::$error;
|
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) {
|
public static function getItem($id) {
|
||||||
global $db;
|
self::loadItems();
|
||||||
|
return self::$items[$id];
|
||||||
$item = $db->select(TABLE_PREFIX . 'items', array('id' => $id));
|
|
||||||
$item['attributes'] = json_decode($item['attributes']);
|
|
||||||
|
|
||||||
return $item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDescription($id, $count = 1) {
|
public static function getDescription($id, $count = 1) {
|
||||||
|
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