[转]Clucene的PHP接口开发

关于PHP与Clucene的接口开发

前言:
  以前在空闲时,研究过PHP的扩展模块开发,苦于没有找到一个比较好的调试方法,因此只做了少量的基本工作,感觉zend公司开发的PHP扩展模块有点过于复杂,调试真是让人头痛的事,若哪位有比跟踪调试的好方法,请留言。

zend PHP的扩展模块开发帮助方面的,google等能找到,这里把曾经做过的接口部分给出来大家看看
clucene可能有不稳定的部分,在写接口前最好先测试,至于哪里有问题,当时忘做笔记了,暂时忘了

  1. /************************************
  2. ** php_clucene.h
  3. /************************************
  4.  
  5. #ifndef PHP_CLUCENE_H
  6. #define PHP_CLUCENE_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <php.h>
  14. #ifdef HAVE_CLUCENE
  15. #include <php_ini.h>
  16. #include <SAPI.h>
  17. #include <ext/standard/info.h>
  18. #include <Zend/zend_extensions.h>
  19. #include <Zend/zend_exceptions.h>
  20. #ifdef __cplusplus
  21. } /* extern "C" */
  22. #endif
  23. #include <CLucene.h>
  24.  /* Class structures */
  25.  typedef struct _index_searcher_object {
  26.  zend_object std;
  27.  lucene::search::IndexSearcher *searcher;
  28.  const wchar_t **field;
  29.  } index_searcher_object;
  30.  typedef struct _hits_object {
  31.  zend_object std;
  32.  lucene::search::Hits *hits;
  33.  } hits_object;
  34.  //没实现
  35.  /******************************
  36. typedef struct _index_writer_object {
  37. zend_object std;
  38. lucene::index::IndexWriter *writer;
  39. wchar_t *field;
  40. } index_writer_object;
  41. /******************************/
  42.  /* Standard analyzer */
  43.  //ictclas 分词系统
  44.  //static lucene::analysis::standard::StandardAnalyzer analyzer;
  45.  static lucene::analysis::standard::StandardIctclas analyzer;
  46. #ifdef __cplusplus
  47.  extern "C" {
  48. #endif
  49.  extern zend_module_entry clucene_module_entry;
  50. #define phpext_clucene_ptr &clucene_module_entry
  51. #ifdef PHP_WIN32
  52. #define PHP_CLUCENE_API __declspec(dllexport)
  53. #else
  54. #define PHP_CLUCENE_API
  55. #endif
  56.  PHP_MINIT_FUNCTION(clucene);
  57.  PHP_MSHUTDOWN_FUNCTION(clucene);
  58.  PHP_RINIT_FUNCTION(clucene);
  59.  PHP_RSHUTDOWN_FUNCTION(clucene);
  60.  PHP_MINFO_FUNCTION(clucene);
  61. #ifdef ZTS
  62. #include "TSRM.h"
  63. #endif
  64. #define FREE_RESOURCE(resource) zend_list_delete(Z_LVAL_P(resource))
  65. #define PROP_GET_LONG(name) Z_LVAL_P(zend_read_property(_this_ce, _this_zval, #name, strlen(#name), 1 TSRMLS_CC))
  66. #define PROP_SET_LONG(name, l) zend_update_property_long(_this_ce, _this_zval, #name, strlen(#name), l TSRMLS_CC)
  67. #define PROP_GET_DOUBLE(name) Z_DVAL_P(zend_read_property(_this_ce, _this_zval, #name, strlen(#name), 1 TSRMLS_CC))
  68. #define PROP_SET_DOUBLE(name, d) zend_update_property_double(_this_ce, _this_zval, #name, strlen(#name), d TSRMLS_CC)
  69. #define PROP_GET_STRING(name) Z_STRVAL_P(zend_read_property(_this_ce, _this_zval, #name, strlen(#name), 1 TSRMLS_CC))
  70. #define PROP_GET_STRLEN(name) Z_STRLEN_P(zend_read_property(_this_ce, _this_zval, #name, strlen(#name), 1 TSRMLS_CC))
  71. #define PROP_SET_STRING(name, s) zend_update_property_string(_this_ce, _this_zval, #name, strlen(#name), s TSRMLS_CC)
  72. #define PROP_SET_STRINGL(name, s, l) zend_update_property_string(_this_ce, _this_zval, #name, strlen(#name), s, l TSRMLS_CC)
  73.  
  74. #ifdef __cplusplus
  75.  } /* extern "C" */
  76. #endif
  77. #endif /* PHP_HAVE_CLUCENE */
  78. #endif /* PHP_CLUCENE_H */


下载: clucene.cpp
  1. /************************************
  2. ** clucene.cpp
  3. /************************************
  4.  
  5. #ifdef HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif
  8.  
  9. #include <locale>
  10. #include <syslog.h>
  11. #include "CLucene/util/WideCharToMultiByte.h"
  12. #include "CLucene/util/Ictinf.h"
  13. #include "php_clucene.h"
  14.  
  15. #define PHP_CLUCENE_MODULE_VERSION "0.1.5"
  16.  
  17. using namespace lucene::analysis;
  18. using namespace lucene::document;
  19. using namespace lucene::queryParser;
  20. using namespace lucene::search;
  21. using namespace lucene::index;
  22. using namespace lucene::util;
  23.  
  24. #if HAVE_CLUCENE
  25.  
  26. /* Other analyzers should be added in the future */
  27.  //StandardIctclas
  28.  enum {STANDARD_ANALYZER};
  29.  
  30.  static zend_object_handlers index_searcher_object_handlers;
  31.  static zend_object_handlers hits_object_handlers;
  32.  
  33.  zend_class_entry *index_searcher_class_entry;
  34.  zend_class_entry *hits_class_entry;
  35.  
  36.  /* {{{ clucene_module_entry */
  37.  zend_module_entry clucene_module_entry = {
  38.  STANDARD_MODULE_HEADER,
  39.  "clucene",
  40.  NULL,
  41.  PHP_MINIT(clucene), /* Replace with NULL if there is nothing to do at php startup */
  42.  NULL, /* PHP_MSHUTDOWN(clucene), */
  43.  PHP_RINIT(clucene), /* PHP_RINIT(clucene), */
  44.  NULL, /* PHP_RSHUTDOWN(clucene), */
  45.  PHP_MINFO(clucene),
  46.  PHP_CLUCENE_MODULE_VERSION,
  47.  STANDARD_MODULE_PROPERTIES
  48.  };
  49.  /* }}} */
  50.  
  51.  /* {{{ index_searcher_object_dtor */
  52.  static void index_searcher_object_dtor(void *object, zend_object_handle handle TSRMLS_DC)
  53.  {
  54.  index_searcher_object *intern = (index_searcher_object*) object;
  55.  
  56.  zend_hash_destroy(intern->std.properties);
  57.  FREE_HASHTABLE(intern->std.properties);
  58.  
  59.  /* Free other members */
  60.  if (intern->searcher != NULL) {
  61.  intern->searcher->close();
  62.  delete intern->searcher;
  63.  }
  64.  if (intern->field != NULL) {
  65.  delete [] intern->field;
  66.  }
  67.  efree(object);
  68.  } /* }}} */
  69.  
  70.  /* {{{ index_searcher_object_new */
  71.  static zend_object_value index_searcher_object_new(zend_class_entry *class_type TSRMLS_DC)
  72.  {
  73.  zend_object_value retval;
  74.  index_searcher_object *intern;
  75.  zval *tmp;
  76.  
  77.  intern = (index_searcher_object*) emalloc(sizeof(index_searcher_object));
  78.  memset(intern, 0, sizeof(index_searcher_object));
  79.  intern->searcher = NULL;
  80.  intern->field = NULL;
  81.  intern->std.ce = class_type;
  82.  
  83.  ALLOC_HASHTABLE(intern->std.properties);
  84.  zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  85.  zend_hash_copy(intern->std.properties,
  86. &class_type->default_properties,
  87.  (copy_ctor_func_t) zval_add_ref,
  88.  (void *) &tmp, sizeof(zval *));
  89.  retval.handle = zend_objects_store_put(intern,
  90.  index_searcher_object_dtor,
  91.  NULL,
  92.  NULL TSRMLS_CC);
  93.  retval.handlers = &index_searcher_object_handlers;
  94.  return retval;
  95.  } /* }}} */
  96.  
  97.  /* {{{ hits_object_dtor */
  98.  static void hits_object_dtor(void *object, zend_object_handle handle TSRMLS_DC)
  99.  {
  100.  hits_object *intern = (hits_object*) object;
  101.  
  102.  zend_hash_destroy(intern->std.properties);
  103.  FREE_HASHTABLE(intern->std.properties);
  104.  
  105.  /* Free other members */
  106.  if (intern->hits != NULL) {
  107.  delete intern->hits;
  108.  }
  109.  
  110.  efree(object);
  111.  } /* }}} */
  112.  
  113.  /* {{{ hits_object_new */
  114.  static zend_object_value hits_object_new(zend_class_entry *class_type TSRMLS_DC)
  115.  {
  116.  zend_object_value retval;
  117.  hits_object *intern;
  118.  zval *tmp;
  119.  
  120.  intern = (hits_object*) emalloc(sizeof(hits_object));
  121.  memset(intern, 0, sizeof(hits_object));
  122.  intern->hits = NULL;
  123.  intern->std.ce = class_type;
  124.  
  125.  ALLOC_HASHTABLE(intern->std.properties);
  126.  zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  127.  zend_hash_copy(intern->std.properties,
  128. &class_type->default_properties,
  129.  (copy_ctor_func_t) zval_add_ref,
  130.  (void *) &tmp, sizeof(zval *));
  131.  retval.handle = zend_objects_store_put(intern,
  132.  hits_object_dtor,
  133.  NULL,
  134.  NULL TSRMLS_CC);
  135.  retval.handlers = &hits_object_handlers;
  136.  return retval;
  137.  
  138.  } /* }}} */
  139.  
  140.  /* {{{ hits_object_create */
  141.  static zend_object_value hits_object_create(zend_class_entry *class_type, Hits *hits TSRMLS_DC)
  142.  {
  143.  zend_object_value retval;
  144.  hits_object *intern;
  145.  zval *tmp;
  146.  
  147.  intern = (hits_object*) emalloc(sizeof(hits_object));
  148.  memset(intern, 0, sizeof(hits_object));
  149.  intern->hits = hits;
  150.  intern->std.ce = class_type;
  151.  
  152.  ALLOC_HASHTABLE(intern->std.properties);
  153.  zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  154.  zend_hash_copy(intern->std.properties,
  155. &class_type->default_properties,
  156.  (copy_ctor_func_t) zval_add_ref,
  157.  (void *) &tmp, sizeof(zval *));
  158.  retval.handle = zend_objects_store_put(intern,
  159.  hits_object_dtor,
  160.  NULL,
  161.  NULL TSRMLS_CC);
  162.  retval.handlers = &hits_object_handlers;
  163.  return retval;
  164.  } /* }}} */
  165.  
  166. #ifdef COMPILE_DL_CLUCENE
  167.  extern "C" {
  168.  ZEND_GET_MODULE(clucene)
  169.  } /* extern "C" */
  170. #endif
  171.  
  172.  /* Forward declarations for IndexSearcher methods */
  173.  PHP_METHOD(IndexSearcher, __construct);
  174.  PHP_METHOD(IndexSearcher, search);
  175.  PHP_METHOD(IndexSearcher, close);
  176.  
  177.  static zend_function_entry index_searcher_functions[] = {
  178.  PHP_ME(IndexSearcher, __construct, NULL, ZEND_ACC_PUBLIC)
  179.  PHP_ME(IndexSearcher, search, NULL, ZEND_ACC_PUBLIC)
  180.  PHP_ME(IndexSearcher, close, NULL, ZEND_ACC_PUBLIC)
  181.  {NULL, NULL, NULL}
  182.  };
  183.  
  184.  /* Forward declarations for Hits methods */
  185.  PHP_METHOD(Hits, __construct);
  186.  PHP_METHOD(Hits, length);
  187.  PHP_METHOD(Hits, get);
  188.  PHP_METHOD(Hits, id);
  189.  PHP_METHOD(Hits, score);
  190.  
  191.  /* The creation of a Hits object is not allowed */
  192.  static zend_function_entry hits_functions[] = {
  193.  PHP_ME(Hits, __construct, NULL, ZEND_ACC_PRIVATE)
  194.  PHP_ME(Hits, length, NULL, ZEND_ACC_PUBLIC)
  195.  PHP_ME(Hits, get, NULL, ZEND_ACC_PUBLIC)
  196.  PHP_ME(Hits, id, NULL, ZEND_ACC_PUBLIC)
  197.  PHP_ME(Hits, score, NULL, ZEND_ACC_PUBLIC)
  198.  {NULL, NULL, NULL}
  199.  };
  200.  
  201.  /* {{{ proto void IndexSearcher::__construct(string path)
  202. Constructs a new IndexSearcher instance. */
  203.  PHP_METHOD(IndexSearcher, __construct)
  204.  {
  205.  index_searcher_object *intern;
  206.  char *path;
  207.  long path_len;
  208.  zval *fields,**z_item;
  209.  const wchar_t **fields_ary;
  210.  int i,count;
  211.  
  212.  php_set_error_handling(EH_THROW, zend_exception_get_default()
  213.  TSRMLS_CC);
  214.  
  215.  /* TODO: Add the analyzer as a 3rd parameter when several
  216. * analyzers will be available.
  217. */
  218.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &path,
  219. &path_len,&fields) == FAILURE) {
  220.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  221.  return;
  222.  }
  223.  //for(i=0;i<)
  224.  count = zend_hash_num_elements(Z_ARRVAL_P(fields));
  225.  fields_ary =_CL_NEWARRAY(const wchar_t*,count+1);
  226.  
  227.  zend_hash_internal_pointer_reset(Z_ARRVAL_P(fields));
  228.  for (i = 0; i < count; i ++) {
  229.  char* key;
  230.  ulong idx;
  231.  // 获取当前数据
  232.  zend_hash_get_current_data(Z_ARRVAL_P(fields), (void**) &z_item);
  233.  convert_to_string_ex(z_item);
  234.  fields_ary[i] = Misc::_charToWide(Z_STRVAL_PP(z_item));
  235.  /*
  236. if (zend_hash_get_current_key(Z_ARRVAL_P(fields), &key, &idx, 0) == HASH_KEY_IS_STRING) {
  237. // KEY为字符串
  238. php_printf("array[%s] = %s", key, Z_STRVAL_PP(z_item));
  239. } else {
  240. // KEY为数字
  241. php_printf("array[%d] = %s", idx, Z_STRVAL_PP(z_item));
  242. }
  243. */
  244.  // 将数组中的内部指针向前移动一位
  245.  zend_hash_move_forward(Z_ARRVAL_P(fields));
  246.  }
  247.  fields_ary[count]=NULL;
  248.  intern = (index_searcher_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
  249.  
  250.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  251.  
  252.  try {
  253.  intern->searcher = new IndexSearcher(path);
  254.  } catch (CLuceneError& error) {
  255.  zend_throw_exception(zend_exception_get_default(), error.what(), 0 TSRMLS_CC);
  256.  return;
  257.  }
  258.  intern->field = fields_ary;
  259.  } /* }}} */
  260.  
  261.  /* {{{ proto object IndexSearcher::search(string query)
  262. Return documents matching query */
  263.  PHP_METHOD(IndexSearcher, search)
  264.  {
  265.  index_searcher_object *intern;
  266.  char *query_string,*sort_string,*pp;
  267.  WCHAR *wline,*vfield;
  268.  wchar_t *tmp,*field;
  269.  long len,sort_len;
  270.  QueryParser *parser;
  271.  Query *query;
  272.  Hits *hits;
  273.  int n,i;
  274.  
  275.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &query_string, &len,&sort_string,&sort_len) == FAILURE) {
  276.  return;
  277.  }
  278.  intern = (index_searcher_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
  279.  if (intern->searcher == NULL) {
  280.  zend_throw_exception(zend_exception_get_default(), "The IndexSearcher has been closed", 0 TSRMLS_CC);
  281.  //return;
  282.  }
  283.  //parser = new QueryParser(intern->field, &analyzer);
  284.  //parser = new MultiFieldQueryParser(intern->field, &analyzer);
  285.  //tmp = Misc::_charToWide(query_string);
  286.  str_to_UnicodeChar((const char*)sort_string,vfield);
  287.  field = (wchar_t*)vfield;
  288.  str_to_UnicodeChar((const char*)query_string,wline);
  289.  tmp = (wchar_t*)wline;
  290.  query = MultiFieldQueryParser::parse(tmp,intern->field, &analyzer);
  291.  if(strlen(sort_string)>0)
  292.  {
  293.  SortField *sort_Field = new SortField(field);
  294.  Sort *sort = new Sort(sort_Field);
  295.  //Sort *sort = new Sort((const wchar_t*)field,true);
  296.  hits = intern->searcher->search(query,sort);
  297.  }else
  298.  hits = intern->searcher->search(query);
  299.  //syslog(LOG_NOTICE,"pamire993=%d\n",hits->length());
  300.  n=(hits->length()/100);
  301.  
  302.  for(i=0;i<n;i++)
  303.  Document* doc = &hits->doc(100*(i+1));
  304.  
  305.  //delete [] tmp;
  306.  delete query;
  307.  delete wline;wline=NULL;
  308.  //delete parser;
  309.  
  310.  return_value->type = IS_OBJECT;
  311.  return_value->value.obj = hits_object_create(hits_class_entry, hits);
  312.  }
  313.  /* }}} */
  314.  
  315.  /* {{{ proto void IndexSearcher::close()
  316. Frees resources associated with the searcher */
  317.  PHP_METHOD(IndexSearcher, close)
  318.  {
  319.  index_searcher_object *intern;
  320.  
  321.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
  322.  return;
  323.  }
  324.  
  325.  intern = (index_searcher_object*)
  326.  zend_object_store_get_object(getThis() TSRMLS_CC);
  327.  
  328.  if (intern->searcher != NULL)
  329.  {
  330.  intern->searcher->close();
  331.  delete intern->searcher;
  332.  intern->searcher = NULL;
  333.  }
  334.  delete [] intern->field;
  335.  intern->field = NULL;
  336.  }
  337.  /* }}} */
  338.  
  339.  /* {{{ proto void Hits::__construct()
  340. Constructs a new Hits instance. */
  341.  PHP_METHOD(Hits, __construct)
  342.  {
  343.  hits_object *intern;
  344.  
  345.  php_set_error_handling(EH_THROW, zend_exception_get_default()
  346.  TSRMLS_CC);
  347.  
  348.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
  349.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  350.  return;
  351.  }
  352.  
  353.  intern = (hits_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
  354.  
  355.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  356.  
  357.  } /* }}} */
  358.  
  359.  /* {{{ proto int Hits::length()
  360. Returns the total number of hits available in this set. */
  361.  PHP_METHOD(Hits, length)
  362.  {
  363.  hits_object *intern;
  364.  long hits_length;
  365.  
  366.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
  367.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  368.  return;
  369.  }
  370.  
  371.  intern = (hits_object*)
  372.  zend_object_store_get_object(getThis() TSRMLS_CC);
  373.  
  374.  if (intern->hits != NULL) {
  375.  hits_length = (long) intern->hits->length();
  376.  } else {
  377.  /* Should not occur */
  378.  php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Object was not fully initialized");
  379.  return;
  380.  /* hits_length = 0; */
  381.  }
  382.  
  383.  RETURN_LONG(hits_length);
  384.  } /* }}} */
  385.  
  386.  /* {{{ proto string|NULL Hits::get(int n, string name)
  387. Returns the string value of the name field for the nth document in
  388. this set or NULL. */
  389.  PHP_METHOD(Hits, get)
  390.  {
  391.  hits_object *intern = NULL;
  392.  long n;
  393.  long max;
  394.  char *name = NULL;
  395.  char *tmp = NULL;
  396.  char *value = NULL;
  397.  wchar_t *wname = NULL;
  398.  long len;
  399.  size_t sz;
  400.  
  401.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &n, &name, &len) == FAILURE) {
  402.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  403.  return;
  404.  }
  405.  intern = (hits_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
  406.  
  407.  if (intern->hits != NULL) {
  408.  max = (long) intern->hits->length();
  409.  if ((n < 0) || (n >= max)) {
  410.  zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "Seek position %ld is out of range", n);
  411.  return;
  412.  }
  413.  try {
  414.  Document& doc = intern->hits->doc(n);
  415.  wname = Misc::_charToWide(name);
  416.  if (doc.get(wname) != NULL) {
  417.  //tmp = Misc::_wideToChar(doc.get(wname));
  418.  UnicodeChar_to_str((WCHAR*)doc.get(wname),tmp);
  419.  } else {
  420.  tmp = NULL;
  421.  }
  422.  delete [] wname;
  423.  } catch (CLuceneError& error) {
  424.  zend_throw_exception(zend_exception_get_default(), error.what(), 0 TSRMLS_CC);
  425.  return;
  426.  }
  427.  } else {
  428.  php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Object was not fully initialized");
  429.  return;
  430.  }
  431.  
  432.  if (tmp != NULL) {
  433.  value = estrdup(tmp);
  434.  delete tmp;tmp = NULL;
  435.  
  436.  RETURN_STRING(value, 0);
  437.  } else {
  438.  RETURN_NULL();
  439.  }
  440.  } /* }}} */
  441.  
  442.  /* {{{ proto int Hits::id(int n)
  443. Returns the id for the nth document in this set. */
  444.  PHP_METHOD(Hits, id)
  445.  {
  446.  hits_object *intern;
  447.  long n;
  448.  long max;
  449.  long id;
  450.  
  451.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &n) == FAILURE) {
  452.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  453.  return;
  454.  }
  455.  intern = (hits_object*)
  456.  zend_object_store_get_object(getThis() TSRMLS_CC);
  457.  
  458.  if (intern->hits != NULL) {
  459.  max = (long) intern->hits->length();
  460.  if ((n < 0) || (n >= max)) {
  461.  zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "Seek position %ld is out of range", n);
  462.  return;
  463.  }
  464.  try {
  465.  id = (long) (intern->hits->id(n));
  466.  } catch (CLuceneError& error) {
  467.  zend_throw_exception(zend_exception_get_default(), error.what(), 0 TSRMLS_CC);
  468.  return;
  469.  }
  470.  } else {
  471.  php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Object was not fully initialized");
  472.  return;
  473.  }
  474.  RETURN_LONG(id);
  475.  } /* }}} */
  476.  
  477.  /* {{{ proto float Hits::score(int n)
  478. Returns the score for the nth document in this set. */
  479.  PHP_METHOD(Hits, score)
  480.  {
  481.  hits_object *intern;
  482.  long n;
  483.  long max;
  484.  double score;
  485.  
  486.  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &n) == FAILURE) {
  487.  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  488.  return;
  489.  }
  490.  intern = (hits_object*)
  491.  zend_object_store_get_object(getThis() TSRMLS_CC);
  492.  
  493.  if (intern->hits != NULL) {
  494.  max = (long) intern->hits->length();
  495.  if ((n < 0) || (n >= max)) {
  496.  zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "Seek position %ld is out of range", n);
  497.  return;
  498.  }
  499.  try {
  500.  score = (double) (intern->hits->score(n));
  501.  } catch (CLuceneError& error) {
  502.  zend_throw_exception(zend_exception_get_default(), error.what(), 0 TSRMLS_CC);
  503.  return;
  504.  }
  505.  } else {
  506.  php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Object was not fully initialized");
  507.  return;
  508.  }
  509.  RETURN_DOUBLE(score);
  510.  } /* }}} */
  511.  
  512.  /* {{{ PHP_MINIT_FUNCTION */
  513.  PHP_MINIT_FUNCTION(clucene)
  514.  {
  515.  zend_class_entry index_searcher_ce;
  516.  zend_class_entry hits_ce;
  517.  
  518.  /* IndexSearcher class */
  519.  INIT_CLASS_ENTRY(index_searcher_ce, "IndexSearcher", index_searcher_functions);
  520.  index_searcher_class_entry = zend_register_internal_class(&index_searcher_ce TSRMLS_CC);
  521.  index_searcher_class_entry->create_object = index_searcher_object_new;
  522.  memcpy(&index_searcher_object_handlers, zend_get_std_object_handlers(),
  523.  sizeof(zend_object_handlers));
  524.  index_searcher_class_entry->ce_flags |= ZEND_ACC_FINAL_CLASS;
  525.  
  526.  /* Hits class */
  527.  INIT_CLASS_ENTRY(hits_ce, "Hits", hits_functions);
  528.  hits_class_entry = zend_register_internal_class(&hits_ce TSRMLS_CC);
  529.  hits_class_entry->create_object = hits_object_new;
  530.  memcpy(&hits_object_handlers, zend_get_std_object_handlers(),
  531.  sizeof(zend_object_handlers));
  532.  hits_class_entry->ce_flags |= ZEND_ACC_FINAL_CLASS;
  533.  
  534.  REGISTER_LONG_CONSTANT("STANDARD_ANALYZER", STANDARD_ANALYZER, CONST_CS | CONST_PERSISTENT);
  535.  
  536.  return SUCCESS;
  537.  }
  538.  /* }}} */
  539.  
  540.  
  541.  /* {{{ PHP_MSHUTDOWN_FUNCTION */
  542.  PHP_MSHUTDOWN_FUNCTION(clucene)
  543.  {
  544.  
  545.  /* add your stuff here */
  546.  
  547.  return SUCCESS;
  548.  }
  549.  /* }}} */
  550.  
  551.  
  552.  /* {{{ PHP_RINIT_FUNCTION */
  553.  PHP_RINIT_FUNCTION(clucene)
  554.  {
  555.  /* add your stuff here */
  556.  
  557.  return SUCCESS;
  558.  }
  559.  /* }}} */
  560.  
  561.  
  562.  /* {{{ PHP_RSHUTDOWN_FUNCTION */
  563.  PHP_RSHUTDOWN_FUNCTION(clucene)
  564.  {
  565.  /* add your stuff here */
  566.  
  567.  return SUCCESS;
  568.  }
  569.  /* }}} */
  570.  
  571.  
  572.  /* {{{ PHP_MINFO_FUNCTION */
  573.  PHP_MINFO_FUNCTION(clucene)
  574.  {
  575.  php_info_print_table_start();
  576.  {
  577.  php_info_print_table_row(2, "CLucene support", "enabled");
  578.  php_info_print_table_row(2, "Extension version", PHP_CLUCENE_MODULE_VERSION);
  579.  }
  580.  php_info_print_table_end();
  581.  }
  582.  /* }}} */
  583.  
  584. #endif /* HAVE_CLUCENE */
引用通告地址: 点击获取引用地址
评论: 3 | 引用: 0 | 阅读: 2552
发表评论
昵 称: 密 码:
网 址: 邮 箱:
选 项:    
头 像:
内 容: