-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-simple.inc
264 lines (234 loc) · 7.69 KB
/
run-simple.inc
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
<?php
function _get_datasets() {
$query = get_input_query();
$query->addExpression('IF(enabled = 1, name, CONCAT(\'[admin] \', name))', 'name');
$query->orderBy('id', 'DESC');
return $query->execute();
}
function boa_query_form($form, &$form_state) {
global $base_path;
$form['words'] = array(
'#type' => 'fieldset',
'#title' => 'Find articles',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'allwords' => array(
'#type' => 'textfield',
'#title' => t('with <b>all</b> of the words'),
),
'phrase' => array(
'#type' => 'textfield',
'#title' => t('with the <b>exact phrase</b>'),
),
'without' => array(
'#type' => 'textfield',
'#title' => t('<b>without</b> the words'),
),
);
$form['loc'] = array(
'#type' => 'select',
'#title' => t('Where my words occur'),
'#options' => array(
'1' => t('anywhere in the article'),
'2' => t('in the title of the article'),
'3' => t('in the abstract of the article'),
'4' => t('in the body of the article'),
'5' => t('in the findings of the article'),
),
);
$years = array();
for ($i = 1956; $i <= date("Y"); $i++)
$years[$i] = (string)$i;
unset($years[1957]);
unset($years[1958]);
unset($years[1959]);
unset($years[1961]);
unset($years[1963]);
unset($years[1965]);
unset($years[1967]);
$form['range'] = array(
'#type' => 'fieldset',
'#title' => t('Return articles <b>dated</b> between years'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'start' => array(
'#type' => 'select',
'#options' => $years,
'#default_value' => '1956',
),
'end' => array(
'#type' => 'select',
'#options' => $years,
'#default_value' => (string)date("Y"),
),
);
/*
$form['input'] = array(
'#type' => 'select',
'#title' => t('Input Dataset'),
'#default_value' => isset($form['input']['#default_value']) ? $form['input']['#default_value'] : 0
);
$result = _get_datasets();
foreach ($result as $record)
$form['input']['#options'][$record->id] = $record->name;
*/
$form['run'] = array(
'#type' => 'submit',
'#value' => t('Query'),
);
$form['note'] = array(
'#markup' => t('<span style="font-size: 0.8em;">NOTE: All data submitted to this site is subject to our <a href="/policies/privacy.php" target="_blank">privacy policy</a>.</span>'),
);
if (is_boa_admin())
$form['debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug Query'),
);
return $form;
}
function boa_query_form_submit($form, &$form_state) {
global $user;
//$input = $form_state['values']['input'];
$input = 2;
if (!is_valid_input($input))
invalid_input();
if (empty($form_state['values']['allwords'])
&& empty($form_state['values']['phrase'])
&& empty($form_state['values']['without'])) {
drupal_set_message(t('Query is empty!'), 'error');
drupal_goto('boa/run');
drupal_exit();
}
$source = "o: output collection[string] of string;\n\n";
$source .= "filtered := !def(input.metadata) || !def(input.metadata.doi_url) || !def(input.metadata.publish_time);\n\n";
$loc = isset($form_state['values']['loc']) ? (int)$form_state['values']['loc'] : -1;
switch ($loc) {
default:
$searchFunc = "";
break;
case 1:
$searchFunc = "search_paper";
break;
case 2:
$searchFunc = "search_title";
break;
case 3:
$searchFunc = "search_abstract";
break;
case 4:
$searchFunc = "search_body";
break;
case 5:
$searchFunc = "has_finding";
break;
}
if (!empty($form_state['values']['allwords'])) {
$source .= "if (!filtered) {\n";
$words = preg_split('/\s+/', trim($form_state['values']['allwords']));
asort($words);
foreach ($words as $k)
$source .= "\tif (!filtered && !$searchFunc(\"$k\", input))\n\t\tfiltered = true;\n";
$source .= "}\n\n";
}
if (!empty($form_state['values']['phrase'])) {
$source .= "if (!filtered) {\n";
$source .= "\tif (!filtered && !$searchFunc(\"" . trim($form_state['values']['phrase']) . "\", input))\n\t\tfiltered = true;\n";
$source .= "}\n\n";
}
if (!empty($form_state['values']['without'])) {
$source .= "if (!filtered) {\n";
$words = preg_split('/\s+/', trim($form_state['values']['without']));
asort($words);
foreach ($words as $k)
$source .= "\tif (!filtered && $searchFunc(\"$k\", input))\n\t\tfiltered = true;\n";
$source .= "}\n\n";
}
if (!empty($form_state['values']['start'])) {
$source .= "if (!filtered) {\n";
$source .= "\tif (yearof(input.metadata.publish_time) < " . $form_state['values']['start'] . ")\n\t\tfiltered = true;\n";
$source .= "}\n\n";
}
if (!empty($form_state['values']['end'])) {
$source .= "if (!filtered) {\n";
$source .= "\tif (yearof(input.metadata.publish_time) > " . $form_state['values']['end'] . ")\n\t\tfiltered = true;\n";
$source .= "}\n\n";
}
if (!empty($form_state['values']['allwords']) || !empty($form_state['values']['phrase'])) {
$source .= "if (!filtered) {\n";
$source .= "\tcontext := \"\";\n\n";
$source .= "\tvisit(input, visitor {\n";
$source .= "\t\tbefore p: Paragraph -> {\n";
$source .= "\t\t\tlc := lowercase(p.text);\n\n";
$source .= "\t\t\tif (";
if (!empty($form_state['values']['allwords'])) {
$words = preg_split('/\s+/', strtolower(trim($form_state['values']['allwords'])));
asort($words);
$first = true;
foreach ($words as $k) {
if (!$first)
$source .= "\n\t\t\t\t\t|| ";
$source .= "strfind(\"$k\", lc) != -1";
$first = false;
}
if (!empty($form_state['values']['phrase']))
$source .= "\n\t\t\t\t\t|| ";
}
if (!empty($form_state['values']['phrase']))
$source .= "strfind(\"" . strtolower($form_state['values']['phrase']) . "\", lc) != -1";
$source .= ")\n";
$source .= "\t\t\t\tcontext = context + \"[div class=\\\"para\\\"]\" + p.text + \"[/div]\";\n";
$source .= "\t\t}\n";
$source .= "\t});\n\n";
$source .= "\tif (len(context) > 0) {\n";
if (!empty($form_state['values']['allwords'])) {
$words = preg_split('/\s+/', strtolower(trim($form_state['values']['allwords'])));
asort($words);
$source .= "\t\tcontext = hilite_phrases(context";
foreach ($words as $k)
$source .= ", \"$k\"";
$source .= ");\n";
}
if (!empty($form_state['values']['phrase']))
$source .= "\t\tcontext = hilite_phrases(context, \"" . strtolower($form_state['values']['phrase']) . "\");\n";
$source .= "\t\tcontext = \" [div class=\\\"context\\\"]\" + context + \"[/div]\";\n";
$source .= "\t}\n\n";
$source .= "\to[format(\"%03d\", yearof(now()) - yearof(input.metadata.publish_time))] << pretty_print(input) + context;\n";
$source .= "}";
} else {
$source .= "if (!filtered) o[format(\"%03d\", yearof(now()) - yearof(input.metadata.publish_time))] << pretty_print(input) + context;";
}
if (isset($form_state['values']['debug']) && (bool)$form_state['values']['debug'] && is_boa_admin()) {
print("<pre>");
print($source);
print("</pre>");
drupal_exit();
}
$running = job_run_count($user->uid);
$query = db_insert('boa_jobs');
$tz = date_default_timezone_get();
date_default_timezone_set('America/Chicago');
$query->fields(array(
'submitted' => date('Y-m-d G:i:s'),
'uid' => $user->uid,
'parent_id' => NULL,
'source' => $source,
'input' => $input,
));
date_default_timezone_set($tz);
$id = $query->execute();
$query = db_insert('boa_output');
$query->fields(array(
'id' => $id,
'length' => 0,
'web_result' => ''
));
$query->execute();
drupal_set_message(t('Your job was submitted. Please note it can take several minutes to get results, depending on server load.'));
if ($running > 0)
drupal_set_message(t('You have other job(s) running. This job will queue until those jobs finish or are stopped.'));
if (job_run_count() > 3)
drupal_set_message(t('Server load is currently high. It will take some time for this job to finish.'));
drupal_goto("boa/result/$id");
drupal_exit();
}
?>