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    | http://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    | Author:                                                              |
14    |          Omar Shaban <omars@php.net>                                 |
15    +----------------------------------------------------------------------+
16 */
17 
18 #include "php_solr.h"
19 
20 /* {{{ proto void SolrFunction::__construct([string field])
21    Constructor. */
PHP_METHOD(SolrCollapseFunction,__construct)22 PHP_METHOD(SolrCollapseFunction, __construct)
23 {
24     long int index = SOLR_UNIQUE_FUNCTION_INDEX();
25     uint32_t nSize = SOLR_INITIAL_HASH_TABLE_SIZE;
26     solr_function_t *solr_function_dest = NULL;
27     zval *objptr = getThis();
28 #ifdef PHP_7
29     solr_function_dest = pemalloc(sizeof(solr_function_t), SOLR_FUNCTIONS_PERSISTENT);
30 #endif
31 
32     solr_char_t *param_name = (solr_char_t *)"field";
33     COMPAT_ARG_SIZE_T param_name_len = sizeof("field");
34 
35     solr_char_t *field_name = NULL;
36     COMPAT_ARG_SIZE_T field_name_len = 0;
37 
38     if ((solr_function_dest = zend_hash_index_update_ptr(SOLR_GLOBAL(functions),index,(void *) solr_function_dest)) == NULL)
39     {
40         php_error_docref(NULL, E_ERROR, "Error while registering query parameters in HashTable");
41 
42         return ;
43     }
44     zend_update_property_long(solr_ce_SolrCollapseFunction, OBJ_FOR_PROP(objptr), SOLR_INDEX_PROPERTY_NAME, sizeof(SOLR_INDEX_PROPERTY_NAME) - 1, index);
45 
46     solr_function_dest->function_index = index;
47     solr_function_dest->name_length = strlen("collapse");
48 
49     solr_function_dest->name = (solr_char_t *)"collapse";
50 
51     /* Allocated memory for the params HashTable using fast cache for HashTables */
52     ALLOC_HASHTABLE(solr_function_dest->params);
53     zend_hash_init(solr_function_dest->params, nSize, NULL, (dtor_func_t) solr_destroy_solr_string, SOLR_FUNCTIONS_PERSISTENT);
54 
55     if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_name, &field_name_len) == FAILURE)
56     {
57         php_error_docref(NULL, E_ERROR, "Error Parsing Parameters");
58         return;
59     }
60 
61     if (field_name_len > 0 ) {
62         solr_solrfunc_update_string(getThis(), param_name, param_name_len, field_name, field_name_len);
63     }
64 
65     Z_OBJ_HT_P(getThis()) = &solr_collapse_function_object_handlers;
66 }
67 /* }}} */
68 
69 /* {{{ proto void SolrFunction::__destruct(void)
70    Destructor. */
PHP_METHOD(SolrCollapseFunction,__destruct)71 PHP_METHOD(SolrCollapseFunction, __destruct)
72 {
73     solr_function_t *function = NULL;
74     /* Retrieve the document entry for this SolrDocument */
75     if (solr_fetch_function_entry(getThis(), &function) == SUCCESS )
76     {
77         zend_hash_index_del(SOLR_GLOBAL(functions), function->function_index);
78     }
79 
80     return;
81 }
82 
83 /* {{{ proto string SolrFunction::setField(string field)
84   Set the field that is being collapsed on. The field must be a single valued String, Int or Float. */
PHP_METHOD(SolrCollapseFunction,setField)85 PHP_METHOD(SolrCollapseFunction, setField)
86 {
87     solr_char_t *key = "field", *arg;
88     COMPAT_ARG_SIZE_T key_len = sizeof("field"), arg_len;
89 
90     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
91         RETURN_NULL();
92     }
93 
94     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
95         php_error_docref(NULL, E_ERROR, "Error assigning field");
96         RETURN_NULL();
97     }
98     SOLR_RETURN_THIS();
99 }
100 /* }}} */
101 
102 
103 /* {{{ proto string SolrFunction::getField(void)
104   Get Collapse Field */
PHP_METHOD(SolrCollapseFunction,getField)105 PHP_METHOD(SolrCollapseFunction, getField)
106 {
107     solr_char_t *key = "field";
108     COMPAT_ARG_SIZE_T  key_len = sizeof("field");
109 
110     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
111 }
112 /* }}} */
113 
114 /* {{{ proto string SolrFunction::setMin(string value)
115   Selects the group heads by the min value of a numeric field or function query. */
PHP_METHOD(SolrCollapseFunction,setMin)116 PHP_METHOD(SolrCollapseFunction, setMin)
117 {
118     solr_char_t *key = "min", *arg;
119     COMPAT_ARG_SIZE_T  key_len = sizeof("min"), arg_len;
120 
121     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
122         RETURN_NULL();
123     }
124 
125     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
126         php_error_docref(NULL, E_ERROR, "Error assigning field");
127         RETURN_NULL();
128     }
129     SOLR_RETURN_THIS();
130 }
131 /* }}} */
132 
133 /* {{{ proto string SolrFunction::setMax(string value)
134   Selects the group heads by the max value of a numeric field or function query. */
PHP_METHOD(SolrCollapseFunction,setMax)135 PHP_METHOD(SolrCollapseFunction, setMax)
136 {
137     solr_char_t *key = "max", *arg;
138     COMPAT_ARG_SIZE_T  key_len = sizeof("max"), arg_len;
139 
140     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
141         RETURN_NULL();
142     }
143 
144     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
145         php_error_docref(NULL, E_ERROR, "Error assigning field");
146         RETURN_NULL();
147     }
148     SOLR_RETURN_THIS();
149 }
150 /* }}} */
151 
152 /* {{{ proto string SolrFunction::getMin(void)
153   Get min parameter */
PHP_METHOD(SolrCollapseFunction,getMin)154 PHP_METHOD(SolrCollapseFunction, getMin)
155 {
156     solr_char_t *key = "min";
157     COMPAT_ARG_SIZE_T  key_len = sizeof("min");
158 
159     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
160 }
161 /* }}} */
162 
163 
164 /* {{{ proto string SolrFunction::getMax(void)
165   Get max parameter */
PHP_METHOD(SolrCollapseFunction,getMax)166 PHP_METHOD(SolrCollapseFunction, getMax)
167 {
168     solr_char_t *key = "max";
169     COMPAT_ARG_SIZE_T  key_len = sizeof("max");
170 
171     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
172 }
173 /* }}} */
174 
175 /* {{{ proto string SolrFunction::setSize(void)
176   Sets the initial size of the collapse data structures when collapsing on a numeric field only */
PHP_METHOD(SolrCollapseFunction,setSize)177 PHP_METHOD(SolrCollapseFunction, setSize)
178 {
179     solr_char_t *key = "size", *arg;
180     COMPAT_ARG_SIZE_T  key_len = sizeof("size"), arg_len;
181 
182     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
183         RETURN_NULL();
184     }
185 
186     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
187         php_error_docref(NULL, E_ERROR, "Error assigning field");
188         RETURN_NULL();
189     }
190     SOLR_RETURN_THIS();
191 }
192 /* }}} */
193 
194 /* {{{ proto string SolrFunction::getSize(void)
195   Get size parameter */
PHP_METHOD(SolrCollapseFunction,getSize)196 PHP_METHOD(SolrCollapseFunction, getSize)
197 {
198     solr_char_t *key = "size";
199     COMPAT_ARG_SIZE_T  key_len = sizeof("size");
200 
201     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
202 }
203 /* }}} */
204 
205 
206 /* {{{ proto string SolrFunction::setHint(void)
207   Set collapse hint
208   Currently there is only one hint available "top_fc", which stands for top level FieldCache */
PHP_METHOD(SolrCollapseFunction,setHint)209 PHP_METHOD(SolrCollapseFunction, setHint)
210 {
211     solr_char_t *key = "hint", *arg;
212     COMPAT_ARG_SIZE_T  key_len = sizeof("hint"), arg_len;
213 
214     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
215         RETURN_NULL();
216     }
217 
218     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
219         php_error_docref(NULL, E_ERROR, "Error assigning field");
220         RETURN_NULL();
221     }
222     SOLR_RETURN_THIS();
223 }
224 /* }}} */
225 
226 /* {{{ proto string SolrFunction::getHint(void)
227   Get Hint */
PHP_METHOD(SolrCollapseFunction,getHint)228 PHP_METHOD(SolrCollapseFunction, getHint)
229 {
230     solr_char_t *key = "hint";
231     COMPAT_ARG_SIZE_T  key_len = sizeof("hint");
232 
233     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
234 }
235 /* }}} */
236 
237 /* {{{ proto string SolrFunction::setNullPolicy(void)
238   Sets the NULL Policy. Accepts ignore, expand, or collapse */
PHP_METHOD(SolrCollapseFunction,setNullPolicy)239 PHP_METHOD(SolrCollapseFunction, setNullPolicy)
240 {
241     solr_char_t *key = "nullPolicy", *arg;
242     COMPAT_ARG_SIZE_T  key_len = sizeof("nullPolicy"), arg_len;
243 
244     if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
245         RETURN_NULL();
246     }
247 
248     if (solr_solrfunc_update_string(getThis(), key, key_len, (solr_char_t *)arg, arg_len) == FAILURE) {
249         php_error_docref(NULL, E_ERROR, "Error assigning field");
250         RETURN_NULL();
251     }
252     SOLR_RETURN_THIS();
253 }
254 /* }}} */
255 
256 /* {{{ proto string SolrFunction::getNullPolicy(void)
257   Get NullPolicy */
PHP_METHOD(SolrCollapseFunction,getNullPolicy)258 PHP_METHOD(SolrCollapseFunction, getNullPolicy)
259 {
260     solr_char_t *key = "nullPolicy";
261     COMPAT_ARG_SIZE_T  key_len = sizeof("nullPolicy");
262 
263     solr_solrfunc_display_string(getThis(), key, key_len, &return_value);
264 }
265 /* }}} */
266 
267 /* {{{ proto string SolrFunction::__toString(void)
268   String value of the function object */
PHP_METHOD(SolrCollapseFunction,__toString)269 PHP_METHOD(SolrCollapseFunction, __toString)
270 {
271     solr_function_t *collapse_func;
272     solr_string_t *buffer = NULL;
273 
274     if(solr_fetch_function_entry(getThis(), &collapse_func) == FAILURE) {
275         RETURN_NULL();
276     }
277 
278     buffer = (solr_string_t *)emalloc(sizeof(solr_string_t));
279     memset(buffer, 0, sizeof(solr_string_t));
280 
281     solr_solrfunc_to_string(collapse_func, &buffer);
282     ZVAL_STRINGL(return_value, buffer->str, buffer->len);
283     solr_string_free(buffer);
284     efree(buffer);
285 }
286 /* }}} */
287 
288 /* {{{ proto SolrClient::__sleep(void)
289    Should not be called directly. Serialization is not supported. */
PHP_METHOD(SolrCollapseFunction,__sleep)290 PHP_METHOD(SolrCollapseFunction, __sleep)
291 {
292     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1001, SOLR_FILE_LINE_FUNC, "Serialization of SolrCollapseFunction objects is currently not supported");
293 }
294 /* }}} */
295 
296 /* {{{ proto SolrClient::__wakeup(void)
297    Should not be called directly. Serialization is not supported. */
PHP_METHOD(SolrCollapseFunction,__wakeup)298 PHP_METHOD(SolrCollapseFunction, __wakeup)
299 {
300     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1001, SOLR_FILE_LINE_FUNC, "Unserialization of SolrCollapseFunction objects is currently not supported");
301 }
302 /* }}} */
303 
304 /* {{{ throw exception on cloning (clone handler) */
305 #if PHP_VERSION_ID < 80000
solr_collapse_function_object_handler_clone(zval * object)306 zend_object* solr_collapse_function_object_handler_clone(zval *object)
307 #else
308 zend_object* solr_collapse_function_object_handler_clone(zend_object *object)
309 #endif
310 {
311     zend_object *fake;
312     fake = zend_objects_new(zend_standard_class_def);
313     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_4001, SOLR_FILE_LINE_FUNC, "Cloning of SolrCollapseFunction objects is currently not supported");
314     return fake;
315 }
316 /* }}} */
317