diff options
Diffstat (limited to 'ext/xmlreader/php_xmlreader.c')
-rw-r--r-- | ext/xmlreader/php_xmlreader.c | 537 |
1 files changed, 197 insertions, 340 deletions
diff --git a/ext/xmlreader/php_xmlreader.c b/ext/xmlreader/php_xmlreader.c index 20960d96d5..afc8cc80ba 100644 --- a/ext/xmlreader/php_xmlreader.c +++ b/ext/xmlreader/php_xmlreader.c @@ -1,7 +1,5 @@ /* +----------------------------------------------------------------------+ - | PHP Version 7 | - +----------------------------------------------------------------------+ | Copyright (c) The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | @@ -25,6 +23,7 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "php_xmlreader.h" +#include "php_xmlreader_arginfo.h" #ifdef HAVE_DOM #include "ext/dom/xml_common.h" #include "ext/dom/dom_ce.h" @@ -38,6 +37,9 @@ static zend_object_handlers xmlreader_object_handlers; static HashTable xmlreader_prop_handlers; +static zend_internal_function xmlreader_open_fn; +static zend_internal_function xmlreader_xml_fn; + typedef int (*xmlreader_read_int_t)(xmlTextReaderPtr reader); typedef unsigned char *(*xmlreader_read_char_t)(xmlTextReaderPtr reader); typedef const unsigned char *(*xmlreader_read_const_char_t)(xmlTextReaderPtr reader); @@ -114,34 +116,20 @@ static int xmlreader_property_reader(xmlreader_object *obj, xmlreader_prop_handl /* }}} */ /* {{{ xmlreader_get_property_ptr_ptr */ -zval *xmlreader_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) +zval *xmlreader_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) { xmlreader_object *obj; - zval tmp_member; zval *retval = NULL; xmlreader_prop_handler *hnd = NULL; - if (Z_TYPE_P(member) != IS_STRING) { - zend_string *str = zval_try_get_string_func(member); - if (UNEXPECTED(!str)) { - return NULL; - } - ZVAL_STR(&tmp_member, str); - member = &tmp_member; - } - - obj = Z_XMLREADER_P(object); + obj = php_xmlreader_fetch_object(object); if (obj->prop_handler != NULL) { - hnd = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member)); + hnd = zend_hash_find_ptr(obj->prop_handler, name); } if (hnd == NULL) { - retval = zend_std_get_property_ptr_ptr(object, member, type, cache_slot); - } - - if (member == &tmp_member) { - zval_ptr_dtor_str(&tmp_member); + retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot); } return retval; @@ -149,26 +137,16 @@ zval *xmlreader_get_property_ptr_ptr(zval *object, zval *member, int type, void /* }}} */ /* {{{ xmlreader_read_property */ -zval *xmlreader_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) +zval *xmlreader_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) { xmlreader_object *obj; - zval tmp_member; zval *retval = NULL; xmlreader_prop_handler *hnd = NULL; - if (Z_TYPE_P(member) != IS_STRING) { - zend_string *str = zval_try_get_string_func(member); - if (UNEXPECTED(!str)) { - return &EG(uninitialized_zval); - } - ZVAL_STR(&tmp_member, str); - member = &tmp_member; - } - - obj = Z_XMLREADER_P(object); + obj = php_xmlreader_fetch_object(object); if (obj->prop_handler != NULL) { - hnd = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member)); + hnd = zend_hash_find_ptr(obj->prop_handler, name); } if (hnd != NULL) { @@ -178,51 +156,53 @@ zval *xmlreader_read_property(zval *object, zval *member, int type, void **cache retval = rv; } } else { - retval = zend_std_read_property(object, member, type, cache_slot, rv); + retval = zend_std_read_property(object, name, type, cache_slot, rv); } - if (member == &tmp_member) { - zval_ptr_dtor_str(&tmp_member); - } return retval; } /* }}} */ /* {{{ xmlreader_write_property */ -zval *xmlreader_write_property(zval *object, zval *member, zval *value, void **cache_slot) +zval *xmlreader_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot) { xmlreader_object *obj; - zval tmp_member; xmlreader_prop_handler *hnd = NULL; - if (Z_TYPE_P(member) != IS_STRING) { - zend_string *str = zval_try_get_string_func(member); - if (UNEXPECTED(!str)) { - return value; - } - ZVAL_STR(&tmp_member, str); - member = &tmp_member; - } - - obj = Z_XMLREADER_P(object); + obj = php_xmlreader_fetch_object(object); if (obj->prop_handler != NULL) { - hnd = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member)); + hnd = zend_hash_find_ptr(obj->prop_handler, name); } if (hnd != NULL) { - php_error_docref(NULL, E_WARNING, "Cannot write to read-only property"); + zend_throw_error(NULL, "Cannot write to read-only property"); } else { - value = zend_std_write_property(object, member, value, cache_slot); - } - - if (member == &tmp_member) { - zval_ptr_dtor_str(&tmp_member); + value = zend_std_write_property(object, name, value, cache_slot); } return value; } /* }}} */ +/* {{{ */ +static zend_function *xmlreader_get_method(zend_object **obj, zend_string *name, const zval *key) +{ + if (ZSTR_LEN(name) == sizeof("open") - 1 + && (ZSTR_VAL(name)[0] == 'o' || ZSTR_VAL(name)[0] == 'O') + && (ZSTR_VAL(name)[1] == 'p' || ZSTR_VAL(name)[1] == 'P') + && (ZSTR_VAL(name)[2] == 'e' || ZSTR_VAL(name)[2] == 'E') + && (ZSTR_VAL(name)[3] == 'n' || ZSTR_VAL(name)[3] == 'N')) { + return (zend_function*)&xmlreader_open_fn; + } else if (ZSTR_LEN(name) == sizeof("xml") - 1 + && (ZSTR_VAL(name)[0] == 'x' || ZSTR_VAL(name)[0] == 'X') + && (ZSTR_VAL(name)[1] == 'm' || ZSTR_VAL(name)[1] == 'M') + && (ZSTR_VAL(name)[2] == 'l' || ZSTR_VAL(name)[2] == 'L')) { + return (zend_function*)&xmlreader_xml_fn; + } + return zend_std_get_method(obj, name, key);; +} +/* }}} */ + /* {{{ _xmlreader_get_valid_file_path */ /* _xmlreader_get_valid_file_path and _xmlreader_get_relaxNG should be made a common function in libxml extension as code is common to a few xml extensions */ @@ -323,8 +303,7 @@ static const zend_module_dep xmlreader_deps[] = { ZEND_MOD_END }; -/* {{{ xmlreader_module_entry - */ +/* {{{ xmlreader_module_entry */ zend_module_entry xmlreader_module_entry = { STANDARD_MODULE_HEADER_EX, NULL, xmlreader_deps, @@ -408,12 +387,12 @@ static void php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAMETERS, xmlreader_rea char *name; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) { - return; + RETURN_THROWS(); } if (!name_len) { - php_error_docref(NULL, E_WARNING, "Argument cannot be an empty string"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -438,6 +417,10 @@ static void php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAMETERS, xmlreader_read_in int retval; xmlreader_object *intern; + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + id = ZEND_THIS; intern = Z_XMLREADER_P(id); @@ -462,6 +445,10 @@ static void php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAMETERS, xmlreader_ char *retchar = NULL; xmlreader_object *intern; + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + id = ZEND_THIS; intern = Z_XMLREADER_P(id); @@ -489,12 +476,12 @@ static void php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAMETERS, int t char *source; if (zend_parse_parameters(ZEND_NUM_ARGS(), "p!", &source, &source_len) == FAILURE) { - return; + RETURN_THROWS(); } if (source != NULL && !source_len) { - php_error_docref(NULL, E_WARNING, "Schema data source is required"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -519,27 +506,31 @@ static void php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAMETERS, int t intern->schema = schema; RETURN_TRUE; + } else { + php_error_docref(NULL, E_WARNING, "Schema contains errors"); + RETURN_FALSE; } + } else { + zend_throw_error(NULL, "Schema must be set prior to reading"); + RETURN_THROWS(); } - - php_error_docref(NULL, E_WARNING, "Unable to set schema. This must be set prior to reading or schema contains errors."); - - RETURN_FALSE; #else - php_error_docref(NULL, E_WARNING, "No Schema support built into libxml."); - + php_error_docref(NULL, E_WARNING, "No schema support built into libxml"); RETURN_FALSE; #endif } /* }}} */ -/* {{{ proto bool XMLReader::close() -Closes xmlreader - current frees resources until xmlTextReaderClose is fixed in libxml */ -PHP_METHOD(xmlreader, close) +/* {{{ Closes xmlreader - current frees resources until xmlTextReaderClose is fixed in libxml */ +PHP_METHOD(XMLReader, close) { zval *id; xmlreader_object *intern; + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + id = ZEND_THIS; intern = Z_XMLREADER_P(id); /* libxml is segfaulting in versions up to 2.6.8 using xmlTextReaderClose so for @@ -551,17 +542,15 @@ PHP_METHOD(xmlreader, close) } /* }}} */ -/* {{{ proto string XMLReader::getAttribute(string name) -Get value of an attribute from current element */ -PHP_METHOD(xmlreader, getAttribute) +/* {{{ Get value of an attribute from current element */ +PHP_METHOD(XMLReader, getAttribute) { php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderGetAttribute); } /* }}} */ -/* {{{ proto string XMLReader::getAttributeNo(int index) -Get value of an attribute at index from current element */ -PHP_METHOD(xmlreader, getAttributeNo) +/* {{{ Get value of an attribute at index from current element */ +PHP_METHOD(XMLReader, getAttributeNo) { zval *id; zend_long attr_pos; @@ -569,7 +558,7 @@ PHP_METHOD(xmlreader, getAttributeNo) xmlreader_object *intern; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &attr_pos) == FAILURE) { - return; + RETURN_THROWS(); } id = ZEND_THIS; @@ -585,9 +574,8 @@ PHP_METHOD(xmlreader, getAttributeNo) } /* }}} */ -/* {{{ proto string XMLReader::getAttributeNs(string name, string namespaceURI) -Get value of a attribute via name and namespace from current element */ -PHP_METHOD(xmlreader, getAttributeNs) +/* {{{ Get value of a attribute via name and namespace from current element */ +PHP_METHOD(XMLReader, getAttributeNs) { zval *id; size_t name_len = 0, ns_uri_len = 0; @@ -595,12 +583,17 @@ PHP_METHOD(xmlreader, getAttributeNs) char *name, *ns_uri, *retchar = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &ns_uri, &ns_uri_len) == FAILURE) { - return; + RETURN_THROWS(); } - if (name_len == 0 || ns_uri_len == 0) { - php_error_docref(NULL, E_WARNING, "Attribute Name and Namespace URI cannot be empty"); - RETURN_FALSE; + if (name_len == 0) { + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); + } + + if (ns_uri_len == 0) { + zend_argument_value_error(2, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -616,9 +609,8 @@ PHP_METHOD(xmlreader, getAttributeNs) } /* }}} */ -/* {{{ proto bool XMLReader::getParserProperty(int property) -Indicates whether given property (one of the parser option constants) is set or not on parser */ -PHP_METHOD(xmlreader, getParserProperty) +/* {{{ Indicates whether given property (one of the parser option constants) is set or not on parser */ +PHP_METHOD(XMLReader, getParserProperty) { zval *id; zend_long property; @@ -626,7 +618,7 @@ PHP_METHOD(xmlreader, getParserProperty) xmlreader_object *intern; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &property) == FAILURE) { - return; + RETURN_THROWS(); } id = ZEND_THIS; @@ -636,35 +628,32 @@ PHP_METHOD(xmlreader, getParserProperty) retval = xmlTextReaderGetParserProp(intern->ptr,property); } if (retval == -1) { - php_error_docref(NULL, E_WARNING, "Invalid parser property"); - RETURN_FALSE; + zend_argument_value_error(1, "must be a valid parser property"); + RETURN_THROWS(); } RETURN_BOOL(retval); } /* }}} */ -/* {{{ proto bool XMLReader::isValid() -Returns boolean indicating if parsed document is valid or not. +/* {{{ Returns boolean indicating if parsed document is valid or not. Must set XMLREADER_LOADDTD or XMLREADER_VALIDATE parser option prior to the first call to read or this method will always return FALSE */ -PHP_METHOD(xmlreader, isValid) +PHP_METHOD(XMLReader, isValid) { php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderIsValid); } /* }}} */ -/* {{{ proto string XMLReader::lookupNamespace(string prefix) -Return namespaceURI for associated prefix on current node */ -PHP_METHOD(xmlreader, lookupNamespace) +/* {{{ Return namespaceURI for associated prefix on current node */ +PHP_METHOD(XMLReader, lookupNamespace) { php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderLookupNamespace); } /* }}} */ -/* {{{ proto bool XMLReader::moveToAttribute(string name) -Positions reader at specified attribute - Returns TRUE on success and FALSE on failure */ -PHP_METHOD(xmlreader, moveToAttribute) +/* {{{ Positions reader at specified attribute - Returns TRUE on success and FALSE on failure */ +PHP_METHOD(XMLReader, moveToAttribute) { zval *id; size_t name_len = 0; @@ -673,12 +662,12 @@ PHP_METHOD(xmlreader, moveToAttribute) char *name; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) { - return; + RETURN_THROWS(); } if (name_len == 0) { - php_error_docref(NULL, E_WARNING, "Attribute Name is required"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -695,10 +684,9 @@ PHP_METHOD(xmlreader, moveToAttribute) } /* }}} */ -/* {{{ proto bool XMLReader::moveToAttributeNo(int index) -Positions reader at attribute at specified index. +/* {{{ Positions reader at attribute at specified index. Returns TRUE on success and FALSE on failure */ -PHP_METHOD(xmlreader, moveToAttributeNo) +PHP_METHOD(XMLReader, moveToAttributeNo) { zval *id; zend_long attr_pos; @@ -706,7 +694,7 @@ PHP_METHOD(xmlreader, moveToAttributeNo) xmlreader_object *intern; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &attr_pos) == FAILURE) { - return; + RETURN_THROWS(); } id = ZEND_THIS; @@ -723,10 +711,9 @@ PHP_METHOD(xmlreader, moveToAttributeNo) } /* }}} */ -/* {{{ proto bool XMLReader::moveToAttributeNs(string name, string namespaceURI) -Positions reader at attribute spcified by name and namespaceURI. +/* {{{ Positions reader at attribute spcified by name and namespaceURI. Returns TRUE on success and FALSE on failure */ -PHP_METHOD(xmlreader, moveToAttributeNs) +PHP_METHOD(XMLReader, moveToAttributeNs) { zval *id; size_t name_len=0, ns_uri_len=0; @@ -735,12 +722,17 @@ PHP_METHOD(xmlreader, moveToAttributeNs) char *name, *ns_uri; if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &ns_uri, &ns_uri_len) == FAILURE) { - return; + RETURN_THROWS(); } - if (name_len == 0 || ns_uri_len == 0) { - php_error_docref(NULL, E_WARNING, "Attribute Name and Namespace URI cannot be empty"); - RETURN_FALSE; + if (name_len == 0) { + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); + } + + if (ns_uri_len == 0) { + zend_argument_value_error(2, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -757,57 +749,56 @@ PHP_METHOD(xmlreader, moveToAttributeNs) } /* }}} */ -/* {{{ proto bool XMLReader::moveToElement() -Moves the position of the current instance to the node that contains the current Attribute node. */ -PHP_METHOD(xmlreader, moveToElement) +/* {{{ Moves the position of the current instance to the node that contains the current Attribute node. */ +PHP_METHOD(XMLReader, moveToElement) { php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToElement); } /* }}} */ -/* {{{ proto bool XMLReader::moveToFirstAttribute() -Moves the position of the current instance to the first attribute associated with the current node. */ -PHP_METHOD(xmlreader, moveToFirstAttribute) +/* {{{ Moves the position of the current instance to the first attribute associated with the current node. */ +PHP_METHOD(XMLReader, moveToFirstAttribute) { php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToFirstAttribute); } /* }}} */ -/* {{{ proto bool XMLReader::moveToNextAttribute() -Moves the position of the current instance to the next attribute associated with the current node. */ -PHP_METHOD(xmlreader, moveToNextAttribute) +/* {{{ Moves the position of the current instance to the next attribute associated with the current node. */ +PHP_METHOD(XMLReader, moveToNextAttribute) { php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToNextAttribute); } /* }}} */ -/* {{{ proto bool XMLReader::read() -Moves the position of the current instance to the next node in the stream. */ -PHP_METHOD(xmlreader, read) +/* {{{ Moves the position of the current instance to the next node in the stream. */ +PHP_METHOD(XMLReader, read) { zval *id; int retval; xmlreader_object *intern; + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + id = ZEND_THIS; intern = Z_XMLREADER_P(id); - if (intern != NULL && intern->ptr != NULL) { - retval = xmlTextReaderRead(intern->ptr); - if (retval == -1) { - RETURN_FALSE; - } else { - RETURN_BOOL(retval); - } + if (intern == NULL || intern->ptr == NULL) { + zend_throw_error(NULL, "Data must be loaded before reading"); + RETURN_THROWS(); } - php_error_docref(NULL, E_WARNING, "Load Data before trying to read"); - RETURN_FALSE; + retval = xmlTextReaderRead(intern->ptr); + if (retval == -1) { + RETURN_FALSE; + } else { + RETURN_BOOL(retval); + } } /* }}} */ -/* {{{ proto bool XMLReader::next([string localname]) -Moves the position of the current instance to the next node in the stream. */ -PHP_METHOD(xmlreader, next) +/* {{{ Moves the position of the current instance to the next node in the stream. */ +PHP_METHOD(XMLReader, next) { zval *id; int retval; @@ -815,8 +806,8 @@ PHP_METHOD(xmlreader, next) xmlreader_object *intern; char *name = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &name, &name_len) == FAILURE) { - return; + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &name, &name_len) == FAILURE) { + RETURN_THROWS(); } id = ZEND_THIS; @@ -836,14 +827,12 @@ PHP_METHOD(xmlreader, next) } } - php_error_docref(NULL, E_WARNING, "Load Data before trying to read"); - RETURN_FALSE; + zend_throw_error(NULL, "Data must be loaded before reading"); } /* }}} */ -/* {{{ proto bool XMLReader::open(string URI [, string encoding [, int options]]) -Sets the URI that the XMLReader will parse. */ -PHP_METHOD(xmlreader, open) +/* {{{ Sets the URI that the XMLReader will parse. */ +PHP_METHOD(XMLReader, open) { zval *id; size_t source_len = 0, encoding_len = 0; @@ -855,7 +844,7 @@ PHP_METHOD(xmlreader, open) xmlTextReaderPtr reader = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!l", &source, &source_len, &encoding, &encoding_len, &options) == FAILURE) { - return; + RETURN_THROWS(); } id = getThis(); @@ -869,8 +858,8 @@ PHP_METHOD(xmlreader, open) } if (!source_len) { - php_error_docref(NULL, E_WARNING, "Empty string supplied as input"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } valid_file = _xmlreader_get_valid_file_path(source, resolved_path, MAXPATHLEN ); @@ -899,39 +888,35 @@ PHP_METHOD(xmlreader, open) /* }}} */ /* Not Yet Implemented in libxml - functions exist just not coded -PHP_METHOD(xmlreader, resetState) +PHP_METHOD(XMLReader, resetState) { } */ -/* {{{ proto string XMLReader::readInnerXml() -Reads the contents of the current node, including child nodes and markup. */ -PHP_METHOD(xmlreader, readInnerXml) +/* {{{ Reads the contents of the current node, including child nodes and markup. */ +PHP_METHOD(XMLReader, readInnerXml) { php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadInnerXml); } /* }}} */ -/* {{{ proto bool XMLReader::readOuterXml() -Reads the contents of the current node, including child nodes and markup. */ -PHP_METHOD(xmlreader, readOuterXml) +/* {{{ Reads the contents of the current node, including child nodes and markup. */ +PHP_METHOD(XMLReader, readOuterXml) { php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadOuterXml); } /* }}} */ -/* {{{ proto bool XMLReader::readString() -Reads the contents of an element or a text node as a string. */ -PHP_METHOD(xmlreader, readString) +/* {{{ Reads the contents of an element or a text node as a string. */ +PHP_METHOD(XMLReader, readString) { php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadString); } /* }}} */ -/* {{{ proto bool XMLReader::setSchema(string filename) -Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first Read(). */ -PHP_METHOD(xmlreader, setSchema) +/* {{{ Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first Read(). */ +PHP_METHOD(XMLReader, setSchema) { #ifdef LIBXML_SCHEMAS_ENABLED zval *id; @@ -941,12 +926,12 @@ PHP_METHOD(xmlreader, setSchema) char *source; if (zend_parse_parameters(ZEND_NUM_ARGS(), "p!", &source, &source_len) == FAILURE) { - return; + RETURN_THROWS(); } if (source != NULL && !source_len) { - php_error_docref(NULL, E_WARNING, "Schema data source is required"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } id = ZEND_THIS; @@ -957,24 +942,24 @@ PHP_METHOD(xmlreader, setSchema) if (retval == 0) { RETURN_TRUE; + } else { + php_error_docref(NULL, E_WARNING, "Schema contains errors"); + RETURN_FALSE; } + } else { + zend_throw_error(NULL, "Schema must be set prior to reading"); + RETURN_THROWS(); } - - php_error_docref(NULL, E_WARNING, "Unable to set schema. This must be set prior to reading or schema contains errors."); - - RETURN_FALSE; #else - php_error_docref(NULL, E_WARNING, "No Schema support built into libxml."); - + php_error_docref(NULL, E_WARNING, "No schema support built into libxml"); RETURN_FALSE; #endif } /* }}} */ -/* {{{ proto bool XMLReader::setParserProperty(int property, bool value) -Sets parser property (one of the parser option constants). +/* {{{ Sets parser property (one of the parser option constants). Properties must be set after open() or XML() and before the first read() is called */ -PHP_METHOD(xmlreader, setParserProperty) +PHP_METHOD(XMLReader, setParserProperty) { zval *id; zend_long property; @@ -983,7 +968,7 @@ PHP_METHOD(xmlreader, setParserProperty) xmlreader_object *intern; if (zend_parse_parameters(ZEND_NUM_ARGS(), "lb", &property, &value) == FAILURE) { - return; + RETURN_THROWS(); } id = ZEND_THIS; @@ -993,25 +978,23 @@ PHP_METHOD(xmlreader, setParserProperty) retval = xmlTextReaderSetParserProp(intern->ptr,property, value); } if (retval == -1) { - php_error_docref(NULL, E_WARNING, "Invalid parser property"); - RETURN_FALSE; + zend_argument_value_error(1, "must be a valid parser property"); + RETURN_THROWS(); } RETURN_TRUE; } /* }}} */ -/* {{{ proto bool XMLReader::setRelaxNGSchema(string filename) -Sets the string that the XMLReader will parse. */ -PHP_METHOD(xmlreader, setRelaxNGSchema) +/* {{{ Sets the string that the XMLReader will parse. */ +PHP_METHOD(XMLReader, setRelaxNGSchema) { php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAM_PASSTHRU, XMLREADER_LOAD_FILE); } /* }}} */ -/* {{{ proto bool XMLReader::setRelaxNGSchemaSource(string source) -Sets the string that the XMLReader will parse. */ -PHP_METHOD(xmlreader, setRelaxNGSchemaSource) +/* {{{ Sets the string that the XMLReader will parse. */ +PHP_METHOD(XMLReader, setRelaxNGSchemaSource) { php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAM_PASSTHRU, XMLREADER_LOAD_STRING); } @@ -1023,9 +1006,8 @@ XMLPUBFUN int XMLCALL xmlSchemaPtr schema); */ -/* {{{ proto bool XMLReader::XML(string source [, string encoding [, int options]]) -Sets the string that the XMLReader will parse. */ -PHP_METHOD(xmlreader, XML) +/* {{{ Sets the string that the XMLReader will parse. */ +PHP_METHOD(XMLReader, XML) { zval *id; size_t source_len = 0, encoding_len = 0; @@ -1038,7 +1020,7 @@ PHP_METHOD(xmlreader, XML) xmlTextReaderPtr reader; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!l", &source, &source_len, &encoding, &encoding_len, &options) == FAILURE) { - return; + RETURN_THROWS(); } id = getThis(); @@ -1051,17 +1033,17 @@ PHP_METHOD(xmlreader, XML) } if (!source_len) { - php_error_docref(NULL, E_WARNING, "Empty string supplied as input"); - RETURN_FALSE; + zend_argument_value_error(1, "cannot be empty"); + RETURN_THROWS(); } inputbfr = xmlParserInputBufferCreateMem(source, source_len, XML_CHAR_ENCODING_NONE); if (inputbfr != NULL) { /* Get the URI of the current script so that we can set the base directory in libxml */ -#if HAVE_GETCWD +#ifdef HAVE_GETCWD directory = VCWD_GETCWD(resolved_path, MAXPATHLEN); -#elif HAVE_GETWD +#elif defined(HAVE_GETWD) directory = VCWD_GETWD(resolved_path); #endif if (directory) { @@ -1107,9 +1089,8 @@ PHP_METHOD(xmlreader, XML) } /* }}} */ -/* {{{ proto bool XMLReader::expand() -Moves the position of the current instance to the next node in the stream. */ -PHP_METHOD(xmlreader, expand) +/* {{{ Moves the position of the current instance to the next node in the stream. */ +PHP_METHOD(XMLReader, expand) { #ifdef HAVE_DOM zval *id, *basenode = NULL; @@ -1121,7 +1102,7 @@ PHP_METHOD(xmlreader, expand) id = ZEND_THIS; if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &basenode, dom_node_class_entry) == FAILURE) { - return; + RETURN_THROWS(); } if (basenode != NULL) { @@ -1135,7 +1116,7 @@ PHP_METHOD(xmlreader, expand) node = xmlTextReaderExpand(intern->ptr); if (node == NULL) { - php_error_docref(NULL, E_WARNING, "An Error Occurred while expanding "); + php_error_docref(NULL, E_WARNING, "An Error Occurred while expanding"); RETURN_FALSE; } else { nodec = xmlDocCopyNode(node, docp, 1); @@ -1147,150 +1128,22 @@ PHP_METHOD(xmlreader, expand) } } } else { - php_error_docref(NULL, E_WARNING, "Load Data before trying to expand"); - RETURN_FALSE; + zend_throw_error(NULL, "Data must be loaded before expanding"); + RETURN_THROWS(); } #else - php_error(E_WARNING, "DOM support is not enabled"); - return; + zval *dummy; + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &dummy) == FAILURE) { + RETURN_THROWS(); + } + + zend_throw_error(NULL, "XMLReader::expand() requires the DOM extension to be enabled"); + RETURN_THROWS(); #endif } /* }}} */ -/* {{{ arginfo */ -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_close, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttribute, 0) - ZEND_ARG_INFO(0, name) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttributeNo, 0) - ZEND_ARG_INFO(0, index) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttributeNs, 0) - ZEND_ARG_INFO(0, name) - ZEND_ARG_INFO(0, namespaceURI) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getParserProperty, 0) - ZEND_ARG_INFO(0, property) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_isValid, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_lookupNamespace, 0) -ZEND_ARG_INFO(0, prefix) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttribute, 0) - ZEND_ARG_INFO(0, name) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttributeNo, 0) - ZEND_ARG_INFO(0, index) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttributeNs, 0) - ZEND_ARG_INFO(0, name) - ZEND_ARG_INFO(0, namespaceURI) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToElement, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToFirstAttribute, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToNextAttribute, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_read, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_next, 0, 0, 0) - ZEND_ARG_INFO(0, localname) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_open, 0, 0, 1) - ZEND_ARG_INFO(0, URI) - ZEND_ARG_INFO(0, encoding) - ZEND_ARG_INFO(0, options) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readInnerXml, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readOuterXml, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readString, 0) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setSchema, 0) - ZEND_ARG_INFO(0, filename) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setParserProperty, 0) - ZEND_ARG_INFO(0, property) - ZEND_ARG_INFO(0, value) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setRelaxNGSchema, 0) - ZEND_ARG_INFO(0, filename) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setRelaxNGSchemaSource, 0) - ZEND_ARG_INFO(0, source) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_XML, 0, 0, 1) - ZEND_ARG_INFO(0, source) - ZEND_ARG_INFO(0, encoding) - ZEND_ARG_INFO(0, options) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_expand, 0, 0, 0) - ZEND_ARG_INFO(0, basenode) -ZEND_END_ARG_INFO() -/* }}} */ - -static const zend_function_entry xmlreader_functions[] /* {{{ */ = { - PHP_ME(xmlreader, close, arginfo_xmlreader_close, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, getAttribute, arginfo_xmlreader_getAttribute, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, getAttributeNo, arginfo_xmlreader_getAttributeNo, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, getAttributeNs, arginfo_xmlreader_getAttributeNs, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, getParserProperty, arginfo_xmlreader_getParserProperty, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, isValid, arginfo_xmlreader_isValid, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, lookupNamespace, arginfo_xmlreader_lookupNamespace, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToAttributeNo, arginfo_xmlreader_moveToAttributeNo, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToAttribute, arginfo_xmlreader_moveToAttribute, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToAttributeNs, arginfo_xmlreader_moveToAttributeNs, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToElement, arginfo_xmlreader_moveToElement, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToFirstAttribute, arginfo_xmlreader_moveToFirstAttribute, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, moveToNextAttribute, arginfo_xmlreader_moveToNextAttribute, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, open, arginfo_xmlreader_open, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC) - PHP_ME(xmlreader, read, arginfo_xmlreader_read, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, next, arginfo_xmlreader_next, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, readInnerXml, arginfo_xmlreader_readInnerXml, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, readOuterXml, arginfo_xmlreader_readOuterXml, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, readString, arginfo_xmlreader_readString, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, setSchema, arginfo_xmlreader_setSchema, ZEND_ACC_PUBLIC) -/* Not Yet Implemented though defined in libxml as of 2.6.9dev - PHP_ME(xmlreader, resetState, NULL, ZEND_ACC_PUBLIC) -*/ - PHP_ME(xmlreader, setParserProperty, arginfo_xmlreader_setParserProperty, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, setRelaxNGSchema, arginfo_xmlreader_setRelaxNGSchema, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, setRelaxNGSchemaSource, arginfo_xmlreader_setRelaxNGSchemaSource, ZEND_ACC_PUBLIC) - PHP_ME(xmlreader, XML, arginfo_xmlreader_XML, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC) - PHP_ME(xmlreader, expand, arginfo_xmlreader_expand, ZEND_ACC_PUBLIC) - PHP_FE_END -}; /* }}} */ - -/* {{{ PHP_MINIT_FUNCTION - */ +/* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(xmlreader) { @@ -1303,12 +1156,18 @@ PHP_MINIT_FUNCTION(xmlreader) xmlreader_object_handlers.read_property = xmlreader_read_property; xmlreader_object_handlers.write_property = xmlreader_write_property; xmlreader_object_handlers.get_property_ptr_ptr = xmlreader_get_property_ptr_ptr; + xmlreader_object_handlers.get_method = xmlreader_get_method; xmlreader_object_handlers.clone_obj = NULL; - INIT_CLASS_ENTRY(ce, "XMLReader", xmlreader_functions); + INIT_CLASS_ENTRY(ce, "XMLReader", class_XMLReader_methods); ce.create_object = xmlreader_objects_new; xmlreader_class_entry = zend_register_internal_class(&ce); + memcpy(&xmlreader_open_fn, zend_hash_str_find_ptr(&xmlreader_class_entry->function_table, "open", sizeof("open")-1), sizeof(zend_internal_function)); + xmlreader_open_fn.fn_flags &= ~ZEND_ACC_STATIC; + memcpy(&xmlreader_xml_fn, zend_hash_str_find_ptr(&xmlreader_class_entry->function_table, "xml", sizeof("xml")-1), sizeof(zend_internal_function)); + xmlreader_xml_fn.fn_flags &= ~ZEND_ACC_STATIC; + zend_hash_init(&xmlreader_prop_handlers, 0, NULL, php_xmlreader_free_prop_handler, 1); xmlreader_register_prop_handler(&xmlreader_prop_handlers, "attributeCount", xmlTextReaderAttributeCount, NULL, IS_LONG); xmlreader_register_prop_handler(&xmlreader_prop_handlers, "baseURI", NULL, xmlTextReaderConstBaseUri, IS_STRING); @@ -1363,8 +1222,7 @@ PHP_MINIT_FUNCTION(xmlreader) } /* }}} */ -/* {{{ PHP_MSHUTDOWN_FUNCTION - */ +/* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(xmlreader) { zend_hash_destroy(&xmlreader_prop_handlers); @@ -1372,8 +1230,7 @@ PHP_MSHUTDOWN_FUNCTION(xmlreader) } /* }}} */ -/* {{{ PHP_MINFO_FUNCTION - */ +/* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(xmlreader) { php_info_print_table_start(); |