Extensions/Youtube

  __FILE__,        'name'           => 'YouTube',        'version'        => '1.0',        'author'         => 'David Sveningsson',        'description'    => 'Display YouTube video (based on Sylvain Macheferts extension.)',        'url'            => 'https://sidvind.com/wiki/Extensions/Youtube');

function wfYouTube { global $wgParser; $wgParser->setHook('youtube', 'renderYouTube'); }

function youtubeError($str){ return "$str "; }

function renderYouTube($input, array $args, Parser $parser, PPFrame $frame) { /* default size (small) */ $width = 560; $height = 315;

/* default props */ $frame = isset($args['frame']);

/* parse size argument */ if ( isset($args['size']) ){ $size = strtolower($args['size']); if ( strcmp($size, "tiny") == 0 )       { $width =  320; $height = 180; } else if ( strcmp($size, "small") == 0 ) { $width =  560; $height = 315; } else if ( strcmp($size, "medium") == 0 ) { $width = 640; $height = 360; } else if ( strcmp($size, "large") == 0 ) { $width =  853; $height = 480; } else if ( strcmp($size, "huge") == 0 )  { $width = 1280; $height = 720; } else if ( sscanf($size, "%dx%d", $width, $height) != 2 ){ return youtubeError("invalid size (valid sizes are tiny, small, medium, large, huge, WxH)"); }       }

/* validate the video ID */ if ( preg_match('%[^A-Za-z0-9_\\-]%', $input) ) { return youtubeError('bad video ID'); }

/* video element */ $url = "https://www.youtube-nocookie.com/embed/{$input}?rel=0"; $video = Xml::openElement('iframe', array('width'=>$width, 'height'=>$height, 'src'=>$url, 'frameborder'=>0, "style"=>"float: left", 'allowfullscreen')). Xml::closeElement('iframe');

/* caption */ $caption = ''; if ( isset($args['caption']) ){ $caption = Xml::openElement('div', array('class'=>'thumbcaption')). $parser->recursiveTagParse($args['caption']). Xml::closeElement('div'); }

if ( $frame ){ return Xml::openElement('div', array('class'=>'thumb tright')). Xml::openElement('div', array('class'=>'thumbinner')). $video. $caption. Xml::closeElement('div'). Xml::closeElement('div'); } else { return $video; } }