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 | /** |
82 | * @param int $translatorUserId |
83 | * @param string $listName |
84 | * @return int |
85 | */ |
86 | private function createList( $translatorUserId, $listName ) { |
87 | $manager = new SuggestionListManager(); |
88 | $type = SuggestionList::TYPE_DEFAULT; |
89 | |
90 | if ( $listName === 'cx-suggestionlist-discarded' ) { |
91 | $type = SuggestionList::TYPE_DISCARDED; |
92 | } elseif ( $listName === 'cx-suggestionlist-favorite' ) { |
93 | $type = SuggestionList::TYPE_FAVORITE; |
94 | } |
95 | |
96 | $list = new SuggestionList( [ |
97 | 'type' => $type, |
98 | 'name' => $listName, |
99 | 'public' => false, |
100 | 'owner' => $translatorUserId, |
101 | ] ); |
102 | return $manager->insertList( $list ); |
103 | } |
104 | |
105 | /** @inheritDoc */ |
106 | public function getAllowedParams() { |
107 | return [ |
108 | 'listname' => [ |
109 | ParamValidator::PARAM_REQUIRED => true, |
110 | ParamValidator::PARAM_TYPE => 'string', |
111 | ], |
112 | 'listaction' => [ |
113 | ParamValidator::PARAM_REQUIRED => true, |
114 | ParamValidator::PARAM_TYPE => [ 'add', 'remove', 'view' ], |
115 | ], |
116 | 'titles' => [ |
117 | ParamValidator::PARAM_REQUIRED => true, |
118 | ParamValidator::PARAM_TYPE => 'string', |
119 | ParamValidator::PARAM_ISMULTI => true, |
120 | ], |
121 | 'from' => [ |
122 | ParamValidator::PARAM_REQUIRED => true, |
123 | ParamValidator::PARAM_TYPE => 'string', |
124 | ], |
125 | 'to' => [ |
126 | ParamValidator::PARAM_REQUIRED => false, |
127 | ParamValidator::PARAM_TYPE => 'string', |
128 | ], |
129 | 'token' => [ |
130 | ParamValidator::PARAM_REQUIRED => true, |
131 | ], |
132 | ]; |
133 | } |
134 | |
135 | /** @inheritDoc */ |
136 | public function needsToken() { |
137 | return 'csrf'; |
138 | } |
139 | |
140 | /** @inheritDoc */ |
141 | public function isWriteMode() { |
142 | return true; |
143 | } |
144 | |
145 | /** @inheritDoc */ |
146 | protected function getExamplesMessages() { |
147 | return [ |
148 | 'action=cxsuggestionlist&listname=List&listaction=add&' . |
149 | 'titles=Title&from=en&to=es&token=123ABC' |
150 | => 'apihelp-cxsuggestionlist-example-1' |
151 | ]; |
152 | } |
153 | } |