Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.55% covered (success)
93.55%
29 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GLTFTransformOutput
93.55% covered (success)
93.55%
29 / 31
50.00% covered (danger)
50.00%
1 / 2
14.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 toHtml
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
0.00%
0 / 1
13.08
1<?php
2
3namespace MediaWiki\Extension\GLTFHandler;
4
5use MediaWiki\FileRepo\File\File;
6use MediaWiki\Html\Html;
7use MediaWiki\MediaWikiServices;
8use function is_string;
9
10class GLTFTransformOutput extends \MediaTransformOutput {
11
12    private array $pParams;
13
14    /**
15     * @param File $File
16     * @param float $Width
17     * @param float $Height
18     * @param array $Params
19     */
20    public function __construct( $File, $Width, $Height, $Params ) {
21        $this->file = $File;
22        $this->width = (int)round( $Width );
23        $this->height = (int)round( $Height );
24        $this->pParams = $Params;
25        // Make SearchResultThumbnailProvider::buildSearchResultThumbnailFromFile() return null.
26        $this->url = "";
27    }
28
29    /** @inheritDoc */
30    public function toHtml( $options = [] ) {
31        $attributes = [];
32        foreach ( Constants::PARAMS as $value ) {
33            $user_value = $this->pParams[$value["name"]] ?? $value["default"];
34            if ( $value["type"] === "bool" ) {
35                $attributes[$value["name"]] = array_key_exists( $value["name"], $this->pParams ) || $user_value;
36            } elseif ( $value["type"] === "file" ) {
37                $attribute = $value["attributes"]["mv_param"] ?? null;
38                if ( is_string( $user_value ) && $attribute !== null ) {
39                    $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $user_value );
40                    if ( $file !== false && $file->isLocal() && $file->canRender() ) {
41                        $attributes[$attribute] = $file->getUrl();
42                    }
43                }
44            } else {
45                $attributes[$value["name"]] = $user_value;
46            }
47        }
48
49        $attributes["src"] = $this->file->getFullUrl();
50
51        if ( $this->height > 0 && $this->width > 0 ) {
52            if ( isset( $this->pParams["width"] ) ) {
53                $width = (float)$this->pParams["width"];
54                $height = $width * ( $this->height / $this->width );
55                $attributes["style"] = "width: {$width}px; height: {$height}px;";
56            } else {
57                $width = $this->width;
58                $height = $this->height;
59            }
60        } else {
61            $width = $height = null;
62        }
63
64        // attributes for dynamic resizing
65        $attributes["class"] = "model-viewer-dynsize";
66        $attributes["data-width"] = $width;
67        $attributes["data-height"] = $height;
68
69        $output = Html::element( "model-viewer", $attributes );
70        return $this->linkWrap( [ "class" => "mw-file-description" ], $output );
71    }
72}