asset_renderer.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. require_once dirname(__FILE__) . '/http_resource.class.php';
  3. require_once dirname(__FILE__) . '/asset_aggregated_renderer.class.php';
  4. /**
  5. * Renderer for an http resource.
  6. * Extract meta data and snippet html view from an given url/resource.
  7. *
  8. * Base class. Other renderers must inherit from it.
  9. *
  10. * @copyright (c) 2011 University of Geneva
  11. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  12. * @author Laurent Opprecht
  13. */
  14. class AssetRenderer
  15. {
  16. const THUMBNAIL = 'thumbnail';
  17. const EMBED_SNIPPET = 'embed_snippet';
  18. const EMBED_TYPE = 'embed_type';
  19. const EMBED_URL = 'embed_url';
  20. const WIDTH = 'width';
  21. const HEIGHT = 'height';
  22. const LANGUAGE = 'language';
  23. const URL = 'url';
  24. const TAGS = 'tags';
  25. const TITLE = 'title';
  26. const CREATED_TIME = 'created_time';
  27. const DURATION = 'duration';
  28. const DESCRIPTION = 'description';
  29. const ICON = 'icon';
  30. static function get($url, $config = array())
  31. {
  32. if (strpos('url', 'javascript:') !== false)
  33. {
  34. return array();
  35. }
  36. $result = array();
  37. $url = trim($url);
  38. if (empty($url))
  39. {
  40. return array();
  41. }
  42. $asset = new HttpResource($url, $config);
  43. $renderer = new AssetAggregatedRenderer(self::plugins());
  44. $result = $renderer->render($asset);
  45. $result['url'] = $url;
  46. return $result;
  47. }
  48. static function plugins()
  49. {
  50. static $result = array();
  51. if (!empty($result))
  52. {
  53. return $result;
  54. }
  55. /*
  56. * We make sure we load them from most specialized to less specialized.
  57. * The first that provides a value for a field wins.
  58. */
  59. $protocols = array(
  60. 'oembed',
  61. 'og',
  62. 'image',
  63. 'media',
  64. 'rss',
  65. 'google_map',
  66. 'google_document',
  67. 'google_document_viewer',
  68. 'google_widget',
  69. 'mediaserver',
  70. 'scratch',
  71. 'page');
  72. foreach ($protocols as $protocol)
  73. {
  74. $file = "asset_{$protocol}_renderer.class.php";
  75. require_once dirname(__FILE__) . '/protocol/' . $file;
  76. $class = "asset_{$protocol}_renderer";
  77. $class = explode('_', $class);
  78. $class = array_map('ucfirst', $class);
  79. $class = implode($class);
  80. $result[] = new $class();
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Renderer function. Take a http asset as input and return an array containing
  86. * various properties: metadata, html snippet, etc.
  87. *
  88. * @param HttpResource $asset
  89. * @return array
  90. */
  91. public function render($asset)
  92. {
  93. $result = array();
  94. return $result;
  95. }
  96. }