151 lines
4.4 KiB
PHP
151 lines
4.4 KiB
PHP
<?php
|
|
namespace MediaWiki\Extension\GlModelViewer;
|
|
|
|
use ImageHandler;
|
|
use Html;
|
|
|
|
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) {
|
|
$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'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
} |