Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.68% covered (danger)
23.68%
9 / 38
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
23.68% covered (danger)
23.68%
9 / 38
0.00% covered (danger)
0.00%
0 / 2
36.45
0.00% covered (danger)
0.00%
0 / 1
 onBeforePageDisplay
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
6.22
 onResourceLoaderRegisterModules
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace PropertySuggester;
4
5use ExtensionRegistry;
6use MediaWiki\Hook\BeforePageDisplayHook;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Output\OutputPage;
9use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook;
10use MediaWiki\ResourceLoader\ResourceLoader;
11use Skin;
12use Wikibase\DataModel\Entity\Item;
13use Wikibase\Repo\WikibaseRepo;
14
15/**
16 * @author BP2013N2
17 * @license GPL-2.0-or-later
18 */
19final class Hooks implements
20    BeforePageDisplayHook,
21    ResourceLoaderRegisterModulesHook
22{
23
24    /**
25     * Handler for the BeforePageDisplay hook, injects special behaviour
26     * for PropertySuggestions in the EntitySuggester (if page is in EntityNamespace)
27     *
28     * @param OutputPage $out
29     * @param Skin $skin
30     */
31    public function onBeforePageDisplay( $out, $skin ): void {
32        if ( $out->getRequest()->getCheck( 'nosuggestions' ) ) {
33            return;
34        }
35
36        $entityNamespaceLookup = WikibaseRepo::getEntityNamespaceLookup();
37        $itemNamespace = $entityNamespaceLookup->getEntityNamespace( Item::ENTITY_TYPE );
38
39        if ( $out->getTitle() === null || $out->getTitle()->getNamespace() !== $itemNamespace ) {
40            return;
41        }
42
43        if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
44            $services = MediaWikiServices::getInstance();
45            if ( $services->getService( 'MobileFrontend.Context' )->shouldDisplayMobileView() ) {
46                // Wikibase currently does not support editing statements on mobile,
47                // so no need for PropertySuggester either.
48                return;
49            }
50        }
51
52        $out->addModules( 'propertySuggester.suggestions' );
53    }
54
55    public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void {
56        $module = [
57            'localBasePath' => dirname( __DIR__ ) . '/modules',
58            'remoteExtPath' => 'PropertySuggester/modules',
59            'packageFiles' => [
60                'hook.js',
61                'PropertySuggester.js',
62                [
63                    'name' => 'config.json',
64                    'config' => [
65                        'PropertySuggesterABTestingState',
66                    ],
67                ],
68                [
69                    'name' => 'schemas.json',
70                    'content' => [],
71                ],
72            ],
73            'dependencies' => [
74                'wikibase.view.ControllerViewFactory',
75            ],
76        ];
77
78        if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
79            $module['dependencies'][] = 'ext.eventLogging';
80            $schemas = ExtensionRegistry::getInstance()->getAttribute( 'EventLoggingSchemas' );
81            $clientSchema = $schemas['PropertySuggesterClientSidePropertyRequest'];
82            $module['packageFiles'][3]['content']['PropertySuggesterClientSidePropertyRequest'] = $clientSchema;
83        }
84
85        $resourceLoader->register( 'propertySuggester.suggestions', $module );
86    }
87
88}