1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Christian Stocker <chregu@php.net> |
14 | Rob Richards <rrichards@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "zend_interfaces.h"
26
27 /*
28 * class DOMNodeList
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
31 * Since:
32 */
33
objmap_cache_release_cached_obj(dom_nnodemap_object * objmap)34 static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap)
35 {
36 if (objmap->cached_obj) {
37 /* Since the DOM is a tree there can be no cycles. */
38 if (GC_DELREF(&objmap->cached_obj->std) == 0) {
39 zend_objects_store_del(&objmap->cached_obj->std);
40 }
41 objmap->cached_obj = NULL;
42 objmap->cached_obj_index = 0;
43 }
44 }
45
reset_objmap_cache(dom_nnodemap_object * objmap)46 static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap)
47 {
48 objmap_cache_release_cached_obj(objmap);
49 objmap->cached_length = -1;
50 }
51
php_dom_get_nodelist_length(dom_object * obj)52 int php_dom_get_nodelist_length(dom_object *obj)
53 {
54 dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
55 if (!objmap) {
56 return 0;
57 }
58
59 if (objmap->ht) {
60 return xmlHashSize(objmap->ht);
61 }
62
63 if (objmap->nodetype == DOM_NODESET) {
64 HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
65 return zend_hash_num_elements(nodeht);
66 }
67
68 xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
69 if (!nodep) {
70 return 0;
71 }
72
73 if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
74 if (objmap->cached_length >= 0) {
75 return objmap->cached_length;
76 }
77 /* Only the length is out-of-date, the cache tag is still valid.
78 * Therefore, only overwrite the length and keep the currently cached object. */
79 } else {
80 php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
81 reset_objmap_cache(objmap);
82 }
83
84 int count = 0;
85 if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
86 xmlNodePtr curnode = nodep->children;
87 if (curnode) {
88 count++;
89 while (curnode->next != NULL) {
90 count++;
91 curnode = curnode->next;
92 }
93 }
94 } else {
95 xmlNodePtr basep = nodep;
96 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
97 nodep = xmlDocGetRootElement((xmlDoc *) nodep);
98 } else {
99 nodep = nodep->children;
100 }
101 dom_get_elements_by_tag_name_ns_raw(
102 basep, nodep, (char *) objmap->ns, (char *) objmap->local, &count, INT_MAX - 1 /* because of <= */);
103 }
104
105 objmap->cached_length = count;
106
107 return count;
108 }
109
110 /* {{{ length int
111 readonly=yes
112 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
113 Since:
114 */
dom_nodelist_length_read(dom_object * obj,zval * retval)115 zend_result dom_nodelist_length_read(dom_object *obj, zval *retval)
116 {
117 ZVAL_LONG(retval, php_dom_get_nodelist_length(obj));
118 return SUCCESS;
119 }
120
121
122 /* {{{ */
PHP_METHOD(DOMNodeList,count)123 PHP_METHOD(DOMNodeList, count)
124 {
125 zval *id;
126 dom_object *intern;
127
128 id = ZEND_THIS;
129 if (zend_parse_parameters_none() == FAILURE) {
130 RETURN_THROWS();
131 }
132
133 intern = Z_DOMOBJ_P(id);
134 RETURN_LONG(php_dom_get_nodelist_length(intern));
135 }
136 /* }}} end dom_nodelist_count */
137
php_dom_nodelist_get_item_into_zval(dom_nnodemap_object * objmap,zend_long index,zval * return_value)138 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
139 {
140 int ret;
141 xmlNodePtr itemnode = NULL;
142 bool cache_itemnode = false;
143 if (index >= 0) {
144 if (objmap != NULL) {
145 if (objmap->ht) {
146 if (objmap->nodetype == XML_ENTITY_NODE) {
147 itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
148 } else {
149 itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
150 }
151 } else {
152 if (objmap->nodetype == DOM_NODESET) {
153 HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
154 zval *entry = zend_hash_index_find(nodeht, index);
155 if (entry) {
156 ZVAL_COPY(return_value, entry);
157 return;
158 }
159 } else if (objmap->baseobj) {
160 xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
161 if (basep) {
162 xmlNodePtr nodep = basep;
163 /* For now we're only able to use cache for forward search.
164 * TODO: in the future we could extend the logic of the node list such that backwards searches
165 * are also possible. */
166 bool restart = true;
167 int relative_index = index;
168 if (index >= objmap->cached_obj_index && objmap->cached_obj && !php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
169 xmlNodePtr cached_obj_xml_node = dom_object_get_node(objmap->cached_obj);
170
171 /* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
172 * forgot an invalidation somewhere. Take the defensive programming approach and invalidate
173 * it here if it's NULL (except in debug mode where we would want to catch this). */
174 if (UNEXPECTED(cached_obj_xml_node == NULL)) {
175 #if ZEND_DEBUG
176 ZEND_UNREACHABLE();
177 #endif
178 reset_objmap_cache(objmap);
179 } else {
180 restart = false;
181 relative_index -= objmap->cached_obj_index;
182 nodep = cached_obj_xml_node;
183 }
184 }
185 int count = 0;
186 if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
187 if (restart) {
188 nodep = nodep->children;
189 }
190 while (count < relative_index && nodep != NULL) {
191 count++;
192 nodep = nodep->next;
193 }
194 itemnode = nodep;
195 } else {
196 if (restart) {
197 if (basep->type == XML_DOCUMENT_NODE || basep->type == XML_HTML_DOCUMENT_NODE) {
198 nodep = xmlDocGetRootElement((xmlDoc*) basep);
199 } else {
200 nodep = basep->children;
201 }
202 }
203 itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, (char *) objmap->ns, (char *) objmap->local, &count, relative_index);
204 }
205 cache_itemnode = true;
206 }
207 }
208 }
209 }
210
211 if (itemnode) {
212 DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
213 if (cache_itemnode) {
214 /* Hold additional reference for the cache, must happen before releasing the cache
215 * because we might be the last reference holder.
216 * Instead of storing and copying zvals, we store the object pointer directly.
217 * This saves us some bytes because a pointer is smaller than a zval.
218 * This also means we have to manually refcount the objects here, and remove the reference count
219 * in reset_objmap_cache() and the destructor. */
220 dom_object *cached_obj = Z_DOMOBJ_P(return_value);
221 GC_ADDREF(&cached_obj->std);
222 /* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
223 if (php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, itemnode)) {
224 php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, itemnode);
225 reset_objmap_cache(objmap);
226 } else {
227 objmap_cache_release_cached_obj(objmap);
228 }
229 objmap->cached_obj_index = index;
230 objmap->cached_obj = cached_obj;
231 }
232 return;
233 }
234 }
235
236 RETVAL_NULL();
237 }
238
239 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
240 Since:
241 */
PHP_METHOD(DOMNodeList,item)242 PHP_METHOD(DOMNodeList, item)
243 {
244 zend_long index;
245 ZEND_PARSE_PARAMETERS_START(1, 1)
246 Z_PARAM_LONG(index)
247 ZEND_PARSE_PARAMETERS_END();
248
249 zval *id = ZEND_THIS;
250 dom_object *intern = Z_DOMOBJ_P(id);
251 dom_nnodemap_object *objmap = intern->ptr;
252 php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
253 }
254 /* }}} end dom_nodelist_item */
255
ZEND_METHOD(DOMNodeList,getIterator)256 ZEND_METHOD(DOMNodeList, getIterator)
257 {
258 if (zend_parse_parameters_none() == FAILURE) {
259 return;
260 }
261
262 zend_create_internal_iterator_zval(return_value, ZEND_THIS);
263 }
264
265 #endif
266