Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
60.00% covered (warning)
60.00%
6 / 10
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiParseExtender
60.00% covered (warning)
60.00%
6 / 10
50.00% covered (danger)
50.00%
1 / 2
10.14
0.00% covered (danger)
0.00%
0 / 1
 isParseAction
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 onAPIGetAllowedParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MobileFrontend\Api;
4
5use ApiBase;
6use MediaWiki\Api\Hook\APIGetAllowedParamsHook;
7use Wikimedia\ParamValidator\ParamValidator;
8
9/**
10 * Adds the `mobileformat` param to API calls which use ApiParse.
11 *
12 * The param should be used in conjunction with the `useskin` paramater (to
13 * ensure ApiParse is in skin mode), and will cause MobileFrontend's
14 * onOutputPageBeforeHTML to apply mobile-specific page transformations.
15 */
16class ApiParseExtender implements APIGetAllowedParamsHook {
17
18    /**
19     * Check if an API action can have the mobileformat param
20     *
21     * @param string $action
22     * @return bool
23     */
24    public static function isParseAction( string $action ): bool {
25        return $action === 'parse' ||
26            // VE calls parse indirectly
27            $action === 'visualeditoredit' ||
28            // DT calls VE indirectly
29            $action === 'discussiontoolsedit' ||
30            $action === 'discussiontoolspreview';
31    }
32
33    /**
34     * APIGetAllowedParams hook handler
35     * @see https://www.mediawiki.org/wiki/Manual:Hooks/APIGetAllowedParams
36     * @param ApiBase $module
37     * @param array &$params Array of parameters
38     * @param int $flags
39     */
40    public function onAPIGetAllowedParams( $module, &$params, $flags ) {
41        $name = $module->getModuleName();
42        // $name is supposed to always be a string, but in some tests it returns null :/
43        if ( $name && self::isParseAction( $name ) ) {
44            $params['mobileformat'] = [
45                ParamValidator::PARAM_TYPE => 'boolean',
46                ApiBase::PARAM_HELP_MSG => 'apihelp-parse-param-mobileformat',
47            ];
48        }
49    }
50}