246 lines
7.8 KiB
PHP
246 lines
7.8 KiB
PHP
<?php
|
|
namespace MediaWiki\Extension\GlModelViewer;
|
|
|
|
use ImageHandler;
|
|
use BitmapMetadataHandler;
|
|
use Html;
|
|
|
|
class GlModelHandler extends ImageHandler {
|
|
private const GLMV_VERSION = '0.3';
|
|
|
|
/**
|
|
* 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) {
|
|
$params['isFilePageThumb'] = false;
|
|
if (!isset($params['width']) || $params['width'] == 0) {
|
|
$params['width'] = 800;
|
|
}
|
|
if (!isset($params['height']) || $params['height'] == 0) {
|
|
$params['height'] = 600;
|
|
}
|
|
|
|
if ($params['width'] <= 300 || $params['height'] <= 300) {
|
|
$params['isFilePageThumb'] = true;
|
|
}
|
|
|
|
return new GlModelTransformOutput($image, $params);
|
|
}
|
|
|
|
/**
|
|
* Check the incoming media parameters
|
|
*
|
|
* @param string $name
|
|
* @param string $value
|
|
* @return bool
|
|
*/
|
|
public function validateParam( $name, $value ) {
|
|
if (in_array($name, ['width', 'height'])) {
|
|
return $value > 0;
|
|
} else if (in_array($name, ['view', 'hsset'])) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Merge a parameter array into a string appropriate for inclusion in filenames.
|
|
*
|
|
* @param array $params array of parameters that have been through normaliseParams.
|
|
* @return string concatenated, formatted string of parameter values
|
|
*/
|
|
public function makeParamString( $params ) {
|
|
$res = parent::makeParamString( $params );
|
|
if ( $res && isset( $params['hsset'] ) ) {
|
|
$res = "hs{$params['hsset']}-$res";
|
|
}
|
|
if ( $res && isset( $params['view'] ) ) {
|
|
$res = "v{$params['view']}-$res";
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* Parse a param string made with makeParamString back into an array.
|
|
*
|
|
* @param string $str the parameter string without file name
|
|
* @return array|false Array of parameters or false on failure
|
|
*/
|
|
public function parseParamString( $str ) {
|
|
preg_match( '/v([^-]*?)-/', $str, $view );
|
|
preg_match( '/hs([^-]*?)-/', $str, $hsset );
|
|
$otherParams = preg_replace('/(?:(?:hs[^-]*?-)|(?:v[^-]*?-))*(.*)$/', '/$1/', $str);
|
|
$parsedParams = parent::parseParamString( $otherParams );
|
|
$parsedParams['view'] = $view;
|
|
$parsedParams['hsset'] = $hsset;
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* ?
|
|
*
|
|
* @param array $params input parameters
|
|
* @return array same array as input?
|
|
*/
|
|
protected function getScriptParams( $params ) {
|
|
$res = parent::getScriptParams( $params );
|
|
if ( isset( $params['view'] ) ) {
|
|
$res['view'] = $params['view'];
|
|
}
|
|
if ( isset( $params['hsset'] ) ) {
|
|
$res['hsset'] = $params['hsset'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* Get an associative array mapping magic word IDs to parameter names.
|
|
*
|
|
* @return string[]
|
|
*/
|
|
public function getParamMap() {
|
|
return [
|
|
'img_width' => 'width',
|
|
'glmv_view' => 'view',
|
|
'glmv_hsset' => 'hsset'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get image size information and metadata array.
|
|
*
|
|
* @param MediaHandlerState $state An object for
|
|
* saving process-local state. This is normally a
|
|
* File object which will be passed back to other
|
|
* MediaHandler methods like pageCount(), if they
|
|
* are called in the same request. The handler
|
|
* can use this object to save its state.
|
|
* @param string $path The filename
|
|
*
|
|
* @return array|null Null to fall back to
|
|
* getImageSize(), or an array with width, height,
|
|
* bits, and metadata keys. All keys are optional.
|
|
*/
|
|
public function getSizeAndMetadata ($state, $path) {
|
|
$gltf = fopen($path, 'r');
|
|
fseek($gltf,12);
|
|
$glMetaLength = unpack("Iint", fread($gltf, 4));
|
|
fseek($gltf,20);
|
|
$glMeta = json_decode(fread($gltf,$glMetaLength['int']),true);
|
|
fclose($gltf);
|
|
$newMeta = array(
|
|
'glmv-meshes' => count($glMeta['meshes']),
|
|
'glmv-textures' => count($glMeta['textures'])
|
|
);
|
|
foreach($glMeta['asset'] as $key => $value) {
|
|
switch ($key) {
|
|
case 'author':
|
|
$newMeta['Author'] = $value;
|
|
break;
|
|
case 'copyright':
|
|
$newMeta['Copyright'] = $value;
|
|
break;
|
|
case 'generator':
|
|
$newMeta['glmv-generator'] = $value;
|
|
break;
|
|
case 'version':
|
|
$newMeta['glmv-version'] = $value;
|
|
break;
|
|
}
|
|
}
|
|
$newMeta['glmv-metadata'] = self::GLMV_VERSION;
|
|
$metaHandler = new BitmapMetadataHandler;
|
|
$metaHandler->addMetadata($newMeta,'native');
|
|
return array(
|
|
'width' => 800,
|
|
'height' => 600,
|
|
'metadata' => $newMeta
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Check if the metadata is valid for this handler.
|
|
*
|
|
* If it returns MediaHandler::METADATA_BAD (or false), Image will reload the metadata from the file and update the database.
|
|
* MediaHandler::METADATA_GOOD for if the metadata is a-ok,
|
|
* MediaHandler::METADATA_COMPATIBLE if metadata is old but backwards compatible (which may or may not trigger a metadata reload).
|
|
*
|
|
* TODO: This version is for testing/dev only. Better checking will be required in the future
|
|
* @param File $image
|
|
* @return bool|int
|
|
*/
|
|
public function isFileMetadataValid ($image) {
|
|
$meta = $image->getMetadataItems(['glmv-metadata']);
|
|
if (!isset($meta['glmv-metadata']) || $meta['glmv-metadata'] != self::GLMV_VERSION) {
|
|
return self::METADATA_BAD;
|
|
}
|
|
return self::METADATA_GOOD;
|
|
}
|
|
|
|
/**
|
|
* Get an array structure that the UI will format this into a table where the visible
|
|
* fields are always visible, and the collapsed fields are optionally visible.
|
|
*
|
|
* The function should return false if there is no metadata to display.
|
|
*
|
|
* @param File $image
|
|
* @param bool|IContextSource $context Context to use (optional)
|
|
* @return bool|array
|
|
*/
|
|
public function formatMetadata( $image, $context = false ) {
|
|
$glmvMetadata = $image->getMetadataArray();
|
|
|
|
if (!count( $glmvMetadata ) ) {
|
|
return false;
|
|
} else {
|
|
return $this->formatMetadataHelper( $glmvMetadata, $context );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
} |