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);
}
}
?>
@MarkMaldaba
Copy link
Author

Initial draft: Copy of code at https://www.mediawiki.org/wiki/Extension:MiniMp3
Subsequent drafts: Modifying to use HTML5 audio tag, instead.
Ideal: audio tag, with Flash fallback if unsupported, with simple anchor tag to file download if neither supported.

@MarkMaldaba
Copy link
Author

Updated to provide a fallback for non-HTML5 browsers (link to file).

@MarkMaldaba
Copy link
Author

Updated to provide basic (untested) Flash fallback. Requires SWF file to be added and configured as part of deployment.

@MarkMaldaba
Copy link
Author

Now made into a proper extension, and housed in the Wikimedia git repo.

See extension page at mediawiki.org for full details: https://www.mediawiki.org/wiki/Extension:MP3MediaHandler

@zoglun
Copy link

zoglun commented Nov 7, 2016

Hi MarkMaldaba, I am running a npo wiki farm and find out that your extension solve the mp3 play problem under ios system (flash not available) the best. However, would you mind to improve your extension with the flash fall back from https://www.mediawiki.org/wiki/Extension:FlashMP3 ? This extension using the best flash player for mp3 I have ever used so far.
Would be really appreciated if you could combine this flash fallback with options.

In any case I am sincerely thank you for wrote this extension and publish it.

@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