Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranscodableChecker
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isTranscodableTitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isTranscodableFile
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
210
1<?php
2
3namespace MediaWiki\TimedMediaHandler;
4
5use File;
6use MediaWiki\Config\Config;
7use MediaWiki\Linker\LinkTarget;
8use RepoGroup;
9
10/**
11 * @file
12 * @ingroup Extensions
13 */
14class TranscodableChecker {
15    /** @var Config */
16    private $config;
17
18    /** @var RepoGroup */
19    private $repoGroup;
20
21    /**
22     * @param Config $config
23     * @param RepoGroup $repoGroup
24     */
25    public function __construct(
26        Config $config,
27        RepoGroup $repoGroup
28    ) {
29        $this->config = $config;
30        $this->repoGroup = $repoGroup;
31    }
32
33    /**
34     * Wraps the isTranscodableFile function
35     * @param LinkTarget $title
36     * @return bool
37     */
38    public function isTranscodableTitle( $title ) {
39        if ( $title->getNamespace() !== NS_FILE ) {
40            return false;
41        }
42        $file = $this->repoGroup->findFile( $title, [ 'ignoreRedirect' => true ] );
43        return $this->isTranscodableFile( $file );
44    }
45
46    /**
47     * Utility function to check if a given file can be "transcoded"
48     * @param File|false $file File object
49     * @return bool
50     */
51    public function isTranscodableFile( $file ) {
52        // don't show the transcode table if transcode is disabled
53        if (
54            !$this->config->get( 'EnableTranscode' ) &&
55            !$this->config->get( 'EnabledAudioTranscodeSet' )
56        ) {
57            return false;
58        }
59        // Can't find file
60        if ( !$file ) {
61            return false;
62        }
63        // We can only transcode local files
64        if ( !$file->isLocal() ) {
65            return false;
66        }
67
68        $handler = $file->getHandler();
69        // Not able to transcode files without handler
70        if ( !$handler ) {
71            return false;
72        }
73        $mediaType = $handler->getMetadataType( $file );
74        // If ogg or webm format and not audio we can "transcode" this file
75        $isAudio = $handler instanceof TimedMediaHandler && $handler->isAudio( $file );
76        if ( ( $mediaType === 'webm' || $mediaType === 'ogg'
77                || $mediaType === 'mp4' || $mediaType === 'mpeg' )
78            && !$isAudio
79        ) {
80            return true;
81        }
82        if ( $isAudio && count( $this->config->get( 'EnabledAudioTranscodeSet' ) ) ) {
83            return true;
84        }
85        return false;
86    }
87}