-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyql_views_query_plugin_query_yql.inc
468 lines (399 loc) · 14.8 KB
/
yql_views_query_plugin_query_yql.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
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
462
463
464
465
466
467
468
<?php
class yql_views_query_plugin_query_yql extends views_plugin_query {
/**
* Constructor; Create the basic query object and fill with default values.
*/
function init($base_table = 'yql_ep', $base_field, $options) {
parent::init($base_table, $base_field, $options);
$this->api_url = !empty($this->options['api_url']) ? $this->options['api_url'] : 'http://query.yahooapis.com/v1/public/yql';
$this->api_method = $this->options['api_method'] ? $this->options['api_method'] : 'json';
$this->yql_base_table = $this->options['yql_base_table'];
}
/**
* Construct the "WHERE" or "HAVING" part of the query.
*
* @param $where
* 'where' or 'having'.
*/
function condition_query($where = 'where') {
$clauses = array();
foreach ($this->$where as $group => $info) {
if (empty($info['clauses'])) {
continue;
}
$clause = implode(") " . $info['type'] . " (", $info['clauses']);
if (count($info['clauses']) > 1) {
$clause = '(' . $clause . ')';
}
$clauses[] = $clause;
}
if ($clauses) {
$keyword = drupal_strtoupper($where);
if (count($clauses) > 1) {
return "$keyword (" . implode(")\n " . $this->group_operator . ' (', array_filter($clauses)) . ")\n";
}
else {
return "$keyword " . array_shift($clauses) . "\n";
}
}
return "";
}
function use_pager() {
return FALSE;
}
/**
* Generate a query and a countquery from all of the information supplied
* to the object.
*
* @param $get_count
* Provide a countquery if this is true, otherwise provide a normal query.
*/
function query($get_count = FALSE) {
$query = '';
// Add the where clauses
$where = ' ' . $this->condition_query();
// Add the field clauses
$fields = '';
$fields_array = $this->fields;
foreach ($fields_array as $alias => $field) {
if (!empty($fields)) {
$fields .= ', ';
}
$fields .= $field['field'];
}
// Add the base table
$from = $this->yql_base_table;
// Add sort function
if (isset($this->orderby) && $this->orderby) {
// Only get the first orderby
$orderby_field = current($this->orderby);
$orderby = " | sort(field=\"" . $orderby_field['field'] . "\")";
switch ($orderby_field['order']) {
case 'ASC':
case 'asc':
// Do nothing. This is added only for clarity.
break;
case 'DESC':
case 'desc':
$orderby = $orderby . ' | reverse()';
break;
default:
break;
}
}
// Build the query string
$query = "SELECT $fields FROM $from";
if (!empty($where)) {
$query = "$query $where";
}
// Add table definitions
if (!empty($this->options['yql_custom_tables'])) {
$query = $this->options['yql_custom_tables'] . $query;
}
// Add sort definitions
if (!empty($orderby)) {
$query = $query . $orderby;
}
return $query;
}
/**
* Get the arguments attached to the WHERE and HAVING clauses of this query.
*/
function get_where_args() {
$args = array();
foreach ($this->where as $group => $where) {
$args = array_merge($args, $where['args']);
}
if (isset($this->having) && $this->having) {
foreach ($this->having as $group => $having) {
$args = array_merge($args, $having['args']);
}
}
return $args;
}
function add_param($param, $value = '') {
$this->params[$param] = $value;
}
function add_where($group, $clause) {
$args = func_get_args();
array_shift($args); // ditch $group
array_shift($args); // ditch $clause
// Expand an array of args if it came in.
if (count($args) == 1 && is_array(reset($args))) {
$args = current($args);
}
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
// the default group.
if (empty($group)) {
$group = 0;
}
// Check for a group.
if (!isset($this->where[$group])) {
$this->set_where_group('AND', $group);
}
// Add the clause and the args.
if (is_array($args)) {
$this->where[$group]['clauses'][] = $clause;
// we use array_values() here to prevent array_merge errors as keys from multiple
// sources occasionally collide.
$this->where[$group]['args'] = array_merge($this->where[$group]['args'], array_values($args));
}
}
/**
* Let modules modify the query just prior to finalizing it.
*/
function alter(&$view) {
foreach (module_implements('yql_views_query_query_alter') as $module) {
$function = $module . '_yql_views_query_query_alter';
$function($view, $this);
}
}
/**
* Builds the necessary info to execute the query.
*/
function build(&$view) {
$view->init_pager($view);
// Let the pager modify the query to add limits.
$this->pager->query();
$view->build_info['query'] = $this->query($view);
//$view->build_info['count_query'] = $this->query($view, TRUE);
$view->build_info['count_query'] = '';
$view->build_info['query_args'] = $this->get_where_args();
}
/**
* Alter the query for paging settings. This should only be called
* if the view is using pager.
* @param $query
* The query to be altered
* @param $limit
* The number of items limit
* @param $offset
* The offset from the first item
*/
function _query_alter_limit(&$query, $limit, $offset) {
$base_table = $this->options['yql_base_table'];
// $total_aggregate_items = $offset + $limit;
// @todo if there is a better way, fix this!
$total_aggregate_items = $this->options['num_items'];
$query = str_replace('FROM ' . $base_table, 'FROM ' . $base_table . '(0,' . $total_aggregate_items . ')', $query);
/*
// If sort function is specified, the limit and offset should be put
// before the sort function
if ($sort_found = strpos($query, '|')) {
$query = substr($query, 0, $sort_found -1) . "limit $limit offset $offset " . substr($query, $sort_found);
}
else {
$query .= " limit $limit offset $offset";
}
*/
$current_tail = $offset + $limit;
$query = $query . " | truncate(count=$current_tail) | tail(count=$limit)";
}
/**
* Executes the query and fills the associated view object with according
* values.
*
* Values to set: $view->result, $view->total_rows, $view->execute_time,
* $view->pager['current_page'].
*/
function execute(&$view) {
$query = $view->build_info['query'];
$args = $view->build_info['query_args'];
if ($query) {
// Initialize, alter and set the pager settings before executing
if ($this->pager->use_pager()) {
$this->pager->total_items = $this->options['num_items'];
$view->total_rows = $this->options['num_items'];
if (!empty($this->pager->options['offset'])) {
$this->pager->total_items -= $this->pager->options['offset'];
}
$this->pager->update_page_info();
}
// We can't have an offset without a limit, so provide an upperbound limit instead.
$limit = intval(!empty($this->limit) ? $this->limit : $this->options['num_items']);
$offset = intval(!empty($this->offset) ? $this->offset : 0);
$this->_query_alter_limit($query, $limit, $offset);
// Let the pager modify the query to add limits.
$this->pager->pre_execute($query, $args);
$replacements = module_invoke_all('views_query_substitutions', $view);
$query = str_replace(array_keys($replacements), $replacements, $query);
// Encode the query into URL friendly format
$query = urlencode($query);
$format_string = $this->api_method == 'json' ? '&format=json' : '&format=xml';
$url = $this->api_url . '?q=' . $query . $format_string;
$start = $this->float_microtime();
$results = drupal_http_request($url, array('headers' => array()));
// There is an error in the HTTP request
if (isset($results->error)) {
// If the error comes from YQL query, print the YQL error details.
if (isset($results->data)) {
$yql_error = json_decode($results->data, TRUE);
drupal_set_message("YQL query error: " . $yql_error['error']['description'], 'error');
}
// Otherwise, the error comes from HTTP request error.
else {
drupal_set_message("HTTP request error: " . $results->error, 'error');
}
return;
}
switch ($this->api_method) {
case 'xml':
// Not supported yet.
return;
case 'json':
default:
$results = json_decode($results->data, TRUE);
break;
}
if ($results['query']['results']) {
// If the base object is specified, use that. Otherwise use the first object.
if (!empty($this->options['yql_base_object']) && isset($results['query']['results'][$this->options['yql_base_object']])) {
$result_array = $results['query']['results'][$this->options['yql_base_object']];
}
else {
$result_array = current($results['query']['results']);
}
$view->result = $result_array;
foreach ($view->result as $key => $value) {
$view->result[$key] = (object) $value;
}
// Save the metadata into the object
unset($results['query']['results']);
foreach ($results as $key => $value) {
$this->$key = $value;
}
}
$this->pager->post_execute($view->result);
}
//dpm($url);
$view->execute_time = $this->float_microtime() - $start;
}
function float_microtime() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + (float) $usec;
}
function add_signature(&$view) {
//$view->query->add_field(NULL, "'" . $view->name . ':' . $view->current_display . "'", 'view_name');
}
function option_definition() {
$options = parent::option_definition();
$options['api_url'] = array('default' => 'http://query.yahooapis.com/v1/public/yql');
$options['api_method'] = array('default' => 'json');
$options['yql_base_table'] = array('default' => '');
$options['yql_custom_tables'] = array('default' => '');
$options['yql_base_object'] = array('default' => '');
$options['num_items'] = array('default' => 10);
return $options;
}
function options_form(&$form, &$form_state) {
$form['api_url'] = array(
'#type' => 'textfield',
'#title' => t('Alternate API URL'),
'#default_value' => $this->options['api_url'],
'#description' => t("The URL YQL will be queried from (default: Yahoo! YQL Engine)."),
);
$form['api_method'] = array(
'#type' => 'select',
'#title' => t('API method'),
'#description' => t("The format of the data returned by YQL."),
'#default_value' => 'json',
'#options' => array(
'json' => 'JSON',
),
'#required' => TRUE,
);
$form['yql_base_table'] = array(
'#type' => 'textfield',
'#title' => t('YQL base table'),
'#default_value' => $this->options['yql_base_table'],
'#description' => t("A base table that view will be querying by using YQL to. Example: flickr.photos.recent"),
'#required' => TRUE,
);
$form['num_items'] = array(
'#type' => 'textfield',
'#title' => t('Number of items'),
'#required' => TRUE,
'#description' => t('The number of items to be fetched from the base table. The number must be lesser than the remote limit of the base table, To get the remote limit of a table, see: ') . l('YQL Guide: Paging', 'http://developer.yahoo.com/yql/guide/paging.html') . '. NOTE: This amount of item will be fetched on every page of the view, so it might affect the performance of your site',
'#default_value' => $this->options['num_items'],
);
// @todo: add validation for custom open table.
$form['yql_custom_tables'] = array(
'#type' => 'textarea',
'#title' => t('Custom open tables.'),
'#default_value' => $this->options['yql_custom_tables'],
'#description' => t("Add custom table into the YQL Query.<br/>Example:" . 'USE \'http://www.datatables.org/delicious/delicious.feeds.popular.xml\' AS delicious.feeds.popular;'),
);
$form['yql_base_object'] = array(
'#type' => 'textfield',
'#title' => t('YQL base object'),
'#default_value' => $this->options['yql_base_object'],
'#description' => t('The base object YQL will be querying from. If this is left empty, the first object in the result set will be taken as the base object'),
);
}
/**
* Add a field to the query table, possibly with an alias. This will
* automatically call ensure_table to make sure the required table
* exists, *unless* $table is unset.
*
* @param $table
* The table this field is attached to. If NULL, it is assumed this will
* be a formula; otherwise, ensure_table is used to make sure the
* table exists.
* @param $field
* The name of the field to add. This may be a real field or a formula.
* @param $alias
* The alias to create. If not specified, the alias will be $table_$field
* unless $table is NULL. When adding formulae, it is recommended that an
* alias be used.
* @param $params
* An array of parameters additional to the field that will control items
* such as aggregation functions and DISTINCT.
*
* @return $name
* The name that this field can be referred to as. Usually this is the alias.
*/
function add_field($table, $field, $alias = '', $params = array()) {
// We can't use any alias in YQL query, so just use the real field name.
$alias = $field;
// Create a field info array.
$field_info = array(
'field' => $field,
'table' => $table,
'alias' => $alias,
) + $params;
if (empty($this->fields[$alias])) {
$this->fields[$alias] = $field_info;
}
return $alias;
}
/**
* Add an ORDER BY clause to the query.
*
* @param $table
* The table this field is part of. If a formula, enter NULL.
* @param $field
* The field or formula to sort on. If already a field, enter NULL
* and put in the alias.
* @param $order
* Either ASC or DESC.
* @param $alias
* The alias to add the field as. In SQL, all fields in the order by
* must also be in the SELECT portion. If an $alias isn't specified
* one will be generated for from the $field; however, if the
* $field is a formula, this alias will likely fail.
* @param $params
* Any params that should be passed through to the add_field.
*/
function add_orderby($table, $field, $order, $alias = '', $params = array()) {
// We can't use any alias in YQL query, so just use the real field name.
$as = $alias = $field;
if ($field) {
$as = $this->add_field($table, $field, $as, $params);
}
$orderby = array(
'field' => $as,
'order' => $order,
);
$this->orderby[] = $orderby;
}
}