#
# WIKINDX v2.x - createMySQL.txt
# Creates (dropping pre-existing) tables in the named database
#

# --------------------------------------------------------

#
# Table structure for table `WKX_tag`
# Label given by admin to a BibTeX import allowing 
# easy administration later on.
# `id` references `WKX_resource_misc` tag field
#

DROP TABLE IF EXISTS `WKX_tag`;
CREATE TABLE `WKX_tag` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `tag` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_category`
# List of available categories.
# Categories are the main categories that resources are organized into
# as opposed to the more focussed 'keywords'
#

DROP TABLE IF EXISTS `WKX_category`;
CREATE TABLE `WKX_category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `category` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);
# Populate with catch-all 'General' category
INSERT INTO `WKX_category` (`category`) VALUES('General');

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_category`
# `id` is `resource` table's `id`
# `categories` is comma-separated list of id numbers from category's table (easily
# used with SQL's FIND_IN_SET() function)
#

DROP TABLE IF EXISTS `WKX_resource_category`;
CREATE TABLE `WKX_resource_category` (
  `id` int(10) unsigned NOT NULL,
  `categories` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_keyword`
# List of already stored keywords.
# Each resource can also be further categorized by 
# keyword(s).
#

DROP TABLE IF EXISTS `WKX_keyword`;
CREATE TABLE `WKX_keyword` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `keyword` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_keyword`
# `id` is `resource` table's `id`
# `keywords` is comma-separated list of id numbers from keyword's table (easily
# used with SQL's FIND_IN_SET() function)
#

DROP TABLE IF EXISTS `WKX_resource_keyword`;
CREATE TABLE `WKX_resource_keyword` (
  `id` int(10) unsigned NOT NULL,
  `keywords` text default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_creator`
# `id` is referenced in the comma-separated fields 
# `creator1` to `creator5` in the WKX_resource_creator` table.
#

DROP TABLE IF EXISTS `WKX_creator`;
CREATE TABLE `WKX_creator` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `surname` varchar(255) default NULL,
  `firstname` varchar(255) default NULL,
  `initials` varchar(255) default NULL,
  `prefix` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_collection`
# `id` is referenced as `collection` field in the `resource_misc` table.
# Resources may be part of a collection, for example, a journal 
# article is part of a journal, a book chapter is part of a book. Here, 
# 'journal' and 'book' are the `collectionType` and the title of the journal 
# or book is the `collectionTitle`.

DROP TABLE IF EXISTS `WKX_collection`;
CREATE TABLE `WKX_collection` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `collectionTitle` varchar(255) default NULL,
  `collectionTitleShort` varchar(255) default NULL,
  `collectionType` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_publisher`
# `id` is referenced as `publisher` in the `resource` table
#

DROP TABLE IF EXISTS `WKX_publisher`;
CREATE TABLE `WKX_publisher` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `publisherName` varchar(255) default NULL,
  `publisherLocation` varchar(255) default NULL,
  `publisherType` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_musing`
# `resourceId` references `resource` tables's `id`.
# A musing is a user's thoughts and comments on the 
# resource generally rather than comments on specific 
# quotes or paraphrases.

DROP TABLE IF EXISTS `WKX_resource_musing`;
CREATE TABLE `WKX_resource_musing` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `resourceId` int(10) unsigned default NULL,
  `page_start` VARCHAR(10) default NULL,
  `page_end` VARCHAR(10) default NULL,
  `musing_keywords` text default NULL,
  PRIMARY KEY  (`id`),
  KEY `resourceIdKey` (`resourceId`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_musing_text`
# `id` references `resource_musing` table's `id` number.
# `addUserIdMusing` references `users` tables's `id`
#

DROP TABLE IF EXISTS `WKX_resource_musing_text`;
CREATE TABLE `WKX_resource_musing_text` (
  `id` int(10) unsigned NOT NULL,
  `addUserIdMusing` int(10) unsigned default NULL,
  `text` text NOT NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_note`
# General note about the resource.
# `id` references `resource` table's `id`.
# `editUserIdxxxx` and `addUserIdxxxx` reference `users` table's `id`

DROP TABLE IF EXISTS `WKX_resource_note`;
CREATE TABLE `WKX_resource_note` (
  `id` int(10) unsigned  NOT NULL,
  `editUserIdNote` int(10) unsigned default NULL,
  `addUserIdNote` int(10) unsigned default NULL,
  `text` text,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_abstract`
# Abstract of the resource.
# `id` references `resource` table's `id`.
# `editUserIdAbstract` and `addUserIdAbstract` reference `users` table's `id`

DROP TABLE IF EXISTS `WKX_resource_abstract`;
CREATE TABLE `WKX_resource_abstract` (
  `id` int(10) unsigned NOT NULL,
  `editUserIdAbstract` int(10) unsigned default NULL,
  `addUserIdAbstract` int(10) unsigned default NULL,
  `abstract` text default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_quote`
# Quotes and user comments about a resource.
# `resourceId` references `resource` table's `id`

DROP TABLE IF EXISTS `WKX_resource_quote`;
CREATE TABLE `WKX_resource_quote` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `resourceId` int(10) unsigned default NULL,
  `page_start` VARCHAR(10) default NULL,
  `page_end` VARCHAR(10) default NULL,
  `quote_keywords` text default NULL,
  PRIMARY KEY  (`id`),
  KEY `resourceIdKey` (`resourceId`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_quote_text`
# `id` references `resource_quote` table's `id` number
# `addUserIdQuote` reference `users` table's `id`

DROP TABLE IF EXISTS `WKX_resource_quote_text`;
CREATE TABLE `WKX_resource_quote_text` (
  `id` int(10) unsigned NOT NULL,
  `addUserIdQuote` int(10) unsigned default NULL,
  `text` text default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_quote_comment`
# There can be many comments for each quote but only one from each user.
# `quoteId` references `resource_quote` table's `id` number.
# `addUserIdQuote` reference `users` table's `id`.

DROP TABLE IF EXISTS `WKX_resource_quote_comment`;
CREATE TABLE `WKX_resource_quote_comment` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `quoteId` int(10) unsigned NOT NULL,
  `addUserIdQuote` int(10) unsigned default NULL,
  `comment` text default NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_paraphrase`
# paraphrase differs to quotes (direct quotations) and musings (resource inputter's own thoughts)
# in that a paraphrase is not the resource inputter's own thoughts but neither a direct quotation, 
# being a re-wording of the creator's thoughts.
# Allows for a logical separation so it's always clear whose thoughts are whose.
# `resourceId` references `resource` table's `id`.

DROP TABLE IF EXISTS `WKX_resource_paraphrase`;
CREATE TABLE `WKX_resource_paraphrase` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `resourceId` int(10) unsigned default NULL,
  `page_start` VARCHAR(10) default NULL,
  `page_end` VARCHAR(10) default NULL,
  `paraphrase_keywords` text default NULL,
  PRIMARY KEY  (`id`),
  KEY `resourceIdKey` (`resourceId`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_paraphrase_text`
# `id` references `WKX_resource_paraphrase` table's `id` number.
# `addUserIdxxxx` reference `users` table's `id`
#

DROP TABLE IF EXISTS `WKX_resource_paraphrase_text`;
CREATE TABLE `WKX_resource_paraphrase_text` (
  `id` int(10) unsigned NOT NULL,
  `addUserIdParaphrase` int(10) unsigned default NULL,
  `text` text default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_paraphrase_comment`
# There can be many comments for each quote but only one from each user.
# `paraphraseId` references `resource_paraphrase` table's `id` number.
# `addUserIdxxxx` reference `users` table's `id`

DROP TABLE IF EXISTS `WKX_resource_paraphrase_comment`;
CREATE TABLE `WKX_resource_paraphrase_comment` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `paraphraseId` int(10) unsigned NOT NULL,
  `addUserIdParaphrase` int(10) unsigned default NULL,
  `comment` text default NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_database_summary`
# Stores summary data for entire database for quick access
# Incremented/decremented on inputting/deleting resources.

DROP TABLE IF EXISTS `WKX_database_summary`;
CREATE TABLE `WKX_database_summary` (
  `totalResources` int(10) unsigned NOT NULL,
  `totalQuotes` int(10) unsigned default NULL,
  `totalParaphrases` int(10) unsigned default NULL,
  `totalMusings` int(10) unsigned default NULL,
/* version number of db is compared with hard-wired version number forcing WIKINDX to */
/* load the relevant admin pages to convert the database as necessary. */
  `dbVersion` varchar(10) default NULL
);
# Initial population of table:
INSERT INTO `WKX_database_summary` (`totalResources`, `totalQuotes`, `totalParaphrases`, `totalMusings`) VALUES('0', '0', '0', '0');

#
#---------------------------------------------------------
# RESOURCE's TABLES
#---------------------------------------------------------
#

# --------------------------------------------------------

#
# Table structure for table `WKX_resource`

DROP TABLE IF EXISTS `WKX_resource`;
CREATE TABLE `WKX_resource` (
  `id` int(10) unsigned NOT NULL auto_increment,
/* book, journal article, web article, music, film, proceedings etc. */
  `type` varchar(255) default NULL,
  `title` varchar(255) default NULL,
  `subtitle` varchar(255) default NULL,
/* For fields such as thesis type, newspaper section, conference organiser et. */
  `field1` varchar(255) default NULL,
  `field2` varchar(255) default NULL,
  `field3` varchar(255) default NULL,
  `field4` varchar(255) default NULL,
  `field5` varchar(255) default NULL,
  `field6` varchar(255) default NULL,
  `field7` varchar(255) default NULL,
  `field8` varchar(255) default NULL,
  `field9` varchar(255) default NULL,
/* splits 'the, an, a, une, der, die' etc. from title so sorting title is better */
  `noSort` VARCHAR(255) default NULL,
  `isbn` VARCHAR(255) default NULL, 
  `url` VARCHAR(255) default NULL,
/* File attachment */
  `file` VARCHAR(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_misc`
# `id` references `resource` table's `id`
# Stuff that doesn't easily fit anywhere else but is of the same data type (bumped up to INT(10))

DROP TABLE IF EXISTS `WKX_resource_misc`;
CREATE TABLE `WKX_resource_misc` (
  `id` int(10) unsigned NOT NULL,
/* `collection` references `id` in `collection` table */
  `collection` int(10) unsigned default NULL,
/* `publisher` references `id` in `publisher` table */
  `publisher` int(10) unsigned default NULL,
/* `month`, `day`, volumeNumber, issueNumber etc. */
  `miscField1` int(10) unsigned default NULL,
  `miscField2` int(10) unsigned default NULL,
  `miscField3` int(10) unsigned default NULL,
  `miscField4` int(10) unsigned default NULL,
/* tag ID for BibTeX imports - see WKX_tag */
  `tag` int(10) unsigned default NULL,
/* Who added or lasted edited this resource? */
  `editUserIdResource` int(10) unsigned default NULL,
  `addUserIdResource` int(10) unsigned default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_year`
# `id` references `resource` table's `id`
# Stored as VARCHAR(30) to allow for range of years such as 1999-1980 BC

DROP TABLE IF EXISTS `WKX_resource_year`;
CREATE TABLE `WKX_resource_year` (
  `id` int(10) unsigned NOT NULL,
  `year1` varchar(30) default NULL,
  `year2` varchar(30) default NULL,
  `year3` varchar(30) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_page`
# `id` references `resource` table's `id`.
# page start and end stored as VARCHAR to allow for roman numeral paging etc. in prefaces.

DROP TABLE IF EXISTS `WKX_resource_page`;
CREATE TABLE `WKX_resource_page` (
  `id` int(10) unsigned NOT NULL,
  `pageStart` varchar(10) default NULL,
  `pageEnd` varchar(10) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_summary`
# Summary of metadata for each resource.
# `id` references `resource` table's `id`.

DROP TABLE IF EXISTS `WKX_resource_summary`;
CREATE TABLE `WKX_resource_summary` (
  `id` int(10) unsigned NOT NULL,
  `quotes` int(10) unsigned default NULL,
  `paraphrases` int(10) unsigned default NULL,
  `musings` int(10) unsigned default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_timestamp`
# resource creation/edit timestamp.
# `id` references `resource` table's `id`.

DROP TABLE IF EXISTS `WKX_resource_timestamp`;
CREATE TABLE `WKX_resource_timestamp` (
  `id` int(10) unsigned NOT NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_resource_creator`
# Authors, editors, translators, directors, artists, musical group etc.
# `id` references `resource` table's `id`.
# other fields are comma-separated list of ID numbers found in `creator` table.
# NB SQL's FIND_IN_SET() function.

DROP TABLE IF EXISTS `WKX_resource_creator`;
CREATE TABLE `WKX_resource_creator` (
  `id` int(10) unsigned NOT NULL,
/* primary creators */
  `creator1` varchar(255) default NULL,
  `creator2` varchar(255) default NULL,
  `creator3` varchar(255) default NULL,
  `creator4` varchar(255) default NULL,
  `creator5` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

#
# Table structure for table `WKX_config`
# Only the WIKINDX admin has access to this and it must
# be initialised the very first time wikindx is run.

DROP TABLE IF EXISTS `WKX_config`;
CREATE TABLE `WKX_config` (
  `title` varchar(255) default "WIKINDX",
  `contactEmail` varchar(255) default NULL,
  `description` text default NULL,
  `fileDeleteSeconds` int(10) unsigned default 3600,
/* paging should be signed as '-1' is valid (meaning no paging) */
  `paging` int(10) default '20',
  `pagingMaxLinks` int(10) unsigned default '11',
/* stringLimit should be signed as '-1' is valid (meaning no limiting) */
  `stringLimit` int(10) default '40',
  `language` varchar(2) default 'en',
  `style` varchar(255) default 'APA',
  `template` varchar(255) default 'default',
/* multi user mode */
  `multiUser` enum('N', 'Y') default 'N',
/* Allow users to register themselves? */
  `userRegistration` enum('N', 'Y') default 'N',
/* Allow email notification? */
  `notify` enum('Y', 'N') default 'Y',
/* BBCode image display limits in pixels */
  `imgWidthLimit` int(10) default '400',
  `imgHeightLimit` int(10) default '400',
/* Allow for file attachments to resources */
  `fileAttach` enum('N', 'Y') default 'N',
  `fileViewLoggedOnOnly` enum('N', 'Y') default 'N',
/* Maximum number of (bibTeX) entries a user can paste at any one time */
  `maxPaste` int(10) default '10'
);

# --------------------------------------------------------

# Table structure for table `WKX_import_raw`
# Stores extra data from imports that WIKINDX cannot handle 
# and that will be added to exports of the same type (importType) later so that the original 
# file can be reasonably well reconstituted.
# `id` references `WKX_resource.id`
# `stringId` references `WKX_bibtex_string.id`
# `importType` is 'bibtex', 'endnote' etc.

DROP TABLE IF EXISTS `WKX_import_raw`;
CREATE TABLE IF NOT EXISTS `WKX_import_raw` (
  `id` int(10) unsigned  NOT NULL,
  `stringId` int(10) unsigned default NULL,
  `text` text NOT NULL,
  `importType` VARCHAR(255) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_bibtex_string`
# Stores biBtEx @string.
# `id` references `WKX_resource.id`

DROP TABLE IF EXISTS `WKX_bibtex_string`;
CREATE TABLE IF NOT EXISTS `WKX_bibtex_string` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `text` text NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_users`

DROP TABLE IF EXISTS `WKX_users`;
CREATE TABLE IF NOT EXISTS `WKX_users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `fullname` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  `admin` enum('N', 'Y') default 'N',
/* remember this user? */
  `cookie` enum('N', 'Y') default 'N',
/* paging should be signed as '-1' is valid (meaning no paging) */
  `paging` int(10) default '20',
  `pagingMaxLinks` int(10) unsigned default '11',
/* stringLimit should be signed as '-1' is valid (meaning no limiting) */
  `stringLimit` int(10) default '40',
  `language` varchar(2) default 'en',
  `style` varchar(255) default 'CHICAGO',
  `template` varchar(255) default 'default',
/* Email notification on resource additions/edits. */
/* 'N' = never, 'A' = on all resources, 'M' = only for resources from my bibliographies */
  `notify` enum('N', 'A', 'M') default 'N',
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_user_register`
# Temporary storage of request for user registrations.

DROP TABLE IF EXISTS `WKX_user_register`;
CREATE TABLE IF NOT EXISTS `WKX_user_register` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `hashKey` varchar(255) NOT NULL,
  `email` varchar(255) default NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_user_bibliography`
# value of `userId` references `id` in `users` table

DROP TABLE IF EXISTS `WKX_user_bibliography`;
CREATE TABLE `WKX_user_bibliography` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `userId` int(10) unsigned NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` TEXT default NULL,
/* `bibliography` is a comma-separated list of `id` numbers from `WKX_resource` table */
  `bibliography` TEXT default NULL,
  PRIMARY KEY  (`id`),
  KEY `userIdKey` (`userId`)
);

# --------------------------------------------------------

# Table structure for table `WKX_news`
# Admin-added news items.

DROP TABLE IF EXISTS `WKX_news`;
CREATE TABLE `WKX_news` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `news` text default NULL,
  `timestamp` timestamp(14) NOT NULL,
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_custom`
# Admin-added fields.
# `id` references `resource` table's `id`.

DROP TABLE IF EXISTS `WKX_custom`;
CREATE TABLE `WKX_custom` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `custom_label` varchar(255) default NULL,
  `custom_size` enum('S', 'L') default 'S',
  PRIMARY KEY  (`id`)
);

# --------------------------------------------------------

# Table structure for table `WKX_resource_custom`
# Resource data using custom fields.
# `resourceId` references `resource` table's `id`.
# `customId` references `custom` table's `id`.

DROP TABLE IF EXISTS `WKX_resource_custom`;
CREATE TABLE `WKX_resource_custom` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `customId` int(10) unsigned NOT NULL,
  `resourceId` int(10) unsigned NOT NULL,
  `custom_short` varchar(255) default NULL,
  `custom_long` text default NULL,
  `addUserIdCustom` int(10) unsigned default NULL,
  `editUserIdCustom` int(10) unsigned default NULL,
  PRIMARY KEY  (`id`)
);
