From c091819f4030701e636e13f8e10a665607fab9a8 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Thu, 11 Apr 2013 17:39:28 +0100 Subject: [PATCH] Add schema default/fixed value support Added support for adding fixed/default values during XSD validation and added/updated associated tests --- ext/dom/document.c | 16 +++++++++--- ...ocument_schemaValidateSource_addAttrs.phpt | 25 +++++++++++++++++++ ...MDocument_schemaValidateSource_error4.phpt | 2 +- ...ent_schemaValidateSource_missingAttrs.phpt | 25 +++++++++++++++++++ .../DOMDocument_schemaValidate_addAttrs.phpt | 23 +++++++++++++++++ .../DOMDocument_schemaValidate_error4.phpt | 2 +- .../DOMDocument_schemaValidate_error5.phpt | 8 +++--- ...MDocument_schemaValidate_missingAttrs.phpt | 23 +++++++++++++++++ ext/dom/tests/book-attr.xml | 11 ++++++++ ext/dom/tests/book.xsd | 1 + ext/libxml/libxml.c | 6 +++++ 11 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 ext/dom/tests/DOMDocument_schemaValidateSource_addAttrs.phpt create mode 100644 ext/dom/tests/DOMDocument_schemaValidateSource_missingAttrs.phpt create mode 100644 ext/dom/tests/DOMDocument_schemaValidate_addAttrs.phpt create mode 100644 ext/dom/tests/DOMDocument_schemaValidate_missingAttrs.phpt create mode 100644 ext/dom/tests/book-attr.xml diff --git a/ext/dom/document.c b/ext/dom/document.c index d17c7cbd55f1d..efe6d9070fb7c 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -1973,14 +1973,15 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type xmlDoc *docp; dom_object *intern; char *source = NULL, *valid_file = NULL; - int source_len = 0; + int source_len = 0, valid_opts = 0; + long flags = 0; xmlSchemaParserCtxtPtr parser; xmlSchemaPtr sptr; xmlSchemaValidCtxtPtr vptr; int is_valid; char resolved_path[MAXPATHLEN + 1]; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Op", &id, dom_document_class_entry, &source, &source_len) == FAILURE) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Op|l", &id, dom_document_class_entry, &source, &source_len, &flags) == FAILURE) { return; } @@ -2029,6 +2030,13 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type RETURN_FALSE; } +#if LIBXML_VERSION >= 20614 + if (flags & XML_SCHEMA_VAL_VC_I_CREATE) { + valid_opts |= XML_SCHEMA_VAL_VC_I_CREATE; + } +#endif + + xmlSchemaSetValidOptions(vptr, valid_opts); xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr); is_valid = xmlSchemaValidateDoc(vptr, docp); xmlSchemaFree(sptr); @@ -2042,14 +2050,14 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type } /* }}} */ -/* {{{ proto boolean dom_document_schema_validate_file(string filename); */ +/* {{{ proto boolean dom_document_schema_validate_file(string filename, int flags); */ PHP_FUNCTION(dom_document_schema_validate_file) { _dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE); } /* }}} end dom_document_schema_validate_file */ -/* {{{ proto boolean dom_document_schema_validate(string source); */ +/* {{{ proto boolean dom_document_schema_validate(string source, int flags); */ PHP_FUNCTION(dom_document_schema_validate_xml) { _dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING); diff --git a/ext/dom/tests/DOMDocument_schemaValidateSource_addAttrs.phpt b/ext/dom/tests/DOMDocument_schemaValidateSource_addAttrs.phpt new file mode 100644 index 0000000000000..994b94d0c8533 --- /dev/null +++ b/ext/dom/tests/DOMDocument_schemaValidateSource_addAttrs.phpt @@ -0,0 +1,25 @@ +--TEST-- +DomDocument::schemaValidateSource() - Add missing attribute default values from schema +--CREDITS-- +Chris Wright +--SKIPIF-- + +--FILE-- +load(dirname(__FILE__)."/book-attr.xml"); + +$xsd = file_get_contents(dirname(__FILE__)."/book.xsd"); + +$doc->schemaValidateSource($xsd, LIBXML_SCHEMA_CREATE); + +foreach ($doc->getElementsByTagName('book') as $book) { + var_dump($book->getAttribute('is-hardback')); +} + +?> +--EXPECT-- +string(5) "false" +string(4) "true" diff --git a/ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt b/ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt index 65c8d8678f8ea..f841b874288dd 100644 --- a/ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt +++ b/ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt @@ -17,5 +17,5 @@ var_dump($result); ?> --EXPECTF-- -Warning: DOMDocument::schemaValidateSource() expects exactly 1 parameter, 0 given in %s.php on line %d +Warning: DOMDocument::schemaValidateSource() expects at least 1 parameter, 0 given in %s.php on line %d NULL diff --git a/ext/dom/tests/DOMDocument_schemaValidateSource_missingAttrs.phpt b/ext/dom/tests/DOMDocument_schemaValidateSource_missingAttrs.phpt new file mode 100644 index 0000000000000..7c98a74b1da25 --- /dev/null +++ b/ext/dom/tests/DOMDocument_schemaValidateSource_missingAttrs.phpt @@ -0,0 +1,25 @@ +--TEST-- +DomDocument::schemaValidateSource() - Don't add missing attribute default values from schema +--CREDITS-- +Chris Wright +--SKIPIF-- + +--FILE-- +load(dirname(__FILE__)."/book-attr.xml"); + +$xsd = file_get_contents(dirname(__FILE__)."/book.xsd"); + +$doc->schemaValidateSource($xsd); + +foreach ($doc->getElementsByTagName('book') as $book) { + var_dump($book->getAttribute('is-hardback')); +} + +?> +--EXPECT-- +string(0) "" +string(4) "true" diff --git a/ext/dom/tests/DOMDocument_schemaValidate_addAttrs.phpt b/ext/dom/tests/DOMDocument_schemaValidate_addAttrs.phpt new file mode 100644 index 0000000000000..e0b5251b2333f --- /dev/null +++ b/ext/dom/tests/DOMDocument_schemaValidate_addAttrs.phpt @@ -0,0 +1,23 @@ +--TEST-- +DomDocument::schemaValidate() - Add missing attribute default values from schema +--CREDITS-- +Chris Wright +--SKIPIF-- + +--FILE-- +load(dirname(__FILE__)."/book-attr.xml"); + +$doc->schemaValidate(dirname(__FILE__)."/book.xsd", LIBXML_SCHEMA_CREATE); + +foreach ($doc->getElementsByTagName('book') as $book) { + var_dump($book->getAttribute('is-hardback')); +} + +?> +--EXPECT-- +string(5) "false" +string(4) "true" diff --git a/ext/dom/tests/DOMDocument_schemaValidate_error4.phpt b/ext/dom/tests/DOMDocument_schemaValidate_error4.phpt index d4817deca0b44..9e4b6c4b7c963 100644 --- a/ext/dom/tests/DOMDocument_schemaValidate_error4.phpt +++ b/ext/dom/tests/DOMDocument_schemaValidate_error4.phpt @@ -17,5 +17,5 @@ var_dump($result); ?> --EXPECTF-- -Warning: DOMDocument::schemaValidate() expects exactly 1 parameter, 0 given in %s.php on line %d +Warning: DOMDocument::schemaValidate() expects at least 1 parameter, 0 given in %s.php on line %d NULL diff --git a/ext/dom/tests/DOMDocument_schemaValidate_error5.phpt b/ext/dom/tests/DOMDocument_schemaValidate_error5.phpt index d3f0658c1f9f1..d5743bc6cf5c7 100644 --- a/ext/dom/tests/DOMDocument_schemaValidate_error5.phpt +++ b/ext/dom/tests/DOMDocument_schemaValidate_error5.phpt @@ -1,5 +1,5 @@ --TEST-- -DomDocument::schemaValidate() - non-existant schema file +DomDocument::schemaValidate() - non-existent schema file --CREDITS-- Daniel Convissor # TestFest 2009 NYPHP @@ -12,14 +12,14 @@ $doc = new DOMDocument; $doc->load(dirname(__FILE__)."/book.xml"); -$result = $doc->schemaValidate(dirname(__FILE__)."/non-existant-file"); +$result = $doc->schemaValidate(dirname(__FILE__)."/non-existent-file"); var_dump($result); ?> --EXPECTF-- -Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "%snon-existant-file" in %s.php on line %d +Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "%snon-existent-file" in %s.php on line %d -Warning: DOMDocument::schemaValidate(): Failed to locate the main schema resource at '%s/non-existant-file'. in %s.php on line %d +Warning: DOMDocument::schemaValidate(): Failed to locate the main schema resource at '%s/non-existent-file'. in %s.php on line %d Warning: DOMDocument::schemaValidate(): Invalid Schema in %s.php on line %d bool(false) diff --git a/ext/dom/tests/DOMDocument_schemaValidate_missingAttrs.phpt b/ext/dom/tests/DOMDocument_schemaValidate_missingAttrs.phpt new file mode 100644 index 0000000000000..d253ad9690071 --- /dev/null +++ b/ext/dom/tests/DOMDocument_schemaValidate_missingAttrs.phpt @@ -0,0 +1,23 @@ +--TEST-- +DomDocument::schemaValidate() - Don't add missing attribute default values from schema +--CREDITS-- +Chris Wright +--SKIPIF-- + +--FILE-- +load(dirname(__FILE__)."/book-attr.xml"); + +$doc->schemaValidate(dirname(__FILE__)."/book.xsd"); + +foreach ($doc->getElementsByTagName('book') as $book) { + var_dump($book->getAttribute('is-hardback')); +} + +?> +--EXPECT-- +string(0) "" +string(4) "true" diff --git a/ext/dom/tests/book-attr.xml b/ext/dom/tests/book-attr.xml new file mode 100644 index 0000000000000..ba4298d0984c3 --- /dev/null +++ b/ext/dom/tests/book-attr.xml @@ -0,0 +1,11 @@ + + + + The Grapes of Wrath + John Steinbeck + + + The Pearl + John Steinbeck + + diff --git a/ext/dom/tests/book.xsd b/ext/dom/tests/book.xsd index 45986fc4b3989..6b4a8ea54569c 100755 --- a/ext/dom/tests/book.xsd +++ b/ext/dom/tests/book.xsd @@ -9,6 +9,7 @@ + diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index b1cb45db7645e..354cb548a7a76 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -44,6 +44,7 @@ #include #ifdef LIBXML_SCHEMAS_ENABLED #include +#include #endif #include "php_libxml.h" @@ -798,6 +799,11 @@ static PHP_MINIT_FUNCTION(libxml) #endif REGISTER_LONG_CONSTANT("LIBXML_NOEMPTYTAG", LIBXML_SAVE_NOEMPTYTAG, CONST_CS | CONST_PERSISTENT); + /* Schema validation options */ +#if defined(LIBXML_SCHEMAS_ENABLED) && LIBXML_VERSION >= 20614 + REGISTER_LONG_CONSTANT("LIBXML_SCHEMA_CREATE", XML_SCHEMA_VAL_VC_I_CREATE, CONST_CS | CONST_PERSISTENT); +#endif + /* Additional constants for use with loading html */ #if LIBXML_VERSION >= 20707 REGISTER_LONG_CONSTANT("LIBXML_HTML_NOIMPLIED", HTML_PARSE_NOIMPLIED, CONST_CS | CONST_PERSISTENT);