| 1 |
<?php
|
| 2 |
// $Id: field_indexer.module,v 1.7 2009/01/05 01:30:46 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Indexes field content for use by modules that implement field search.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function field_indexer_menu() {
|
| 13 |
$items = array();
|
| 14 |
$items['admin/settings/field_indexer'] = array(
|
| 15 |
'title' => 'Field indexer',
|
| 16 |
'access callback' => 'user_access',
|
| 17 |
'access arguments' => array('administer search'),
|
| 18 |
'description' => 'Enable fields for use by field searches.',
|
| 19 |
'page callback' => 'drupal_get_form',
|
| 20 |
'page arguments' => array('field_indexer_admin'),
|
| 21 |
'file' => 'field_indexer.admin.inc',
|
| 22 |
);
|
| 23 |
|
| 24 |
return $items;
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Update the full-text search index for a particular field value.
|
| 29 |
*
|
| 30 |
* @param $nid
|
| 31 |
* Id of the node containing the value.
|
| 32 |
* @param $fiid
|
| 33 |
* Id of the field containing the value.
|
| 34 |
* @param $text
|
| 35 |
* The content of the field. Must be a piece of HTML text.
|
| 36 |
*/
|
| 37 |
function field_indexer_index($nid, $fiid, $text) {
|
| 38 |
search_index($nid, field_indexer_type($fiid), $text);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Mark all nodes for re-indexing.
|
| 43 |
*/
|
| 44 |
function field_indexer_reindex() {
|
| 45 |
node_search('reset');
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Delete index entries for the specified field.
|
| 50 |
*/
|
| 51 |
function field_indexer_wipe($fiid) {
|
| 52 |
$type = field_indexer_type($fiid);
|
| 53 |
db_query("DELETE FROM {search_dataset} WHERE type = '%s'", $type);
|
| 54 |
db_query("DELETE FROM {search_index} WHERE type = '%s'", $type);
|
| 55 |
db_query("DELETE FROM {search_node_links} WHERE type = '%s'", $type);
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Retrieve a fresh list of fields that are exposed by modules for indexing.
|
| 60 |
*/
|
| 61 |
function field_indexer_find_fields() {
|
| 62 |
$fields = array();
|
| 63 |
foreach (module_implements('field_indexer_list') as $module) {
|
| 64 |
$module_fields = module_invoke($module, 'field_indexer_list');
|
| 65 |
foreach ($module_fields as $field) {
|
| 66 |
// Add the field.
|
| 67 |
$fields[_field_indexer_key($field)] = $field;
|
| 68 |
}
|
| 69 |
}
|
| 70 |
return $fields;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Retrieve the fields settings from the database.
|
| 75 |
*
|
| 76 |
* @param $status
|
| 77 |
* Optional status of the fields to be returned (TRUE for fields that are
|
| 78 |
* enabled for indexing, FALSE for fields that are not being indexed). When
|
| 79 |
* set, only fields with the given status are returned. When not set, all
|
| 80 |
* indexable fields are returned.
|
| 81 |
* @param $namespace
|
| 82 |
* Optional namespace of whose fields we are interested in. When not
|
| 83 |
* specified, all indexable fields are returned.
|
| 84 |
* @return
|
| 85 |
* An array describing the fields.
|
| 86 |
*/
|
| 87 |
function field_indexer_load_fields($status = NULL, $namespace = NULL) {
|
| 88 |
$where = array();
|
| 89 |
$args = array();
|
| 90 |
if (isset($status)) {
|
| 91 |
$where[] = 'status = %d';
|
| 92 |
$args[] = $status ? 1 : 0;
|
| 93 |
}
|
| 94 |
if (isset($namespace)) {
|
| 95 |
$where[] = "namespace = '%s'";
|
| 96 |
$args[] = $namespace;
|
| 97 |
}
|
| 98 |
if (count($where)) {
|
| 99 |
$where = ' WHERE '. implode(' AND ', $where);
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
$where = '';
|
| 103 |
}
|
| 104 |
$fields = array();
|
| 105 |
$results = db_query('SELECT * FROM {field_indexer_map}'. $where, $args);
|
| 106 |
while ($field = db_fetch_array($results)) {
|
| 107 |
$fields[_field_indexer_key($field)] = $field;
|
| 108 |
}
|
| 109 |
return $fields;
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Return the index type of the specified field.
|
| 114 |
*/
|
| 115 |
function field_indexer_type($fiid) {
|
| 116 |
return 'field_'. $fiid;
|
| 117 |
}
|
| 118 |
|
| 119 |
/**
|
| 120 |
* Return a unique key for a field. This is useful when collecting fields whose
|
| 121 |
* fiid have not yet been assigned.
|
| 122 |
*/
|
| 123 |
function _field_indexer_key($field) {
|
| 124 |
return $field['namespace'] .'_'. $field['name'] .'_'. $field['extra_name'];
|
| 125 |
}
|