PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - dom - xpath.c
Test: PHP Code Coverage
Date: 2009-11-23 Instrumented lines: 268
Code covered: 61.6 % Executed lines: 165
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 6                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 The PHP Group                                |
       6                 :    +----------------------------------------------------------------------+
       7                 :    | This source file is subject to version 3.01 of the PHP license,      |
       8                 :    | that is bundled with this package in the file LICENSE, and is        |
       9                 :    | available through the world-wide-web at the following url:           |
      10                 :    | http://www.php.net/license/3_01.txt                                  |
      11                 :    | If you did not receive a copy of the PHP license and are unable to   |
      12                 :    | obtain it through the world-wide-web, please send a note to          |
      13                 :    | license@php.net so we can mail you a copy immediately.               |
      14                 :    +----------------------------------------------------------------------+
      15                 :    | Authors: Christian Stocker <chregu@php.net>                          |
      16                 :    |          Rob Richards <rrichards@php.net>                            |
      17                 :    +----------------------------------------------------------------------+
      18                 : */
      19                 : 
      20                 : /* $Id: xpath.c 281741 2009-06-06 02:40:14Z mattwil $ */
      21                 : 
      22                 : #ifdef HAVE_CONFIG_H
      23                 : #include "config.h"
      24                 : #endif
      25                 : 
      26                 : #include "php.h"
      27                 : #if HAVE_LIBXML && HAVE_DOM
      28                 : #include "php_dom.h"
      29                 : 
      30                 : #define PHP_DOM_XPATH_QUERY 0
      31                 : #define PHP_DOM_XPATH_EVALUATE 1
      32                 : 
      33                 : /*
      34                 : * class DOMXPath 
      35                 : */
      36                 : 
      37                 : #if defined(LIBXML_XPATH_ENABLED)
      38                 : 
      39                 : /* {{{ arginfo */
      40                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_construct, 0, 0, 1)
      41                 :         ZEND_ARG_OBJ_INFO(0, doc, DOMDocument, 0)
      42                 : ZEND_END_ARG_INFO();
      43                 : 
      44                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_register_ns, 0, 0, 2)
      45                 :         ZEND_ARG_INFO(0, prefix)
      46                 :         ZEND_ARG_INFO(0, uri)
      47                 : ZEND_END_ARG_INFO();
      48                 : 
      49                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_query, 0, 0, 1)
      50                 :         ZEND_ARG_INFO(0, expr)
      51                 :         ZEND_ARG_OBJ_INFO(0, context, DOMNode, 0)
      52                 : ZEND_END_ARG_INFO();
      53                 : 
      54                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_evaluate, 0, 0, 1)
      55                 :         ZEND_ARG_INFO(0, expr)
      56                 :         ZEND_ARG_OBJ_INFO(0, context, DOMNode, 0)
      57                 : ZEND_END_ARG_INFO();
      58                 : 
      59                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_register_php_functions, 0, 0, 0)
      60                 : ZEND_END_ARG_INFO();
      61                 : /* }}} */
      62                 : 
      63                 : const zend_function_entry php_dom_xpath_class_functions[] = {
      64                 :         PHP_ME(domxpath, __construct, arginfo_dom_xpath_construct, ZEND_ACC_PUBLIC)
      65                 :         PHP_FALIAS(registerNamespace, dom_xpath_register_ns, arginfo_dom_xpath_register_ns)
      66                 :         PHP_FALIAS(query, dom_xpath_query, arginfo_dom_xpath_query)
      67                 :         PHP_FALIAS(evaluate, dom_xpath_evaluate, arginfo_dom_xpath_evaluate)
      68                 :         PHP_FALIAS(registerPhpFunctions, dom_xpath_register_php_functions, arginfo_dom_xpath_register_php_functions)
      69                 :         {NULL, NULL, NULL}
      70                 : };
      71                 : 
      72                 : 
      73                 : static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */
      74               1 : {
      75                 :         zval **args;
      76                 :         zval *retval;
      77                 :         int result, i, ret;
      78               1 :         int error = 0;
      79                 :         zend_fcall_info fci;
      80                 :         zval handler;
      81                 :         xmlXPathObjectPtr obj;
      82                 :         char *str;
      83                 :         zval callable;
      84                 :         dom_xpath_object *intern;
      85                 :         
      86                 :         TSRMLS_FETCH();
      87                 : 
      88               1 :         if (! zend_is_executing(TSRMLS_C)) {
      89               0 :                 xmlGenericError(xmlGenericErrorContext,
      90                 :                 "xmlExtFunctionTest: Function called from outside of PHP\n");
      91               0 :                 error = 1;
      92                 :         } else {
      93               1 :                 intern = (dom_xpath_object *) ctxt->context->userData;
      94               1 :                 if (intern == NULL) {
      95               0 :                         xmlGenericError(xmlGenericErrorContext,
      96                 :                         "xmlExtFunctionTest: failed to get the internal object\n");
      97               0 :                         error = 1;
      98                 :                 }
      99               1 :                 else if (intern->registerPhpFunctions == 0) {
     100               0 :                         xmlGenericError(xmlGenericErrorContext,
     101                 :                         "xmlExtFunctionTest: PHP Object did not register PHP functions\n");
     102               0 :                         error = 1;
     103                 :                 }
     104                 :         }
     105                 :         
     106               1 :         if (error == 1) {
     107               0 :                 for (i = nargs - 1; i >= 0; i--) {
     108               0 :                         obj = valuePop(ctxt);
     109               0 :                         xmlXPathFreeObject(obj);
     110                 :                 }
     111               0 :                 return;
     112                 :         }
     113                 :                 
     114               1 :         fci.param_count = nargs - 1;
     115               1 :         if (fci.param_count > 0) {
     116               1 :                 fci.params = safe_emalloc(fci.param_count, sizeof(zval**), 0);
     117               1 :                 args = safe_emalloc(fci.param_count, sizeof(zval *), 0);
     118                 :         }
     119                 :         /* Reverse order to pop values off ctxt stack */
     120               2 :         for (i = nargs - 2; i >= 0; i--) {
     121               1 :                 obj = valuePop(ctxt);
     122               1 :                 MAKE_STD_ZVAL(args[i]);
     123               1 :                 switch (obj->type) {
     124                 :                         case XPATH_STRING:
     125               0 :                                 ZVAL_STRING(args[i],  (char *)obj->stringval, 1);
     126               0 :                                 break;
     127                 :                         case XPATH_BOOLEAN:
     128               0 :                                 ZVAL_BOOL(args[i],  obj->boolval);
     129               0 :                                 break;
     130                 :                         case XPATH_NUMBER:
     131               0 :                                 ZVAL_DOUBLE(args[i], obj->floatval);
     132               0 :                                 break;
     133                 :                         case XPATH_NODESET:
     134               1 :                                 if (type == 1) {
     135               0 :                                         str = (char *)xmlXPathCastToString(obj);
     136               0 :                                         ZVAL_STRING(args[i], str, 1);
     137               0 :                                         xmlFree(str);
     138               1 :                                 } else if (type == 2) {
     139                 :                                         int j;
     140               1 :                                         array_init(args[i]);
     141               1 :                                         if (obj->nodesetval && obj->nodesetval->nodeNr > 0) {
     142               5 :                                                 for (j = 0; j < obj->nodesetval->nodeNr; j++) {
     143               4 :                                                         xmlNodePtr node = obj->nodesetval->nodeTab[j];
     144                 :                                                         zval *child;
     145               4 :                                                         MAKE_STD_ZVAL(child);
     146                 :                                                         /* not sure, if we need this... it's copied from xpath.c */
     147               4 :                                                         if (node->type == XML_NAMESPACE_DECL) {
     148                 :                                                                 xmlNsPtr curns;
     149                 :                                                                 xmlNodePtr nsparent;
     150                 :                                                                 
     151               0 :                                                                 nsparent = node->_private;
     152               0 :                                                                 curns = xmlNewNs(NULL, node->name, NULL);
     153               0 :                                                                 if (node->children) {
     154               0 :                                                                         curns->prefix = xmlStrdup((xmlChar *) node->children);
     155                 :                                                                 }
     156               0 :                                                                 if (node->children) {
     157               0 :                                                                         node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name);
     158                 :                                                                 } else {
     159               0 :                                                                         node = xmlNewDocNode(node->doc, NULL, (xmlChar *) "xmlns", node->name);
     160                 :                                                                 }
     161               0 :                                                                 node->type = XML_NAMESPACE_DECL;
     162               0 :                                                                 node->parent = nsparent;
     163               0 :                                                                 node->ns = curns;
     164                 :                                                         }
     165               4 :                                                         child = php_dom_create_object(node, &ret, NULL, child, (dom_object *)intern TSRMLS_CC);
     166               4 :                                                         add_next_index_zval(args[i], child);
     167                 :                                                 }
     168                 :                                         }
     169                 :                                 }
     170               1 :                                 break;
     171                 :                         default:
     172               0 :                         ZVAL_STRING(args[i], (char *)xmlXPathCastToString(obj), 1);
     173                 :                 }
     174               1 :                 xmlXPathFreeObject(obj);
     175               1 :                 fci.params[i] = &args[i];
     176                 :         }
     177                 :         
     178               1 :         fci.size = sizeof(fci);
     179               1 :         fci.function_table = EG(function_table);
     180                 :         
     181               1 :         obj = valuePop(ctxt);
     182               1 :         if (obj->stringval == NULL) {
     183               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Handler name must be a string");
     184               0 :                 xmlXPathFreeObject(obj);
     185               0 :                 if (fci.param_count > 0) {
     186               0 :                         for (i = 0; i < nargs - 1; i++) {
     187               0 :                                 zval_ptr_dtor(&args[i]);
     188                 :                         }
     189               0 :                         efree(args);
     190               0 :                         efree(fci.params);
     191                 :                 }
     192               0 :                 return; 
     193                 :         }
     194               1 :         INIT_PZVAL(&handler);
     195               1 :         ZVAL_XML_STRING(&handler, (char *)obj->stringval, 1);
     196               1 :         xmlXPathFreeObject(obj);
     197                 : 
     198               1 :         fci.function_name = &handler;
     199               1 :         fci.symbol_table = NULL;
     200               1 :         fci.object_ptr = NULL;
     201               1 :         fci.retval_ptr_ptr = &retval;
     202               1 :         fci.no_separation = 0;
     203                 :         /*fci.function_handler_cache = &function_ptr;*/
     204               1 :         if (!zend_make_callable(&handler, &callable TSRMLS_CC)) {
     205               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call handler %R()", Z_TYPE(callable), Z_UNIVAL(callable));
     206                 :                 
     207               1 :         } else if ( intern->registerPhpFunctions == 2 && zend_u_hash_exists(intern->registered_phpfunctions, Z_TYPE(callable), Z_UNIVAL(callable), Z_UNILEN(callable) + 1) == 0) { 
     208               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not allowed to call handler '%R()'", Z_TYPE(callable), Z_UNIVAL(callable));
     209                 :                 /* Push an empty string, so that we at least have a result... */
     210               0 :                 valuePush(ctxt, xmlXPathNewString((xmlChar *)""));
     211                 :         } else {
     212               1 :                 result = zend_call_function(&fci, NULL TSRMLS_CC);
     213               1 :                 if (result == FAILURE) {
     214               0 :                         if (Z_TYPE(callable) == IS_STRING || Z_TYPE(callable) == IS_UNICODE) {
     215               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call handler '%R()'", Z_TYPE(callable), Z_UNIVAL(callable));
     216                 :                         }
     217                 :                 /* retval is == NULL, when an exception occured, don't report anything, because PHP itself will handle that */
     218               1 :                 } else if (retval == NULL) {
     219                 :                 } else {
     220               1 :                         if (retval->type == IS_OBJECT && instanceof_function( Z_OBJCE_P(retval), dom_node_class_entry TSRMLS_CC)) {
     221                 :                                 xmlNode *nodep;
     222                 :                                 dom_object *obj;
     223               0 :                                 if (intern->node_list == NULL) {
     224               0 :                                         ALLOC_HASHTABLE(intern->node_list);
     225               0 :                                         zend_hash_init(intern->node_list, 0, NULL, ZVAL_PTR_DTOR, 0);
     226                 :                                 }
     227               0 :                                 zval_add_ref(&retval);
     228               0 :                                 zend_hash_next_index_insert(intern->node_list, &retval, sizeof(zval *), NULL);
     229               0 :                                 obj = (dom_object *)zend_object_store_get_object(retval TSRMLS_CC);
     230               0 :                                 nodep = dom_object_get_node(obj);
     231               0 :                                 valuePush(ctxt, xmlXPathNewNodeSet(nodep));
     232               1 :                         } else if (retval->type == IS_BOOL) {
     233               0 :                                 valuePush(ctxt, xmlXPathNewBoolean(retval->value.lval));
     234               1 :                         } else if (retval->type == IS_OBJECT) {
     235               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A PHP Object cannot be converted to a XPath-string");
     236               0 :                                 valuePush(ctxt, xmlXPathNewString((xmlChar *)""));
     237                 :                         } else {
     238               1 :                                 convert_to_string_with_converter(retval, UG(utf8_conv));
     239               1 :                                 valuePush(ctxt, xmlXPathNewString((xmlChar *) Z_STRVAL_P(retval)));
     240                 :                         }
     241               1 :                         zval_ptr_dtor(&retval);
     242                 :                 }
     243                 :         }
     244               1 :         zval_dtor(&callable);
     245               1 :         zval_dtor(&handler);
     246               1 :         if (fci.param_count > 0) {
     247               2 :                 for (i = 0; i < nargs - 1; i++) {
     248               1 :                         zval_ptr_dtor(&args[i]);
     249                 :                 }
     250               1 :                 efree(args);
     251               1 :                 efree(fci.params);
     252                 :         }
     253                 : }
     254                 : /* }}} */
     255                 : 
     256                 : static void dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
     257               0 : {
     258               0 :         dom_xpath_ext_function_php(ctxt, nargs, 1);
     259               0 : }
     260                 : /* }}} */
     261                 : 
     262                 : static void dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
     263               1 : {
     264               1 :         dom_xpath_ext_function_php(ctxt, nargs, 2);
     265               1 : }
     266                 : /* }}} */
     267                 : 
     268                 : /* {{{ proto void DOMXPath::__construct(DOMDocument doc) U */
     269                 : PHP_METHOD(domxpath, __construct)
     270               6 : {
     271                 :         zval *id, *doc;
     272               6 :         xmlDocPtr docp = NULL;
     273                 :         dom_object *docobj;
     274                 :         dom_xpath_object *intern;
     275                 :         xmlXPathContextPtr ctx, oldctx;
     276                 :         zend_error_handling error_handling;
     277                 : 
     278               6 :         zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
     279               6 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_xpath_class_entry, &doc, dom_document_class_entry) == FAILURE) {
     280               0 :                 zend_restore_error_handling(&error_handling TSRMLS_CC);
     281               0 :                 return;
     282                 :         }
     283                 : 
     284               6 :         zend_restore_error_handling(&error_handling TSRMLS_CC);
     285               6 :         DOM_GET_OBJ(docp, doc, xmlDocPtr, docobj);
     286                 : 
     287               6 :         ctx = xmlXPathNewContext(docp);
     288               6 :         if (ctx == NULL) {
     289               0 :                 php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
     290               0 :                 RETURN_FALSE;
     291                 :         }
     292                 : 
     293               6 :         intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     294               6 :         if (intern != NULL) {
     295               6 :                 oldctx = (xmlXPathContextPtr)intern->ptr;
     296               6 :                 if (oldctx != NULL) {
     297               0 :                         php_libxml_decrement_doc_ref((php_libxml_node_object *)intern TSRMLS_CC);
     298               0 :                         xmlXPathFreeContext(oldctx);
     299                 :                 }
     300                 : 
     301               6 :                 xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",
     302                 :                                            (const xmlChar *) "http://php.net/xpath",
     303                 :                                            dom_xpath_ext_function_string_php);
     304               6 :                 xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "function",
     305                 :                                            (const xmlChar *) "http://php.net/xpath",
     306                 :                                            dom_xpath_ext_function_object_php);
     307                 : 
     308               6 :                 intern->ptr = ctx;
     309               6 :                 ctx->userData = (void *)intern;
     310               6 :                 intern->document = docobj->document;
     311               6 :                 php_libxml_increment_doc_ref((php_libxml_node_object *)intern, docp TSRMLS_CC);
     312                 :         }
     313                 : }
     314                 : /* }}} end DOMXPath::__construct */
     315                 : 
     316                 : /* {{{ document DOMDocument*/
     317                 : int dom_xpath_document_read(dom_object *obj, zval **retval TSRMLS_DC)
     318               1 : {
     319               1 :         xmlDoc *docp = NULL;
     320                 :         xmlXPathContextPtr ctx;
     321                 :         int ret;
     322                 : 
     323               1 :         ctx = (xmlXPathContextPtr) obj->ptr;
     324                 : 
     325               1 :         if (ctx) {
     326               1 :                 docp = (xmlDocPtr) ctx->doc;
     327                 :         }
     328                 : 
     329               1 :         ALLOC_ZVAL(*retval);
     330               1 :         if (NULL == (*retval = php_dom_create_object((xmlNodePtr) docp, &ret, NULL, *retval, obj TSRMLS_CC))) {
     331               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object");
     332               0 :                 return FAILURE;
     333                 :         }
     334               1 :         return SUCCESS;
     335                 : }
     336                 : /* }}} */
     337                 : 
     338                 : /* {{{ proto boolean dom_xpath_register_ns(string prefix, string uri) U */
     339                 : PHP_FUNCTION(dom_xpath_register_ns)
     340               2 : {
     341                 :         zval *id;
     342                 :         xmlXPathContextPtr ctxp;
     343                 :         int prefix_len, ns_uri_len;
     344                 :         dom_xpath_object *intern;
     345                 :         unsigned char *prefix, *ns_uri;
     346                 : 
     347               2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&s&", &id, dom_xpath_class_entry, &prefix, &prefix_len, UG(utf8_conv), &ns_uri, &ns_uri_len, UG(utf8_conv)) == FAILURE) {
     348               0 :                 return;
     349                 :         }
     350                 : 
     351               2 :         intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     352                 : 
     353               2 :         ctxp = (xmlXPathContextPtr) intern->ptr;
     354               2 :         if (ctxp == NULL) {
     355               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid XPath Context");
     356               0 :                 RETURN_FALSE;
     357                 :         }
     358                 : 
     359               2 :         if (xmlXPathRegisterNs(ctxp, prefix, ns_uri) != 0) {
     360               0 :                 RETURN_FALSE
     361                 :         }
     362               2 :         RETURN_TRUE;
     363                 : }
     364                 : /* }}} */
     365                 : 
     366                 : static void dom_xpath_iter(zval *baseobj, dom_object *intern) /* {{{ */
     367               6 : {
     368                 :         dom_nnodemap_object *mapptr;
     369                 : 
     370               6 :         mapptr = (dom_nnodemap_object *)intern->ptr;
     371               6 :         mapptr->baseobjptr = baseobj;
     372               6 :         mapptr->nodetype = DOM_NODESET;
     373                 : 
     374               6 : }
     375                 : /* }}} */
     376                 : 
     377                 : static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
     378               8 : {
     379               8 :         zval *id, *retval, *context = NULL;
     380                 :         xmlXPathContextPtr ctxp;
     381               8 :         xmlNodePtr nodep = NULL;
     382                 :         xmlXPathObjectPtr xpathobjp;
     383               8 :         int expr_len, ret, nsnbr = 0, xpath_type;
     384                 :         dom_xpath_object *intern;
     385                 :         dom_object *nodeobj;
     386                 :         char *expr;
     387               8 :         xmlDoc *docp = NULL;
     388                 :         xmlNsPtr *ns;
     389                 : 
     390               8 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&|O", &id, dom_xpath_class_entry, &expr, &expr_len, UG(utf8_conv), &context, dom_node_class_entry) == FAILURE) {
     391               0 :                 return;
     392                 :         }
     393                 : 
     394               8 :         intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     395                 : 
     396               8 :         ctxp = (xmlXPathContextPtr) intern->ptr;
     397               8 :         if (ctxp == NULL) {
     398               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid XPath Context");
     399               0 :                 RETURN_FALSE;
     400                 :         }
     401                 : 
     402               8 :         docp = (xmlDocPtr) ctxp->doc;
     403               8 :         if (docp == NULL) {
     404               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid XPath Document Pointer");
     405               0 :                 RETURN_FALSE;
     406                 :         }
     407                 : 
     408               8 :         if (context != NULL) {
     409               1 :                 DOM_GET_OBJ(nodep, context, xmlNodePtr, nodeobj);
     410                 :         }
     411                 : 
     412               8 :         if (!nodep) {
     413               7 :                 nodep = xmlDocGetRootElement(docp);
     414                 :         }
     415                 : 
     416               8 :         if (nodep && docp != nodep->doc) {
     417               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Node From Wrong Document");
     418               0 :                 RETURN_FALSE;
     419                 :         }
     420                 : 
     421               8 :         ctxp->node = nodep;
     422                 : 
     423                 :         /* Register namespaces in the node */
     424               8 :         ns = xmlGetNsList(docp, nodep);
     425                 : 
     426               8 :     if (ns != NULL) {
     427              12 :         while (ns[nsnbr] != NULL)
     428               4 :             nsnbr++;
     429                 :     }
     430                 : 
     431                 : 
     432               8 :     ctxp->namespaces = ns;
     433               8 :     ctxp->nsNr = nsnbr;
     434                 : 
     435               8 :         xpathobjp = xmlXPathEvalExpression(expr, ctxp);
     436               8 :         ctxp->node = NULL;
     437                 : 
     438               8 :         if (ns != NULL) {
     439               4 :                 xmlFree(ns);
     440               4 :                 ctxp->namespaces = NULL;
     441               4 :                 ctxp->nsNr = 0;
     442                 :         }
     443                 : 
     444               8 :         if (! xpathobjp) {
     445               0 :                 RETURN_FALSE;
     446                 :         }
     447                 : 
     448               8 :         if (type == PHP_DOM_XPATH_QUERY) {
     449               6 :                 xpath_type = XPATH_NODESET;
     450                 :         } else {
     451               2 :                 xpath_type = xpathobjp->type;
     452                 :         }
     453                 : 
     454               8 :         switch (xpath_type) {
     455                 : 
     456                 :                 case  XPATH_NODESET:
     457                 :                 {
     458                 :                         int i;
     459                 :                         xmlNodeSetPtr nodesetp;
     460                 : 
     461               6 :                         MAKE_STD_ZVAL(retval);
     462               6 :                         array_init(retval);
     463                 : 
     464               6 :                         if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval)) {
     465                 : 
     466              11 :                                 for (i = 0; i < nodesetp->nodeNr; i++) {
     467               5 :                                         xmlNodePtr node = nodesetp->nodeTab[i];
     468                 :                                         zval *child;
     469                 : 
     470               5 :                                         MAKE_STD_ZVAL(child);
     471                 :                                         
     472               5 :                                         if (node->type == XML_NAMESPACE_DECL) {
     473                 :                                                 xmlNsPtr curns;
     474                 :                                                 xmlNodePtr nsparent;
     475                 : 
     476               0 :                                                 nsparent = node->_private;
     477               0 :                                                 curns = xmlNewNs(NULL, node->name, NULL);
     478               0 :                                                 if (node->children) {
     479               0 :                                                         curns->prefix = xmlStrdup((char *) node->children);
     480                 :                                                 }
     481               0 :                                                 if (node->children) {
     482               0 :                                                         node = xmlNewDocNode(docp, NULL, (char *) node->children, node->name);
     483                 :                                                 } else {
     484               0 :                                                         node = xmlNewDocNode(docp, NULL, "xmlns", node->name);
     485                 :                                                 }
     486               0 :                                                 node->type = XML_NAMESPACE_DECL;
     487               0 :                                                 node->parent = nsparent;
     488               0 :                                                 node->ns = curns;
     489                 :                                         }
     490               5 :                                         child = php_dom_create_object(node, &ret, NULL, child, (dom_object *)intern TSRMLS_CC);
     491               5 :                                         add_next_index_zval(retval, child);
     492                 :                                 }
     493                 :                         }
     494               6 :                         php_dom_create_interator(return_value, DOM_NODELIST TSRMLS_CC);
     495               6 :                         nodeobj = (dom_object *)zend_objects_get_address(return_value TSRMLS_CC);
     496               6 :                         dom_xpath_iter(retval, nodeobj);
     497               6 :                         break;
     498                 :                 }
     499                 : 
     500                 :                 case XPATH_BOOLEAN:
     501               0 :                         RETVAL_BOOL(xpathobjp->boolval);
     502               0 :                         break;
     503                 : 
     504                 :                 case XPATH_NUMBER:
     505               2 :                         RETVAL_DOUBLE(xpathobjp->floatval)
     506               2 :                         break;
     507                 : 
     508                 :                 case XPATH_STRING:
     509               0 :                         RETVAL_STRING(xpathobjp->stringval, 1);
     510               0 :                         break;
     511                 : 
     512                 :                 default:
     513               0 :                         RETVAL_NULL();
     514                 :                         break;
     515                 :         }
     516                 : 
     517               8 :         xmlXPathFreeObject(xpathobjp);
     518                 : }
     519                 : /* }}} */
     520                 : 
     521                 : /* {{{ proto DOMNodeList dom_xpath_query(string expr [,DOMNode context]) U */
     522                 : PHP_FUNCTION(dom_xpath_query)
     523               6 : {
     524               6 :         php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_QUERY);
     525               6 : }
     526                 : /* }}} end dom_xpath_query */
     527                 : 
     528                 : /* {{{ proto mixed dom_xpath_evaluate(string expr [,DOMNode context]) U */
     529                 : PHP_FUNCTION(dom_xpath_evaluate)
     530               2 : {
     531               2 :         php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_EVALUATE);
     532               2 : }
     533                 : /* }}} end dom_xpath_evaluate */
     534                 : 
     535                 : /* {{{ proto void dom_xpath_register_php_functions() U */
     536                 : PHP_FUNCTION(dom_xpath_register_php_functions)
     537               1 : {
     538                 :         zval *id;
     539                 :         dom_xpath_object *intern;
     540                 :         zval *array_value, **entry, *new_string;
     541               1 :         int  name_len = 0;
     542                 :         zstr name;
     543                 :         zend_uchar name_type;
     544                 : 
     545               1 :         DOM_GET_THIS(id);
     546                 :         
     547               1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "a",  &array_value) == SUCCESS) {
     548               0 :                 intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     549               0 :                 zend_hash_internal_pointer_reset(Z_ARRVAL_P(array_value));
     550                 : 
     551               0 :                 while (zend_hash_get_current_data(Z_ARRVAL_P(array_value), (void **)&entry) == SUCCESS) {
     552               0 :                         MAKE_STD_ZVAL(new_string);
     553               0 :                         ZVAL_LONG(new_string,1);
     554                 :                 
     555               0 :                         zend_u_hash_update(intern->registered_phpfunctions, Z_TYPE_PP(entry), Z_UNIVAL_PP(entry), Z_UNILEN_PP(entry) + 1, &new_string, sizeof(zval*), NULL);
     556               0 :                         zend_hash_move_forward(Z_ARRVAL_P(array_value));
     557                 :                 }
     558               0 :                 intern->registerPhpFunctions = 2;
     559               0 :                 RETURN_TRUE;
     560                 : 
     561               1 :         } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "t",  &name, &name_len, &name_type) == SUCCESS) {
     562               1 :                 intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     563                 :                 
     564               1 :                 MAKE_STD_ZVAL(new_string);
     565               1 :                 ZVAL_LONG(new_string,1);
     566               1 :                 zend_u_hash_update(intern->registered_phpfunctions, name_type, name, name_len + 1, &new_string, sizeof(zval*), NULL);
     567               1 :                 intern->registerPhpFunctions = 2;
     568                 :                 
     569                 :         } else {
     570               0 :                 intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
     571               0 :                 intern->registerPhpFunctions = 1;
     572                 :         }
     573                 :         
     574                 : }
     575                 : /* }}} end dom_xpath_register_php_functions */
     576                 : 
     577                 : #endif /* LIBXML_XPATH_ENABLED */
     578                 : 
     579                 : #endif
     580                 : 
     581                 : /*
     582                 :  * Local variables:
     583                 :  * tab-width: 4
     584                 :  * c-basic-offset: 4
     585                 :  * End:
     586                 :  * vim600: noet sw=4 ts=4 fdm=marker
     587                 :  * vim<600: noet sw=4 ts=4
     588                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Mon, 23 Nov 2009 17:39:29 +0000 (35 hours ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.