]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Changed tokenizer for ECML to support attribute quotes.
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 14 Apr 2010 22:25:37 +0000 (22:25 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 14 Apr 2010 22:25:37 +0000 (22:25 +0000)
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
mod/ecml/start.php
mod/ecml/views/default/ecml/keywords/slideshare.php [new file with mode: 0644]
mod/ecml/views/default/ecml/keywords/youtube.php [new file with mode: 0644]

index dca4e03f98777982ab0c10fea1abda8f657f3b28..f0dcafa241dd32fd9f165f46841a1a9325d7ba77 100644 (file)
@@ -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;
 }
 
 /**
index a0fd3c929dc68adcc95ffa65e80e2f49fd8e0689..6bf1a565de72c1bd1d63eabdae5a553783969744 100644 (file)
@@ -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 (file)
index 0000000..1881a1a
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * ECML Slideshare support
+ *
+ * @package ECML
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008-2010
+ * @link http://elgg.org/
+ */
+
+// this wants the "wordpress.com" embed code.
+// to make life easier on users, don't require them to add the "s
+// and just chop out the id= bit here from the full attr list
+
+$id = str_replace('id=', '', $vars['ecml_params_string']);
+$width = (isset($vars['width'])) ? $vars['width'] : 450;
+$height = (isset($vars['height'])) ? $vars['height'] : 369;
+
+if ($id) {
+       // @todo need to check if the & should be encoded.
+
+       $slide_url = "http://static.slideshare.net/swf/ssplayer2.swf?id=$id";
+
+       echo "
+<object type=\"application/x-shockwave-flash\" wmode=\"opaque\" data=\"$slide_url\" width=\"$width\" height=\"$height\">
+       <param name=\"movie\" value=\"$slide_url\" />
+       <param name=\"allowFullScreen\" value=\"true\" />
+       <param name=\"allowScriptAccess\" value=\"always\" />
+
+       <embed src=\"$slide_url\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"$width\" height=\"$height\"></embed>
+</object>
+";
+}
\ 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 (file)
index 0000000..87fb669
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * ECML Youtube support
+ *
+ * @package ECML
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008-2010
+ * @link http://elgg.org/
+ */
+
+$src = (isset($vars['src'])) ? $vars['src'] : FALSE;
+$width = (isset($vars['width'])) ? $vars['width'] : 480;
+$height = (isset($vars['height'])) ? $vars['height'] : 385;
+
+// need to extract the video id.
+// the src arg can take a full url or an id.
+// assume if no youtube.com that it's an id.
+if (strpos($src, 'youtube.com') === FALSE) {
+       $vid = $src;
+} else {
+       // grab the v param
+       if ($parts = parse_url($src)) {
+               if (isset($parts['query'])) {
+                       parse_str($parts['query'], $query_arr);
+                       $vid = (isset($query_arr['v'])) ? $query_arr['v'] : FALSE;
+               }
+       }
+}
+
+if ($vid) {
+       $movie_url = "http://www.youtube.com/v/$vid";
+
+       echo "
+<object width=\"$width\" height=\"$height\">
+       <param name=\"movie\" value=\"$movie_url\"></param>
+       <param name=\"allowFullScreen\" value=\"true\"></param>
+       <param name=\"allowscriptaccess\" value=\"always\"></param>
+
+       <embed src=\"$movie_url\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"$width\" height=\"$height\"></embed>
+</object>
+       ";
+}
\ No newline at end of file