-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOS2Synonym.module
461 lines (363 loc) · 13.4 KB
/
OS2Synonym.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/**
* @file
* OS2Synonym handles the management of synoyms. The synonyms rae exported
* and imported in the format of Solr synonyms.txt.
*
*/
/*
Please read the README.txt file in the root directory.
*/
/**
* Implements hook_menu().
*/
function OS2Synonym_menu() {
$items = array();
$items['OS2Synonym'] = array(
'title' => 'OS2Synonym',
'description' => '',
'page callback' => '_OS2Synonym',
'access arguments' => array('edit OS2Synonym'),
'type' => MENU_CALLBACK,
);
$items['OS2Synonym/actions'] = array(
'title' => 'OS2Synonym actions',
'description' => '',
'page callback' => '_OS2Synonym_actions',
'access arguments' => array('edit OS2Synonym'),
'type' => MENU_CALLBACK,
);
$items['OS2Synonym/import'] = array(
'title' => 'OS2Synonym import',
'description' => '',
'page callback' => '_OS2Synonym_import',
'access arguments' => array('edit OS2Synonym'),
'type' => MENU_CALLBACK,
);
$items['OS2Synonym_download'] = array(
'title' => 'OS2Synonym download',
'description' => '',
'page callback' => '_OS2Synonym_download',
'access arguments' => array('edit OS2Synonym'),
'type' => MENU_CALLBACK,
);
$items['OS2Synonym_API'] = array(
'title' => 'OS2Synonym API',
'description' => '',
'page callback' => '_OS2Synonym_API',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['OS2Synonym_send_solr'] = array(
'title' => 'OS2Synonym send to Solr',
'description' => '',
'page callback' => '_OS2Synonym_send_solr',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/config/content/OS2Synonym'] = array(
'title' => 'OS2Synonym',
'description' => 'Configuration for the OS2Synonym module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('OS2Synonym_admin_form'),
'access arguments' => array('administer OS2Synonym'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function OS2Synonym_permission() {
return array(
'administer OS2Synonym' => array(
'title' => t('OS2Synonym administration'),
'description' => 'Allows a user to edit the administrative settings.',
),
'edit OS2Synonym' => array(
'title' => t('OS2Synonym editor'),
'description' => 'Allows a user to edit the synonyms.',
),
);
}
/**
* Implements hook_help().
*/
function OS2Synonym_help($path, $arg) {
switch ($path) {
case 'admin/help#OS2Synonym':
module_load_include('inc', 'OS2Synonym');
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides a system to handle synonyms. Each group of synonyms occupies one line.') . '</p>';
return $output;
}
}
/**
* The administration form.
*/
function OS2Synonym_admin_form($form, &$form_state) {
$form['OS2Synonym_Solr_filename'] = array(
'#type' => 'textfield',
'#title' => t('The name of Solr synonym file'),
'#description' => t('The name of the Solr synonym file.'),
'#size' => 60,
'#maxlength' => 255,
'#required' => TRUE,
'#default_value' => variable_get('OS2Synonym_Solr_filename', "synonyms.txt"),
);
$form['OS2Synonym_Solr_core_reload_url'] = array(
'#type' => 'textfield',
'#title' => t('The Solr core reload URL'),
'#description' => t('Example http://localhost:8983/solr/admin/cores?action=RELOAD&core=collection1'),
'#size' => 60,
'#maxlength' => 255,
'#default_value' => variable_get('OS2Synonym_Solr_core_reload_url', ''),
);
$form['OS2Synonym_import_url'] = array(
'#type' => 'textfield',
'#title' => t('The URL of the shared Solr synonyms text file'),
'#description' => htmlspecialchars(t('The URL of the shared Solr synonyms text file. For example http://<servername>/OS2Synonym_download')),
'#size' => 60,
'#maxlength' => 255,
'#required' => TRUE,
'#default_value' => variable_get('OS2Synonym_import_url', ""),
);
return system_settings_form($form);
}
/**
* The main page of the module.
*/
function _OS2Synonym() {
if ((isset($_FILES['synonyms']['tmp_name'])) && (is_uploaded_file($_FILES['synonyms']['tmp_name']))) {
if ($_FILES['synonyms']['error'] == UPLOAD_ERR_OK) {
$uploaded_file_contents = file_get_contents($_FILES['synonyms']['tmp_name']);
// Get all the synonyms from the database as JSON
$synonyms = variable_get('OS2Synonym_synonyms_json', array());
$uploaded_lines = explode("\n", $uploaded_file_contents);
$imported_lines = array();
foreach($uploaded_lines as $index => $synonym_line) {
if(strpos($synonym_line, "#") === false) {
if(! in_array($synonym_line, $synonyms)) {
$imported_lines[] = $synonym_line;
}
}
}
$html = buildBodyContent($imported_lines);
}
else {
}
}
else {
$html = buildBodyContent(array());
}
// Return the page HTML.
return $html;
}
/**
* The main page of the module.
*/
function _OS2Synonym_actions() {
$action = isset($_POST['action']) ? $_POST['action'] : "";
$result = "";
switch($action)
{
case 'save':
if(isset($_POST['OS2Synonyms_lines_json'])) {
$synonym_lines_json = $_POST['OS2Synonyms_lines_json'];
$synonym_lines = json_decode($synonym_lines_json, true);
variable_set('OS2Synonym_synonyms_json', $synonym_lines);
$result .= "<div class = 'OS2Synonym-messages-ok'>" . t("The synonyms have been saved.") . "</div>";
}
break;
}
print $result;
// Return empty to avoid theming
return;
}
function _OS2Synonym_import() {
// Get file.
$import_url = variable_get('OS2Synonym_import_url');
// Open connection.
$ch = curl_init();
// Set curl to POST the data and return the result.
curl_setopt($ch, CURLOPT_URL, $import_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute post.
$default_synonym_file = curl_exec($ch);
$result = array();
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$result['status'] = 'error';
$result['data'] = "<div class = 'OS2Synonym-messages-error'>" . curl_strerror($errno) . "</div>";
}
else {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code == 200) {
// Add some information
$result['status'] = 'ok';
$result['data'] = $default_synonym_file;
}
else {
$result['status'] = 'error';
$result['data'] = "<div class = 'OS2Synonym-messages-error'>An error occurred. HTTP code = " . $http_code . "</div>";
}
}
print json_encode($result);
// Return empty to avoid theming
return;
}
function _OS2Synonym_download() {
$filename = "synonyms.txt";
$synonym_lines = variable_get('OS2Synonym_synonyms_json', array());
$synonym_text = implode("\n", $synonym_lines);
// Serve file download.
drupal_add_http_header('Pragma', 'public');
drupal_add_http_header('Expires', '0');
drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
drupal_add_http_header('Content-Type', 'text/plain');
drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename . ';');
drupal_add_http_header('Content-Length', strlen($synonym_text));
drupal_send_headers();
// Text from the synonyms.txt of Solr
print "# Contains synonyms to use for your index. For the format used, see\n";
print "# http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.SynonymFilterFactory\n";
print "# (Lines starting with a pound character # are ignored.)\n";
// Add some information
print "# Created by OS2Synonym\n";
print $synonym_text;
drupal_exit();
// Return empty to avoid theming
return;
}
/**
* Handles calls from external systems.
*/
function _OS2Synonym_API() {
$action = isset($_POST['action']) ? $_POST['action'] : "";
switch($action) {
case 'download':
$filename = "synonyms.txt";
$synonym_lines = variable_get('OS2Synonym_synonyms_json');
$synonym_text = implode("\n", $synonym_lines);
// Serve file download.
drupal_add_http_header('Pragma', 'public');
drupal_add_http_header('Expires', '0');
drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
drupal_add_http_header('Content-Type', 'text/plain');
drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename . ';');
drupal_add_http_header('Content-Length', strlen($synonym_text));
drupal_send_headers();
// Text from the synonyms.txt of Solr
print "# Contains synonyms to use for your index. For the format used, see\n";
print "# http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.SynonymFilterFactory\n";
print "# (Lines starting with a pound character # are ignored.)\n";
// Add some information
print "# Created by OS2Synonym\n";
print $synonym_text;
drupal_exit();
break;
default:
print "Unknown action '$action'";
break;
}
// Return empty so no theming.
return;
}
function _OS2Synonym_send_solr() {
// Store synonyms in SOLR synonym file
$synonym_filename = variable_get('OS2Synonym_Solr_filename', FALSE);
if (! $synonym_filename) {
// Report some error to user via Drupal...
return;
}
// Get synonyms from database
$synonym_lines = variable_get('OS2Synonym_synonyms_json', array());
$synonym_text = implode("\n", $synonym_lines);
$synonym_text .= "\n";
// Write synonyms file
if (! file_put_contents($synonym_filename, $synonym_text)) {
//Report some error to user via Drupal...
return;
}
_OS2Synonym_reload_solr_core();
}
/**
* Reload Solr core
*/
function _OS2Synonym_reload_solr_core() {
$reload_url = variable_get('OS2Synonym_Solr_core_reload_url', FALSE);
if (! $reload_url) {
return FALSE;
}
// Call the URL
file_get_contents($reload_url);
return TRUE;
}
function buildBodyContent($imported_lines = array()) {
$html = "";
// Get all the synonyms from the database as JSON
$synonyms = variable_get('OS2Synonym_synonyms_json', array());
$html .= "<div id = 'OS2Synonym-page'>";
// The title and description
$html .= "<div id = 'OS2Synonym-description'>";
$html .= " <h1>OS2Synonym</h1>";
$html .= "</div>";
// Messages from the system
$html .= "<div id = 'OS2Synonym-messages'>";
$html .= "</div>";
// The filter field
$html .= "<div id = 'OS2Synonym-filter'>";
$html .= " <div id = 'OS2Synonym-filter-header'>";
$html .= " " . t("Filter");
$html .= " </div>";
$html .= " <input id = 'OS2Synonym-filter-field' value = ''>";
$html .= "</div>";
// The action buttons
$html .= "<div id = 'OS2Synonym-actions'>";
$html .= "<div id = 'OS2Synonym-internal-actions'>";
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-new' class = 'OS2Synonym-actions-button' value = '" . t("Add new synonym line") . "'>";
if(count($imported_lines) > 0) {
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-save' class = 'OS2Synonym-actions-button' value = '" . t("Save") . "'>";
}
else {
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-save' class = 'OS2Synonym-actions-button-disabled' value = '" . t("Save") . "'>";
}
$html .= "</div>";
$html .= "<div id = 'OS2Synonym-external-actions'>";
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-send-solr' class = 'OS2Synonym-actions-button-right' value = '" . t("Send til SOLR") . "'>";
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-export' class = 'OS2Synonym-actions-button-right' value = '" . t("Export") . "'>";
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-import-url' class = 'OS2Synonym-actions-button-right' value = '" . t("Import from url") . "'>";
$html .= " <input type = 'submit' id = 'OS2Synonym-actions-import-file' class = 'OS2Synonym-actions-button-right' value = '" . t("Import from file") . "'>";
$html .= "</div>";
// /OS2Synonym-actions
$html .= "</div>";
// The actual synonym lines
$html .= "<div id = 'OS2Synonym-lines'>";
$html .= " <div id = 'OS2Synonym-lines-header'>";
$html .= " " . t("Synonyms");
$html .= " </div>";
foreach($synonyms as $index => $synonyms_line) {
$html .= "<div class = 'OS2Synonym-wrapper'>";
$html .= " <div class = 'OS2Synonym-synonyms'>";
$html .= " <input class = 'OS2Synonym-synonyms-line' value = '$synonyms_line'>";
$html .= " <input type = 'submit' class = 'OS2Synonym-synonyms-line-delete' value = '" . t("Delete") . "'>";
$html .= " </div>";
$html .= "</div>";
}
if(count($imported_lines) > 0) {
$html .= "<div class = 'OS2Synonym-import-header'>Imported synonyms</div>";
foreach($imported_lines as $index => $synonyms_line) {
$html .= "<div class = 'OS2Synonym-wrapper'>";
$html .= " <div class = 'OS2Synonym-synonyms'>";
$html .= " <input class = 'OS2Synonym-synonyms-line' value = '$synonyms_line'>";
$html .= " <input type = 'submit' class = 'OS2Synonym-synonyms-line-delete' value = '" . t("Delete") . "'>";
$html .= " </div>";
$html .= "</div>";
}
}
// /OS2Synonym-lines
$html .= "</div>";
// /OS2Synonym-page
$html .= "</div>";
return $html;
}