| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* search_block.module
|
| 4 |
* This module allows for the removal of individual nodes or
|
| 5 |
* entire content types from the search index.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_form_alter
|
| 10 |
*/
|
| 11 |
function search_block_form_alter($form_id, &$form) {
|
| 12 |
if (isset($form['type'])) {
|
| 13 |
$type = $form['type']['#value'];
|
| 14 |
}
|
| 15 |
elseif (isset($form['old_type'])) {
|
| 16 |
$type = $form['old_type']['#value'];
|
| 17 |
}
|
| 18 |
else {
|
| 19 |
return;
|
| 20 |
}
|
| 21 |
|
| 22 |
$enabled = variable_get('search_block_'. $type, FALSE);
|
| 23 |
|
| 24 |
switch($form_id) {
|
| 25 |
case 'node_type_form':
|
| 26 |
// Display each radio option
|
| 27 |
$form['workflow']['search_block'] = array(
|
| 28 |
'#type' => 'radios',
|
| 29 |
'#title' => t('Block Searches of this Content Type'),
|
| 30 |
'#default_value' => $enabled,
|
| 31 |
'#options' => array(
|
| 32 |
FALSE => t('Allow Searches'),
|
| 33 |
TRUE => t('Block Searches'),
|
| 34 |
),
|
| 35 |
'#description' => t('Use the above options to hide this content type
|
| 36 |
from the search index. Changing this overwrites all previous node settings.'),
|
| 37 |
);
|
| 38 |
break;
|
| 39 |
|
| 40 |
// Here, we allow for a per-node search block.
|
| 41 |
// This is not shown if the content is globally blocked
|
| 42 |
// Perhaps we want to include the reverse option (index a single node)?
|
| 43 |
case $type . '_node_form':
|
| 44 |
$form['search_block_set'] = array(
|
| 45 |
'#type' => 'fieldset',
|
| 46 |
'#title' => t('Search Block'),
|
| 47 |
'#collapsible' => TRUE,
|
| 48 |
'#collapsed' => TRUE,
|
| 49 |
);
|
| 50 |
if(!$enabled) {
|
| 51 |
$form['search_block_set']['search_block'] = array(
|
| 52 |
'#type' => 'checkbox',
|
| 53 |
'#title' => t('Block this node from the search index'),
|
| 54 |
'#default_value' => $form['#node']->search_block,
|
| 55 |
'#description' => t('Enable this option to hide this node from the search index.'),
|
| 56 |
);
|
| 57 |
}
|
| 58 |
else {
|
| 59 |
// We need the divs otherwise the fieldset misrenders
|
| 60 |
$form['search_block_set']['notice'] = array(
|
| 61 |
'#prefix' => '<div>',
|
| 62 |
'#suffix' => '</div>',
|
| 63 |
'#value' => t('This content type is globally blocked from searches.'),
|
| 64 |
);
|
| 65 |
}
|
| 66 |
break;
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Implementation of hook_nodeapi
|
| 72 |
*/
|
| 73 |
function search_block_nodeapi(&$node, $op, $teaser, $page) {
|
| 74 |
$type_enabled = variable_get('search_block_'. $node->type, FALSE);
|
| 75 |
switch($op) {
|
| 76 |
case 'validate':
|
| 77 |
break;
|
| 78 |
case 'load':
|
| 79 |
$query = "SELECT enabled FROM {search_block} WHERE nid = %d";
|
| 80 |
$result = db_query($query, $node->nid);
|
| 81 |
if((db_result($result, 0) == TRUE) || $type_enabled) {
|
| 82 |
$node->search_block = TRUE;
|
| 83 |
}
|
| 84 |
else {
|
| 85 |
$node->search_block = FALSE;
|
| 86 |
}
|
| 87 |
break;
|
| 88 |
case 'insert':
|
| 89 |
$query = "";
|
| 90 |
if($node->search_block || $type_enabled) {
|
| 91 |
$query = "INSERT INTO {search_block} VALUES ('%d', '1', '1')";
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
$query = "INSERT INTO {search_block} VALUES ('%d', '0', '1')";
|
| 95 |
}
|
| 96 |
$result = db_query($query, $node->nid);
|
| 97 |
break;
|
| 98 |
case 'update':
|
| 99 |
$query = "";
|
| 100 |
if($node->search_block || $type_enabled) {
|
| 101 |
$query = "INSERT INTO {search_block} (nid, enabled, dirty) VALUES ('%d', '1', '1')
|
| 102 |
ON DUPLICATE KEY UPDATE enabled='1', dirty='1'";
|
| 103 |
}
|
| 104 |
else {
|
| 105 |
$query = "INSERT INTO {search_block} (nid, enabled, dirty) VALUES ('%d', '0', '1')
|
| 106 |
ON DUPLICATE KEY UPDATE enabled='0'";
|
| 107 |
}
|
| 108 |
$result = db_query($query, $node->nid);
|
| 109 |
break;
|
| 110 |
case 'delete':
|
| 111 |
$query = "DELETE FROM {search_block} WHERE nid=%d";
|
| 112 |
$result = db_query($query, $node->nid);
|
| 113 |
break;
|
| 114 |
case 'view':
|
| 115 |
break;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
/**
|
| 119 |
* Implementatino of hook_update_index
|
| 120 |
*
|
| 121 |
* We might have a race condition here with other instances
|
| 122 |
* of module's hook_update_index, but I'm not sure and it's
|
| 123 |
* been working so far.
|
| 124 |
*/
|
| 125 |
function search_block_update_index() {
|
| 126 |
$query = "SELECT nid FROM {search_block} WHERE enabled='1' AND dirty='1'";
|
| 127 |
$result = db_query($query);
|
| 128 |
$row = 0;
|
| 129 |
while($nid = db_result($result, $row++)) {
|
| 130 |
watchdog('search_block', 'Dropping index for nid ' . $nid);
|
| 131 |
search_index($nid, 'node', '');
|
| 132 |
$query = "UPDATE {search_block} SET dirty='0' WHERE nid=%d";
|
| 133 |
db_query($query, $nid);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Implementation of hook_cron
|
| 139 |
*
|
| 140 |
* As there is no way to implement a callback function from
|
| 141 |
* hook_form_alter, we keep a flag variable and check for changes.
|
| 142 |
* If the variable has changed, mark appropriate nodes as dirty
|
| 143 |
* to either be removed from the index or reindex.
|
| 144 |
*
|
| 145 |
* If cron times out, the dirty flags will be attempted to be reset
|
| 146 |
* on the next run as the flag is only cleared after success.
|
| 147 |
*/
|
| 148 |
function search_block_cron() {
|
| 149 |
$types= node_get_types();
|
| 150 |
foreach($types as $type=>$object) {
|
| 151 |
$enabled = variable_get('search_block_' . $type, FALSE);
|
| 152 |
$previous = variable_get('search_block_previous_' . $type, FALSE);
|
| 153 |
if($previous < $enabled) {
|
| 154 |
watchdog('search_block', 'Enabling and setting dirty for ' . $type);
|
| 155 |
$query = "SELECT DISTINCT nid FROM {node} WHERE type='%s'";
|
| 156 |
$result = db_query($query, $type);
|
| 157 |
$row = 0;
|
| 158 |
while($nid = db_result($result, $row++)) {
|
| 159 |
$query = "INSERT INTO {search_block} (nid, enabled, dirty) VALUES ('%d', '1', '1')
|
| 160 |
ON DUPLICATE KEY UPDATE enabled='1', dirty='1'";
|
| 161 |
db_query($query, $nid);
|
| 162 |
}
|
| 163 |
variable_set('search_block_previous_' . $type, $enabled);
|
| 164 |
}
|
| 165 |
else if ($previous > $enabled) {
|
| 166 |
watchdog('search_block', 'Disabling and clearing dirty for ' . $type);
|
| 167 |
$query = "SELECT DISTINCT nid FROM {node} WHERE type='%s'";
|
| 168 |
$result = db_query($query, $type);
|
| 169 |
$row = 0;
|
| 170 |
while($nid = db_result($result, $row++)) {
|
| 171 |
$query = "INSERT INTO {search_block} (nid, enabled, dirty) VALUES ('%d', '0', '1')
|
| 172 |
ON DUPLICATE KEY UPDATE enabled=0, dirty=1";
|
| 173 |
db_query($query, $nid);
|
| 174 |
}
|
| 175 |
variable_set('search_block_previous_' . $type, $enabled);
|
| 176 |
}
|
| 177 |
}
|
| 178 |
|
| 179 |
// If we've disabled search blocking for a content type or a node
|
| 180 |
// we may still need to reindex it
|
| 181 |
$query = "SELECT nid FROM {search_block} WHERE enabled=0 AND dirty=1";
|
| 182 |
$result = db_query($query);
|
| 183 |
|
| 184 |
$row = 0;
|
| 185 |
while($nid = db_result($result, $row++)) {
|
| 186 |
_search_block_reindex(node_load($nid));
|
| 187 |
$query = "UPDATE {search_block} SET dirty=0 WHERE nid=%d";
|
| 188 |
db_query($query, $nid);
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Reindex a node, adding it to the search index. As this is
|
| 194 |
* from node.module, keep note of any changes to this code
|
| 195 |
* from there and update it.
|
| 196 |
*/
|
| 197 |
function _search_block_reindex($node) {
|
| 198 |
// Build the node body.
|
| 199 |
$node = node_build_content($node, FALSE, FALSE);
|
| 200 |
$node->body = drupal_render($node->content);
|
| 201 |
|
| 202 |
$text = '<h1>'. check_plain($node->title) .'</h1>'. $node->body;
|
| 203 |
|
| 204 |
// Fetch extra data normally not visible
|
| 205 |
$extra = node_invoke_nodeapi($node, 'update index');
|
| 206 |
foreach ($extra as $t) {
|
| 207 |
$text .= $t;
|
| 208 |
}
|
| 209 |
|
| 210 |
// Update index
|
| 211 |
search_index($node->nid, 'node', $text);
|
| 212 |
}
|