Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 onBeforePageDisplay
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 onUploadFormInitDescriptor
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
 onUploadForm_getInitialPageText
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20namespace MediaWiki\Extension\ThreeD;
21
22use ExtensionRegistry;
23use MediaWiki\Config\Config;
24use MediaWiki\Hook\BeforePageDisplayHook;
25use MediaWiki\Hook\UploadForm_getInitialPageTextHook;
26use MediaWiki\Hook\UploadFormInitDescriptorHook;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Output\OutputPage;
29use RequestContext;
30use Skin;
31
32// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
33
34class Hooks implements
35    BeforePageDisplayHook,
36    UploadFormInitDescriptorHook,
37    UploadForm_getInitialPageTextHook
38{
39    /**
40     * @param OutputPage $out
41     * @param Skin $skin
42     */
43    public function onBeforePageDisplay( $out, $skin ): void {
44        $title = $out->getTitle();
45        if ( $title->getNamespace() === NS_FILE ) {
46            $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title );
47            if ( $file && $file->getMediaType() === MEDIATYPE_3D ) {
48                // Load JS on file pages for placeholder functionality
49                $out->addModules( [ 'ext.3d' ] );
50                if ( ExtensionRegistry::getInstance()->isLoaded( 'MultimediaViewer' ) ) {
51                    $out->addModules( [ 'mmv.3d.head' ] );
52                }
53            }
54        }
55    }
56
57    /**
58     * @param array &$descriptor
59     */
60    public function onUploadFormInitDescriptor( &$descriptor ) {
61        if ( !array_key_exists( 'License', $descriptor ) ) {
62            return;
63        }
64
65        $patentMsg = PatentFormField::getMessageFromParams( [] );
66        if ( $patentMsg === '' || $patentMsg === '-' ) {
67            return;
68        }
69
70        // no JS should be added on UploadWizard, which only inherits from Special:Upload as
71        // fallback when no JS is available...
72        $context = RequestContext::getMain();
73        $title = $context->getTitle();
74        $addJs = $title && !$title->isSpecial( 'UploadWizard' );
75
76        $patentDescriptor = [
77            'Patent' => [
78                'type' => 'select',
79                'class' => PatentFormField::class,
80                'cssclass' => 'mw-htmlform-field-3D-Patents',
81                'section' => 'description',
82                'id' => 'wpPatent',
83                'label-message' => '3d-patent',
84            ]
85        ];
86
87        // $descriptor is an associative array, but the order of the items matters for where in
88        // the form they will appear; we want it right before 'License'
89        $position = array_search( 'License', array_keys( $descriptor ), true );
90        $descriptor = array_slice( $descriptor, 0, $position, true ) +
91            $patentDescriptor +
92            array_slice( $descriptor, $position, null, true );
93
94        if ( $addJs ) {
95            $config = $context->getConfig();
96            $useAjaxPatentPreview = $config->get( 'AjaxPatentPreview' );
97
98            // scripts & styles added separately to ensure CSS also loads without JS
99            $out = $context->getOutput();
100            $out->addModules( [ 'ext.3d.special.upload' ] );
101            $out->addModuleStyles( [ 'ext.3d.special.upload.styles' ] );
102            $out->addJsConfigVars( [ 'wgAjaxPatentPreview' => $useAjaxPatentPreview ] );
103        }
104    }
105
106    /**
107     * @param string &$pageText
108     * @param array $msg
109     * @param Config $config
110     */
111    public function onUploadForm_getInitialPageText( &$pageText, $msg, $config ) {
112        global $wgRequest;
113        $patent = $wgRequest->getText( 'wpPatent' );
114        if ( $patent === '' ) {
115            // no patent text to be added
116            return;
117        }
118
119        $licenseHeader = '== ' . $msg['license-header'] . " ==\n";
120        $patentText = '{{' . $patent . "}}\n";
121        if ( strpos( $pageText, $licenseHeader ) >= 0 ) {
122            // license header already exists; add it right there
123            $pageText = str_replace( $licenseHeader, $licenseHeader . $patentText, $pageText );
124        } else {
125            // as the license header does not exist; create it & add patent info
126            $pageText .= $licenseHeader . $patentText;
127        }
128    }
129}