⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adodb-xmlschema.inc.php

📁 下载系统 # 汉化:Fising # 邮箱:fising@163.com # 网址:http://www.fising.cn # 声明:本人水平有限
💻 PHP
📖 第 1 页 / 共 4 页
字号:
		}
		
		$xsl_file = dirname( __FILE__ ) . '/xsl/' . $xsl . '.xsl';
		
		// look for xsl
		if( !is_readable( $xsl_file ) ) {
			return FALSE;
		}
		
		switch( $schematype )
		{
			case 'file':
				if( !is_readable( $schema ) ) {
					return FALSE;
				}
				
				$schema = implode('', file( $schema ));
				break;
			case 'string':
			default:
				if( !is_string( $schema ) ) {
					return FALSE;
				}
		}
		
		$arguments = array (
			'/_xml' => $schema,
			'/_xsl' => implode('', file(  $xsl_file ))
		);
		
		// create an XSLT processor
		$xh = xslt_create ();
		
		// set error handler
		xslt_set_error_handler ($xh, array (&$this, 'xslt_error_handler'));
		
		// process the schema
		$result = xslt_process ($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments); 
		
		xslt_free ($xh);
		
		return $result;
	}
	
	/**
	* Processes XSLT transformation errors
	*
	* @param object $parser XML parser object
	* @param integer $errno Error number
	* @param integer $level Error level
	* @param array $fields Error information fields
	*
	* @access private
	*/
	function xslt_error_handler( $parser, $errno, $level, $fields ) {
		if( is_array( $fields ) ) {
			$msg = array(
				'Message Type' => ucfirst( $fields['msgtype'] ),
				'Message Code' => $fields['code'],
				'Message' => $fields['msg'],
				'Error Number' => $errno,
				'Level' => $level
			);
			
			switch( $fields['URI'] ) {
				case 'arg:/_xml':
					$msg['Input'] = 'XML';
					break;
				case 'arg:/_xsl':
					$msg['Input'] = 'XSL';
					break;
				default:
					$msg['Input'] = $fields['URI'];
			}
			
			$msg['Line'] = $fields['line'];
		} else {
			$msg = array(
				'Message Type' => 'Error',
				'Error Number' => $errno,
				'Level' => $level,
				'Fields' => var_export( $fields, TRUE )
			);
		}
		
		$error_details = $msg['Message Type'] . ' in XSLT Transformation' . "\n"
					   . '<table>' . "\n";
		
		foreach( $msg as $label => $details ) {
			$error_details .= '<tr><td><b>' . $label . ': </b></td><td>' . htmlentities( $details ) . '</td></tr>' . "\n";
		}
		
		$error_details .= '</table>';
		
		trigger_error( $error_details, E_USER_ERROR );
	}
	
	/**
	* Returns the AXMLS Schema Version of the requested XML schema file.
	*
	* Call this method to obtain the AXMLS DTD version of the requested XML schema file.
	* @see SchemaStringVersion()
	*
	* @param string $filename AXMLS schema file
	* @return string Schema version number or FALSE on error
	*/
	function SchemaFileVersion( $filename ) {
		// Open the file
		if( !($fp = fopen( $filename, 'r' )) ) {
			// die( 'Unable to open file' );
			return FALSE;
		}
		
		// Process the file
		while( $data = fread( $fp, 4096 ) ) {
			if( preg_match( $this->versionRegex, $data, $matches ) ) {
				return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
			}
		}
		
		return FALSE;
	}
	
	/**
	* Returns the AXMLS Schema Version of the provided XML schema string.
	*
	* Call this method to obtain the AXMLS DTD version of the provided XML schema string.
	* @see SchemaFileVersion()
	*
	* @param string $xmlstring XML schema string
	* @return string Schema version number or FALSE on error
	*/
	function SchemaStringVersion( $xmlstring ) {
		if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
			return FALSE;
		}
		
		if( preg_match( $this->versionRegex, $xmlstring, $matches ) ) {
			return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
		}
		
		return FALSE;
	}
	
	/**
	* Extracts an XML schema from an existing database.
	*
	* Call this method to create an XML schema string from an existing database.
	* If the data parameter is set to TRUE, AXMLS will include the data from the database
	* in the schema. 
	*
	* @param boolean $data Include data in schema dump
	* @return string Generated XML schema
	*/
	function ExtractSchema( $data = FALSE, $sep_tables = FALSE ) {
		$old_mode = $this->db->SetFetchMode( ADODB_FETCH_NUM );
		
		if($sep_tables)
		{
			$xml_table_array = array();
			$schema_header = '<?xml version="1.0"?>' . "\n"
					. '<schema version="' . $this->schemaVersion . '">' . "\n";
			$schema_footer .= '</schema>';
		}
		
		$schema = '<?xml version="1.0"?>' . "\n"
				. '<schema version="' . $this->schemaVersion . '">' . "\n";
		
		if( is_array( $tables = $this->dict->MetaTables( 'TABLES' ) ) ) {
			foreach( $tables as $table ) {
				$schema_temp = '	<table name="' . $table . '">' . "\n";
				
				// grab details from database
				$rs = $this->db->Execute( 'SELECT * FROM ' . $table . ' WHERE 1=1' );
				$fields = $this->dict->MetaColumns( $table );
				$indexes = $this->dict->MetaIndexes( $table );
				
				if( is_array( $fields ) ) {
					foreach( $fields as $details ) {
						$extra = '';
						$content = array();
						
						if( $details->max_length > 0 ) {
							$extra .= ' size="' . $details->max_length . '"';
						}
						
						if( $details->primary_key ) {
							$content[] = '<KEY/>';
						} elseif( $details->not_null ) {
							$content[] = '<NOTNULL/>';
						}
						
						if( $details->has_default ) {
							$content[] = '<DEFAULT value="' . $details->default_value . '"/>';
						}
						
						if( $details->auto_increment ) {
							$content[] = '<AUTOINCREMENT/>';
						}
						
						// this stops the creation of 'R' columns,
						// AUTOINCREMENT is used to create auto columns
						$details->primary_key = 0;
						//$type = $rs->MetaType( $details );
						$type = $this->dict->MetaType($details);
						
						$schema_temp .= '		<field name="' . $details->name . '" type="' . $type . '"' . $extra . '>';
						
						if( !empty( $content ) ) {
							$schema_temp .= "\n			" . implode( "\n			", $content ) . "\n		";
						}
						
						$schema_temp .= '</field>' . "\n";
					}
				}
				
				if( is_array( $indexes ) ) {
					foreach( $indexes as $index => $details ) {
						$schema_temp .= '		<index name="' . $index . '">' . "\n";
						
						if( $details['unique'] ) {
							$schema_temp .= '			<UNIQUE/>' . "\n";
						}
						
						foreach( $details['columns'] as $column ) {
							$schema_temp .= '			<col>' . $column . '</col>' . "\n";
						}
						
						$schema_temp .= '		</index>' . "\n";
					}
				}
				
				if( $data ) {
					$rs = $this->db->Execute( 'SELECT * FROM ' . $table );
					
					if( is_object( $rs ) ) {
						$schema_temp .= '		<data>' . "\n";
						
						while(!$rs->EOF)
						{
							$row = $rs->fields;
							
							foreach( $row as $key => $val )
							{
								$row[$key] = htmlentities($val);
							}
							
							$schema_temp .= '			<row><f>' . implode( '</f><f>', $row ) . '</f></row>' . "\n";
							$rs->MoveNext();
						}
						
						$schema_temp .= '		</data>' . "\n";
					}
				}
				
				$schema_temp .= '	</table>' . "\n";
				$schema .= $schema_temp;
				$xml_table_array[] = $schema_header . $schema_temp . $schema_footer;
			}
		}
		
		$this->db->SetFetchMode( $old_mode );
		
		$schema .= '</schema>';
		if($sep_tables)
		{
			return $xml_table_array;
		}
		else
		{
			return $schema;
		}
	}
	

	function ExtractTableSchema($tables, $data = FALSE )
	{
		$schema = '<?xml version="1.0"?>' . "\n"
				. '<schema version="' . $this->schemaVersion . '">' . "\n";

		if(is_array($tables))
		{
			foreach($tables as $table)
			{
				$schema .= $this->_ExtractTableSchema($table,$data);
			}
		}
		else
		{
			$schema .= $this->_ExtractTableSchema($tables,$data);
		}

		$schema .= '</schema>';
		return $schema;

	}
	function _ExtractTableSchema($table, $data = FALSE )
	{
		$old_mode = $this->db->SetFetchMode( ADODB_FETCH_NUM );
		$schema = "";

				$schema .= '	<table name="' . $table . '">' . "\n";

				// grab details from database
				$rs = $this->db->Execute( 'SELECT * FROM ' . $table . ' WHERE 1=1' );
				$fields = $this->dict->MetaColumns( $table );
				$indexes = $this->dict->MetaIndexes( $table );

				if( is_array( $fields ) ) {
					foreach( $fields as $details ) {
						$extra = '';
						$content = array();

						if( $details->max_length > 0 ) {
							$extra .= ' size="' . $details->max_length . '"';
						}

						if( $details->primary_key ) {
							$content[] = '<KEY/>';
						} elseif( $details->not_null ) {
							$content[] = '<NOTNULL/>';
						}

						if( $details->has_default ) {
							$content[] = '<DEFAULT value="' . $details->default_value . '"/>';
						}

						if( $details->auto_increment ) {
							$content[] = '<AUTOINCREMENT/>';
						}

						// this stops the creation of 'R' columns,
						// AUTOINCREMENT is used to create auto columns
						$details->primary_key = 0;
						//$type = $rs->MetaType( $details );
						$type = $this->dict->MetaType($details);

						$schema .= '		<field name="' . $details->name . '" type="' . $type . '"' . $extra . '>';

						if( !empty( $content ) ) {
							$schema .= "\n			" . implode( "\n			", $content ) . "\n		";
						}

						$schema .= '</field>' . "\n";
					}
				}

				if( is_array( $indexes ) ) {
					foreach( $indexes as $index => $details ) {
						$schema .= '		<index name="' . $index . '">' . "\n";

						if( $details['unique'] ) {
							$schema .= '			<UNIQUE/>' . "\n";
						}

						foreach( $details['columns'] as $column ) {
							$schema .= '			<col>' . $column . '</col>' . "\n";
						}

						$schema .= '		</index>' . "\n";
					}
				}

				if( $data ) {
					$rs = $this->db->Execute( 'SELECT * FROM ' . $table );

					if( is_object( $rs ) ) {
						$schema .= '		<data>' . "\n";

						while(!$rs->EOF)
						{
							$row = $rs->fields;

							foreach( $row as $key => $val )
							{
								$row[$key] = htmlentities($val);
							}

							$schema .= '			<row><f>' . implode( '</f><f>', $row ) . '</f></row>' . "\n";
							$rs->MoveNext();
						}

						$schema .= '		</data>' . "\n";
					}
				}

				$schema .= '	</table>' . "\n";



		$this->db->SetFetchMode( $old_mode );
		return $schema;
	}

	/**
	* Sets a prefix for database objects
	*
	* Call this method to set a standard prefix that will be prepended to all database tables 
	* and indices when the schema is parsed. Calling setPrefix with no arguments clears the prefix.
	*
	* @param string $prefix Prefix that will be prepended.
	* @param boolean $underscore If TRUE, automatically append an underscore character to the prefix.
	* @return boolean TRUE if successful, else FALSE
	*/
	function SetPrefix( $prefix = '', $underscore = TRUE ) {
		switch( TRUE ) {
			// clear prefix
			case empty( $prefix ):
				logMsg( 'Cleared prefix' );
				$this->objectPrefix = '';
				return TRUE;
			// prefix too long
			case strlen( $prefix ) > XMLS_PREFIX_MAXLEN:
			// prefix contains invalid characters
			case !preg_match( '/^[a-z][a-z0-9_]+$/i', $prefix ):
				logMsg( 'Invalid prefix: ' . $prefix );
				return FALSE;
		}
		
		if( $underscore AND substr( $prefix, -1 ) != '_' ) {
			$prefix .= '_';
		}
		
		// prefix valid
		logMsg( 'Set prefix: ' . $prefix );
		$this->objectPrefix = $prefix;
		return TRUE;
	}
	
	/**
	* Returns an object name with the current prefix prepended.
	*
	* @param string	$name Name
	* @return string	Prefixed name
	*
	* @access private
	*/
	function prefix( $name = '' ) {
		// if prefix is set
		if( !empty( $this->objectPrefix ) ) {
			// Prepend the object prefix to the table name
			// prepend after quote if used
			return preg_replace( '/^(`?)(.+)$/', '$1' . $this->objectPrefix . '$2', $name );
		}
		
		// No prefix set. Use name provided.
		return $name;
	}
	
	/**
	* Checks if element references a specific platform
	*
	* @param string $platform Requested platform
	* @returns boolean TRUE if platform check succeeds
	*
	* @access private
	*/
	function supportedPlatform( $platform = NULL ) {
		$regex = '/^(\w*\|)*' . $this->db->dbtype . '(\|\w*)*$/';
		
		if( !isset( $platform ) OR preg_match( $regex, $platform ) ) {
			logMsg( "Platform $platform is supported" );
			return TRUE;
		} else {
			logMsg( "Platform $platform is NOT supported" );
			return FALSE;
		}
	}
	
	/**
	* Clears the array of generated SQL.
	*
	* @access private
	*/
	function clearSQL() {
		$this->sqlArray = array();
	}
	
	/**
	* Adds SQL into the SQL array.
	*
	* @param mixed $sql SQL to Add
	* @return boolean TRUE if successful, else FALSE.
	*
	* @access private
	*/	
	function addSQL( $sql = NULL ) {
		if( is_array( $sql ) ) {
			foreach( $sql as $line ) {
				$this->addSQL( $line );
			}
			
			return TRUE;
		}
		
		if( is_string( $sql ) ) {
			$this->sqlArray[] = $sql;
			
			// if executeInline is enabled, and either no errors have occurred or continueOnError is enabled, execute SQL.
			if( $this->ExecuteInline() && ( $this->success == 2 || $this->ContinueOnError() ) ) {
				$saved = $this->db->debug;
				$this->db->debug = $this->debug;
				$ok = $this->db->Execute( $sql );
				$this->db->debug = $saved;
				
				if( !$ok ) {
					if( $this->debug ) {
						ADOConnection::outp( $this->db->ErrorMsg() );
					}
					
					$this->success = 1;
				}
			}
			
			return TRUE;
		}
		
		return FALSE;
	}
	
	/**
	* Gets the SQL array in the specified format.
	*
	* @param string $format Format
	* @return mixed SQL
	*	
	* @access private
	*/
	function getSQL( $format = NULL, $sqlArray = NULL ) {
		if( !is_array( $sqlArray ) ) {
			$sqlArray = $this->sqlArray;
		}
		
		if( !is_array( $sqlArray ) ) {
			return FALSE;
		}
		
		switch( strtolower( $format ) ) {
			case 'string':
			case 'text':
				return !empty( $sqlArray ) ? implode( ";\n\n", $sqlArray ) . ';' : '';
			case'html':
				return !empty( $sqlArray ) ? nl2br( htmlentities( implode( ";\n\n", $sqlArray ) . ';' ) ) : '';
		}
		
		return $this->sqlArray;
	}
	
	/**
	* Destroys an adoSchema object.
	*
	* Call this method to clean up after an adoSchema object that is no longer in use.
	* @deprecated adoSchema now cleans up automatically.
	*/
	function Destroy() {
		set_magic_quotes_runtime( $this->mgq );
		unset( $this );
	}
}

/**
* Message logging function
*
* @access private
*/
function logMsg( $msg, $title = NULL, $force = FALSE ) {
	if( XMLS_DEBUG or $force ) {
		echo '<pre>';
		
		if( isset( $title ) ) {
			echo '<h3>' . htmlentities( $title ) . '</h3>';
		}
		
		if( is_object( $this ) ) {
			echo '[' . get_class( $this ) . '] ';
		}
		
		print_r( $msg );
		
		echo '</pre>';
	}
}
?>

⌨️ 快捷键说明

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