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