updated synonyms to 1.5

This commit is contained in:
Bachir Soussi Chiadmi
2016-11-05 17:54:56 +01:00
parent 773e7fbddd
commit 252abe9b0e
51 changed files with 6500 additions and 2484 deletions

View File

@@ -0,0 +1,12 @@
<?php
/**
* @file
* Enables Product Reference field type for synonyms "search" integration.
*/
/**
* Definition of SearchCommerceProductReferenceSynonymsBehavior class.
*/
class SearchCommerceProductReferenceSynonymsBehavior extends CommerceProductReferenceSynonymsBehavior implements SearchSynonymsBehavior {
}

View File

@@ -9,6 +9,6 @@
/**
* Interface of search integration synonyms behavior.
*/
interface SearchSynonymsBehavior extends SynonymsSynonymsBehavior {
interface SearchSynonymsBehavior extends SynonymsBehavior {
}

View File

@@ -19,9 +19,11 @@ $plugin = array(
* Trigger re-indexing of all the nodes that reference terms from the vocabulary
* where the change has taken place.
*/
function synonyms_search_behavior_search_enabled($behavior_definition, $settings, $instance) {
module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
synonyms_search_reindex_nodes_by_vocabulary(taxonomy_vocabulary_machine_name_load($instance['bundle']));
function synonyms_search_behavior_search_enabled($behavior_definition, $behavior_implementation) {
if ($behavior_implementation['entity_type'] == 'taxonomy_term') {
module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
synonyms_search_reindex_nodes_by_vocabulary(taxonomy_vocabulary_machine_name_load($behavior_implementation['bundle']));
}
}
/**
@@ -30,7 +32,9 @@ function synonyms_search_behavior_search_enabled($behavior_definition, $settings
* Trigger re-indexing of all the nodes that reference terms from the vocabulary
* where the change has taken place.
*/
function synonyms_search_behavior_search_disabled($behavior_definition, $behavior_implementation, $instance) {
module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
synonyms_search_reindex_nodes_by_vocabulary(taxonomy_vocabulary_machine_name_load($instance['bundle']));
function synonyms_search_behavior_search_disabled($behavior_definition, $behavior_implementation) {
if ($behavior_implementation['entity_type'] == 'taxonomy_term') {
module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
synonyms_search_reindex_nodes_by_vocabulary(taxonomy_vocabulary_machine_name_load($behavior_implementation['bundle']));
}
}

View File

@@ -1,9 +1,13 @@
name = Synonyms Search
description = "Provides synonyms integration with searching."
package = Taxonomy
package = Synonyms
core = 7.x
dependencies[] = synonyms
dependencies[] = search
dependencies[] = taxonomy
test_dependencies[] = entityreference:entityreference
test_dependencies[] = term_search:term_search
files[] = synonyms_search.test
@@ -11,10 +15,11 @@ files[] = includes/SearchSynonymsBehavior.interface.inc
files[] = includes/SearchTextSynonymsBehavior.class.inc
files[] = includes/SearchTaxonomySynonymsBehavior.class.inc
files[] = includes/SearchEntityReferenceSynonymsBehavior.class.inc
files[] = includes/SearchCommerceProductReferenceSynonymsBehavior.class.inc
; Information added by Drupal.org packaging script on 2015-12-02
version = "7.x-1.4"
; Information added by Drupal.org packaging script on 2016-05-07
version = "7.x-1.5"
core = "7.x"
project = "synonyms"
datestamp = "1449079740"
datestamp = "1462586641"

View File

@@ -34,8 +34,8 @@ function synonyms_search_taxonomy_term_update($term) {
$previous_search_synonyms = array();
foreach ($behavior_implementations as $behavior_implementation) {
$current_search_synonyms = array_merge($current_search_synonyms, synonyms_extract_synonyms($term, $behavior_implementation));
$previous_search_synonyms = array_merge($previous_search_synonyms, synonyms_extract_synonyms($term->original, $behavior_implementation));
$current_search_synonyms = array_merge($current_search_synonyms, $behavior_implementation['object']->extractSynonyms($term));
$previous_search_synonyms = array_merge($previous_search_synonyms, $behavior_implementation['object']->extractSynonyms($term->original));
}
$diff = array_diff($current_search_synonyms, $previous_search_synonyms);
if (!empty($diff) || count($current_search_synonyms) != count($previous_search_synonyms)) {
@@ -51,11 +51,13 @@ function synonyms_search_taxonomy_term_update($term) {
function synonyms_search_node_update_index($node) {
$output = array();
foreach (field_info_instances('node', $node->type) as $instance) {
// We go a field by field looking for taxonomy term reference and if that
// vocabulary has enabled search synonyms, we add them to the search index.
// We go a field by field looking for taxonomy term reference or entity
// reference of taxonomy term type and if that vocabulary has enabled search
// synonyms, we add them to the search index.
// TODO: implement this through foreign keys information. See
// term_merge_fields_with_foreign_key() function.
$field_info = field_info_field($instance['field_name']);
if ($field_info['type'] == 'taxonomy_term_reference') {
// For each term referenced in this node we have to add synonyms.
$terms = field_get_items('node', $node, $instance['field_name']);
if (is_array($terms) && !empty($terms)) {
foreach ($terms as $v) {
@@ -63,6 +65,14 @@ function synonyms_search_node_update_index($node) {
}
}
}
if ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
$terms = field_get_items('node', $node, $instance['field_name']);
if (is_array($terms) && !empty($terms)) {
foreach ($terms as $v) {
$output[] = $v['target_id'];
}
}
}
}
if (!empty($output)) {
@@ -72,7 +82,7 @@ function synonyms_search_node_update_index($node) {
$bundle = field_extract_bundle('taxonomy_term', $term);
$behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
foreach ($behavior_implementations as $implementation) {
$output = array_merge($output, synonyms_extract_synonyms($term, $implementation));
$output = array_merge($output, $implementation['object']->extractSynonyms($term));
}
}
}
@@ -90,15 +100,15 @@ function synonyms_search_term_update_index($term) {
$synonyms = array();
foreach ($behavior_implementations as $implementation) {
$synonyms = array_merge($synonyms, synonyms_extract_synonyms($term, $implementation));
$synonyms = array_merge($synonyms, $implementation['object']->extractSynonyms($term));
}
return implode(', ', $synonyms);
}
/**
* Implements hook_synonyms_behavior_implementation_info().
* Implements hook_synonyms_provider_field_behavior_implementation_info().
*/
function synonyms_search_synonyms_behavior_implementation_info($behavior) {
function synonyms_search_synonyms_provider_field_behavior_implementation_info($behavior) {
switch ($behavior) {
case 'search':
return array(
@@ -108,6 +118,7 @@ function synonyms_search_synonyms_behavior_implementation_info($behavior) {
'text' => 'SearchTextSynonymsBehavior',
'taxonomy_term_reference' => 'SearchTaxonomySynonymsBehavior',
'entityreference' => 'SearchEntityReferenceSynonymsBehavior',
'commerce_product_reference' => 'SearchCommerceProductReferenceSynonymsBehavior',
);
break;
}

View File

@@ -20,9 +20,9 @@ function synonyms_search_reindex_nodes_by_terms($tids) {
// that would imply a big amount of SQL queries on some websites.
$found_nids = array();
foreach (field_info_field_map() as $field_name => $v) {
// TODO: explore possibility of using foreign keys instead of hard coding
// fields types in here.
if ($v['type'] == 'taxonomy_term_reference' && isset($v['bundles']['node'])) {
// This field is taxonomy term reference and it is attached to nodes, so
// we will run EntityFieldQuery on it.
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->fieldCondition($field_name, 'tid', $tids, 'IN')
@@ -31,6 +31,18 @@ function synonyms_search_reindex_nodes_by_terms($tids) {
$found_nids = array_merge($found_nids, array_keys($result['node']));
}
}
if ($v['type'] == 'entityreference' && isset($v['bundles']['node'])) {
$field = field_info_field($field_name);
if ($field['settings']['target_type'] == 'taxonomy_term') {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->fieldCondition($field_name, 'target_id', $tids, 'IN')
->execute();
if (isset($result['node'])) {
$found_nids = array_merge($found_nids, array_keys($result['node']));
}
}
}
}
if (!empty($found_nids)) {
db_update('search_dataset')

View File

@@ -10,8 +10,6 @@
*/
abstract class AbstractSearchSynonymsWebTestCase extends SynonymsWebTestCase {
protected $behavior = 'search';
/**
* What search type is being tested.
*
@@ -30,7 +28,9 @@ abstract class AbstractSearchSynonymsWebTestCase extends SynonymsWebTestCase {
* SetUp method.
*/
public function setUp($modules = array()) {
$modules[] = 'synonyms_search';
$this->behavior_implementation['behavior'] = 'search';
array_unshift($modules, 'synonyms_search');
parent::setUp($modules);
// Create a few terms and synonyms.
@@ -121,6 +121,7 @@ class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
* SetUp method.
*/
public function setUp($modules = array()) {
$modules[] = 'entityreference';
parent::setUp($modules);
// Creating a test content type.
@@ -128,7 +129,18 @@ class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
'name' => 'Synonyms Test Content',
'type' => 'synonyms_test_content',
), 'Save content type');
}
/**
* Test searching nodes by a term synonym.
*
* Since logically term and its synonyms represent the same entity, the idea
* is that searching by a term synonym should trigger all content referencing
* that term to be included in search results. Additionally we test that when
* a synonym is deleted/edited in a term, corresponding content is no longer
* encountered when searched by ex-synonym.
*/
public function testSearchTermSynonym() {
// Attaching term reference field to the new content type.
$field = array(
'type' => 'taxonomy_term_reference',
@@ -150,24 +162,10 @@ class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
'entity_type' => 'node',
'bundle' => 'synonyms_test_content',
'label' => 'Synonym Terms',
'widget' => array(
'type' => 'synonyms_autocomplete',
),
);
field_create_instance($instance);
}
/**
* Test searching nodes by a term synonym.
*
* Since logically term and its synonyms represent the same entity, the idea
* is that searching by a term synonym should trigger all content referencing
* that term to be included in search results. Additionally we test that when
* a synonym is deleted/edited in a term, corresponding content is no longer
* encountered when searched by ex-synonym.
*/
public function testSearchTermSynonym() {
// Creating a node, which references all the terms we have.
$node = (object) array(
'type' => 'synonyms_test_content',
@@ -217,7 +215,99 @@ class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
// We disable entire field from search integration and make sure for all
// synonyms search results are empty.
synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
synonyms_behavior_implementation_delete($this->behavior_implementation);
$this->cronRun();
foreach ($this->terms as $k => $term) {
$items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
if (is_array($items)) {
foreach ($items as $synonym) {
$this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
}
}
}
}
/**
* Test searching nodes by a term synonym when referenced by entity reference.
*
* This test pretty much does the same thing as the testSearchTermSynonym()
* with the only different that the terms are referenced through entity
* reference field type.
*/
public function testSearchTermSynonymEntityReference() {
// Attaching entity reference field to the new content type.
$field = array(
'type' => 'entityreference',
'field_name' => 'synonyms_term_enabled',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'target_type' => 'taxonomy_term',
'handler_settings' => array(
'target_bundles' => array($this->vocabulary->machine_name),
),
),
);
$field = field_create_field($field);
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'node',
'bundle' => 'synonyms_test_content',
'label' => 'Synonym Terms',
);
field_create_instance($instance);
// Creating a node, which references all the terms we have.
$node = (object) array(
'type' => 'synonyms_test_content',
'title' => $this->randomName(),
'synonyms_term_enabled' => array(LANGUAGE_NONE => array(
array('target_id' => $this->terms['no_synonyms']->tid),
array('target_id' => $this->terms['one_synonym']->tid),
array('target_id' => $this->terms['two_synonyms']->tid),
)),
);
node_save($node);
// Rebuilding Search index.
$this->cronRun();
foreach ($this->terms as $k => $term) {
$this->assertSearchResults($term->name, array($node), 'Searching by name of the term ' . $k);
$items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
if (is_array($items)) {
foreach ($items as $delta => $item) {
$this->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
}
}
$items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
if (is_array($items)) {
foreach ($items as $delta => $item) {
$this->assertSearchResults($item['value'], array($node), 'Searching by synonym #' . $delta . ' of the term ' . $k);
}
}
}
// Removing a synonym from the term. Then asserting node got re-indexed with
// new values of synonyms.
$deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
taxonomy_term_save($this->terms['one_synonym']);
$this->cronRun();
$this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
// Editing a synonym in a term. Then asserting node got re-indexed with new
// values of synonyms.
$ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
$this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this->randomName();
taxonomy_term_save($this->terms['two_synonyms']);
$this->cronRun();
$this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
// We disable entire field from search integration and make sure for all
// synonyms search results are empty.
synonyms_behavior_implementation_delete($this->behavior_implementation);
$this->cronRun();
foreach ($this->terms as $k => $term) {
$items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
@@ -326,7 +416,7 @@ class TermSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
// We disable entire field from search integration and make sure for all
// synonyms search results are empty.
synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
synonyms_behavior_implementation_delete($this->behavior_implementation);
$this->cronRun();
foreach ($this->terms as $k => $term) {
$items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);