Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiContentTranslationSuggestionList
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 7
272
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
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
72
 createList
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @copyright See AUTHORS.txt
4 * @license GPL-2.0-or-later
5 */
6
7namespace ContentTranslation\ActionApi;
8
9use ApiBase;
10use ApiMain;
11use ContentTranslation\Service\UserService;
12use ContentTranslation\Suggestion;
13use ContentTranslation\SuggestionList;
14use ContentTranslation\SuggestionListManager;
15use MediaWiki\Title\Title;
16use Wikimedia\ParamValidator\ParamValidator;
17
18class ApiContentTranslationSuggestionList extends ApiBase {
19
20    private UserService $userService;
21
22    public function __construct( ApiMain $mainModule, $action, UserService $userService ) {
23        parent::__construct( $mainModule, $action );
24        $this->userService = $userService;
25    }
26
27    public function execute() {
28        $params = $this->extractRequestParams();
29        $user = $this->getUser();
30
31        if ( !$this->getUser()->isRegistered() ) {
32            $this->dieWithError( 'apierror-cx-mustbeloggedin-suggestions', 'notloggedin' );
33        }
34
35        $translatorUserId = $this->userService->getGlobalUserId( $user );
36        $listName = $params['listname'];
37        $manager = new SuggestionListManager();
38
39        $list = $manager->getListByName( $listName, $translatorUserId );
40
41        if ( $list === null ) {
42            $listId = $this->createList( $translatorUserId, $listName );
43        } else {
44            $listId = $list->getId();
45        }
46
47        $suggestions = [];
48        foreach ( $params['titles'] as $page ) {
49            if ( !Title::newFromText( $page ) ) {
50                $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $page ) ] );
51            }
52            $suggestions[] = new Suggestion( [
53                'listId' => $listId,
54                'title' => $page,
55                'sourceLanguage' => $params['from'],
56                'targetLanguage' => $params['to']
57            ] );
58        }
59
60        if ( $params['listaction'] === 'add' ) {
61            $listAction = $manager->addSuggestions( $suggestions );
62        } elseif ( $params['listaction'] === 'remove' ) {
63            $listAction = $manager->removeSuggestions( $suggestions );
64        } elseif ( $params[ 'listaction' ] === 'view' ) {
65            $listAction = $manager->doesSuggestionExist( $suggestions[ 0 ] );
66        } else {
67            return;
68        }
69
70        $result = [
71            'result' => 'success',
72            'listaction' => $listAction
73        ];
74        $this->getResult()->addValue( null, $this->getModuleName(), $result );
75    }
76
77    private function createList( $translatorUserId, $listName ) {
78        $manager = new SuggestionListManager();
79        $type = SuggestionList::TYPE_DEFAULT;
80
81        if ( $listName === 'cx-suggestionlist-discarded' ) {
82            $type = SuggestionList::TYPE_DISCARDED;
83        } elseif ( $listName === 'cx-suggestionlist-favorite' ) {
84            $type = SuggestionList::TYPE_FAVORITE;
85        }
86
87        $list = new SuggestionList( [
88            'type' => $type,
89            'name' => $listName,
90            'public' => false,
91            'owner'  => $translatorUserId,
92        ] );
93        return $manager->insertList( $list );
94    }
95
96    public function getAllowedParams() {
97        return [
98            'listname' => [
99                ParamValidator::PARAM_REQUIRED => true,
100                ParamValidator::PARAM_TYPE => 'string',
101            ],
102            'listaction' => [
103                ParamValidator::PARAM_REQUIRED => true,
104                ParamValidator::PARAM_TYPE => [ 'add', 'remove', 'view' ],
105            ],
106            'titles' => [
107                ParamValidator::PARAM_REQUIRED => true,
108                ParamValidator::PARAM_TYPE => 'string',
109                ParamValidator::PARAM_ISMULTI => true,
110            ],
111            'from' => [
112                ParamValidator::PARAM_REQUIRED => true,
113                ParamValidator::PARAM_TYPE => 'string',
114            ],
115            'to' => [
116                ParamValidator::PARAM_REQUIRED => false,
117                ParamValidator::PARAM_TYPE => 'string',
118            ],
119            'token' => [
120                ParamValidator::PARAM_REQUIRED => true,
121            ],
122        ];
123    }
124
125    public function needsToken() {
126        return 'csrf';
127    }
128
129    public function isWriteMode() {
130        return true;
131    }
132
133    protected function getExamplesMessages() {
134        return [
135            'action=cxsuggestionlist&listname=List&listaction=add&' .
136                'titles=Title&from=en&to=es&token=123ABC'
137                => 'apihelp-cxsuggestionlist-example-1'
138        ];
139    }
140}