Topic on Extension talk:Html5mediator

Update which gives the possibility to upload and use "webm", "mp4" and "og" video files as alternatives

2
Baxi69 (talkcontribs)

I made an update for this extension. I did not touch the original functioning of the extension.


You can now add the 3 additional parameters webm, mp4 and ogv in the <html5media> tag.


When you add minimum one of the 3 parameters, the extension will work in alternatives mode. In this case you must not give the file extension of the videa file.


Example: <html5media mp4 webm>File:SampleVideo</html5media> For this example you have to upload the two files SampleVideo.mp4 and SampleVideo.webm. If someone will access this page with Opera SampleVideo.webm will be shown, with Firefox SampleVideo.mp4 will be shown.


To install the update just replace the file /etensions/Html5mediator/Html5mediator.php with the following source code:


<?php

if ( !defined( 'MEDIAWIKI' ) ) die();

$wgExtensionCredits['html5mediator'][] = array(
	'path' => __FILE__,
	'name' => 'Html5mediator',
	'url' => 'https://www.mediawiki.org/wiki/Extension:Html5mediator',
	'description' => 'A simple way to embed audio and video files in a wiki',
	'author' => 'Seung Park, Update: Frank Baxmann'
 	);

/* Register the registration function */
$wgHooks['ParserFirstCallInit'][] = 'wfHtml5Mediator';

function wfHtml5Mediator($parser)
{
	$parser->setHook('html5media' , 'wfHtml5MediatorParse');
	return true;
}

function wfHtml5MediatorParse($data, $params, $parser, $frame)
{
	global $wgContLang;
	
	// init the VideoFormatList
	$videoFormats = null;

	// escape from XSS vulnerabilities
	foreach ($params as $param => $paramval)
	{
		$params[$param] = htmlspecialchars ($paramval);
		if (isset($params['mp4'])) 
		{
			$videoFormats [] = "mp4";
		}
		if (isset($params['webm'])) 
		{
			$videoFormats [] = "webm";
		}
		if (isset($params['ogv']))
		{
			$videoFormats [] = "ogv";
		}
	}
	$data = htmlspecialchars($data);


	// load international name of File namespace
	$namespaceNames = $wgContLang->getNamespaces();
	$fileNS = strtolower($namespaceNames[NS_FILE]);
	$ns = strtolower(strstr($data,':',true));

	// check to see if a file specified
	if ($ns == 'file' || $ns == $fileNS)
	{
		if (!isset ($videoFormats))
		{
			$image = wfFindFile(substr(strstr($data,':'), 1));
			if ($image)
			{
				$data = $image->getFullURL();
			}
			else
			{
				return 'Html5mediator: error loading file:' . Xml::encodeJsVar(substr($data, 5));
			}
		} else
		{
			foreach ($videoFormats as &$format)
			{
				$image = wfFindFile(substr(strstr($data,':'), 1) . "." . $format);
				if ($image)
				{
					$format = $image->getFullURL();
				} // end if
				else
				{
					return 'Html5mediator: error loading file:' . Xml::encodeJsVar(substr(strstr($data,':'), 1) . "." . $format);
				} // end else
				
			} //end foreach
		} // end else
	}

	// Perform validation on the purported URL
	if ((!isset ($videoFormats)) and (!filter_var($data, FILTER_VALIDATE_URL))) return 'Html5mediator: 10 not a valid URL';
	if (isset($videoFormats))
	{
		foreach ($videoFormats as $formatURL)
		{
			if (!filter_var($formatURL, FILTER_VALIDATE_URL)) return 'Html5mediator: 20 not a valid URL: ' . $formatURL;
		} //end for each
	} // end if

	// Get the file extension -- first check for a 3-char extension (mp3, mp4), then 4-char (webm)
	if (!isset ($videoFormats))
	{
		if (substr($data, -4, 1) == ".") $ext = substr($data, -3);
		else if (substr($data, -5, 1) == ".") $ext = substr($data, -4);
		else if (strtolower(substr($data, 0, 23)) == "http://www.youtube.com/" || strtolower(substr($data, 0, 24)) == "https://www.youtube.com/") $ext = "youtube";
	} //end if

	// Write out the actual HTML
	$code = "<script src=\"http://api.html5media.info/1.1.5/html5media.min.js\"></script>";

	if (!isset ($videoFormats))
	{
		switch ($ext)
		{
			// video file extensions
			case "mp4":
			case "webm":
			case "mov":
			case "ogv":
				$code = $code . "<video src=\"" . $data . "\" controls";
				foreach ($params as $param => $paramval)
				{
					$code = $code . " " . htmlspecialchars($param) . "=\"" . $paramval . "\"";
				}
				$code = $code . "></video>";
				break;

			// audio file extensions
			case "mp3":
			case "ogg":
				$code = $code . "<audio src=\"" . $data . "\" controls preload";
				foreach ($params as $param => $paramval)
				{
					$code = $code . " " . htmlspecialchars($param) . "=\"" . $paramval . "\"";
				}
				$code = $code . "></audio>";
				break;
			
			// youtube
			case "youtube":
				$code = "<iframe";
				foreach ($params as $param => $paramval)
				{
					$code = $code . " " . htmlspecialchars($param) . "=\"" . $paramval . "\"";
				}
				$code = $code . " src=\"//www.youtube.com/embed/" . substr($data, -11) . "?rel=0\" frameborder=\"0\" allowfullscreen></iframe>";
				break;

			// unrecognized file extensions
			default:
				return "Html5mediator: 30 file extension not recognized";
		} // end switch
	} else // create the lines for the videoFormats
	{
		$code = $code . "<video controls";
				foreach ($params as $param => $paramval)
				{
					if (($param <> "webm") and ($param <> "mp4") and ($param <> "ogv"))
					{
						$code = $code . " " . htmlspecialchars($param) . "=\"" . $paramval . "\"";
					} // end if
				}
				$code = $code . ">";
				foreach ($videoFormats as $formatURL)
				{
					$code = $code . "<source src=\"" . $formatURL . "\"></source>";
				}
				$code = $code . "</video>";
	} // end else

	return $code;
}

?>
129.94.63.59 (talkcontribs)

This works for me, but I had to switch the "$data = $image->getFullURL();" lines to "$data = $image->getCanonicalURL();" as I use a protocol-independant $wgServer setting, so there was no http: or https: at the front of the File names resulting in an error 20. Not sure if there's a better way to do it.

Reply to "Update which gives the possibility to upload and use "webm", "mp4" and "og" video files as alternatives"