Skip to content

Commit

Permalink
Merge branch 'pull-request/325' into PHP-5.5
Browse files Browse the repository at this point in the history
* pull-request/325:
  Add schema default/fixed value support
  • Loading branch information
smalyshev committed Aug 11, 2013
2 parents 6b4971a + c091819 commit 473d665
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 6 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ PHP NEWS
. Fixed bug #61268 (--enable-dtrace leads make to clobber
Zend/zend_dtrace.d) (Chris Jones)

- DOM:
. Added flags option to DOMDocument::schemaValidate() and
DOMDocument::schemaValidateSource(). Added LIBXML_SCHEMA_CREATE flag.
(Chris Wright)

- Sessions:
. Implemented strict sessions RFC (https://wiki.php.net/rfc/strict_sessions)
which protects against session fixation attacks and session collisions.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ PHP 5.5 UPGRADE NOTES
- Since 5.5.2, spl_autoload_functions() returns different names for
different lambda functions registered via spl_autoload_register().

- Since 5.5.3, DOMDocument::schemaValidateSource() and
DOMDocument::schemaValidate() accept flag parameter. Only flag
available now is LIBXML_SCHEMA_CREATE. Default is 0.

========================================
5. New Functions
========================================
Expand Down
16 changes: 12 additions & 4 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions ext/dom/tests/DOMDocument_schemaValidateSource_addAttrs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
DomDocument::schemaValidateSource() - Add missing attribute default values from schema
--CREDITS--
Chris Wright <[email protected]>
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php

$doc = new DOMDocument;

$doc->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"
2 changes: 1 addition & 1 deletion ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions ext/dom/tests/DOMDocument_schemaValidateSource_missingAttrs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
DomDocument::schemaValidateSource() - Don't add missing attribute default values from schema
--CREDITS--
Chris Wright <[email protected]>
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php

$doc = new DOMDocument;

$doc->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"
23 changes: 23 additions & 0 deletions ext/dom/tests/DOMDocument_schemaValidate_addAttrs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
DomDocument::schemaValidate() - Add missing attribute default values from schema
--CREDITS--
Chris Wright <[email protected]>
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php

$doc = new DOMDocument;

$doc->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"
2 changes: 1 addition & 1 deletion ext/dom/tests/DOMDocument_schemaValidate_error4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions ext/dom/tests/DOMDocument_schemaValidate_missingAttrs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
DomDocument::schemaValidate() - Don't add missing attribute default values from schema
--CREDITS--
Chris Wright <[email protected]>
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php

$doc = new DOMDocument;

$doc->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"
11 changes: 11 additions & 0 deletions ext/dom/tests/book-attr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<books>
<book>
<title>The Grapes of Wrath</title>
<author>John Steinbeck</author>
</book>
<book is-hardback="true">
<title>The Pearl</title>
<author>John Steinbeck</author>
</book>
</books>
1 change: 1 addition & 0 deletions ext/dom/tests/book.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
<xs:attribute name="is-hardback" type="xs:boolean" default="false" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
Expand Down
6 changes: 6 additions & 0 deletions ext/libxml/libxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <libxml/xmlsave.h>
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif

#include "php_libxml.h"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 473d665

Please sign in to comment.