Refactor to MediaHandler transform

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2024-10-26 15:47:57 -07:00
parent 39329f5497
commit 7ee162430f
4 changed files with 192 additions and 25 deletions

View File

@@ -7,17 +7,19 @@
"license-name": "MIT",
"type": "media",
"manifest_version": 2,
"AutoloadClasses": {
"GlModelViewer": "includes/GlModelViewer.php"
},
"AutoloadNamespaces": {
"MediaWiki\\Extension\\GlModelViewer\\": "includes/"
},
"MediaHandlers": {
"model/gltf-binary": "MediaWiki\\Extension\\GlModelViewer\\GlModelHandler"
},
"Hooks": {
"MimeMagicInit": "GlModelViewer::onMimeMagicInit",
"MimeMagicImproveFromExtension": "GlModelViewer::onMimeMagicImproveFromExtension",
"BeforePageDisplay": "GlModelViewer::onBeforePageDisplay",
"ImageBeforeProduceHTML": "GlModelViewer::onImageBeforeProduceHTML",
"ImageOpenShowImageInlineBefore": "GlModelViewer::onImageOpenShowImageInlineBefore",
"AlternateEditPreview": "GlModelViewer::onAlternateEditPreview"
},
"MimeMagicInit": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onMimeMagicInit",
"MimeMagicImproveFromExtension": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onMimeMagicImproveFromExtension",
"BeforePageDisplay": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onBeforePageDisplay",
"ImageBeforeProduceHTML": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onImageBeforeProduceHTML",
"AlternateEditPreview": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onAlternateEditPreview"
},
"ResourceFileModulePaths": {
"localBasePath": "modules",
"remoteExtPath": "GlModelViewer/modules"

View File

@@ -0,0 +1,67 @@
<?php
namespace MediaWiki\Extension\GlModelViewer;
use ImageHandler;
class GlModelHandler extends ImageHandler {
/**
* Model cannot be displayed directly in a browser but can be rendered.
*
* @param File $file
* @return bool
*/
public function mustRender( $file ) {
return true;
}
/**
* Model can be rendered.
*
* @param File $file
* @return bool
*/
public function canRender( $file ) {
return true;
}
/**
* Get a MediaTransformOutput object representing the transformed output.
*
* @param File $image
* @param string $dstPath Filesystem destination path
* @param string $dstUrl Destination URL to use in output HTML
* @param array $params Arbitrary set of parameters validated by $this->validateParam()
* @param int $flags A bitfield, may contain self::TRANSFORM_LATER
* @return MediaTransformOutput
*/
public function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
echo '<script>';
self::console_log($image);
self::console_log($dstPath);
self::console_log($dstUrl);
self::console_log($params);
self::console_log($flags);
echo '</script>';
return new GlModelTransformOutput($image, ['type' => 'glb']);
}
/**
* Small helper function to display information on the browser console
*
* Usage:
* echo '<script>';
* self::console_log('logged string');
* echo '</script>';
*
* @param $data information to display
* @param bool $add_script_tags true to put information is inside complete script tag
*/
public static function console_log($data, $add_script_tags = false) {
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
}
echo $command;
}
}

View File

@@ -1,7 +1,10 @@
<?php
use MediaWiki\MediaWikiServices;
namespace MediaWiki\Extension\GlModelViewer;
class GlModelViewer extends ImageHandler {
use MediaWiki\MediaWikiServices;
use Html;
class GlModelHooks {
/**
* MWHook: Add gltf mime types to MimeMagic
*
@@ -9,9 +12,9 @@ class GlModelViewer extends ImageHandler {
*
* @param MimeAnalyzer $mime Instance of MW MimeAnalyzer object
*/
public static function onMimeMagicInit(MimeAnalyzer $mime) {
public static function onMimeMagicInit($mime) {
$mime->addExtraTypes('model/gltf-binary glb gltf');
$mime->addExtraInfo('model/gltf-binary [DRAWING]');
$mime->addExtraInfo('model/gltf-binary [MULTIMEDIA]');
}
/**
@@ -23,7 +26,7 @@ class GlModelViewer extends ImageHandler {
* @param string $ext extention of upload file
* @param string &$mime current assigned mime type
*/
public static function onMimeMagicImproveFromExtension( MimeAnalyzer $mimeAnalyzer, $ext, &$mime ) {
public static function onMimeMagicImproveFromExtension( $mimeAnalyzer, $ext, &$mime ) {
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
$mime = 'model/gltf-binary';
}
@@ -36,7 +39,7 @@ class GlModelViewer extends ImageHandler {
*
* @param OutputPage $out compiled page html and manipulation methods
*/
public static function onBeforePageDisplay(OutputPage $out) {
public static function onBeforePageDisplay($out) {
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
if ($findGltf[0]) {
$mvScriptAttr = array(
@@ -65,7 +68,7 @@ class GlModelViewer extends ImageHandler {
* @param &$widthOption
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onImageBeforeProduceHTML( DummyLinker &$linker, Title &$title, &$file, array &$frameParams, array &$handlerParams, &$time, &$result, Parser $parser, string &$query, &$widthOption ) {
public static function onImageBeforeProduceHTML( &$linker, &$title, &$file, array &$frameParams, array &$handlerParams, &$time, &$result, $parser, string &$query, &$widthOption ) {
if ($file->getMimeType() !== 'model/gltf-binary') {
return true;
}
@@ -84,6 +87,13 @@ class GlModelViewer extends ImageHandler {
*/
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
$file = $imagepage->getFile();
$mh = $file->getHandler();
$mt = $file->getMediaType();
//$mmt = $mh->getHandler();
echo '<script>';
self::console_log($mh);
self::console_log($mt);
echo '</script>';
if ($file->getMimeType() == 'model/gltf-binary') {
$viewer = self::buildViewer($file->getDescriptionText(), $file->getFullUrl(), ['class' => 'view-default']);
$out->addHtml(Html::rawElement('div',['id' => 'file', 'class' => 'fullModelView'],$viewer));
@@ -101,7 +111,7 @@ class GlModelViewer extends ImageHandler {
* @param ?ParserOutput &$parserOutput
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onAlternateEditPreview( EditPage $editor, Content $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
public static function onAlternateEditPreview( $editor, $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile($editor->getTitle());
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
@@ -178,7 +188,9 @@ class GlModelViewer extends ImageHandler {
//Add important additional attributes and render model-viewer with hotspots
$attrModelView = array_merge(['src' => $srcUrl, 'class' => 'mv-model', 'interpolation-decay' => '100'], $attrModelView);
$attrModelView['style'] = 'width: 100%; height: 100%; min-height: 400px;';
$elModel = Html::rawElement('model-viewer', $attrModelView, implode($hotspots));
$hotspotHtml = (isset($hotspots)) ? implode($hotspots) : '';
$elModel = Html::rawElement('model-viewer', $attrModelView, $hotspotHtml);
//Render and return container element with model-viewer
$attrContainer = array(
@@ -233,10 +245,4 @@ class GlModelViewer extends ImageHandler {
}
echo $command;
}
/**
* ? But class won't work without it
*/
function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace MediaWiki\Extension\GlModelViewer;
use MediaTransformOutput;
use Html;
class GlModelTransformOutput extends MediaTransformOutput {
/**
* Main Constructor
*
* @access public
* @param File $file
* @param array $parameters Parameters for constructing HTML.
* @return void
*/
public function __construct($file, $parameters) {
$this->file = $file;
$this->parameters = $parameters;
$this->url = $file->getFullUrl();
}
/**
* Fetch HTML for this transform output
*
* @access public
* @param array $options Associative array of options. Boolean options
* should be indicated with a value of true for true, and false or
* absent for false.
* alt Alternate text or caption
* desc-link Boolean, show a description link
* file-link Boolean, show a file download link
* custom-url-link Custom URL to link to
* custom-title-link Custom Title object to link to
* valign vertical-align property, if the output is an inline element
* img-class Class applied to the "<img>" tag, if there is such a tag
*
* @return string HTML
*/
public function toHtml($options = []) {
return Html::rawElement( 'div',[ 'class' => 'my-model-class'], $this->url . ' is a ' . $this->parameters['type'] . ' 3D model!' );
/*
$parameters = $this->parameters;
$style = [];
$style[] = "max-width: 100%;";
$style[] = "max-height: 100%;";
if (empty($options['no-dimensions'])) {
$parameters['width'] = $this->getWidth();
$parameters['height'] = $this->getHeight();
$style[] = "width: {$this->getWidth()}px;";
$style[] = "height: {$this->getHeight()}px;";
}
if (!empty($options['valign'])) {
$style[] = "vertical-align: {$options['valign']};";
}
if (!empty($options['img-class'])) {
$class = $options['img-class'];
}
if (!isset($parameters['start'])) {
$parameters['start'] = null;
}
if (!isset($parameters['end'])) {
$parameters['end'] = null;
}
$inOut = false;
if ($parameters['start'] !== $parameters['end']) {
if ($parameters['start'] !== false) {
$inOut[] = $parameters['start'];
}
if ($parameters['end'] !== false) {
$inOut[] = $parameters['end'];
}
}
$descLink = Html::element( 'a', [ 'href' => $parameters['descriptionUrl'] ], $parameters['descriptionUrl'] );
return Html::rawElement( 'video', [
'src' => $this->url . ($inOut !== false ? '#t=' . implode(',', $inOut) : ''),
'width' => $this->getWidth(),
'height' => $this->getHeight(),
'class' => $class ?? false,
'style' => $style ? implode( ' ', $style ) : false,
'controls' => true,
], $descLink );
*/
}
}