-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathphp_solr_utils.c
executable file
·189 lines (142 loc) · 5.46 KB
/
php_solr_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Israel Ekpo <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: php_solr_utils.c 289163 2009-10-04 04:49:13Z iekpo $ */
#include "php_solr.h"
/* {{{ proto string SolrUtils::escapeQueryChars(string unescaped)
Escapes the lucene string */
PHP_METHOD(SolrUtils, escapeQueryChars)
{
solr_char_t *unescaped = NULL;
solr_string_t sbuilder;
long int unescaped_length = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &unescaped, &unescaped_length) == FAILURE) {
RETURN_FALSE;
}
if (!unescaped_length) {
RETURN_NULL();
}
memset(&sbuilder, 0, sizeof(solr_string_t));
solr_escape_query_chars(&sbuilder, unescaped, unescaped_length);
RETVAL_STRINGL(sbuilder.str, sbuilder.len, 1);
solr_string_free(&sbuilder);
}
/* }}} */
/* {{{ proto string SolrUtils::queryPhrase(string unescaped)
Prepares a phrase from an unescaped lucene string. */
PHP_METHOD(SolrUtils, queryPhrase)
{
solr_char_t *unescaped = NULL;
solr_string_t sbuilder;
long int unescaped_length = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &unescaped, &unescaped_length) == FAILURE) {
RETURN_FALSE;
}
if (!unescaped_length) {
RETURN_NULL();
}
memset(&sbuilder, 0, sizeof(solr_string_t));
solr_string_appendc(&sbuilder, '"');
solr_escape_query_chars(&sbuilder, unescaped, unescaped_length);
solr_string_appendc(&sbuilder, '"');
RETVAL_STRINGL(sbuilder.str, sbuilder.len, 1);
solr_string_free(&sbuilder);
}
/* }}} */
/* {{{ proto string SolrUtils::digestXMLResponse(string xml_response [, int parse_mode])
Digests the xml response into a php serialize string. */
PHP_METHOD(SolrUtils, digestXmlResponse)
{
solr_char_t *xmlresponse = NULL;
int xmlresponse_len = 0;
long int parse_mode = 0L;
solr_string_t sbuilder;
unsigned char *raw_resp = NULL, *str_end = NULL;
size_t raw_res_length = 0;
php_unserialize_data_t var_hash;
int successful = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &xmlresponse, &xmlresponse_len, &parse_mode) == FAILURE) {
RETURN_FALSE;
}
if (!xmlresponse_len) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response is empty");
RETURN_NULL();
}
parse_mode = ((parse_mode < 0L) ? 0L : ((parse_mode > 1L) ? 1L : parse_mode));
memset(&sbuilder, 0, sizeof(solr_string_t));
solr_encode_generic_xml_response(&sbuilder, xmlresponse, xmlresponse_len, parse_mode TSRMLS_CC);
if (sbuilder.str == NULL || sbuilder.len == 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response was not valid");
RETURN_NULL();
}
memset(&var_hash, 0, sizeof(php_unserialize_data_t));
PHP_VAR_UNSERIALIZE_INIT(var_hash);
raw_resp = (unsigned char *) sbuilder.str;
raw_res_length = sbuilder.len;
str_end = (unsigned char *) (raw_resp + raw_res_length);
if (!php_var_unserialize(&return_value, (const unsigned char **) &raw_resp, str_end, &var_hash TSRMLS_CC))
{
solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response.");
successful = 0;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
solr_string_free(&sbuilder);
if (successful)
{
/* Overriding the default object handlers */
Z_OBJ_HT_P(return_value) = &solr_object_handlers;
}
}
/* }}} */
/* {{{ proto string SolrUtils::getSolrVersion(void)
Returns the current extension version */
PHP_METHOD(SolrUtils, getSolrVersion)
{
RETURN_STRINGL(PHP_SOLR_DOTTED_VERSION, sizeof(PHP_SOLR_DOTTED_VERSION)-1, 1);
}
/* }}} */
/* {{{ proto array SolrUtils::getSolrStats(void)
Returns the number of active documents, clients and SolrParam objects in the current thread. */
PHP_METHOD(SolrUtils, getSolrStats)
{
int document_count = zend_hash_num_elements(SOLR_GLOBAL(documents));
int client_count = zend_hash_num_elements(SOLR_GLOBAL(clients));
int params_count = zend_hash_num_elements(SOLR_GLOBAL(params));
array_init(return_value);
add_assoc_long(return_value, "document_count", document_count);
add_assoc_long(return_value, "client_count", client_count);
add_assoc_long(return_value, "params_count", params_count);
}
/* }}} */
/* {{{ proto string solr_get_version(void)
Returns the current extension version */
PHP_FUNCTION(solr_get_version)
{
RETURN_STRINGL(PHP_SOLR_DOTTED_VERSION, sizeof(PHP_SOLR_DOTTED_VERSION)-1, 1);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/