processor_tokenizer.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Processor for tokenizing fulltext data by replacing (configurable)
  4. * non-letters with spaces.
  5. */
  6. class SearchApiTokenizer extends SearchApiAbstractProcessor {
  7. /**
  8. * @var string
  9. */
  10. protected $spaces;
  11. /**
  12. * @var string
  13. */
  14. protected $ignorable;
  15. public function configurationForm() {
  16. $form = parent::configurationForm();
  17. // Only make fulltext fields available as options.
  18. $fields = $this->index->getFields();
  19. $field_options = array();
  20. foreach ($fields as $name => $field) {
  21. if (empty($field['real_type']) && search_api_is_text_type($field['type'])) {
  22. $field_options[$name] = $field['name'];
  23. }
  24. }
  25. $form['fields']['#options'] = $field_options;
  26. $form += array(
  27. 'spaces' => array(
  28. '#type' => 'textfield',
  29. '#title' => t('Whitespace characters'),
  30. '#description' => t('Specify the characters that should be regarded as whitespace and therefore used as word-delimiters. ' .
  31. 'Specify the characters as a <a href="@link">PCRE character class</a>. ' .
  32. 'Note: For non-English content, the default setting might not be suitable.',
  33. array('@link' => url('http://www.php.net/manual/en/regexp.reference.character-classes.php'))),
  34. '#default_value' => "[^[:alnum:]]",
  35. ),
  36. 'ignorable' => array(
  37. '#type' => 'textfield',
  38. '#title' => t('Ignorable characters'),
  39. '#description' => t('Specify characters which should be removed from fulltext fields and search strings (e.g., "-"). The same format as above is used.'),
  40. '#default_value' => "[']",
  41. ),
  42. );
  43. if (!empty($this->options)) {
  44. $form['spaces']['#default_value'] = $this->options['spaces'];
  45. $form['ignorable']['#default_value'] = $this->options['ignorable'];
  46. }
  47. return $form;
  48. }
  49. public function configurationFormValidate(array $form, array &$values, array &$form_state) {
  50. parent::configurationFormValidate($form, $values, $form_state);
  51. $spaces = str_replace('/', '\/', $values['spaces']);
  52. $ignorable = str_replace('/', '\/', $values['ignorable']);
  53. if (@preg_match('/(' . $spaces . ')+/u', '') === FALSE) {
  54. $el = $form['spaces'];
  55. form_error($el, $el['#title'] . ': ' . t('The entered text is no valid regular expression.'));
  56. }
  57. if (@preg_match('/(' . $ignorable . ')+/u', '') === FALSE) {
  58. $el = $form['ignorable'];
  59. form_error($el, $el['#title'] . ': ' . t('The entered text is no valid regular expression.'));
  60. }
  61. }
  62. protected function processFieldValue(&$value) {
  63. $this->prepare();
  64. if ($this->ignorable) {
  65. $value = preg_replace('/(' . $this->ignorable . ')+/u', '', $value);
  66. }
  67. if ($this->spaces) {
  68. $arr = preg_split('/(' . $this->spaces . ')+/u', $value);
  69. if (count($arr) > 1) {
  70. $value = array();
  71. foreach ($arr as $token) {
  72. $value[] = array('value' => $token);
  73. }
  74. }
  75. }
  76. }
  77. protected function process(&$value) {
  78. // We don't touch integers, NULL values or the like.
  79. if (is_string($value)) {
  80. $this->prepare();
  81. if ($this->ignorable) {
  82. $value = preg_replace('/' . $this->ignorable . '+/u', '', $value);
  83. }
  84. if ($this->spaces) {
  85. $value = preg_replace('/' . $this->spaces . '+/u', ' ', $value);
  86. }
  87. }
  88. }
  89. protected function prepare() {
  90. if (!isset($this->spaces)) {
  91. $this->spaces = str_replace('/', '\/', $this->options['spaces']);
  92. $this->ignorable = str_replace('/', '\/', $this->options['ignorable']);
  93. }
  94. }
  95. }