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

Contents of /contributions/modules/opensearchclient/opensearchclient.module

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


Revision 1.2 - (show annotations) (download) (as text)
Sat Jan 13 19:46:35 2007 UTC (2 years, 10 months ago) by robertDouglass
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +189 -0 lines
File MIME type: text/x-php
#108410 by nterbogt: Port to D5
1 <?php
2 // $Id: opensearchclient.module,v 1.1.2.6 2006/12/12 08:24:56 robertDouglass Exp $
3
4 /**
5 * @file
6 * Allows for searching Opensearch RSS feeds from within Drupal. See http://opensearch.a9.com/ for more information on Opensearch.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function opensearchclient_help($section) {
13 switch ($section) {
14 case 'admin/help#opensearchclient':
15 return '<p>'. t('Allows for searching Opensearch RSS feeds from within Drupal. See <a href="http://opensearch.a9.com/">the Opensearch site</a> for more information on Opensearch.'). '</p>';
16 case 'admin/modules#description':
17 return t('An Opensearch RSS client integrated into a Drupal website.');
18 }
19 }
20
21 function opensearchclient_perm() {
22 return array('configure opensearch feeds');
23 }
24
25
26 function opensearchclient_search($op = 'search', $keys = null) {
27 global $pager_total, $pager_page_array, $pager_total_items, $locale;
28
29 switch ($op) {
30 case 'name':
31 return variable_get('opensearchclient_name', 'OpenSearch');
32
33 case 'search':
34 $from_page = intval($_GET['page']);
35 $keys = search_get_keys();
36
37 if ($url = variable_get('opensearchclient_template', false)) {
38 // TODO: There are more core params to be handled:
39 // http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_URL_template_syntax
40
41 // Define the terms to be replaced and the replacement values. This is done in one array and
42 // split later to keep the code easier to read
43 $mappings = array('{searchTerms}' => urlencode($keys),
44 '{startPage}' => urlencode($from_page+1),
45 '{startIndex}' => urlencode(($from_page) * 10),
46 '{language}' => urlencode($locale),
47 '{inputEncoding}' => 'UTF-8',
48 '{outputEncoding}' => 'UTF-8',
49 // TODO: "10" as the number of items per page is once again hardcoded here.
50 '{count}' => '10');
51
52 // Pull out the terms and their replacement values
53 $terms = array_keys($mappings);
54 $replacements = array_values($mappings);
55
56 // Perform the replacement
57 $url = str_replace($terms, $replacements, $url);
58 }
59 else {
60 drupal_set_message(t('OpenSearch client is not configured correctly. Please specify a URL template for searching.'));
61 }
62
63 $path = drupal_get_path('module', 'opensearchclient').'/magpierss-0.72/rss_fetch.inc';
64 require_once $path;
65
66 $cache_dir = variable_get('opensearch_magpie_cache_dir', file_directory_path(). '/magpiecache');
67 if (file_check_directory($cache_dir)) {
68 define('MAGPIE_CACHE_ON', TRUE);
69 define('MAGPIE_CACHE_DIR', $cache_dir);
70 }
71 else {
72 define('MAGPIE_CACHE_ON', FALSE);
73 }
74 $rss = fetch_rss($url);
75
76 if (count($rss->items) == 0) {
77 return array();
78 }
79
80 $page = isset($_GET['page']) ? $_GET['page'] : FALSE;
81 $pager_page_array = explode(',', $page);
82
83 // TODO: Hardcoded 10 here
84 $pager_total_items[0] = $rss->channel['opensearch']['totalresults'];
85 $pager_total[0] = ceil($pager_total_items[0] / 10);
86 $pager_page_array[0] = max(0, min((int)$pager_page_array[0], ((int)$pager_total[0]) - 1));
87
88 $items = array();
89
90 // Get information on other modules that are interested in the RSS results (like Nutch) by calling the 'opensearchclient_bindings' hook
91 $opensearchclient_bindings = opensearchclient_get_module_bindings();
92
93 foreach ($rss->items as $key => $item) {
94 $items[$key]['link'] = $item['link'];
95 //$items[$key]['type'] = t('opensearch');
96 $items[$key]['title'] = $item['title'];
97
98 // Most of the RSS fields fit nicely with what Drupal expects. 'snippet' is the exception, so we add it by cloning 'description'.
99 $items[$key]['snippet'] = $item['description'];
100
101 // description and summary aren't used by Drupal but we pass them along anyway because the next step is to invoke handlers.
102 // The handlers will be invoked based on the difference between $item and $items[$key], and having description and summary
103 // in $items saves them from being passed on to the handlers.
104 $items[$key]['description'] = $item['description'];
105 $items[$key]['summary'] = $item['summary'];
106
107 // pass along extra information to hook_search_item based on opensearch_bindings, if there are some.
108 if (count($opensearchclient_bindings) > 0) {
109 foreach (array_diff_assoc($item, $items[$key]) as $namespace => $values) {
110 $value = $opensearchclient_bindings[$namespace]($values);
111 if ($value && trim($value) != '') {
112 $items[$key]['extra'][$namespace] = $value;
113 }
114 }
115 }
116 }
117
118 return $items;
119 }
120 }
121
122 /**
123 * Implementation of hook_menu().
124 */
125 function opensearchclient_menu($may_cache) {
126 $items = array ();
127 if ($may_cache) {
128
129 $items[] = array (
130 'path' => 'admin/settings/opensearchclient',
131 'title' => t('OpenSearch client'),
132 'description' => t('Configure settings for interfacing with an OpenSearch compatible search engine.'),
133 'callback' => 'drupal_get_form',
134 'callback arguments' => array('opensearchclient_admin_settings'),
135 'access' => user_access('configure opensearch feeds'),
136 'type' => MENU_NORMAL_ITEM);
137 }
138 return $items;
139 }
140
141
142 function opensearchclient_admin_settings() {
143 $form['template'] = array(
144 '#type' => 'fieldset',
145 '#title' => t('OpenSearch URL template'),
146 '#collapsible' => true,
147 '#collapsed' => false,
148 );
149 $form['template']['opensearchclient_name'] = array(
150 '#type' => 'textfield',
151 '#title' => t('Site name'),
152 '#default_value' =>variable_get('opensearchclient_name', ''),
153 );
154 $form['template']['opensearchclient_template'] = array(
155 '#type' => 'textfield',
156 '#title' => t('Opensearch URL template'),
157 '#default_value' => variable_get('opensearchclient_template', ''),
158 '#description' => t('Available placeholders are <code>{searchTerms}</code>, <code>{startPage}</code>, <code>{startIndex}</code>, <code>{language}</code>, <code>{inputEncoding}</code>, <code>{outputEncoding}</code>, <code>{count}</code>'),
159 // #maxlength defaults to 128, which is too short too often. Setting higher.
160 '#maxlength' => 256,
161 );
162
163 $form['magpie']['opensearch_magpie_cache_dir'] = array(
164 '#type' => 'value',
165 '#value' => variable_get('opensearch_magpie_cache_dir', file_directory_path(). '/magpiecache'),
166 '#after_build' => array('opensearch_check_magpie_cache_dir'),
167 );
168
169 return system_settings_form($form);
170 }
171
172 function opensearch_check_magpie_cache_dir($form_element) {
173 file_check_directory($form_element['#value'], FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
174 return $form_element;
175 }
176
177 function opensearchclient_get_module_bindings() {
178 $result = array();
179 foreach (module_list() as $module) {
180 if (module_hook($module, 'opensearchclient_bindings')) {
181 foreach (module_invoke($module, 'opensearchclient_bindings') as $namespace => $callback) {
182 $result[$namespace] = $callback;
183 }
184 }
185 }
186
187 return $result;
188 }
189

  ViewVC Help
Powered by ViewVC 1.1.2