Skip to content

Instantly share code, notes, and snippets.

@MarkMaldaba
Last active July 3, 2017 20:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkMaldaba/6d52007ffd301d818a8e to your computer and use it in GitHub Desktop.
Save MarkMaldaba/6d52007ffd301d818a8e to your computer and use it in GitHub Desktop.
MP3 media handler for MediaWiki
<?php
# Stream MP3 using HTML5 <audio> tag
$wgMediaHandlers['audio/mp3'] = 'MP3MediaHandler';
$wgFileExtensions[] = 'mp3';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'MP3MediaHandler',
'description' => 'Provides an in-page mp3 player.',
'author' => "Mark Clements (HappyDog)",
'version' => '1.0',
'url' => 'https://www.kennel7.co.uk/testwiki'
);
class MP3MediaHandler extends MediaHandler {
function validateParam( $name, $value ) { return true; }
function makeParamString( $params ) { return ''; }
function parseParamString( $string ) { return array(); }
function normaliseParams( $file, &$params ) { return true; }
function getImageSize( $file, $path ) { return false; }
# Prevent "no higher resolution" message.
function mustRender( $file ) { return true; }
function getParamMap() { return array(); }
function doTransform ($file, $dstPath, $dstUrl, $params, $flags = 0) {
return new MP3OutputRenderer($file->getFullUrl(), $file->getTitle());
}
}
class MP3OutputRenderer extends MediaTransformOutput {
var $pSourceFileURL;
var $pFileName;
function __construct($SourceFileURL, $FileName){
$this->pSourceFileURL = $SourceFileURL;
$this->pFileName = $FileName;
}
function toHtml($options=array ()) {
$Output = '<audio controls="controls">'
. '<source src="$1" type="audio/mp3" />'
. $this->getFlashPlayerHTMLTemplate('<p><a href="$1">$2</a></p>')
. '</audio>';
$Args = array(
'$1' => $this->pSourceFileURL,
'$2' => $this->pFileName,
);
return $this->expandHtml($Output, $Args);
}
function getFlashPlayerHTMLTemplate($NonFlashFallback) {
global $wgFlashPlayerPath, $wgFlashPlayerURLParam, $wgFlashPlayerParams;
if (isset($wgFlashPlayerPath)) {
// A common default parameter name for the audio file to be loaded is 'url',
// so we default to this. Individual implementations can over-ride via
// LocalSettings.php, if necessary.
if (!isset($wgFlashPlayerURLParam)) {
$wgFlashPlayerURLParam = "url";
}
$ExtraParams = "";
if (is_array($wgFlashPlayerParams)) {
foreach ($wgFlashPlayerParams as $Param => $Value) {
$ExtraParams .= '<param name="' . htmlspecialchars($Param)
. '" value="' . htmlspecialchars($Value) . '">';
}
}
$HTML = '<object data="$10" type="application/x-shockwave-flash">'
. '<param name="movie" value="$10" />'
. '<param name="$11" value="$1" />'
. $ExtraParams
. $NonFlashFallback
. '</object>';
$Args = array(
'$10' => $wgFlashPlayerPath,
'$11' => $wgFlashPlayerURLParam,
);
return $this->expandHtml($HTML, $Args);
}
else {
return $NonFlashFallback;
}
}
function expandHtml($HTML, $Args) {
foreach ($Args as $Key => $Value) {
$Args[$Key] = htmlspecialchars($Value);
}
return str_replace(array_keys($Args), array_values($Args), $HTML);
}
}
?>
@jmfergeau
Copy link

jmfergeau commented Nov 30, 2016

For some odd reason, it doesn't work for me. It still displays the link for the file's page instead of a player. But the extension is active anyway. Though, my wiki isn't in english. Has this script been tested on non-english wikis?

@MarkMaldaba
Copy link
Author

Apologies that I've only just seen these comments. Github didn't alert me to your posts.

@zoglun - You can configure which Flash player to use with this extension. It should work with the one you name, but let me know if it doesn't.

@maxlefou - I am not aware of any reason this should not work on non-English wikis. Please try the latest version and if you still have issues let me know.

In all cases, please do not respond here, as I may not see it. The latest version of the code and the best places to report any issues can both be found at the extension's project page on MediaWiki.org: https://www.mediawiki.org/wiki/Extension:MP3MediaHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment