/[drupal]/contributions/modules/google_appliance/google_appliance.module
ViewVC logotype

Contents of /contributions/modules/google_appliance/google_appliance.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue Apr 8 17:50:32 2008 UTC (19 months, 3 weeks ago) by jacobsingh
Branch: MAIN
CVS Tags: DRUPAL-5--1-0BETA1, DRUPAL-5--1-0BETA2, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--2
File MIME type: text/x-php
Initial Commit of the google_appliance module!
1 <?php
2 /**
3 * @file GSA integration
4 */
5
6 /**
7 * Implementation of hook_menu()
8 *
9 */
10 function google_appliance_menu($may_cache = false) {
11 $items = array();
12 if ($may_cache) {
13 $items[] = array (
14 'path' => 'google_appliance',
15 'callback' => 'google_appliance_search',
16 'type' => MENU_CALLBACK,
17 'access' => true,
18 );
19
20 $items[] = array (
21 'path' => 'google_appliance_feeder',
22 'callback' => 'google_apppliance_feeder',
23 'type' => MENU_CALLBACK,
24 'access' => true,
25 );
26
27 $items[] = array(
28 'path' => 'admin/settings/google_appliance',
29 'title' => t('Google Appliance Settings'),
30 'description' => t('Configuration for the Google Appliance search'),
31 'callback' => 'drupal_get_form',
32 'callback arguments' => array('google_appliance_admin_settings'),
33 'access' => user_access('administer search'),
34 'type' => MENU_NORMAL_ITEM,
35 );
36 } else {
37 _google_appliance_add_meta_tags();
38 }
39 return $items;
40 }
41
42
43 /**
44 * Implementation of hook_block().
45 */
46 function google_appliance_block($op = 'list', $delta = 0, $edit = array()) {
47 switch ($op) {
48
49 case 'list':
50 $blocks['recommended_links']['info'] = t("Recommended Links");
51 $blocks['recommended_links']['title'] = t('Recommended Links');
52 $blocks['recommended_links']['pages'] = '*search/google_appliance*';
53 $blocks['recommended_links']['visibility'] = 1;
54
55 return $blocks;
56 break;
57
58 case 'view':
59
60 switch ($delta) {
61
62 case 'recommended_links':
63 if ($result =& google_appliance_static_response_cache()) {
64 $matches = $result->getKeyMatches();
65 if (!$matches) {
66 return;
67 }
68
69 $links = array();
70 foreach ($matches as $link => $title) {
71 $links[] = l($title,$link);
72 }
73
74 if (count($links)) {
75 $block['content'] = theme('item_list',$links);
76 } else {
77 return false;
78 }
79 }
80 break;
81
82 }
83
84 return $block;
85 break;
86 }
87 }
88
89 function google_appliance_admin_settings() {
90 $form = array();
91
92 // initial required config fields
93 $form["config_init"] = array(
94 "#title" => t("Initial Configuration"),
95 "#type" => "fieldset",
96 );
97
98 $form["config_init"]["google_appliance_name"] = array(
99 "#type" => "textfield",
100 "#size" => 30,
101 "#title" => t("Search Name"),
102 "#description" => t('The name of this search, to appear as sub-navigation on the search page.'),
103 "#default_value" => variable_get('google_appliance_name', 'Google Appliance'),
104 "#required" => true,
105 );
106
107 $form["config_init"]["google_appliance_host_name"] = array(
108 "#type" => "textfield",
109 "#size" => 50,
110 "#title" => t("Host Name"),
111 "#description" => t('Your Google Search Appliance host name or IP address (with http:// or https://), which were assigned when the appliance was set up.<br />You do <b>not</b> need to include "/search" at the end, or a trailing slash, but you should include a port number if needed.<br/> Example: http://mygooglebox.com'),
112 "#default_value" => variable_get('google_appliance_host_name', ''),
113 "#required" => true,
114 );
115
116 $form["config_init"]["google_appliance_collection"] = array(
117 "#type" => "textfield",
118 "#size" => 20,
119 "#title" => t("Collection"),
120 "#description" => t('The name of the collection of indexed content to search.'),
121 "#default_value" => variable_get('google_appliance_collection', ''),
122 "#required" => true,
123 );
124
125 $form["config_init"]["google_appliance_client"] = array(
126 "#type" => "textfield",
127 "#size" => 20,
128 "#title" => t("Client"),
129 "#description" => t('The name of a valid front-end, defined when you set up the appliance.'),
130 "#default_value" => variable_get('google_appliance_client', ''),
131 "#required" => true,
132 );
133
134 $form["config_init"]["google_appliance_cache_timeout"] = array(
135 "#type" => "textfield",
136 "#size" => 20,
137 "#title" => t("Cache Timeout"),
138 "#description" => t('If you wish to use caching of results (to reduce load on mini, enter a timeout here'),
139 "#default_value" => variable_get('google_appliance_cache_timeout', ''),
140 );
141
142
143 $form["config_init"]["google_debug"] = array(
144 "#type" => "textfield",
145 "#size" => 20,
146 "#title" => t("Debug Level"),
147 "#description" => t('1 = watchdog, 2 = dpr(needs devel module), 3 = more dpr\'s'),
148 "#default_value" => variable_get('google_debug', ''),
149 );
150
151
152 // error message config
153 $form["config_messages"] = array(
154 "#title" => t("Error Messages"),
155 "#type" => "fieldset",
156 "#collapsible" => true,
157 );
158
159 $form["config_messages"]["google_appliance_errorcode_1"] = array(
160 "#title" => t("No results found"),
161 "#type" => "textfield",
162 "#size" => 100,
163 "#maxlength" => 255,
164 "#required" => true,
165 "#description" => t('If there are no results for the search criteria.'),
166 "#default_value" => variable_get('google_appliance_errorcode_1', 'No results were found that matched your criteria. Please try broadening your search.'),
167 );
168
169 $form["config_messages"]["google_appliance_errorcode_2"] = array(
170 "#title" => t("More than 1,000 results"),
171 "#type" => "textfield",
172 "#size" => 100,
173 "#maxlength" => 255,
174 "#required" => true,
175 "#description" => t('If there are more than 1,000 results for the search criteria.'),
176 "#default_value" => variable_get('google_appliance_errorcode_2', 'Sorry, but our search does not return more than 1,000 records, please refine your criteria.'),
177 );
178
179 $form["config_messages"]["google_appliance_errorcode_neg_99"] = array(
180 "#title" => t("Cannot perform search"),
181 "#type" => "textfield",
182 "#size" => 100,
183 "#maxlength" => 255,
184 "#required" => true,
185 "#description" => t('If the search cannot perform due to a query error.'),
186 "#default_value" => variable_get('google_appliance_errorcode_neg_99', 'We apologize, but your search cannot be completed at this time, please try again later.'),
187 );
188
189 $form["config_messages"]["google_appliance_errorcode_neg_100"] = array(
190 "#title" => t("Cannot connect to Google Appliance"),
191 "#type" => "textfield",
192 "#size" => 100,
193 "#maxlength" => 255,
194 "#required" => true,
195 "#description" => t('If the search cannot connect to the Google Appliance server.'),
196 "#default_value" => variable_get('google_appliance_errorcode_neg_100','We apologize, but the connection to our search engine appears to be down at the moment, please try again later.'),
197 );
198
199 // optional metadata configuration
200 /*
201 $form["config_metadata"] = array(
202 "#title" => t("Metadata Configuration"),
203 "#type" => "fieldset",
204 "#collapsible" => true,
205 );
206 */
207
208 // last but not least, submit
209 $form["submit"] = array(
210 "#type" => "submit",
211 "#value" => t("Save Settings"),
212 );
213
214 return $form;
215 }
216
217 /**
218 * Validation function, though it's actually getting overridden by the #required fields...
219 *
220 */
221 function google_appliance_admin_settings_validate($form_id, $form) {
222 if (empty($form['google_appliance_host_name'])) {
223 form_set_error('google_appliance_host_name', t('Please enter your host name or IP address.'));
224 }
225
226 if (empty($form['google_appliance_collection'])) {
227 form_set_error('google_appliance_collection', t('Please enter the name of the collection you want to search.'));
228 }
229
230 if (empty($form['google_appliance_client'])) {
231 form_set_error('google_appliance_client', t('Please enter name of the client frontend you are searching.'));
232 }
233
234 if (empty($form['google_appliance_name'])) {
235 form_set_error('google_appliance_name', t('Please enter the name of this search, to appear as sub-navigation on the search page.'));
236 }
237 }
238
239 /**
240 * Submits the admin settings form and saves all the variables.
241 */
242 function google_appliance_admin_settings_submit($form_id, $form) {
243 variable_set('google_appliance_host_name', check_plain($form['google_appliance_host_name']));
244 variable_set('google_appliance_collection', check_plain($form['google_appliance_collection']));
245 variable_set('google_appliance_client', check_plain($form['google_appliance_client']));
246 variable_set('google_appliance_name', check_plain($form['google_appliance_name']));
247 variable_set('google_appliance_cache_timeout', check_plain($form['google_appliance_cache_timeout']));
248 variable_set('google_debug', check_plain($form['google_debug']));
249
250 // don't run check_plain on these because they can have HTML
251 variable_set('google_appliance_errorcode_1', $form['google_appliance_errorcode_1']);
252 variable_set('google_appliance_errorcode_2', $form['google_appliance_errorcode_2']);
253 variable_set('google_appliance_errorcode_neg_99', $form['google_appliance_errorcode_neg_99']);
254 variable_set('google_appliance_errorcode_neg_100', $form['google_appliance_errorcode_neg_100']);
255
256 drupal_set_message(t('Your settings have been saved.'));
257 }
258
259 /**
260 * Invokes the google_appliance_appconfig hook to add <meta> tags to nodes
261 * for indexing metadata by the google crawler.
262 *
263 */
264 function _google_appliance_add_meta_tags() {
265 if (arg(0) == 'node' && is_numeric(arg(1))) {
266 $node = node_load(arg(1));
267 theme('add_google_appliance_meta_tags', $node);
268 }
269 }
270
271 function google_appliance_feeder() {
272 //not sure if this is appropriate as everything should be findable via the main site, but this basically just creates a listing of all nodes.
273 $result = db_query('SELECT * from {node} WHERE status = 1');
274 print '<html>';
275 print '<body>';
276
277 while ($row = db_fetch_object($result)) {
278 print l($row->title,'node/' . $row->nid);
279 }
280
281 print '</body>';
282 print '</html>';
283 die();
284 }
285
286 /**
287 * Implementation of hook_search()
288 *
289 * @param string $op
290 * Operation - name, reset, search, status
291 * @param string $keys
292 * Keyword string sent to the search
293 * @return
294 * Array of search results (each is an assoc. array) that can be fed to a theme function
295 */
296 function google_appliance_search($op = 'search', $keys = null) {
297 switch ($op) {
298 case 'name':
299 return t(variable_get('google_appliance_name', "Google Appliance"));
300 break;
301
302 case 'search':
303 $dir = drupal_get_path('module', 'google_appliance');
304 include_once $dir . '/DrupalGoogleMini.php';
305
306 $google_debug = variable_get('google_debug',0);
307 if ($google_debug >= 2 ){
308 $gm = new DrupalGoogleMini(true,'dpr');
309 } elseif ($google_debug == 1) {
310 $gm = new DrupalGoogleMini(true);
311 } else {
312 $gm = new DrupalGoogleMini(false);
313 }
314
315 if ($cache = variable_get('google_appliance_cache_timeout',0) ) {
316 cache_clear_all(null,'cache_google');
317 $gm->cache = true;
318 }
319
320 // initialize search object
321 try {
322 $gm->setOutputEncoding('utf8');
323 $gm->setInputEncoding('utf8');
324 $gm->setMetaDataRequested('*');
325
326 // get configuration from settings page
327 $_tmp_host = variable_get('google_appliance_host_name', false);
328 if (!$_tmp_host) {
329 drupal_set_message(t('No host name has been configured for the search appliance. Please enter it on the <a href="@admin-url">Google Appliance settings page</a>', array("@admin-url" => url("admin/settings/search/google_appliance"))), 'error');
330 return false;
331 }
332 $gm->baseUrl = $_tmp_host . "/search";
333 $gm->collection = variable_get('google_appliance_collection', '');
334 $gm->setQueryPart('client',variable_get('google_appliance_client', ''));
335
336 // set search parameters
337 $gm->setKeywords($keys);
338
339 if (module_exists('i18n')) {
340 if ($lang = i18n_get_lang()) {
341 $gm->setLanguageFilter(array($lang));
342 }
343 }
344 }
345 catch (GoogleMiniCriteriaException $e) {
346 $code = $e->getCode();
347 if ($message = variable_get('google_appliance_errorcode_' . $code,'')) {
348 $user_message = $message;
349 } else {
350 $user_message = GoogleMiniException::getUserMessage($code);
351 }
352
353 $error_message = $e->getMessage();
354 if ($code > 0) {
355 $output .= "<h2>" . $user_message . "</h2>";
356 return $output;
357 } else {
358 watchdog('google_appliance', $error_message);
359 drupal_set_message($error_message, 'error');
360 }
361 }
362
363 // perform the search
364 $results = array();
365 try {
366 $resultIterator = $gm->query();
367 google_appliance_static_response_cache($resultIterator);
368 foreach ($resultIterator as $key => $result) {
369 /*$results[] = array('link' => url('node/'. $doc->nid, NULL, NULL, TRUE),
370 'type' => node_get_types('name', $doc),
371 'title' => $doc->title,
372 'user' => theme('username', $doc),
373 'date' => $doc->changed,
374 'node' => $doc,
375 'extra' => $extra,
376 'score' => $doc->score,
377 'snippet' => $snippet);
378 */
379 $results[] = theme('google_appliance_search_result_array', $result);
380 }
381 }
382 catch(Exception $e) {
383 if ($e->getCode() > 0) {
384 google_appliance_static_response_cache($resultIterator);
385 }
386 drupal_set_message($e->getMessage());
387 return false;
388 }
389
390 return $results;
391 break;
392
393 case 'reset':
394 case 'status':
395 // do nothing
396 break;
397 }
398 }
399
400
401 function theme_add_google_appliance_meta_tags($node) {
402 // create list of tags to add
403 $results = array();
404
405 /**
406 * Adding taxonomy tags
407 */
408 $vocabs = taxonomy_get_vocabularies();
409 if (module_exists('nat') && $node->nat) {
410 $node->taxonomy = array_merge($node->nat,$node->taxonomy);
411 //$node->taoxnomy[] = $
412 }
413 foreach ($node->taxonomy as $term ) {
414 $tagname = 'category-' . strtolower($vocabs[$term->vid]->name);
415 $results[] = array($tagname, $term->name);
416 }
417
418 /**
419 * Adding sort date IMPORTANT: for sorting, mini must be configured to use htis tag
420 */
421
422 $results[] = array('date', date('Y-m-d h:i:s', $node->changed));
423 $results[] = array('created', date('Y-m-d h:i:s', $node->created));
424
425 /**
426 * Normally this doesn't matter,
427 * but if you want to allow the gsa to
428 * access unpublished pages and later filter
429 * on this you will need it.
430 */
431 $results[] = array('status', $node->status);
432
433
434 /**
435 * i18n configuration
436 */
437 if ($node->language) {
438 $results[] = array('content-language', $node->language);
439 }
440
441 /**
442 * node type
443 */
444 $results[] = array('type', $node->type);
445
446 /**
447 * Author
448 */
449 $node->uid = empty($node->uid) ? 0: $node->uid;
450 $user = user_load(array('uid' => 0));
451 $user->name = empty($user->name) ? 'anonymous' : $user->name;
452 $results[] = array('author' => $user->name);
453
454 // add meta tags
455 foreach ($results as $res) {
456 list($name, $content) = $res;
457 $content = strip_tags($content);
458 drupal_set_html_head('<meta name="'. htmlentities($name) .'" content="'. htmlentities($content) .'" />');
459 }
460 }
461
462 function theme_google_appliance_search_result_array($result) {
463 return array(
464 'link' => $result->U,
465 'user' => $result->getMetaData('user'),
466 'type' => $result->getMetaData('type'),
467 'title' => strip_tags($result->T),
468 'date' => strtotime($result->getMetaData('date')),
469 'snippet' => decode_entities((string)$result->S),
470 );
471 }
472
473 function google_appliance_form_alter($form_id,&$form) {
474 if ($form_id == 'block_admin_configure'){
475 $module = $form['module']['#value'];
476 $delta = $form['delta']['#value'];
477 $var_name = $module . '-' .$delta;
478
479 $ga_blocksettings = google_appliance_blocksettings_get();
480
481 $form['block_settings']['google_appliance'] = array (
482 '#type' => 'fieldset',
483 '#title' => t("Google Appliance Settings"),
484 '#description' => t(''),
485 '#collapsed' => true,
486 '#collapsible' => true,
487 '#tree' => true,
488 '#weight' => -1,
489 );
490
491 $form['block_settings']['google_appliance']['hide'] = array (
492 '#type' => 'radios',
493 '#title' => t("Do you want to hide this block from the GSA crawler?"),
494 '#description' => t('Select No if you want this block content to be crawled with the page content.'),
495 '#options' => array(1 => t('Yes'), 0 => t('No')),
496 '#default_value' => isset($ga_blocksettings[$var_name]) ? $ga_blocksettings[$var_name] : 1,
497 '#collapsed' => true,
498 '#collapsible' => true,
499 '#tree' => true,
500 );
501
502 $form['#submit']['google_appliance_block_save'] = array();
503 return $form;
504
505 }
506 }
507
508 function google_appliance_block_save($form_id,$form_values) {
509 $var_name = $form_values['module'] . '-' . $form_values['delta'];
510 $block_settings = google_appliance_blocksettings_get();
511 if (!isset($form_values['google_appliance']['hide'])) {
512 unset($block_settings[$var_name]);
513 } else {
514 $block_settings[$var_name] = $form_values['google_appliance']['hide'];
515 }
516
517 google_appliance_blocksettings_set($block_settings);
518 }
519
520
521 function google_appliance_blocksettings_set($new_val) {
522 variable_set('google_appliance_block_settings',$new_val);
523 }
524
525
526 function google_appliance_blocksettings_get() {
527 static $google_appliance;
528 if (empty($google_appliance)) {
529 $google_appliance = variable_get('google_appliance_block_settings',array());
530 }
531 return $google_appliance;
532 }
533
534 function google_appliance_block_nogoogle($block) {
535 $gsa_block_settings = google_appliance_blocksettings_get();
536 $var_name = $block->module . '-' . $block->delta;
537 if (!isset($gsa_block_settings[$var_name]) || $gsa_block_settings[$var_name]) {
538 return true;
539 }
540 }
541
542
543 /**
544 * It is important to hold on to the Google Appliance response object for the duration of the
545 * page request so that we can use it for things like the keymatch block
546 *
547 * Stolen from apachesolr module
548 *
549 *
550 */
551 function &google_appliance_static_response_cache($response = NULL) {
552 static $_response;
553
554 if (!empty($response)) {
555 $_response = drupal_clone($response);
556 }
557 return $_response;
558 }
559
560
561 function google_appliance_simpletest() {
562 $dir = drupal_get_path('module', 'google_appliance'). '/tests';
563 $tests = file_scan_directory($dir, '\.test$');
564 return array_keys($tests);
565 }
566
567 ?>

  ViewVC Help
Powered by ViewVC 1.1.2