r56586 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56585‎ | r56586 | r56587 >
Date:08:06, 18 September 2009
Author:malvineous
Status:deferred
Tags:
Comment:
[Extension:FlvHandler] Import new extension into SVN
Modified paths:
  • /trunk/extensions/FlvHandler (added) (history)
  • /trunk/extensions/FlvHandler/FlvHandler.i18n.php (added) (history)
  • /trunk/extensions/FlvHandler/FlvHandler.php (added) (history)
  • /trunk/extensions/FlvHandler/FlvImageHandler.php (added) (history)
  • /trunk/extensions/FlvHandler/README (added) (history)

Diff [purge]

Index: trunk/extensions/FlvHandler/FlvHandler.i18n.php
@@ -0,0 +1,18 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for FlvHandler extension.
 5+ *
 6+ * @addtogroup Extensions
 7+*/
 8+
 9+$messages = array();
 10+
 11+/** English
 12+ * @author Adam Nielsen <malvineous@shikadi.net>
 13+ */
 14+$messages['en'] = array(
 15+ 'flvhandler_desc' => 'Allow Flash Video (.flv) files to be used in standard image tags (e.g. <nowiki>[[Image:Movie.flv]]</nowiki>)',
 16+ 'flv-long-desc' => '(Flash video, $1 × $2 pixels, file size: $3)'
 17+);
 18+
 19+?>
\ No newline at end of file
Index: trunk/extensions/FlvHandler/FlvHandler.php
@@ -0,0 +1,140 @@
 2+<?php
 3+
 4+if ( ! defined( 'MEDIAWIKI' ) )
 5+ die();
 6+
 7+/**
 8+ * An image handler which adds support for Flash video (.flv) files.
 9+ *
 10+ * @addtogroup Extensions
 11+ *
 12+ * @link http://www.mediawiki.org/wiki/Extension:FlvHandler Documentation
 13+ *
 14+ * @author Adam Nielsen <malvineous@shikadi.net>
 15+ * @copyright Copyright © 2009 Adam Nielsen
 16+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 17+ */
 18+
 19+// Extension credits that will show up on Special:Version
 20+$wgExtensionCredits['parserhook'][] = array(
 21+ 'name' => 'FLV Image Handler',
 22+ 'version' => 'r3',
 23+ 'author' => 'Adam Nielsen',
 24+ 'url' => 'http://www.mediawiki.org/wiki/Extension:FlvHandler',
 25+ 'description' => 'Allow Flash Video (.flv) files to be used in standard image tags (e.g. <nowiki>[[Image:Movie.flv]]</nowiki>)',
 26+ 'descriptionmsg' => 'flvhandler_desc'
 27+);
 28+
 29+// Register the media handler
 30+$dir = dirname( __FILE__ ) . '/';
 31+$wgExtensionMessagesFiles['FlvHandler'] = $dir . 'FlvHandler.i18n.php';
 32+$wgAutoloadClasses['FlvImageHandler'] = $dir . 'FlvImageHandler.php';
 33+$wgMediaHandlers['video/x-flv'] = 'FlvImageHandler';
 34+
 35+// Commands to extract still frames out of the FLV files
 36+$wgFLVConverters = array(
 37+ 'ffmpeg' => '$path/ffmpeg -vcodec png -i $input -ss 0 -vframes 1 -s $widthx$height -f image2 $output'
 38+);
 39+// Probe command (to get video width and height.) 'regex' is run over the
 40+// command's output to get the dimensions.
 41+$wgFLVProbes = array(
 42+ 'ffmpeg' => array(
 43+ 'cmd' => '$path/ffmpeg -i $input',
 44+ 'regex' => '/Stream.*Video.* (\d+)x(\d+),/' // [1] == width, [2] == height
 45+ )
 46+);
 47+
 48+// Pick one of the above as the converter to use
 49+if (empty($wgFLVConverter)) $wgFLVConverter = 'ffmpeg';
 50+
 51+// If not in the executable PATH, specify
 52+if (empty($wgFLVConverterPath)) $wgFLVConverterPath = '';
 53+
 54+// Minimum size for the flash player (width,height). Used to make sure the
 55+// controls don't get all squashed up on really small .flv movies.
 56+if (empty($wgMinFLVSize)) $wgMinFLVSize = array(250, 250);
 57+
 58+
 59+//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
 60+/*if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
 61+ $wgHooks['ParserFirstCallInit'][] = 'efFLVHandlerParserInit';
 62+} else { // Otherwise do things the old fashioned way
 63+ $wgExtensionFunctions[] = 'efFLVHandlerParserInit';
 64+}*/
 65+
 66+$wgHooks['ImageBeforeProduceHTML'][] = 'efFlvHandlerRender';
 67+/*
 68+function efFLVHandlerParserInit()
 69+{
 70+ global $wgMessageCache;
 71+
 72+ // Default (English) message for Image page itself, if one doesn't already exist.
 73+ $wgMessageCache->addMessages(array('flv-long-desc' => '(Flash video, $1 × $2 pixels, file size: $3)'));
 74+
 75+ return true;
 76+}*/
 77+
 78+// Hook function called just before image code is displayed as HTML. If the
 79+// image is an FLV file, embed a flash player, otherwise ignore it and let
 80+// the default MW code display the image.
 81+function efFlvHandlerRender(&$skin, &$title, &$file, &$frameParams, &$handlerParams, &$time, &$res)
 82+{
 83+ global $wgMinFLVSize, $wgFlashPlayer, $wgScriptPath;
 84+
 85+ // Ignore files that don't exist yet
 86+ if (!$file) return true;
 87+
 88+ // Ignore files/images that aren't Flash video
 89+ if ($file->getMimeType() != 'video/x-flv') return true;
 90+
 91+ // Ignore any .flv files that are thumbnails. The image handler will pick
 92+ // these up and render a still frame.
 93+ if (isset($frameParams['thumbnail'])) return true;
 94+
 95+ // Default address of Flash video playing applet
 96+ if (empty($wgFlashPlayer)) $wgFlashPlayer = $wgScriptPath . '/extensions/FlvHandler/flowplayer/flowplayer-3.0.3.swf';
 97+
 98+ $prefix = $postfix = '';
 99+ if (!empty($frameParams['align'])) {
 100+ switch ($frameParams['align']) {
 101+ case 'center': $className = 'center'; break;
 102+ case 'left': $className = 'floatleft'; break;
 103+ case 'right': $className = 'floatright'; break;
 104+ default: $className = 'floatnone'; break;
 105+ }
 106+ $prefix = '<div class="' . $className . '">';
 107+ $postfix = '</div>';
 108+ }
 109+
 110+ if (!isset($handlerParams['width'])) $handlerParams['width'] = $file->getWidth(0);
 111+ if (!isset($handlerParams['height'])) $handlerParams['height'] = $file->getHeight(0);
 112+
 113+ if ($handlerParams['width'] < $wgMinFLVSize[0]) $handlerParams['width'] = $wgMinFLVSize[0];
 114+ if ($handlerParams['height'] < $wgMinFLVSize[1]) $handlerParams['height'] = $wgMinFLVSize[1];
 115+
 116+ $strURL = $file->getUrl();
 117+
 118+ // Generate a "thumbnail" to display in the video window before the user
 119+ // clicks the play button.
 120+ $thumb = $file->transform($handlerParams);
 121+ $strThumbURL = $thumb->getUrl();
 122+
 123+ $strConfig = 'config={"playlist":[ {"url":"' . $strThumbURL . '", "autoPlay":true}, {"url":"' . $strURL . '","autoPlay":false,"fadeInSpeed":0} ] }';
 124+
 125+ $s = <<<EOF
 126+ <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{$handlerParams['width']}" height="{$handlerParams['height']}">
 127+ <param name="movie" value="{$wgFlashPlayer}" />
 128+ <param name="allowfullscreen" value="true" />
 129+ <param name="flashvars" value='{$strConfig}' />
 130+ <embed type="application/x-shockwave-flash" width="{$handlerParams['width']}" height="{$handlerParams['height']}"
 131+ allowfullscreen="true"
 132+ src="{$wgFlashPlayer}"
 133+ flashvars='{$strConfig}' />
 134+ </object>
 135+EOF;
 136+
 137+ $res = str_replace("\n", ' ', $prefix . $s . $postfix);
 138+ return false;
 139+}
 140+
 141+?>
\ No newline at end of file
Index: trunk/extensions/FlvHandler/README
@@ -0,0 +1,22 @@
 2+== FlvHandler ==
 3+Copyright © 2009 Adam Nielsen <malvineous@shikadi.net>
 4+GNU General Public License 2.0 or later
 5+http://www.gnu.org/copyleft/gpl.html
 6+
 7+Make Flash video (.flv files) behave just like any other image uploaded to the
 8+wiki. Videos can be inserted as [[Image:Movie.flv]], made part of image
 9+galleries, etc.
 10+
 11+See http://www.mediawiki.org/wiki/Extension:FlvHandler for full instructions.
 12+
 13+Briefly:
 14+
 15+ 1. Make sure ffmpeg is available on your web host to generate the thumbnails.
 16+
 17+ 2. Add to LocalSettings.php:
 18+
 19+ $wgFileExtensions[] = 'flv'; // Allow .flv files to be uploaded
 20+ include_once("$IP/extensions/FlvHandler/FlvHandler.php");
 21+
 22+ 3. Download flowplayer v3.0.3 (http://www.flowplayer.org/) and unzip it into
 23+ extensions/FlvHandler/flowplayer/
Index: trunk/extensions/FlvHandler/FlvImageHandler.php
@@ -0,0 +1,132 @@
 2+<?php
 3+
 4+/**
 5+ * An image handler which adds support for Flash video (.flv) files.
 6+ *
 7+ * @author Adam Nielsen <malvineous@shikadi.net>
 8+ * @copyright Copyright © 2009 Adam Nielsen
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+/**
 13+ * @file
 14+ * @ingroup Media
 15+ */
 16+
 17+/**
 18+ * @ingroup Media
 19+ */
 20+class FlvImageHandler extends ImageHandler {
 21+ function isEnabled() {
 22+ global $wgFLVConverters, $wgFLVConverter, $wgFLVProbes;
 23+ wfDebug('probes is ' . print_r($wgFLVProbes, true) . "\n");
 24+ if ((!isset( $wgFLVConverters[$wgFLVConverter])) || (!isset($wgFLVProbes[$wgFLVConverter]))) {
 25+ wfDebug( "\$wgFLVConverter is invalid, disabling FLV preview frames.\n" );
 26+ return false;
 27+ } else {
 28+ return true;
 29+ }
 30+ }
 31+
 32+ function getImageSize($image, $filename) {
 33+ global $wgFLVProbes, $wgFLVConverter, $wgFLVConverterPath;
 34+ if( isset( $wgFLVProbes[$wgFLVConverter]['cmd'] ) ) {
 35+ $cmd = str_replace(
 36+ array( '$path/', '$input' ),
 37+ array( $wgFLVConverterPath ? wfEscapeShellArg( "$wgFLVConverterPath/" ) : "",
 38+ wfEscapeShellArg( $filename ) ),
 39+ $wgFLVProbes[$wgFLVConverter]['cmd'] ) . " 2>&1";
 40+ wfProfileIn( 'rsvg' );
 41+ wfDebug( __METHOD__.": $cmd\n" );
 42+ $out = wfShellExec( $cmd, $retval );
 43+ wfProfileOut( 'rsvg' );
 44+
 45+ if (preg_match($wgFLVProbes[$wgFLVConverter]['regex'], $out, $matches)) {
 46+ return array($matches[1], $matches[2]); // width/height
 47+ } else {
 48+ wfDebug(__METHOD__ . ': Unable to extract video dimensions from ' . $wgFLVConverter . ' output: ' . $out . "\n");
 49+ }
 50+ }
 51+ wfDebug(__METHOD__ . ": No probe function defined, .flv previews unavailable.\n");
 52+ return false;
 53+ }
 54+
 55+ function mustRender( $file ) {
 56+ return true;
 57+ }
 58+
 59+ function normaliseParams( $image, &$params ) {
 60+ if ( !parent::normaliseParams( $image, $params ) ) {
 61+ return false;
 62+ }
 63+
 64+ $params['physicalWidth'] = $params['width'];
 65+ $params['physicalHeight'] = $params['height'];
 66+ return true;
 67+ }
 68+
 69+ function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
 70+ global $wgFLVConverters, $wgFLVConverter, $wgFLVConverterPath;
 71+
 72+ if ( !$this->normaliseParams( $image, $params ) ) {
 73+ return new TransformParameterError( $params );
 74+ }
 75+ $clientWidth = $params['width'];
 76+ $clientHeight = $params['height'];
 77+ $physicalWidth = $params['physicalWidth'];
 78+ $physicalHeight = $params['physicalHeight'];
 79+ $srcPath = $image->getPath();
 80+
 81+ if ( $flags & self::TRANSFORM_LATER ) {
 82+ return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
 83+ }
 84+
 85+ if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
 86+ return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
 87+ wfMsg( 'thumbnail_dest_directory' ) );
 88+ }
 89+
 90+ $err = false;
 91+ if( isset( $wgFLVConverters[$wgFLVConverter] ) ) {
 92+ $cmd = str_replace(
 93+ array( '$path/', '$width', '$height', '$input', '$output' ),
 94+ array( $wgFLVConverterPath ? wfEscapeShellArg( "$wgFLVConverterPath/" ) : "",
 95+ intval( $physicalWidth ),
 96+ intval( $physicalHeight ),
 97+ wfEscapeShellArg( $srcPath ),
 98+ wfEscapeShellArg( $dstPath ) ),
 99+ $wgFLVConverters[$wgFLVConverter] ) . " 2>&1";
 100+ wfProfileIn( 'rsvg' );
 101+ wfDebug( __METHOD__.": $cmd\n" );
 102+ $err = wfShellExec( $cmd, $retval );
 103+ wfProfileOut( 'rsvg' );
 104+ }
 105+
 106+ $removed = $this->removeBadFile( $dstPath, $retval );
 107+ if ( $retval != 0 || $removed ) {
 108+ wfDebugLog( 'thumbnail',
 109+ sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
 110+ wfHostname(), $retval, trim($err), $cmd ) );
 111+ return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
 112+ } else {
 113+ return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
 114+ }
 115+ }
 116+
 117+ /*function getImageSize( $image, $path ) {
 118+ return wfGetFLVsize( $path );
 119+ }*/
 120+
 121+ function getThumbType( $ext, $mime ) {
 122+ return array( 'png', 'image/png' );
 123+ }
 124+
 125+ function getLongDesc( $file ) {
 126+ global $wgLang;
 127+ wfLoadExtensionMessages('FlvHandler');
 128+ return wfMsgExt( 'flv-long-desc', 'parseinline',
 129+ $wgLang->formatNum( $file->getWidth() ),
 130+ $wgLang->formatNum( $file->getHeight() ),
 131+ $wgLang->formatSize( $file->getSize() ) );
 132+ }
 133+}

Status & tagging log