schema.inc.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,058 行 · 第 1/3 页

PHP
1,058
字号
<?php

/**
 * $Id: $
 *
 * Database access utility class
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008 KnowledgeTree Inc.
 * Portions copyright The Jam Warehouse Software (Pty) Limited
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 */

class KTSchemaUtil
{
	/**
	 * Indicates if statements to the database must be performed.
	 *
	 * @var boolean
	 */
	public $persist;

	/**
	 * Primary key definitions.
	 *
	 * @var array
	 */
	private $primaryKeys;

	/**
	 * Foreign key definitions
	 *
	 * @var array
	 */
	private $foreignKeys;

	/**
	 * Index definitions
	 *
	 * @var array
	 */
	private $indexes;

	/**
	 * Schema of database
	 *
	 * @var array
	 */
	private $schema;

	private $primary;


	private function __construct($setup=false)
	{
		$this->persist = true;

		$this->getDBSchema();
		if ($setup)
		{
			$this->setupAdminDatabase();
		}

		$this->definePrimaryKeys();
		$this->defineForeignKeys();
		$this->defineOtherIndexes();
	}

	public function setTablesToInnoDb()
	{
		foreach($this->schema as $tablename=>$schema)
		{
			$schema = strtolower($schema);

			$isInnoDb = (strpos($schema, 'innodb') !== false);
			$hasFulltext = (strpos($schema, 'fulltext') !== false);

			// if the table is innodb already, don't have to do anything
			// only myisam tables can do fulltext

			if (!$isInnoDb && !$hasFulltext)
			{
				$sql = "ALTER TABLE $tablename TYPE=innodb;";
				$this->_exec($sql);
			}
		}
	}


	private function createFixUser()
	{
		$sql = "SELECT 1 FROM users WHERE id = -10;";
		$rs = DBUtil::getResultArray($sql);
		if (PEAR::isError($rs))
		{
			print '';
		}
		if (count($rs) == 0)
		{
			$sql = "INSERT INTO users (id,username,name,password,max_sessions) VALUES (-10,'_deleted_helper','Deleted User','---------------',0)";
			$this->_exec($sql);
		}
	}

	public function setupAdminDatabase()
	{
		global $default;
		$dsn = array(
			'phptype'  => $default->dbType,
			'username' => $default->dbAdminUser,
			'password' => $default->dbAdminPass,
			'hostspec' => $default->dbHost,
			'database' => $default->dbName,
			'port' => $default->dbPort,
		);

		$options = array(
				'debug'       => 2,
				'portability' => DB_PORTABILITY_ERRORS,
				'seqname_format' => 'zseq_%s',
			);

		$default->_admindb = &DB::connect($dsn, $options);
		if (PEAR::isError($default->_admindb))
		{
			die($default->_admindb->toString());
		}
		$default->_admindb->setFetchMode(DB_FETCHMODE_ASSOC);
		return;
	}

	private function removeDuplicateIndexes()
	{
		foreach($this->primary as $table=>$key)
		{
			$this->dropIndex($table,$key);
		}

	}


	/**
	 * Enter description here...
	 *
	 * @return KTSchemaUtil
	 */
	public function getSingleton()
	{
		static $singleton = null;
		if (is_null($singleton))
		{
			$singleton = new KTSchemaUtil();
		}
		return $singleton;
	}

	/**
	 * Adds primary keys to the database
	 *
	 */
	private function definePrimaryKeys()
	{
		$this->primaryKeys = array();
		$this->definePrimaryKey('active_sessions', 'id');
		$this->definePrimaryKey('archive_restoration_request','id');
		$this->definePrimaryKey('archiving_settings','id');
		$this->definePrimaryKey('archiving_type_lookup','id');
		$this->definePrimaryKey('authentication_sources','id');
		$this->definePrimaryKey('baobab_keys','id');
		$this->definePrimaryKey('baobab_user_keys','id');
		$this->definePrimaryKey('column_entries','id');
		$this->definePrimaryKey('comment_searchable_text','comment_id');
		$this->definePrimaryKey('dashlet_disables','id');
		$this->definePrimaryKey('data_types','id');
		$this->definePrimaryKey('discussion_comments','id');
		$this->definePrimaryKey('discussion_threads','id');
		$this->definePrimaryKey('document_archiving_link','id');
		$this->definePrimaryKey('document_content_version','id');
		$this->definePrimaryKey('document_fields','id');
		$this->definePrimaryKey('document_fields_link','id');
		$this->definePrimaryKey('document_incomplete','id');
		$this->definePrimaryKey('document_link','id');
		$this->definePrimaryKey('document_link_types','id');
		$this->definePrimaryKey('document_metadata_version','id');
		$this->definePrimaryKey('document_role_allocations','id');
		$this->definePrimaryKey('document_subscriptions','id');
		$this->definePrimaryKey('document_tags',array('document_id','tag_id'));
		$this->definePrimaryKey('document_text', 'document_id');
		$this->definePrimaryKey('document_transaction_types_lookup', 'id');
		$this->definePrimaryKey('document_transaction_text', 'document_id');
		$this->definePrimaryKey('document_transactions','id');
		$this->definePrimaryKey('document_type_fields_link','id');
		$this->definePrimaryKey('document_type_fieldsets_link','id');
		$this->definePrimaryKey('document_types_lookup','id');
		$this->definePrimaryKey('documents','id');
		$this->definePrimaryKey('download_files',array('document_id','session'));
		$this->definePrimaryKey('field_behaviours','id');
		$this->definePrimaryKey('field_value_instances','id');
		$this->definePrimaryKey('fieldsets','id');
		$this->definePrimaryKey('folder_doctypes_link','id');
		$this->definePrimaryKey('folder_searchable_text','folder_id');
		$this->definePrimaryKey('folder_subscriptions','id');
		$this->definePrimaryKey('folder_transactions','id');
		$this->definePrimaryKey('folder_workflow_map','folder_id');
		$this->definePrimaryKey('folders','id');
		$this->definePrimaryKey('folders_users_roles_link','id');
		$this->definePrimaryKey('groups_groups_link','id');
		$this->definePrimaryKey('groups_lookup','id');
		$this->definePrimaryKey('help','id');
		$this->definePrimaryKey('help_replacement','id');
		$this->definePrimaryKey('index_files','document_id');
		$this->definePrimaryKey('interceptor_instances','id');
		$this->definePrimaryKey('links','id');
		$this->definePrimaryKey('metadata_lookup','id');
		$this->definePrimaryKey('metadata_lookup_tree','id');
		$this->definePrimaryKey('mime_documents','id');
		$this->definePrimaryKey('mime_extractors','id');
		$this->definePrimaryKey('mime_document_mapping',array('mime_type_id','mime_document_id'));
		$this->definePrimaryKey('mime_types','id');
		$this->definePrimaryKey('news','id');
		$this->definePrimaryKey('notifications','id');
		$this->definePrimaryKey('organisations_lookup','id');
		$this->definePrimaryKey('permission_assignments','id');
		$this->definePrimaryKey('permission_descriptor_groups', array('descriptor_id','group_id'));
		$this->definePrimaryKey('permission_descriptor_roles', array('descriptor_id','role_id'));
		$this->definePrimaryKey('permission_descriptor_users', array('descriptor_id','user_id'));
		$this->definePrimaryKey('permission_descriptors','id');
		$this->definePrimaryKey('permission_dynamic_conditions','id');
		$this->definePrimaryKey('permission_lookup_assignments','id');
		$this->definePrimaryKey('permission_lookups','id');
		$this->definePrimaryKey('permission_objects','id');
		$this->definePrimaryKey('permissions','id');
		$this->definePrimaryKey('plugin_rss','id');
		$this->definePrimaryKey('plugins','id');
		$this->definePrimaryKey('plugin_helper','id');
		$this->definePrimaryKey('quicklinks','id');
		$this->definePrimaryKey('role_allocations','id');
		$this->definePrimaryKey('roles','id');
		$this->definePrimaryKey('saved_searches','id');
		$this->definePrimaryKey('scheduler_tasks','id');
		$this->definePrimaryKey('search_ranking',array('groupname','itemname'));
		$this->definePrimaryKey('search_saved','id');
		$this->definePrimaryKey('search_saved_events','document_id');
		$this->definePrimaryKey('status_lookup','id');
		$this->definePrimaryKey('system_settings','id');
		$this->definePrimaryKey('tag_words','id');
		$this->definePrimaryKey('time_period','id');
		$this->definePrimaryKey('time_unit_lookup','id');
		$this->definePrimaryKey('trigger_selection','event_ns');
		$this->definePrimaryKey('type_workflow_map','document_type_id');
		$this->definePrimaryKey('units_lookup','id');
		$this->definePrimaryKey('units_organisations_link','id');
		$this->definePrimaryKey('upgrades','id');
		$this->definePrimaryKey('uploaded_files','tempfilename');
		$this->definePrimaryKey('user_history','id');
		$this->definePrimaryKey('user_history_documents','id');
		$this->definePrimaryKey('user_history_folders','id');
		$this->definePrimaryKey('users','id');
		$this->definePrimaryKey('users_groups_link','id');
		$this->definePrimaryKey('workflow_actions','workflow_id');
		$this->definePrimaryKey('workflow_documents','document_id');
		$this->definePrimaryKey('workflow_state_permission_assignments','id');
		$this->definePrimaryKey('workflow_states','id');
		$this->definePrimaryKey('workflow_state_transitions',array('state_id','transition_id'));
		$this->definePrimaryKey('workflow_transitions','id');
		$this->definePrimaryKey('workflow_trigger_instances','id');
		$this->definePrimaryKey('workflows','id');
	}

	/**
	 * Adds foreign keys to the database
	 *
	 */
	private function defineForeignKeys()
	{
		$this->foreignKeys = array();
		$this->defineForeignKey('active_sessions', 'user_id', 'users', 'id');

		$this->defineForeignKey('archive_restoration_request', 'document_id', 'documents', 'id');
		$this->defineForeignKey('archive_restoration_request', 'request_user_id', 'users', 'id');
		$this->defineForeignKey('archive_restoration_request', 'admin_user_id', 'users', 'id');

		$this->defineForeignKey('archiving_settings', 'archiving_type_id', 'archiving_type_lookup', 'id');
		$this->defineForeignKey('archiving_settings', 'time_period_id', 'time_period', 'id');

		$this->defineForeignKey('baobab_user_keys', 'user_id', 'users', 'id');
		$this->defineForeignKey('baobab_user_keys', 'key_id', 'baobab_keys', 'id');

		$this->defineForeignKey('comment_searchable_text', 'comment_id', 'discussion_comments', 'id');
		$this->defineForeignKey('comment_searchable_text', 'document_id', 'documents', 'id');

		$this->defineForeignKey('dashlet_disables', 'user_id', 'users', 'id');

		$this->defineForeignKey('discussion_comments', 'thread_id', 'discussion_threads', 'id');
		$this->defineForeignKey('discussion_comments', 'user_id', 'users', 'id');
		$this->defineForeignKey('discussion_comments', 'in_reply_to', 'discussion_comments', 'id');

		$this->defineForeignKey('discussion_threads', 'document_id', 'documents', 'id');
		$this->defineForeignKey('discussion_threads', 'first_comment_id', 'discussion_comments', 'id');
		$this->defineForeignKey('discussion_threads', 'last_comment_id', 'discussion_comments', 'id');
		$this->defineForeignKey('discussion_threads', 'creator_id', 'users', 'id');

		$this->defineForeignKey('document_archiving_link', 'document_id', 'documents', 'id');
		$this->defineForeignKey('document_archiving_link', 'archiving_settings_id', 'archiving_settings', 'id');

		$this->defineForeignKey('document_content_version', 'document_id', 'documents', 'id');
		$this->defineForeignKey('document_content_version', 'mime_id', 'mime_types', 'id');

		$this->defineForeignKey('document_fields','parent_fieldset','fieldsets','id');

		$this->defineForeignKey('document_fields_link','document_field_id','document_fields','id');
		$this->defineForeignKey('document_fields_link','metadata_version_id','document_metadata_version','id');

		$this->defineForeignKey('document_link','parent_document_id', 'documents', 'id');
		$this->defineForeignKey('document_link','child_document_id', 'documents', 'id');
		$this->defineForeignKey('document_link','link_type_id','document_link_types','id');

		$this->defineForeignKey('document_metadata_version','document_type_id','document_types_lookup','id');
		$this->defineForeignKey('document_metadata_version','status_id','status_lookup','id');
		$this->defineForeignKey('document_metadata_version','document_id','documents','id');
		$this->defineForeignKey('document_metadata_version','version_creator_id','users','id');
		$this->defineForeignKey('document_metadata_version','content_version_id','document_content_version','id');
		$this->defineForeignKey('document_metadata_version','workflow_id','workflows','id');
		$this->defineForeignKey('document_metadata_version','workflow_state_id','workflow_states','id');

		$this->defineForeignKey('document_role_allocations','role_id','roles','id');

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?