Extensions/Youtube
From Sidvind
- <?php
- # YouTube Videos
- #
- # Tag:
- # <youtube>v</youtube>
- # Ex:
- # from url http://www.youtube.com/watch?v=WZpeeRSk-0A
- # <youtube>WZpeeRSk-0A</youtube>
- $wgExtensionFunctions[] = 'wfYouTube';
- $wgExtensionCredits['parserhook'][] = array(
- 'path' => __FILE__,
- 'name' => 'YouTube',
- 'version' => '1.0',
- 'author' => 'David Sveningsson',
- 'description' => 'Display YouTube video (based on [http://www.mediawiki.org/wiki/Extension:YouTube_(Iubito) Sylvain Macheferts extension].)',
- 'url' => 'https://sidvind.com/wiki/Extensions/Youtube');
- function wfYouTube() {
- global $wgParser;
- $wgParser->setHook('youtube', 'renderYouTube');
- }
- function youtubeError($str){
- return "<div style=\"border: solid red 1px; padding: .5em;\">$str</div>";
- }
- 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 <tt>tiny</tt>, <tt>small</tt>, <tt>medium</tt>, <tt>large</tt>, <tt>huge</tt>, <tt>WxH</tt>)");
- }
- }
- /* 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;
- }
- }