Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryContentTranslationFavoriteSuggestions
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5/**
6 * Api module for querying user's favorite translation suggestions.
7 *
8 * @author Nik Gkountas
9 * @copyright See AUTHORS.txt
10 * @license GPL-2.0-or-later
11 */
12
13namespace ContentTranslation\ActionApi;
14
15use ContentTranslation\Service\UserService;
16use ContentTranslation\Store\FavoriteSuggestionStore;
17use MediaWiki\Api\ApiBase;
18use MediaWiki\Api\ApiQuery;
19use MediaWiki\Api\ApiQueryBase;
20use Wikimedia\ParamValidator\ParamValidator;
21use Wikimedia\ParamValidator\TypeDef\IntegerDef;
22
23/**
24 * Api module for fetching favorite translation suggestions.
25 */
26class ApiQueryContentTranslationFavoriteSuggestions extends ApiQueryBase {
27    public function __construct(
28        ApiQuery $query,
29        string $moduleName,
30        private readonly UserService $userService,
31        private readonly FavoriteSuggestionStore $favoriteSuggestionStore
32    ) {
33        parent::__construct( $query, $moduleName );
34    }
35
36    public function execute() {
37        $params = $this->extractRequestParams();
38        $result = $this->getResult();
39        $user = $this->getUser();
40
41        if ( !$user->isRegistered() ) {
42            $this->dieWithError( 'apierror-cx-mustbeloggedin-get-suggestions', 'notloggedin' );
43        }
44
45        $translatorUserId = $this->userService->getGlobalUserId( $user );
46
47        // Get personalized suggestions.
48        // We do not want to send personalized suggestions in paginated results
49        // other than the first page. Hence, checking offset.
50
51        $favoriteSuggestions = $this->favoriteSuggestionStore->getFavoriteSuggestions( $translatorUserId );
52        $suggestions = [];
53        foreach ( $favoriteSuggestions as $suggestion ) {
54            $suggestions[] = [
55                'title' => $suggestion->getTitle()->getPrefixedText(),
56                'sourceLanguage' => $suggestion->getSourceLanguage(),
57                'targetLanguage' => $suggestion->getTargetLanguage(),
58            ];
59        }
60
61        $result->addValue( [ 'query', $this->getModuleName() ], 'suggestions', $suggestions );
62    }
63
64    /** @inheritDoc */
65    public function getAllowedParams() {
66        return [
67            // 'limit' and 'offset' are not really used by any API client, as users are never expected to have
68            // more than 100 favorite suggestions at the same time, but we still keep them here, in case we
69            // need to support 'continue' functionality in the future
70            'limit' => [
71                ParamValidator::PARAM_DEFAULT => 100,
72                ParamValidator::PARAM_TYPE => 'limit',
73                IntegerDef::PARAM_MIN => 1,
74                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
75                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
76            ],
77            'offset' => [
78                ParamValidator::PARAM_TYPE => 'string',
79            ]
80        ];
81    }
82
83    /** @inheritDoc */
84    protected function getExamplesMessages() {
85        return [
86            'action=query&list=contenttranslationfavoritesuggestions' =>
87                'apihelp-query+contenttranslationfavoritesuggestions-example-1',
88        ];
89    }
90}