From 160230943592591fe105efb85255284f6ff48a97 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 19 May 2012 13:40:15 +0200 Subject: ArticleExtradata class and other changes to models.php * ArticleExtradata class added. This class gives plugins the possibility to assign additional data to articles. * Abstract class KVStorage added. ArticleExtradata and PluginKVStorage extend from it. * Added dbversion function, it returns the version of the database model currently in use. The version is stored in the PREFIX_meta table. If there is no PREFIX_meta table, dbversion returns version 0. This will be useful for updating the system. * New MySQL tables. --- ratatoeskr/setup/create_tables.php | 49 ++++++---- ratatoeskr/sys/models.php | 180 +++++++++++++++++++++++++------------ 2 files changed, 154 insertions(+), 75 deletions(-) diff --git a/ratatoeskr/setup/create_tables.php b/ratatoeskr/setup/create_tables.php index 819970a..97a4cd5 100644 --- a/ratatoeskr/setup/create_tables.php +++ b/ratatoeskr/setup/create_tables.php @@ -20,12 +20,12 @@ CREATE TABLE IF NOT EXISTS `PREFIX_articles` ( `timestamp` bigint(20) NOT NULL, `allow_comments` int(11) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_article_tag_relations` ( `tag` int(11) NOT NULL, `article` int(11) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -38,30 +38,30 @@ CREATE TABLE IF NOT EXISTS `PREFIX_comments` ( `visible` tinyint(4) NOT NULL, `read_by_admin` tinyint(4) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_group_members` ( `user` int(11) NOT NULL, `group` int(11) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text COLLATE utf8_unicode_ci NOT NULL, `file` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_multilingual` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_plugins` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -82,13 +82,13 @@ CREATE TABLE IF NOT EXISTS `PREFIX_plugins` ( `update` tinyint(4) NOT NULL, `api` int(11) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_plugin_kvstorage` ( `plugin` int(11) NOT NULL, `key` text COLLATE utf8_unicode_ci NOT NULL, `value` text COLLATE utf8_unicode_ci NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_repositories` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -98,7 +98,7 @@ CREATE TABLE IF NOT EXISTS `PREFIX_repositories` ( `pkgcache` text COLLATE utf8_unicode_ci NOT NULL, `lastrefresh` bigint(20) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_sections` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -107,38 +107,38 @@ CREATE TABLE IF NOT EXISTS `PREFIX_sections` ( `template` text COLLATE utf8_unicode_ci NOT NULL, `styles` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_section_style_relations` ( `section` int(11) NOT NULL, `style` int(11) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_settings_kvstorage` ( `key` text COLLATE utf8_unicode_ci NOT NULL, `value` text COLLATE utf8_unicode_ci NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_styles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text COLLATE utf8_unicode_ci NOT NULL, `code` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text COLLATE utf8_unicode_ci NOT NULL, `title` int(11) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_translations` ( `multilingual` int(11) NOT NULL, `language` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `text` text COLLATE utf8_unicode_ci NOT NULL, `texttype` text COLLATE utf8_unicode_ci NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `PREFIX_users` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -148,7 +148,20 @@ CREATE TABLE IF NOT EXISTS `PREFIX_users` ( `fullname` text COLLATE utf8_unicode_ci NOT NULL, `language` varchar(10) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +CREATE TABLE IF NOT EXISTS `PREFIX_meta` ( + `key` text COLLATE utf8_unicode_ci NOT NULL, + `value` text COLLATE utf8_unicode_ci NOT NULL +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + + +CREATE TABLE IF NOT EXISTS `PREFIX_article_extradata` ( + `article` int(11) NOT NULL, + `plugin` int(11) NOT NULL, + `key` text COLLATE utf8_unicode_ci NOT NULL, + `value` text COLLATE utf8_unicode_ci NOT NULL +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; SQL; function create_mysql_tables() @@ -161,6 +174,8 @@ function create_mysql_tables() if(!empty($q)) qdb($q); } + + qdb("INSERT INTO `PREFIX_meta` (`key`, `value`) VALUES ('dbversion', '%s')", base64_encode(serialize(1))); } ?> diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index fe80ce4..bc48908 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -88,6 +88,79 @@ abstract class BySQLRowEnabled } } +abstract class KVStorage implements Countable, ArrayAccess, Iterator +{ + private $keybuffer; + private $counter; + private $prepared_queries; + + final protected function init($sqltable, $common_fields) + { + $this->keybuffer = array(); + + $selector = "WHERE " . (empty($common_fields) ? 1 : implode(" AND ", array_map(function($x) { return qdb_fmt("`{$x[0]}` = {$x[1]}", $x[2]); }, $common_fields))); + $this->prepared_queries = array( + "get" => "SELECT `value` FROM `$sqltable` $selector AND `key` = '%s'", + "unset" => "DELETE FROM `$sqltable` $selector AND `key` = '%s'", + "update" => "UPDATE `$sqltable` SET `value` = '%s' $selector AND `key` = '%s'", + "create" => "INSERT INTO `$sqltable` (`key`, `value` " + . (empty($common_fields) ?: ", " . implode(", ", array_map(function($x) { return "`".$x[0]."`"; }, $common_fields))) + . ") VALUES ('%s', '%s'" + . (empty($common_fields) ?: ", " . implode(", ", array_map(function($x) { return qdb_fmt($x[1], $x[2]); }, $common_fields))) + . ")" + ); + + $result = qdb("SELECT `key` FROM `$sqltable` $selector"); + while($sqlrow = mysql_fetch_assoc($result)) + $this->keybuffer[] = $sqlrow["key"]; + + $this->counter = 0; + } + + /* Countable interface implementation */ + final public function count() { return count($this->keybuffer); } + + /* ArrayAccess interface implementation */ + final public function offsetExists($offset) { return in_array($offset, $this->keybuffer); } + final public function offsetGet($offset) + { + if($this->offsetExists($offset)) + { + $result = qdb($this->prepared_queries["get"], $offset); + $sqlrow = mysql_fetch_assoc($result); + return unserialize(base64_decode($sqlrow["value"])); + } + else + throw new DoesNotExistError(); + } + final public function offsetUnset($offset) + { + if($this->offsetExists($offset)) + { + unset($this->keybuffer[array_search($offset, $this->keybuffer)]); + $this->keybuffer = array_merge($this->keybuffer); + qdb($this->prepared_queries["unset"], $offset); + } + } + final public function offsetSet($offset, $value) + { + if($this->offsetExists($offset)) + qdb($this->prepared_queries["update"], base64_encode(serialize($value)), $offset); + else + { + qdb($this->prepared_queries["create"], $offset, base64_encode(serialize($value))); + $this->keybuffer[] = $offset; + } + } + + /* Iterator interface implementation */ + final public function rewind() { return $this->counter = 0; } + final public function current() { return $this->offsetGet($this->keybuffer[$this->counter]); } + final public function key() { return $this->keybuffer[$this->counter]; } + final public function next() { ++$this->counter; } + final public function valid() { return isset($this->keybuffer[$this->counter]); } +} + /* * Class: User * Data model for Users @@ -772,12 +845,8 @@ $ratatoeskr_settings = Settings::get_instance(); * Can be accessed like an array. * Keys are strings and Values can be everything serialize() can process. */ -class PluginKVStorage implements Countable, ArrayAccess, Iterator +class PluginKVStorage extends KVStorage { - private $plugin_id; - private $keybuffer; - private $counter; - /* * Constructor: __construct * @@ -786,60 +855,10 @@ class PluginKVStorage implements Countable, ArrayAccess, Iterator */ public function __construct($plugin_id) { - $this->keybuffer = array(); - $this->plugin_id = $plugin_id; - - $result = qdb("SELECT `key` FROM `PREFIX_plugin_kvstorage` WHERE `plugin` = %d", $plugin_id); - while($sqlrow = mysql_fetch_assoc($result)) - $this->keybuffer[] = $sqlrow["key"]; - - $this->counter = 0; - } - - /* Countable interface implementation */ - public function count() { return count($this->keybuffer); } - - /* ArrayAccess interface implementation */ - public function offsetExists($offset) { return in_array($offset, $this->keybuffer); } - public function offsetGet($offset) - { - if($this->offsetExists($offset)) - { - $result = qdb("SELECT `value` FROM `PREFIX_plugin_kvstorage` WHERE `key` = '%s' AND `plugin` = %d", $offset, $this->plugin_id); - $sqlrow = mysql_fetch_assoc($result); - return unserialize(base64_decode($sqlrow["value"])); - } - else - throw new DoesNotExistError(); - } - public function offsetUnset($offset) - { - if($this->offsetExists($offset)) - { - unset($this->keybuffer[array_search($offset, $this->keybuffer)]); - $this->keybuffer = array_merge($this->keybuffer); - qdb("DELETE FROM `PREFIX_plugin_kvstorage` WHERE `key` = '%s' AND `plugin` = %d", $offset, $this->plugin_id); - } - } - public function offsetSet($offset, $value) - { - if($this->offsetExists($offset)) - qdb("UPDATE `PREFIX_plugin_kvstorage` SET `value` = '%s' WHERE `key` = '%s' AND `plugin` = %d", - base64_encode(serialize($value)), $offset, $this->plugin_id); - else - { - qdb("INSERT INTO `PREFIX_plugin_kvstorage` (`plugin`, `key`, `value`) VALUES (%d, '%s', '%s')", - $this->plugin_id, $offset, base64_encode(serialize($value))); - $this->keybuffer[] = $offset; - } + $this->init("PREFIX_plugin_kvstorage", array( + array("plugin", "%d", $plugin_id) + )); } - - /* Iterator interface implementation */ - function rewind() { return $this->position = 0; } - function current() { return $this->offsetGet($this->keybuffer[$this->position]); } - function key() { return $this->keybuffer[$this->position]; } - function next() { ++$this->position; } - function valid() { return isset($this->keybuffer[$this->position]); } } /* @@ -1372,6 +1391,7 @@ class Plugin extends BySQLRowEnabled { qdb("DELETE FROM `PREFIX_plugins` WHERE `id` = %d", $this->id); qdb("DELETE FROM `PREFIX_plugin_kvstorage` WHERE `plugin` = %d", $this->id); + qdb("DELETE FROM `PREFIX_article_extradata` WHERE `plugin` = %d", $this->id); if(is_dir(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/private/" . $this->id)) delete_directory(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/private/" . $this->id); if(is_dir(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/public/" . $this->id)) @@ -2604,10 +2624,54 @@ WHERE " . implode(" AND ", $subqueries) . " $sorting"); $comment->delete(); qdb("DELETE FROM `PREFIX_article_tag_relations` WHERE `article` = %d", $this->id); + qdb("DELETE FROM `PREFIX_article_extradata` WHERE `article` = %d", $this->id); qdb("DELETE FROM `PREFIX_articles` WHERE `id` = %d", $this->id); } } +/* + * Class: ArticleExtradata + * A Key-Value-Storage assigned to Articles for plugins to store additional data. + * Can be accessed like an array. + * Keys are strings and Values can be everything serialize() can process. + */ +class ArticleExtradata extends KVStorage +{ + /* + * Constructor: __construct + * + * Parameters: + * $article_id - The ID of the Article. + * $plugin_id - The ID of the Plugin. + */ + public function __construct($article_id, $plugin_id) + { + $this->init("PREFIX_article_extradata", array( + array("article", "%d", $article_id), + array("plugin", "%d", $plugin_id) + )); + } +} + +/* + * Function: dbversion + * Get the version of the database structure currently used. + * + * Returns: + * The numerical version of the current database structure. + */ +function dbversion() +{ + /* Is the meta table present? If no, the version is 0. */ + $result = qdb("SHOW TABLES LIKE 'PREFIX_meta'"); + if(mysql_num_rows($result) == 0) + return 0; + + $result = qdb("SELECT `value` FROM `PREFIX_meta` WHERE `key` = 'dbversion'"); + $sqlrow = mysql_fetch_assoc($result); + return unserialize(base64_decode($sqlrow["value"])); +} + /* * Function: clean_database * Clean up the database -- cgit v1.2.3-54-g00ecf