You are here

public function MediaInternetYouTubeHandler::parse in Media: YouTube 7.3

Same name and namespace in other branches
  1. 7 includes/MediaInternetYouTubeHandler.inc \MediaInternetYouTubeHandler::parse()
  2. 7.2 includes/MediaInternetYouTubeHandler.inc \MediaInternetYouTubeHandler::parse()
4 calls to MediaInternetYouTubeHandler::parse()
MediaInternetYouTubeHandler::claim in includes/MediaInternetYouTubeHandler.inc
MediaInternetYouTubeHandler::getFileObject in includes/MediaInternetYouTubeHandler.inc
MediaInternetYouTubeHandler::getOEmbed in includes/MediaInternetYouTubeHandler.inc
Returns information about the media.
MediaYouTubeTestHandler::getOEmbed in tests/includes/MediaYouTubeTestHandler.inc
Returns information about the media.

File

includes/MediaInternetYouTubeHandler.inc, line 15
Extends the MediaInternetBaseHandler class to handle YouTube videos.

Class

MediaInternetYouTubeHandler
Implementation of MediaInternetBaseHandler.

Code

public function parse($embedCode) {
  $list_patterns = array(
    '@youtube\\.com/playlist[#\\?].*?list=([^"#\\& ]+)@i',
    '@youtube\\.com/view_play_list[#\\?].*?p=([^"#\\& ]+)@i',
  );
  foreach ($list_patterns as $pattern) {
    preg_match($pattern, $embedCode, $matches);
    if (isset($matches[1]) && $this
      ->validId($matches[1], 'l')) {
      return file_stream_wrapper_uri_normalize('youtube://l/' . $matches[1]);
    }
  }

  // https://youtube.com/watch/*
  // https://youtube.com/embed/*
  // https://youtube.com/v/*
  // https://youtube.com/?v=*
  // https://youtu.be/*
  // https://gdata.youtube.com/feeds/api/videos/*
  $patterns = array(
    '@youtube\\.com/watch[#\\?].*?v=([^"#\\& ]+).*&list=([^"#\\& ]+)@i',
    '@youtu\\.be/([^"#\\&\\? ]+)\\?list=([^"#\\& ]+)@i',
    '@youtube\\.com/embed/([^"#\\&\\? ]+)\\?list=([^"#\\& ]+)@i',
    '@youtube\\.com/watch[#\\?].*?v=([^"#\\& ]+)@i',
    '@youtube\\.com/embed/([^"#\\&\\? ]+)@i',
    '@youtube\\.com/v/([^"#\\&\\? ]+)@i',
    '@youtube\\.com/\\?v=([^"#\\& ]+)@i',
    '@youtu\\.be/([^"#\\&\\? ]+)@i',
    '@gdata\\.youtube\\.com/feeds/api/videos/([^"#\\&\\? ]+)@i',
  );
  foreach ($patterns as $pattern) {
    preg_match_all($pattern, $embedCode, $matches);

    // @TODO: Parse is called often. Refactor so that valid ID is checked
    // when a video is added, but not every time the embedCode is parsed.
    if (isset($matches[1][0]) && $this
      ->validId($matches[1][0])) {
      $uri = 'youtube://v/' . $matches[1][0];
      if (isset($matches[2][0]) && $this
        ->validId($matches[2][0], 'l')) {
        $uri .= '/l/' . $matches[2][0];
      }
      return file_stream_wrapper_uri_normalize($uri);
    }
  }
}