/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Antony Dovgal <tony at daylessday.org> |
| Based on Sphinx PHP API by Andrew Aksyonoff <shodan at shodan.ru> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "zend_operators.h"
#include "php_sphinx.h"
#include <sphinxclient.h>
static zend_class_entry *ce_sphinx_client;
static zend_object_handlers php_sphinx_client_handlers;
static zend_object_handlers cannot_be_cloned;
typedef struct _php_sphinx_client {
sphinx_client *sphinx;
zend_bool array_result;
zend_object std;
} php_sphinx_client;
#ifdef COMPILE_DL_SPHINX
ZEND_GET_MODULE(sphinx)
#endif
#ifndef E_RECOVERABLE_ERROR
#define E_RECOVERABLE_ERROR E_WARNING
#endif
#define SPHINX_CONST(name) REGISTER_LONG_CONSTANT(#name, name, CONST_CS | CONST_PERSISTENT)
#define SPHINX_INITIALIZED(c) \
if (!(c) || !(c)->sphinx) { \
php_error_docref(NULL, E_WARNING, "using uninitialized SphinxClient object"); \
RETURN_FALSE; \
}
static inline php_sphinx_client *php_sphinx_client_object(zend_object *obj) {
return (php_sphinx_client *)((char*)(obj) - XtOffsetOf(php_sphinx_client, std));
}
#define sphinx_client(zv) php_sphinx_client_object(Z_OBJ_P(zv))
static void php_sphinx_client_obj_dtor(zend_object *object) /* {{{ */
{
php_sphinx_client *c = php_sphinx_client_object(object);
sphinx_destroy(c->sphinx);
zend_object_std_dtor(&c->std);
}
/* }}} */
static zend_object *php_sphinx_client_new(zend_class_entry *ce) /* {{{ */
{
php_sphinx_client *c;
c = ecalloc(1, sizeof(*c) + zend_object_properties_size(ce));
zend_object_std_init(&c->std, ce);
object_properties_init(&c->std, ce);
c->std.handlers = &php_sphinx_client_handlers;
return &c->std;
}
/* }}} */
static zval *php_sphinx_client_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
{
//php_sphinx_client *c;
zval tmp_member;
zval *retval;
zend_object_handlers *std_hnd;
//c = sphinx_client(object);
if (Z_TYPE_P(member) != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
/* XXX we can either create retval ourselves (for custom properties) or use standard handlers */
std_hnd = zend_get_std_object_handlers();
retval = std_hnd->read_property(object, member, type, cache_slot, rv);
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
/* }}} */
static HashTable *php_sphinx_client_get_properties(zval *object) /* {{{ */
{
php_sphinx_client *c;
const char *warning, *error;
zval tmp;
HashTable *props;
c = sphinx_client(object);
props = zend_std_get_properties(object);
error = sphinx_error(c->sphinx);
ZVAL_STRING(&tmp, (char *)error);
zend_hash_str_update(props, "error", strlen("error"), &tmp);
warning = sphinx_warning(c->sphinx);
ZVAL_STRING(&tmp, (char *)warning);
zend_hash_str_update(props, "warning", strlen("warning"), &tmp);
return c->std.properties;
}
/* }}} */
static void php_sphinx_result_to_array(php_sphinx_client *c, sphinx_result *result, zval *array) /* {{{ */
{
zval tmp;
int i, j;
array_init(array);
/* error */
if (!result->error) {
add_assoc_string(array, "error", "");
} else {
add_assoc_string(array, "error", (char *)(result->error));
}
/* warning */
if (!result->warning) {
add_assoc_string(array, "warning", "");
} else {
add_assoc_string(array, "warning", (char *)result->warning);
}
/* status */
add_assoc_long(array, "status", result->status);
switch(result->status) {
case SEARCHD_OK:
/* ok, continue reading data */
break;
case SEARCHD_WARNING:
/* this seems to be safe, too */
break;
default:
/* libsphinxclient doesn't nullify the data
in case of error, so it's not safe to continue. */
return;
}
/* fields */
array_init(&tmp);
for (i = 0; i < result->num_fields; i++) {
add_next_index_string(&tmp, result->fields[i]);
}
add_assoc_zval(array, "fields", &tmp);
/* attrs */
array_init(&tmp);
for (i = 0; i < result->num_attrs; i++) {
#if SIZEOF_LONG == 8
add_assoc_long(&tmp, result->attr_names[i], result->attr_types[i]);
#else
double float_value;
char buf[128];
float_value = (double)result->attr_types[i];
slprintf(buf, sizeof(buf), "%.0f", float_value);
add_assoc_string(&tmp, result->attr_names[i], buf);
#endif
}
add_assoc_zval(array, "attrs", &tmp);
/* matches */
if (result->num_matches) {
array_init(&tmp);
for (i = 0; i < result->num_matches; i++) {
zval tmp_element, sub_element;
array_init(&tmp_element);
if (c->array_result) {
/* id */
#if SIZEOF_LONG == 8
add_assoc_long(&tmp_element, "id", sphinx_get_id(result, i));
#else
double float_id;
char buf[128];
float_id = (double)sphinx_get_id(result, i);
slprintf(buf, sizeof(buf), "%.0f", float_id);
add_assoc_string(&tmp_element, "id", buf);
#endif
}
/* weight */
add_assoc_long(&tmp_element, "weight", sphinx_get_weight(result, i));
/* attrs */
array_init(&sub_element);
for (j = 0; j < result->num_attrs; j++) {
zval sub_sub_element;
#if SIZEOF_LONG != 8
double float_value;
char buf[128];
#endif
switch(result->attr_types[j]) {
case SPH_ATTR_MULTI | SPH_ATTR_INTEGER:
{
int k;
unsigned int *mva = sphinx_get_mva(result, i, j);
unsigned int tmp, num;
array_init(&sub_sub_element);
if (!mva) {
break;
}
memcpy(&num, mva, sizeof(unsigned int));
for (k = 1; k <= num; k++) {
mva++;
memcpy(&tmp, mva, sizeof(unsigned int));
#if SIZEOF_LONG == 8
add_next_index_long(&sub_sub_element, tmp);
#else
float_value = (double)tmp;
slprintf(buf, sizeof(buf), "%.0f", float_value);
add_next_index_string(&sub_sub_element, buf);
#endif
}
} break;
case SPH_ATTR_FLOAT:
ZVAL_DOUBLE(&sub_sub_element, sphinx_get_float(result, i, j));
break;
#if LIBSPHINX_VERSION_ID >= 110
case SPH_ATTR_STRING:
ZVAL_STRING(&sub_sub_element, sphinx_get_string(result, i, j));
break;
#endif
default:
#if SIZEOF_LONG == 8
ZVAL_LONG(&sub_sub_element, sphinx_get_int(result, i, j));
#else
float_value = (double)sphinx_get_int(result, i, j);
slprintf(buf, sizeof(buf), "%.0f", float_value);
ZVAL_STRING(&sub_sub_element, buf);
#endif
break;
}
add_assoc_zval(&sub_element, result->attr_names[j], &sub_sub_element);
}
add_assoc_zval(&tmp_element, "attrs", &sub_element);
if (c->array_result) {
add_next_index_zval(&tmp, &tmp_element);
} else {
#if SIZEOF_LONG == 8
add_index_zval(&tmp, sphinx_get_id(result, i), &tmp_element);
#else
char buf[128];
double float_id;
int buf_len;
float_id = (double)sphinx_get_id(result, i);
buf_len = slprintf(buf, sizeof(buf), "%.0f", float_id);
没有合适的资源?快使用搜索试试~ 我知道了~
php7 sphinx扩展
5星 · 超过95%的资源 需积分: 26 39 下载量 116 浏览量
2018-06-09
13:10:58
上传
评论 1
收藏 28KB GZ 举报
温馨提示
共16个文件
php:6个
conf:2个
sql:1个
php7 sphinx扩展
资源推荐
资源详情
资源评论
收起资源包目录
sphinx-9a3d08c.tar.gz (16个子文件)
sphinx-9a3d08c
config.m4 3KB
tests
pecl_17007.php 201B
example.sql 903B
api_0.9.9.php 2KB
api
sphinxapi-1.10.php 45KB
sphinx-0.9.9.conf 1KB
api_0.9.8.php 2KB
test_1.10.php 4KB
pecl_18635.php 255B
sphinx-1.10.conf 1KB
LICENSE 3KB
php_sphinx.h 2KB
sphinx.c 50KB
package.xml 6KB
config.w32 338B
CREDITS 85B
共 16 条
- 1
资源评论
- 王同学sy2019-05-14可以用,感谢楼主
u014556021
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功