Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LocalApiBackend
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
72
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
 getKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 apiCall
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Flow\Import\LiquidThreadsApi;
4
5use ApiErrorFormatter;
6use ApiMain;
7use ApiMessage;
8use ApiUsageException;
9use Exception;
10use MediaWiki\Request\FauxRequest;
11use MediaWiki\User\User;
12use RequestContext;
13
14class LocalApiBackend extends ApiBackend {
15    /**
16     * @var User|null
17     */
18    protected $user;
19
20    public function __construct( User $user = null ) {
21        parent::__construct();
22        $this->user = $user;
23    }
24
25    public function getKey() {
26        return 'local';
27    }
28
29    public function apiCall( array $params, $retry = 1 ) {
30        try {
31            $context = new RequestContext;
32            $context->setRequest( new FauxRequest( $params ) );
33            if ( $this->user ) {
34                $context->setUser( $this->user );
35            }
36
37            $api = new ApiMain( $context );
38            $api->execute();
39
40            return $api->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
41        } catch ( ApiUsageException $exception ) {
42            // Mimic the behaviour when called remotely
43            $errors = $exception->getStatusValue()->getErrorsByType( 'error' );
44            if ( !$errors ) {
45                $errors = $exception->getStatusValue()->getErrorsByType( 'warning' );
46            }
47            if ( !$errors ) {
48                $errors = [
49                    [
50                        'message' => 'unknownerror-nocode',
51                        'params' => []
52                    ]
53                ];
54            }
55            $msg = ApiMessage::create( $errors[0] );
56
57            return [
58                'error' => [
59                        'code' => $msg->getApiCode(),
60                        'info' => ApiErrorFormatter::stripMarkup(
61                        // @phan-suppress-next-line PhanUndeclaredMethod Phan is mostly right
62                            $msg->inLanguage( 'en' )->useDatabase( 'false' )->text()
63                        ),
64                    ] + $msg->getApiData()
65            ];
66        } catch ( Exception $exception ) {
67            // Mimic behaviour when called remotely
68            return [
69                'error' => [
70                    'code' => 'internal_api_error_' . get_class( $exception ),
71                    'info' => 'Exception Caught: ' . $exception->getMessage(),
72                ],
73            ];
74        }
75    }
76}