Extension:YouTube (emijrp)

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png

Release status: unknown

Implementation Tag
Description provides tag for embedding YouTube videos
Author(s) (emijrptalk)
License No license specified
Download see below

Translate the YouTube (emijrp) extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

A modification of original Extension:YouTube (Iubito).

Warning: The code below had major security issues prior to being fixed on July 9, 2008.


Code[edit | edit source]

<?php
# YouTube Videos
# 
# Tag :
#   <youtube>video</youtube>
# Ex :
#   from url http://www.youtube.com/watch?v=xYHsaDDSF
#   <youtube>xYHsaDDSF</youtube>
# Size:
# <youtube size="small">xYHsaDDSF</youtube>
# for a small window
# Other sizes: normal, big
# Personal size:
# <youtube width="100" height="100">xYHsaDDSF</youtube>
# for a window of 100x100
# Align:
# <youtube align="right" width="100" height="100">xYHsaDDSF</youtube>
# for a window of 100x100 on the right
# Other aligns: left
# Enjoy !

$wgExtensionFunctions[] = 'wfYouTube';
$wgExtensionCredits['parserhook'][] = array(
		'name' => 'YouTube',
		'description' => 'Display YouTube video',
		'author' => 'Sylvain Machefert & emijrp',
		'url' => 'http://www.mediawiki.org/wiki/Extension:YouTube_%28emijrp%29'
);
 
function wfYouTube() {
		global $wgParser;
		$wgParser->setHook('youtube', 'renderYouTube');
}
 
# The callback function for converting the input text to HTML output
function renderYouTube($input, $argv) {
		//$input = "WZpeeRSk-0A"
 
		if (@$argv['align']!='left' and @$argv['align']!='right')
		{
			$align = 'right';
		}else{
			$align = $argv['align'];
		}
 
		if (@$argv['size'])
		{
			switch($argv['size'])
			{
				case 'small':
					$width = 210;
					$height = 175;
					break;
				case 'medium':
					$width = 330;
					$height = 260;
					break;
				case 'normal':
					$width = 425;
					$height = 350;
					break;
				case 'big':
					$width = 640;
					$height = 525;
					break;
				default:
					$width = 425;
					$height = 350;
					break;
			}
		}else{
			if (@$argv['width'])
				$width = $argv['width'];
			else
				$width = 425;
 
			if (@$argv['height'])
				$height = $argv['height'];
			else
				$height = 350;
		}
 
		//Validate the video ID
		if (preg_match('%[^A-Za-z0-9_\\-]%',$input)) {
				return 'YouTube : bad video ID !';
		}
 
		$url = 'http://www.youtube.com/v/' . $input;
		$output =
			Xml::openElement( 'object',
				array(
					'align' => $align,
					'width' => $width,
					'height' => $height ) ) .
			Xml::openElement( 'param',
				array(
					'name' => 'movie',
					'value' => $url ) ) .
			'</param>' .
			Xml::openElement( 'embed',
				array(
					'src' => $url,
					'type' => 'application/x-shockwave-flash',
					'wmode' => 'transparent',
					'width' => $width,
					'height' => $height ) ) .
			'</embed>' .
			'</object>';
 
		return $output;
}

If you want videos to be shown in a separate line ...[edit | edit source]

... you can modify the above code as follows:

...
                $output =
			'<p style="display:block; height:'.$height.'px">' .
                        Xml::openElement( 'object',
                                array(
                                        'align' => $align,
                                        'width' => $width,
                                        'height' => $height ) ) .
                        Xml::openElement( 'param',
                                array(
                                        'name' => 'movie',
                                        'value' => $url ) ) .
                        '</param>' .
                        Xml::openElement( 'embed',
                                array(
                                        'src' => $url,
                                        'type' => 'application/x-shockwave-flash',
                                        'wmode' => 'transparent',
                                        'width' => $width,
                                        'height' => $height ) ) .
                        '</embed>' .
                        '</object>' .
			'</p>';
...