<?php
	/**
	* Base class for upgrade checks
	* @package upgrade
	*/
	class UPGRADE
	{

		/**
		* @var $error The last error message
		*/
		var $error = '';

		/**
		* IsAlreadyUpgraded stub function
		*
		* @return boolean returns false if we need to run the upgrade function
		*/
		function IsAlreadyUpgraded()
		{
		}

		/**
		* Upgrade stub function
		*
		* @return void
		*/
		function Upgrade()
		{
		}

		/**
		* Check if a database table exists
		*
		* @param string $table The table name to check
		*
		* @return boolean If the table exists return true
		*/
		function TableExists($table)
		{
			$query = "SHOW TABLES LIKE '".$table."'";
			$result = $GLOBALS['AKB_DB']->FetchOne($query);
			// If there is a table with this name already we don't need to run this
			// part of the upgrade script

			// We have to convert the name to lower case before comparing since
			// if MySQL is on windows the tables are converted to lowercase
			// when they are returned from our sql statement
			if (strtolower($result) == strtolower($table)) {
				return true;
			} else {
				return false;
			}
		}

		/**
		* Check if a column exists in a table
		*
		* @param $table The table with the column we are checking for
		* @param $column The column to check for
		*
		* @return boolean If the column exists in the table return true
		*/
		function ColumnExists($table, $column)
		{
			$query = 'SHOW COLUMNS FROM '.$table." LIKE '".$column."'";

			$result = $GLOBALS['AKB_DB']->Query($query);
			$row = $GLOBALS['AKB_DB']->Fetch($result);

			if ($row['Field'] == $column) {
				return true;
			} else {
				return false;
			}
		}

		/**
		* GetIndexInfo
		*
		* Get information about the indexes on $table and return an array with
		* this information
		*
		* @param string $table The name of the table to get information about the indexes for
		*
		* @return array The array containing the index information in the format
			$indexes = array (
				'indexname' => array (
					'unique' => true/false,
					'type' => btree/fulltext,
					'columns' = array (
						'field1',
						'field2',
						'field3',
					),
				),
				'index2name' => array (
					...
				),
			)
		*/
		function GetIndexInfo($table)
		{
			$indexes = array();

			$query = 'SHOW INDEX FROM '.$table;
			$result = mysql_query($query);
			if ($result === false) {
				debug(mysql_error());
			}
			while ($row = mysql_fetch_assoc($result)) {
				if (!isset($indexes[$row['Key_name']])) {
					if (!isset($row['Index_type'])) {
						$row['Index_type'] = 'UNDEFINED';
					}

					$indexes[$row['Key_name']] = array (
						'unique' => ($row['Non_unique'] == 0),
						'type' => $row['Index_type'],
					);

					if (!isset($indexes[$row['Key_name']]['columns'])
					|| !is_array($indexes[$row['Key_name']]['columns'])) {
						$indexes[$row['Key_name']]['columns'] = array();
					}
				}

				$indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
			}
			return $indexes;
		}

		/**
		* IndexExists
		*
		* Check if an index exists on some table columns
		*
		* @param string $table The name of the table
		* @param array $columns The array of column names the index is on. Order counts.
		* @param bool $unique Is the index a unique index ?
		* @param string $type The type of index to check for (BTREE or FULLTEXT)
		*
		* @return bool Does the index exist as expected or not ?
		*/
		function IndexExists($table, $columns, $unique=false, $type='BTREE')
		{
			$keymatches = array();
			$indexname = '';

			if (empty($table)) {
				$this->error = 'Table name is empty';
				return false;
			}

			if (empty($columns)) {
				$this->error = 'Columns is empty';
				return false;
			}

			if (!is_array($columns)) {
				$this->error = 'Columns is not an array';
				return false;
			}

			if (!in_array($type, array('BTREE', 'FULLTEXT', 'UNDEFINED'))) {
				$this->error = 'Type of index was not an expected type';
				return false;
			}

			$indexes = $this->GetIndexInfo($table);

			if (empty($indexes)) {
				$this->error = 'Could not get index information';
				return false;
			}

			foreach ($indexes as $name => $index) {
				// Since MySQL can use the first part of the array, lets check
				// to see if the required index is already part of another index
				$slice = array_slice($index['columns'], 0, count($columns));

				// Check if the index is one we can use
				if (($index['type'] == $type || $index['type'] == 'UNDEFINED')
				&& $index['unique'] == $unique
				&& $slice == $columns) {
					return true;
				}
			}
			return false;
		}

	}
?>
