-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.yql.php
345 lines (277 loc) · 8.09 KB
/
pi.yql.php
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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* YQL Plugin
*
* @package ExpressionEngine
* @subpackage Addons
* @category Plugin
* @author Jesse Bunch
* @link http://paramore.is/
*/
$plugin_info = array(
'pi_name' => 'YQL',
'pi_version' => '1.0',
'pi_author' => 'Jesse Bunch',
'pi_author_url' => 'http://paramore.is/',
'pi_description'=> 'Simple plugin that allows you to query the YQL service from your ExpressionEngine templates.',
'pi_usage' => Yql::usage()
);
class Yql {
/**
* The EE cache group
* @param string
* @author Jesse Bunch
*/
const CACHE_GROUP = 'ee_yql';
/**
* Return data for the constructor
* @author Jesse Bunch
*/
public $return_data;
/**
* Constructor
* @author Jesse Bunch
*/
public function __construct() {
$this->EE =& get_instance();
}
/**
* exp:yql:query
* @param sql The query to execute
* @param param:key Replaces the @variables in your YQL query
* @param cache_timeout Local caching time. Defaults to 0 (no cache)
* @author Jesse Bunch
*/
public function query() {
// Fetch params
$sql = $this->EE->TMPL->fetch_param('sql', FALSE);
$cache_timeout = $this->EE->TMPL->fetch_param('cache_timeout', 0);
$debug = $this->EE->TMPL->fetch_param('debug', 'no');
$params = $this->_fetch_colon_params('param');
$prefix = $this->EE->TMPL->fetch_param('prefix', 'no');
// No SQL, no results
if (empty($sql)) {
return $this->EE->TMPL->no_results();
}
// Construct the cache key
$cache_key = $sql.serialize($params);
$cache_key = md5($cache_key);
// Fetch Cache
if ($cache_timeout > 0) {
$this->EE->load->library('yql_caching_library');
$cached_results = $this->EE->yql_caching_library->read_cache($cache_key, Yql::CACHE_GROUP, $cache_timeout);
// Find cache?
if (FALSE !== $cached_results) {
if ($debug == 'yes') {
var_dump('CACHED RESULTS', $sql, $params, $cached_results);
exit;
}
$cached_results = unserialize($cached_results);
if (empty($cached_results)) {
return $this->EE->TMPL->no_results();
}
return $this->_parse_results(array($cached_results), $this->EE->TMPL->tagdata, ($prefix == 'yes'));
}
}
// Run the query
$this->EE->load->library('yql_library');
$results = $this->EE->yql_library->run_query($sql, $params);
// Set the cache
if ($cache_timeout > 0
&& !empty($results)) {
$this->EE->load->library('yql_caching_library', NULL, 'yql_caching_library');
$cache_value = serialize($results);
$this->EE->yql_caching_library->set_cache($cache_key, $cache_value, Yql::CACHE_GROUP);
}
if ($debug == 'yes') {
var_dump('FETCHED RESULTS', $sql, $params, $results);
exit;
}
if (empty($results)) {
return $this->EE->TMPL->no_results();
}
// Parse template
return $this->_parse_results(array($results), $this->EE->TMPL->tagdata, ($prefix == 'yes'));
}
/**
* Parses the YQL results
* @param $results array Array of results to parse
* @param $tagdata string The template code to parse
* @param $prefix bool Should we prefix all vars?
* @return string
* @author Jesse Bunch
*/
private function _parse_results($results, $tagdata, $prefix) {
// Parse {results path="element.table[2].element2.array[0]"} tags
if (preg_match_all("/{\s*results\s+path=(.*?)}/", $tagdata, $matches)) {
foreach($matches[0] as $index => $match) {
foreach($results as $result) {
$tagdata = str_replace($match,
$this->_traverse_array($result,
trim($matches[1][$index], '\'"')
),
$tagdata
);
}
}
}
// Prefix?
if ($prefix) {
$results = $this->_prefix_array($results);
}
// Make sure all arrays are indexed arrays
$results = $this->_force_array($results);
// Parse away!
return $this->EE->TMPL->parse_variables($tagdata, $results);
}
/**
* Recursively traverses an array, returning
* the value specified by the dot notated path
* @param $array array The array to traverse
* @param #path string The dot notated path to return (ie results.item.forecast.temperature)
* @return mixed
* @author Jesse Bunch
*/
private function _traverse_array(&$array, $path) {
$next_path = $path;
$paths = explode('.', $path);
$next_path = $paths[0];
// Index or Assoc Key?
$key_matches;
$matched_value;
if ($num_matches = preg_match('/([a-zA-Z\-\_]*)\[["]?([0-9a-zA-Z]+)["]?\]/', $next_path, $key_matches)) {
if ($key_matches[1] != "") {
$matched_value = (isset($array[$key_matches[1]][$key_matches[2]]))
? $array[$key_matches[1]][$key_matches[2]]
: FALSE;
} else {
$matched_value = (isset($array[$key_matches[2]]))
? $array[$key_matches[2]]
: FALSE;
}
} else {
$matched_value = (isset($array[$next_path]))
? $array[$next_path]
: FALSE;
}
// Matched value an array?
if (is_array($matched_value)) {
array_shift($paths);
return $this->_traverse_array($matched_value, implode('.', $paths));
}
return $matched_value;
}
/**
* Extracts parameters from the tag param array that are
* considered to be colon parameters. e.g. attribute:param="value"
* @param string $colon_key The "attribute" part
* @return array key/value pairs (param = "value")
* @author Jesse Bunch
*/
private function _fetch_colon_params($colon_key) {
// Get all params
$all_params = $this->EE->TMPL->tagparams;
// Pull out params that start with "custom:"
$colon_params = array();
if (is_array($all_params) && count($all_params)) {
$colon_key_end_index = strlen($colon_key) + 1;
foreach ($all_params as $key => $val) {
if (strncmp($key, $colon_key, $colon_key_end_index-1) == 0) {
$colon_params[substr($key, $colon_key_end_index)] = $val;
}
}
}
return $colon_params;
}
/**
* Prefixes every variable under the first level
* to prevent conflicts in EE's template parser
* @param $array array
* @param $level int
* @author Jesse Bunch
*/
private function _prefix_array($array, $prefix = '', $level = 1) {
$new_array = array();
foreach($array as $key => $value) {
if (empty($prefix)) {
$actual_prefix = $key;
} else {
$actual_prefix = $prefix.':'.$key;
}
if (is_array($value)) {
if (is_int($key)) {
$new_array[$key] = $this->_prefix_array($value, $prefix, $level + 1);;
} else {
$new_array[$actual_prefix] = $this->_prefix_array($value, $actual_prefix, $level + 1);
}
} else {
$new_array[$actual_prefix] = $value;
}
}
return $new_array;
}
/**
* Force Array
*
* Take a totally mixed item and parse it into an array compatible with EE's Template library
*
* Taken from Phil Sturgeon's REST plugin
*
* @access private
* @param mixed
* @return string
*/
private function _force_array($var, $level = 1) {
if (is_object($var)) {
$var = (array) $var;
}
if ($level == 1 && ! isset($var[0])) {
$var = array($var);
}
if (is_array($var)) {
// Make sure everything else is array or single value
foreach($var as $index => &$child) {
$child = self::_force_array($child, $level + 1);
if (is_object($child)) {
$child = (array) $child;
}
// Format dates to unix timestamps
elseif (isset($this->EE->TMPL->date_vars[$index]) and ! is_numeric($child)) {
$child = strtotime($child);
}
// Format for EE syntax looping
if (is_array($child) && ! is_int($index) && ! isset($child[0])) {
$child = array($child);
}
}
}
return $var;
}
/**
* Plugin usage
* @author Jesse Bunch
*/
public static function usage() {
ob_start();
?>
See the README at github for usage instructions.
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
/* End of file pi.yql.php */
/* Location: /system/expressionengine/third_party/yql/pi.yql.php */