Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiHooks
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 onApiMain__moduleManager
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 onAPIGetAllowedParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 onAPIAfterExecute
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * DiscussionTools API hooks
4 *
5 * @file
6 * @ingroup Extensions
7 * @license MIT
8 */
9
10namespace MediaWiki\Extension\DiscussionTools\Hooks;
11
12use MediaWiki\Api\ApiBase;
13use MediaWiki\Api\ApiEditPage;
14use MediaWiki\Api\ApiModuleManager;
15use MediaWiki\Api\Hook\ApiMain__moduleManagerHook;
16use MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsThank;
17use MediaWiki\Extension\DiscussionTools\Notifications\EventDispatcher;
18use MediaWiki\Registration\ExtensionRegistry;
19use Wikimedia\ParamValidator\ParamValidator;
20
21class ApiHooks implements
22    ApiMain__moduleManagerHook
23{
24    /**
25     * @param ApiModuleManager $moduleManager
26     * @return bool|void
27     */
28    public function onApiMain__moduleManager( $moduleManager ) {
29        if ( ExtensionRegistry::getInstance()->isLoaded( 'Thanks' ) ) {
30            $moduleManager->addModule(
31                'discussiontoolsthank',
32                'action',
33                [
34                    'class' => ApiDiscussionToolsThank::class,
35                    'services' => [
36                        'PermissionManager',
37                        'ThanksLogStore',
38                        'RevisionLookup',
39                        'UserFactory',
40                    ]
41                ]
42            );
43        }
44    }
45
46    /**
47     * @param ApiBase $module API module
48     * @param array &$params Array of parameter specifications
49     * @param int $flags
50     * @return bool
51     */
52    public function onAPIGetAllowedParams( $module, &$params, $flags ) {
53        if ( $module instanceof ApiEditPage ) {
54            $params['discussiontoolsautosubscribe'] = [
55                ParamValidator::PARAM_TYPE => [
56                    'yes',
57                    'no',
58                    'preferences',
59                ],
60                ParamValidator::PARAM_DEFAULT => 'preferences',
61                ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-discussiontoolsautosubscribe',
62            ];
63        }
64        return true;
65    }
66
67    /**
68     * @param ApiBase $module
69     */
70    public function onAPIAfterExecute( $module ) {
71        if ( $module instanceof ApiEditPage ) {
72            $dtAutoSubscribe = $module->extractRequestParams( [
73                'safeMode' => true,
74            ] )['discussiontoolsautosubscribe'];
75
76            // HACK: Ideally we would pass this through properly in the edit result,
77            // rather than setting a static property.
78            EventDispatcher::setAutosubscribe( $dtAutoSubscribe );
79        }
80    }
81}