245 lines
8.4 KiB
PHP
245 lines
8.4 KiB
PHP
<?php
|
|
namespace MediaWiki\Extension\GlModelViewer;
|
|
|
|
use ImageHandler;
|
|
use Html;
|
|
|
|
class GlModelHandler extends ImageHandler {
|
|
private const glmvVersion = '0.1';
|
|
|
|
/**
|
|
* 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'
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* This is used to generate an array element for each metadata value. That array is then used to generate the table of metadata values on the image page.
|
|
*
|
|
* @param array &$array An array containing elements for each type of visibility and each of those elements being an array of metadata items. This function adds a value to that array.
|
|
* @param string $visibility ('visible' or 'collapsed') if this value is hidden by default.
|
|
* @param string $type Type of metadata tag (currently always 'exif')
|
|
* @param string $id The name of the metadata tag (like 'artist' for example). its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
|
|
* @param string $value Thingy goes into a wikitext table; it used to be escaped but that was incompatible with previous practise of customized display with wikitext formatting via messages such as 'exif-model-value'. So the escaping is taken back out, but generally this seems a confusing interface.
|
|
* @param bool | string $param Value to pass to the message for the name of the field as $1. Currently this parameter doesn't seem to ever be used.
|
|
*/
|
|
//public static function addMeta (&$array, $visibility, $type, $id, $value, $param = false ) {
|
|
// $array[$visibility][] = [
|
|
// 'id' => "$type-$id",
|
|
// 'name' => $name,
|
|
// 'value' => $value
|
|
// ];
|
|
//}
|
|
|
|
|
|
/**
|
|
* 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(
|
|
'Type' => $glType
|
|
);
|
|
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-meshes'] = count($glMeta['meshes']);
|
|
$newMeta['glmv-textures'] = count($glMeta['textures']);
|
|
$newMeta['glmv-metadata'] = self::glmvVersion;
|
|
return array(
|
|
'width' => 600,
|
|
'height' => 800,
|
|
'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 requried 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::glmvVersion) {
|
|
return self::METADATA_BAD;
|
|
}
|
|
return self::METADATA_GOOD;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
} |