From: Chris S. <san...@us...> - 2005-07-18 10:14:16
|
Update of /cvsroot/stack/stack-1-0/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27783/scripts Modified Files: stackXML.php Log Message: Index: stackXML.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackXML.php,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** stackXML.php 18 Jul 2005 10:07:18 -0000 1.4 --- stackXML.php 18 Jul 2005 10:13:51 -0000 1.5 *************** *** 242,276 **** function stack_schema_write_metadata($directory) { ! //take the stackQuestion and extract all the current metadata tags, putting them into a schema ! //file extension .xsd represents schema ! $schema_file_name='stack_metadata.xsd'; ! //create file used for writing ! $xml_file_handle = fopen($directory.$schema_file_name,"w"); ! if (!$xml_file_handle) ! { ! echo "File could not be opened for writing. Please contact the network administrator."; ! } ! else ! { ! //Write the XML schema header - same for all XML schemas ! $xml = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">'. ! "\n". ! '<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>'. ! //Create root element 'metadata' ! stack_schema_create_root("metadata"). ! stack_schema_create_metadata(). ! stack_schema_escape_root(). ! stack_schema_create_enum_types(). ! //End of schema ! '</xs:schema>'; ! ! //write out xml string ! fputs($xml_file_handle,$xml); ! //close xml file ! fclose($xml_file_handle); ! } ! return $schema_file_name; } --- 242,276 ---- function stack_schema_write_metadata($directory) { ! //take the stackQuestion and extract all the current metadata tags, putting them into a schema ! //file extension .xsd represents schema ! $schema_file_name='stack_metadata.xsd'; ! //create file used for writing ! $xml_file_handle = fopen($directory.$schema_file_name,"w"); ! if (!$xml_file_handle) ! { ! echo "File could not be opened for writing. Please contact the network administrator."; ! } ! else ! { ! //Write the XML schema header - same for all XML schemas ! $xml = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">'; ! "\n"; ! '<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>'; ! //Create root element 'metadata'; ! stack_schema_create_root("metadata"); ! stack_schema_create_metadata(); ! stack_schema_escape_root(); ! stack_schema_create_enum_types(); ! //End of schema ! '</xs:schema>'; ! ! //write out xml string ! fputs($xml_file_handle,$xml); ! //close xml file ! fclose($xml_file_handle); ! } ! return $schema_file_name; } *************** *** 285,291 **** function stack_schema_create_root($rootElement) { ! return "<xs:element name=".'"'.$rootElement.'">'. ! '<xs:complexType>'. ! '<xs:sequence>'; } --- 285,291 ---- function stack_schema_create_root($rootElement) { ! return "<xs:element name=".'"'.$rootElement.'">'. ! '<xs:complexType>'. ! '<xs:sequence>'; } *************** *** 297,305 **** function stack_schema_escape_root() { ! return '</xs:sequence>'. ! '</xs:complexType>'. ! "</xs:element>"; } ! /** * Writes the current stackQuestion metadata structure to an XML string --- 297,305 ---- function stack_schema_escape_root() { ! return '</xs:sequence>'. ! '</xs:complexType>'. ! "</xs:element>"; } ! /** * Writes the current stackQuestion metadata structure to an XML string *************** *** 315,387 **** function stack_schema_create_metadata() { ! global $stackQuestion; //use definitive stackQuestion data structure ! global $sqlToSchemaMapping; ! ! $xml = ""; ! ! //Do not export questionID - internal SQL variable ! if (array_key_exists('questionID',$stackQuestion)) { ! unset($stackQuestion['questionID']); ! } ! //Do not export body - internal SQL variable. ! if (array_key_exists('questionBody',$stackQuestion)) { ! unset($stackQuestion['questionBody']); ! } ! foreach($stackQuestion as $arrayKey=>$questionField) //Iterate through sub array of each array object ! { ! //Array item is metadata ! if ($questionField['type']=='meta') { ! ! //Check that a metadata tag has been supplied and do not write out if tag has not been supplied ! if (empty($questionField['metatag'])) ! { ! echo'No metadata tag for stackQuestion field: '.$arrayKey.'.<br> As a consequence it has not been written to the XML metadata schema'; ! } ! ! else ! { ! //If metadata is not a selection from a list of specified types ! if (empty($questionField['values'])) ! { ! //Create XML schema fragment ! //Documentation available for column ! if (!empty($questionField['doc'])) { ! $xml.=stack_schema_write_element_document($questionField['metatag'],$questionField['mysql'],$questionField['doc']); ! ! } ! ! //Documentation not available for column ! else { ! //echo "Doucmentation not avilable!"; ! $xml.=stack_schema_write_element($questionField['metatag'],$questionField['mysql']); ! } ! } ! ! //The values field is not empty ! else ! { ! //Create XML schema fragment, and default possible enumeration simpleTypes to be the lower case ! //of the current metadata tag ! if (!empty($questionField['doc'])) { ! $xml.=stack_schema_write_element_document_enum_type($questionField['metatag'],strtolower($questionField['metatag']),$questionField['doc']); ! ! } ! ! //Documentation not available for column ! else { ! $xml.=stack_schema_write_element_enum_type($questionField['metatag'],strtolower($questionField['metatag'])); ! } ! ! //Furthermore, need to create ! } ! ! ! } ! } ! } ! ! return $xml; } --- 315,387 ---- function stack_schema_create_metadata() { ! global $stackQuestion; //use definitive stackQuestion data structure ! global $sqlToSchemaMapping; ! $xml = ""; ! ! //Do not export questionID - internal SQL variable ! if (array_key_exists('questionID',$stackQuestion)) { ! unset($stackQuestion['questionID']); ! } ! //Do not export body - internal SQL variable. ! if (array_key_exists('questionBody',$stackQuestion)) { ! unset($stackQuestion['questionBody']); ! } ! ! foreach($stackQuestion as $arrayKey=>$questionField) //Iterate through sub array of each array object ! { ! //Array item is metadata ! if ($questionField['type']=='meta') { ! ! //Check that a metadata tag has been supplied and do not write out if tag has not been supplied ! if (empty($questionField['metatag'])) ! { ! echo'No metadata tag for stackQuestion field: '.$arrayKey.'.<br> As a consequence it has not been written to the XML metadata schema'; ! } ! ! else ! { ! //If metadata is not a selection from a list of specified types ! if (empty($questionField['values'])) ! { ! //Create XML schema fragment ! //Documentation available for column ! if (!empty($questionField['doc'])) { ! $xml.=stack_schema_write_element_document($questionField['metatag'],$questionField['mysql'],$questionField['doc']); ! ! } ! ! //Documentation not available for column ! else { ! //echo "Doucmentation not avilable!"; ! $xml.=stack_schema_write_element($questionField['metatag'],$questionField['mysql']); ! } ! } ! ! //The values field is not empty ! else ! { ! //Create XML schema fragment, and default possible enumeration simpleTypes to be the lower case ! //of the current metadata tag ! if (!empty($questionField['doc'])) { ! $xml.=stack_schema_write_element_document_enum_type($questionField['metatag'],strtolower($questionField['metatag']),$questionField['doc']); ! ! } ! ! //Documentation not available for column ! else { ! $xml.=stack_schema_write_element_enum_type($questionField['metatag'],strtolower($questionField['metatag'])); ! } ! ! //Furthermore, need to create ! } ! ! ! } ! } ! } ! ! return $xml; } *************** *** 404,433 **** global $sqlToSchemaMapping; ! //Error checking code ! //1. If there is no type associated with the element, then default to a string ! if (empty($elementType)) ! { ! ! //SQL type - text = XML schema type - string ! $elementType='TEXT'; ! } ! ! //2. Ensure element type is upper case to ensure comparison array (SQL->XML variable) will work. ! else ! { ! $elementType = strtoupper($elementType); ! } ! ! //3. Now we are only interested in the first word of the SQL datatype in order to convert to XML datatype, hence explode ! //Make sensitive to white space ! $elementDataType = explode(" ", $elementType); ! ! //4. But we also can have SQL datatypes which give array length - CHAR(22) for example - we are not interested in this ! //Make sensitive to open bracket ! $elementDataType = explode("(", $elementDataType[0]); ! ! echo $sqlToSchemaMapping[$elementDataType[0]]; ! ! return "<xs:element name=".'"'.$elementName.'"'.' type="xs:'.$sqlToSchemaMapping[$elementDataType[0]].'"/>'; } --- 404,433 ---- global $sqlToSchemaMapping; ! //Error checking code ! //1. If there is no type associated with the element, then default to a string ! if (empty($elementType)) ! { ! ! //SQL type - text = XML schema type - string ! $elementType='TEXT'; ! } ! ! //2. Ensure element type is upper case to ensure comparison array (SQL->XML variable) will work. ! else ! { ! $elementType = strtoupper($elementType); ! } ! ! //3. Now we are only interested in the first word of the SQL datatype in order to convert to XML datatype, hence explode ! //Make sensitive to white space ! $elementDataType = explode(" ", $elementType); ! ! //4. But we also can have SQL datatypes which give array length - CHAR(22) for example - we are not interested in this ! //Make sensitive to open bracket ! $elementDataType = explode("(", $elementDataType[0]); ! ! echo $sqlToSchemaMapping[$elementDataType[0]]; ! ! return "<xs:element name=".'"'.$elementName.'"'.' type="xs:'.$sqlToSchemaMapping[$elementDataType[0]].'"/>'; } *************** *** 441,448 **** * Example: * <xs:element name="Keyword" type="xs:string"> ! * <xs:annotation> ! * <xs:documentation>Keywords, for searching over banks of questions. Note that any question with the keyword 'demo' will appear ! * as a demo question for guest users and students to try and edit.</xs:documentation> ! * </xs:annotation> * </xs:element> * --- 441,448 ---- * Example: * <xs:element name="Keyword" type="xs:string"> ! * <xs:annotation> ! * <xs:documentation>Keywords, for searching over banks of questions. Note that any question with the keyword 'demo' will appear ! * as a demo question for guest users and students to try and edit.</xs:documentation> ! * </xs:annotation> * </xs:element> * *************** *** 453,490 **** function stack_schema_write_element_document($elementName, $elementType, $documentTag) { ! global $sqlToSchemaMapping; ! ! //Error checking code ! //1. If there is no type associated with the element, then default to a string ! if (empty($elementType)) ! { ! ! //SQL type - text = XML schema type - string ! $elementType='TEXT'; ! } ! ! //2. Ensure element type is upper case to ensure comparison array (SQL->XML variable) will work. ! else ! { ! $elementType = strtoupper($elementType); ! } ! ! //3. Now we are only interested in the first word of the SQL datatype in order to convert to XML datatype, hence explode ! //Make sensitive to white space ! $elementDataType = explode(" ", $elementType); ! ! //4. But we also can have SQL datatypes which give array length - CHAR(22) for example - we are not interested in this ! //Make sensitive to open bracket ! $elementDataType = explode("(", $elementDataType[0]); ! ! $xml = "<xs:element name=".'"'.$elementName.'"'.' type="xs:'.$sqlToSchemaMapping[$elementDataType[0]].'">'; ! ! //Add documentation to element ! $xml.= '<xs:annotation><xs:documentation>'. ! $documentTag. ! '</xs:documentation></xs:annotation>'. ! '</xs:element>'; ! ! return $xml; } --- 453,490 ---- function stack_schema_write_element_document($elementName, $elementType, $documentTag) { ! global $sqlToSchemaMapping; ! ! //Error checking code ! //1. If there is no type associated with the element, then default to a string ! if (empty($elementType)) ! { ! ! //SQL type - text = XML schema type - string ! $elementType='TEXT'; ! } ! ! //2. Ensure element type is upper case to ensure comparison array (SQL->XML variable) will work. ! else ! { ! $elementType = strtoupper($elementType); ! } ! ! //3. Now we are only interested in the first word of the SQL datatype in order to convert to XML datatype, hence explode ! //Make sensitive to white space ! $elementDataType = explode(" ", $elementType); ! ! //4. But we also can have SQL datatypes which give array length - CHAR(22) for example - we are not interested in this ! //Make sensitive to open bracket ! $elementDataType = explode("(", $elementDataType[0]); ! ! $xml = "<xs:element name=".'"'.$elementName.'"'.' type="xs:'.$sqlToSchemaMapping[$elementDataType[0]].'">'; ! ! //Add documentation to element ! $xml.= '<xs:annotation><xs:documentation>'. ! $documentTag. ! '</xs:documentation></xs:annotation>'. ! '</xs:element>'; ! ! return $xml; } *************** *** 502,516 **** function stack_schema_write_element_enum_type($elementName, $elementType) { ! //Error checking code ! //1. If there is no type associated with the element, return an empty string - this should not be possible ! if (empty($elementType)) ! { ! return ""; ! } ! ! else ! { ! return "<xs:element name=".'"'.$elementName.'"'.' type='.'"'.$elementType.'"'.'/>'; ! } } --- 502,516 ---- function stack_schema_write_element_enum_type($elementName, $elementType) { ! //Error checking code ! //1. If there is no type associated with the element, return an empty string - this should not be possible ! if (empty($elementType)) ! { ! return ""; ! } ! ! else ! { ! return "<xs:element name=".'"'.$elementName.'"'.' type='.'"'.$elementType.'"'.'/>'; ! } } *************** *** 521,528 **** * Example: * <xs:element name="LearningContext" type="learningcontext"> ! * <xs:annotation> ! * <xs:documentation>Describes the educational context of the intended target audience of the resource. ! * User defined list type</xs:documentation> ! * </xs:annotation> * </xs:element> * --- 521,528 ---- * Example: * <xs:element name="LearningContext" type="learningcontext"> ! * <xs:annotation> ! * <xs:documentation>Describes the educational context of the intended target audience of the resource. ! * User defined list type</xs:documentation> ! * </xs:annotation> * </xs:element> * *************** *** 533,558 **** function stack_schema_write_element_document_enum_type($elementName, $elementType, $documentTag) { ! //Error checking code ! //1. If there is no type associated with the element, then return an empty string - cannot continue! ! if (empty($elementType)) ! { ! ! return ""; ! } ! ! //Otherwise elementType is given the lowercase of the metadata tag - correct XML schema construct. ! else ! { ! ! $xml = "<xs:element name=".'"'.$elementName.'"'.' type='.'"'.$elementType.'">'; ! ! //Add documentation to element ! $xml.= '<xs:annotation><xs:documentation>'. ! $documentTag. ! '</xs:documentation></xs:annotation>'. ! '</xs:element>'; ! ! return $xml; ! } } --- 533,558 ---- function stack_schema_write_element_document_enum_type($elementName, $elementType, $documentTag) { ! //Error checking code ! //1. If there is no type associated with the element, then return an empty string - cannot continue! ! if (empty($elementType)) ! { ! ! return ""; ! } ! ! //Otherwise elementType is given the lowercase of the metadata tag - correct XML schema construct. ! else ! { ! ! $xml = "<xs:element name=".'"'.$elementName.'"'.' type='.'"'.$elementType.'">'; ! ! //Add documentation to element ! $xml.= '<xs:annotation><xs:documentation>'. ! $documentTag. ! '</xs:documentation></xs:annotation>'. ! '</xs:element>'; ! ! return $xml; ! } } *************** *** 575,601 **** $xml = ""; ! foreach($stackQuestion as $arrayKey=>$questionField) //Iterate through sub array of each array object ! { ! //Array item is metadata ! if ($questionField['type']=='meta') { ! ! //check to see whether an enum type of values needs to be written out ! if (!empty($questionField['values'])) ! { ! //Check that a metadata tag has been supplied and do not write out if tag has not been supplied ! if (empty($questionField['metatag'])) ! { ! echo'No metadata tag for stackQuestion field: '.$arrayKey.'.<br> As a consequence this enum type has not been written to the XML metadata schema'; ! } ! ! else ! { ! $xml.=stack_schema_create_simpletype($questionField['metatag'],$questionField['values']); ! } ! } ! } ! } ! ! return $xml; } --- 575,601 ---- $xml = ""; ! foreach($stackQuestion as $arrayKey=>$questionField) //Iterate through sub array of each array object ! { ! //Array item is metadata ! if ($questionField['type']=='meta') { ! ! //check to see whether an enum type of values needs to be written out ! if (!empty($questionField['values'])) ! { ! //Check that a metadata tag has been supplied and do not write out if tag has not been supplied ! if (empty($questionField['metatag'])) ! { ! echo'No metadata tag for stackQuestion field: '.$arrayKey.'.<br> As a consequence this enum type has not been written to the XML metadata schema'; ! } ! ! else ! { ! $xml.=stack_schema_create_simpletype($questionField['metatag'],$questionField['values']); ! } ! } ! } ! } ! ! return $xml; } *************** *** 608,622 **** * Example: * <xs:simpleType name="language"> ! * <xs:annotation> ! * <xs:documentation>ISO 639 languages</xs:documentation> ! * </xs:annotation> ! * <xs:restriction base="xs:string"> ! * <xs:length value="2" /> ! * <xs:enumeration value="en" /> ! * <xs:enumeration value="fr" /> ! * <xs:enumeration value="nl" /> ! * <xs:enumeration value="es" /> ! * </xs:restriction> ! * </xs:simpleType> * @param string $simpleTypeName the name of the field - usually lower case derivative of 'metatag' * @param array $possibleValues the possible enumerations of that field --- 608,622 ---- * Example: * <xs:simpleType name="language"> ! * <xs:annotation> ! * <xs:documentation>ISO 639 languages</xs:documentation> ! * </xs:annotation> ! * <xs:restriction base="xs:string"> ! * <xs:length value="2" /> ! * <xs:enumeration value="en" /> ! * <xs:enumeration value="fr" /> ! * <xs:enumeration value="nl" /> ! * <xs:enumeration value="es" /> ! * </xs:restriction> ! * </xs:simpleType> * @param string $simpleTypeName the name of the field - usually lower case derivative of 'metatag' * @param array $possibleValues the possible enumerations of that field *************** *** 625,641 **** function stack_schema_create_simpletype($simpleTypeName,$possibleValues) { ! //Begin element definition ! $xml = "<xs:simpleType name=".'"'.$simpleTypeName.'"'.">"; ! $xml.= '<xs:restriction base="xs:string">'; ! ! foreach ($possibleValues as $value) //Iterate through each of the possible values ! { ! $xml.= "<xs:enumeration value=".'"'.$value.'"'."/>"; //Add to XML enumeration ! } ! ! //Terminate element definition ! $xml.= "</xs:restriction></xs:simpleType>"; ! ! return $xml; } --- 625,641 ---- function stack_schema_create_simpletype($simpleTypeName,$possibleValues) { ! //Begin element definition ! $xml = "<xs:simpleType name=".'"'.$simpleTypeName.'"'.">"; ! $xml.= '<xs:restriction base="xs:string">'; ! ! foreach ($possibleValues as $value) //Iterate through each of the possible values ! { ! $xml.= "<xs:enumeration value=".'"'.$value.'"'."/>"; //Add to XML enumeration ! } ! ! //Terminate element definition ! $xml.= "</xs:restriction></xs:simpleType>"; ! ! return $xml; } |