Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TimedMediaIframeOutput
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
90
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
 onMediaWikiPerformAction
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 outputIframe
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Adds iframe output ( bug 25862 )
4 *
5 * This enables iframe based embeds of the wikimedia player with the following syntax:
6 *
7 * <iframe src="http://commons.wikimedia.org/wiki/File:Folgers.ogv?embedplayer=yes"
8 *     width="240" height="180" frameborder="0" ></iframe>
9 *
10 */
11
12namespace MediaWiki\TimedMediaHandler;
13
14use Article;
15use Exception;
16use MediaWiki\Actions\ActionEntryPoint;
17use MediaWiki\Config\Config;
18use MediaWiki\Hook\MediaWikiPerformActionHook;
19use MediaWiki\Html\Html;
20use MediaWiki\Output\OutputPage;
21use MediaWiki\Request\WebRequest;
22use MediaWiki\Title\Title;
23use MediaWiki\User\User;
24use RepoGroup;
25
26class TimedMediaIframeOutput implements MediaWikiPerformActionHook {
27
28    /** @var Config */
29    private $config;
30
31    /** @var RepoGroup */
32    private $repoGroup;
33
34    public function __construct(
35        Config $config,
36        RepoGroup $repoGroup
37    ) {
38        $this->config = $config;
39        $this->repoGroup = $repoGroup;
40    }
41
42    /**
43     * The iframe hook check file pages embedplayer=yes
44     * @param OutputPage $output
45     * @param Article $article
46     * @param Title $title
47     * @param User $user
48     * @param WebRequest $request
49     * @param ActionEntryPoint $mediaWiki
50     * @return bool
51     * @throws Exception
52     */
53    public function onMediaWikiPerformAction( $output, $article, $title, $user, $request, $mediaWiki ) {
54        if ( !$this->config->get( 'EnableIframeEmbed' ) ) {
55            // continue normal output iframes are "off" (maybe throw a warning in the future)
56            return true;
57        }
58
59        // Make sure we are in the right namespace and iframe=true was called:
60        if ( is_object( $title ) && $title->getNamespace() === NS_FILE &&
61            $request->getVal( 'embedplayer' ) &&
62            $this->outputIframe( $title, $output )
63        ) {
64            // Turn off output of anything other than the iframe
65            $output->disable();
66            return false;
67        }
68
69        return true;
70    }
71
72    /**
73     * Output an iframe
74     * @param Title $title
75     * @param OutputPage $out
76     * @return bool
77     * @throws Exception
78     */
79    private function outputIframe( Title $title, OutputPage $out ): bool {
80        global $wgBreakFrames;
81
82        // Setup the render param
83        $file = $this->repoGroup->findFile( $title );
84        if ( !$file ) {
85            // file was removed, show wiki page with warning
86            return false;
87        }
88        $params = [
89            'inline' => true,
90            'fillwindow' => true,
91            'width' => $file->getWidth()
92        ];
93
94        $videoTransform = $file->transform( $params );
95
96        // Definitely do not want to break frames
97        $wgBreakFrames = false;
98        $out->setPreventClickjacking( false );
99        $out->disallowUserJs();
100
101        $out->addModules( [ 'ext.tmh.player', 'ext.tmh.player.inline' ] );
102        $out->addModuleStyles( [ 'embedPlayerIframeStyle' ] );
103
104        $out->sendCacheControl();
105        $rlClient = $out->getRlClient();
106
107        // Stripped-down version of OutputPage::headElement()
108        // No skin modules are enqueued because we never call $wgOut->output()
109        $pieces = [
110            Html::htmlHeader( $rlClient->getDocumentAttributes() ),
111
112            Html::openElement( 'head' ),
113
114            Html::element( 'meta', [ 'charset' => 'UTF-8' ] ),
115            Html::element( 'title', [], $title->getText() ),
116            $out->getRlClient()->getHeadHtml(),
117            implode( "\n", $out->getHeadLinksArray() ),
118
119            Html::closeElement( 'head' ),
120        ];
121
122        echo implode( "\n", $pieces );
123    ?>
124<body>
125        <div id="videoContainer">
126            <?php echo $videoTransform->toHtml(); ?>
127        </div>
128    <?php echo $out->getBottomScripts(); ?>
129</body>
130</html>
131    <?php
132        return true;
133    }
134
135}