From 436ecaf476e5052434d0841aeeb7c0ccf8dd76f1 Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 14 Apr 2010 22:25:37 +0000 Subject: Changed tokenizer for ECML to support attribute quotes. Added core youtube and slideshare keywords. Passing the full keyword and attribute string to views. git-svn-id: http://code.elgg.org/elgg/trunk@5735 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/ecml/ecml_functions.php | 101 +++++++++++++++++---- mod/ecml/start.php | 29 +++--- .../views/default/ecml/keywords/slideshare.php | 34 +++++++ mod/ecml/views/default/ecml/keywords/youtube.php | 43 +++++++++ 4 files changed, 175 insertions(+), 32 deletions(-) create mode 100644 mod/ecml/views/default/ecml/keywords/slideshare.php create mode 100644 mod/ecml/views/default/ecml/keywords/youtube.php diff --git a/mod/ecml/ecml_functions.php b/mod/ecml/ecml_functions.php index dca4e03f9..f0dcafa24 100644 --- a/mod/ecml/ecml_functions.php +++ b/mod/ecml/ecml_functions.php @@ -52,6 +52,8 @@ function ecml_parse_view_match($matches) { // match against custom keywords with optional args $keyword_info = $CONFIG->ecml_keywords[$keyword]; $vars = ecml_keywords_tokenize_params($params_string); + $vars['ecml_keyword'] = $keyword; + $vars['ecml_params_string'] = $params_string; $content = elgg_view($keyword_info['view'], $vars); break; } @@ -65,35 +67,102 @@ function ecml_parse_view_match($matches) { } /** - * Creates an array from a "name=value, name2=value2" string. + * Creates an array from a name=value name2='value2' name3="value3" string. * * @param $string * @return array */ function ecml_keywords_tokenize_params($string) { - $pairs = array_map('trim', explode(',', $string)); - $params = array(); - - foreach ($pairs as $pair) { - list($name, $value) = explode('=', $pair); - $name = trim($name); - $value = trim($value); + if (empty($string)) { + return array(); + } - // normalize BOOL values - if ($value === 'true') { - $value = TRUE; - } elseif ($value === 'false') { - $value = FALSE; + $params = array(); + $pos = 0; + $char = substr($string, $pos, 1); + + // working var for assembling name and values + $operand = $name = ''; + + while ($char !== FALSE) { + switch ($char) { + // handle quoted names/values + case '"': + case "'": + $quote = $char; + + $next_char = substr($string, ++$pos, 1); + while ($next_char != $quote) { + if ($next_char === FALSE) { + // no matching quote. bail. + return array(); + } + $operand .= $next_char; + $next_char = substr($string, ++$pos, 1); + } + break; + + case ECML_ATTR_SEPARATOR: + // normalize true and false + if ($operand == 'true'){ + $operand = TRUE; + } elseif ($operand == 'false') { + $operand = FALSE; + } + $params[$name] = $operand; + $operand = $name = ''; + break; + + case ECML_ATTR_OPERATOR: + // save name, switch to value + $name = $operand; + $operand = ''; + break; + + default: + $operand .= $char; } - // don't check against value since a falsy/empty value is valid. - if ($name) { - $params[$name] = $value; + $char = substr($string, ++$pos, 1); + } + + // need to get the last attr + if ($name && $operand) { + if ($operand == 'true'){ + $operand = TRUE; + } elseif ($operand == 'false') { + $operand = FALSE; } + $params[$name] = $operand; } return $params; + + // this is much faster, but doesn't allow quoting. +// $pairs = array_map('trim', explode(',', $string)); +// $params = array(); +// +// foreach ($pairs as $pair) { +// list($name, $value) = explode('=', $pair); +// +// $name = trim($name); +// $value = trim($value); +// +// // normalize BOOL values +// if ($value === 'true') { +// $value = TRUE; +// } elseif ($value === 'false') { +// $value = FALSE; +// } +// +// // don't check against value since a falsy/empty value is valid. +// if ($name) { +// $params[$name] = $value; +// } +// } +// +// return $params; } /** diff --git a/mod/ecml/start.php b/mod/ecml/start.php index a0fd3c929..6bf1a565d 100644 --- a/mod/ecml/start.php +++ b/mod/ecml/start.php @@ -13,7 +13,6 @@ * Update docs / help * Check for SQL injection problems. * Check entity keyword views against fullview. Force to FALSE? - * */ /** @@ -23,6 +22,9 @@ function ecml_init() { require_once(dirname(__FILE__) . '/ecml_functions.php'); global $CONFIG; + define('ECML_ATTR_SEPARATOR', ' '); + define('ECML_ATTR_OPERATOR', '='); + // help page register_page_handler('ecml', 'ecml_help_page_handler'); @@ -107,7 +109,8 @@ function ecml_parse_view($hook, $entity_type, $return_value, $params) { global $CONFIG; // give me everything that is not a ], possibly followed by a :, and surrounded by [[ ]]s - $keyword_regex = '/\[\[([a-z0-9_]+):?([^\]]+)?\]\]/'; + //$keyword_regex = '/\[\[([a-z0-9_]+):?([^\]]+)?\]\]/'; + $keyword_regex = '/\[\[([a-z0-9_]+)([^\]]+)?\]\]/'; $CONFIG->ecml_current_view = $params['view']; $return_value = preg_replace_callback($keyword_regex, 'ecml_parse_view_match', $return_value); @@ -116,7 +119,7 @@ function ecml_parse_view($hook, $entity_type, $return_value, $params) { /** - * Register some default keywords. + * Register default keywords. * * @param unknown_type $hook * @param unknown_type $entity_type @@ -125,20 +128,14 @@ function ecml_parse_view($hook, $entity_type, $return_value, $params) { * @return unknown_type */ function ecml_keyword_hook($hook, $entity_type, $return_value, $params) { - $return_value['login_box'] = array( - 'view' => 'account/forms/login', - 'description' => elgg_echo('ecml:keywords:login_box') - ); + $keywords = array('youtube', 'slideshare'); - $return_value['user_list'] = array( - 'view' => 'ecml/keywords/user_list', - 'description' => elgg_echo('ecml:keywords:user_list') - ); - - $return_value['site_stats'] = array( - 'view' => 'ecml/keywords/site_stats', - 'description' => elgg_echo('ecml:keywords:site_stats') - ); + foreach ($keywords as $keyword) { + $return_value[$keyword] = array( + 'view' => "ecml/keywords/$keyword", + 'description' => elgg_echo("ecml:keywords:$keyword") + ); + } return $return_value; } diff --git a/mod/ecml/views/default/ecml/keywords/slideshare.php b/mod/ecml/views/default/ecml/keywords/slideshare.php new file mode 100644 index 000000000..1881a1a89 --- /dev/null +++ b/mod/ecml/views/default/ecml/keywords/slideshare.php @@ -0,0 +1,34 @@ + + + + + + + +"; +} \ No newline at end of file diff --git a/mod/ecml/views/default/ecml/keywords/youtube.php b/mod/ecml/views/default/ecml/keywords/youtube.php new file mode 100644 index 000000000..87fb669d2 --- /dev/null +++ b/mod/ecml/views/default/ecml/keywords/youtube.php @@ -0,0 +1,43 @@ + + + + + + + + "; +} \ No newline at end of file -- cgit v1.2.3