Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
65.38% |
34 / 52 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
ApiReadingListsCreate | |
65.38% |
34 / 52 |
|
25.00% |
2 / 8 |
14.15 | |
0.00% |
0 / 1 |
execute | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
getAllowedParams | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
getExtendedDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\ReadingLists\Api; |
4 | |
5 | use ApiBase; |
6 | use MediaWiki\Extension\ReadingLists\ReadingListRepository; |
7 | use Wikimedia\ParamValidator\ParamValidator; |
8 | |
9 | /** |
10 | * API module for all write operations. |
11 | * Each operation (command) is implemented as a submodule. |
12 | */ |
13 | class ApiReadingListsCreate extends ApiBase { |
14 | |
15 | use ApiTrait; |
16 | |
17 | /** @var string API module prefix */ |
18 | private static $prefix = ''; |
19 | |
20 | /** |
21 | * Entry point for executing the module |
22 | * @inheritDoc |
23 | */ |
24 | public function execute() { |
25 | $params = $this->extractRequestParams(); |
26 | $this->requireOnlyOneParameter( $params, 'name', 'batch' ); |
27 | $this->requireOnlyOneParameter( $params, 'description', 'batch' ); |
28 | |
29 | $repository = $this->getReadingListRepository( $this->getUser() ); |
30 | if ( isset( $params['name'] ) ) { |
31 | $description = $params['description'] ?? ''; |
32 | $list = $repository->addList( $params['name'], $description ); |
33 | $listData = $this->getListFromRow( $list ); |
34 | $this->getResult()->addValue( null, $this->getModuleName(), |
35 | [ 'id' => $list->rl_id, 'list' => $listData ] ); |
36 | } else { |
37 | $listData = $listIds = []; |
38 | foreach ( $this->getBatchOps( $params['batch'] ) as $op ) { |
39 | $description = $op['description'] ?? ''; |
40 | $this->requireAtLeastOneBatchParameter( $op, 'name' ); |
41 | $list = $repository->addList( $op['name'], $description ); |
42 | $listIds[] = $list->rl_id; |
43 | $listData[] = $this->getListFromRow( $list ); |
44 | } |
45 | $this->getResult()->addValue( null, $this->getModuleName(), |
46 | [ 'ids' => $listIds, 'lists' => $listData ] ); |
47 | $this->getResult()->addIndexedTagName( [ $this->getModuleName(), 'ids' ], 'id' ); |
48 | $this->getResult()->addIndexedTagName( [ $this->getModuleName(), 'lists' ], 'list' ); |
49 | } |
50 | } |
51 | |
52 | /** |
53 | * @inheritDoc |
54 | */ |
55 | protected function getAllowedParams() { |
56 | return [ |
57 | 'name' => [ |
58 | ParamValidator::PARAM_TYPE => 'string', |
59 | self::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_name'], |
60 | ], |
61 | 'description' => [ |
62 | ParamValidator::PARAM_TYPE => 'string', |
63 | self::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_description'], |
64 | ], |
65 | 'batch' => [ |
66 | ParamValidator::PARAM_TYPE => 'string', |
67 | ] |
68 | ]; |
69 | } |
70 | |
71 | /** |
72 | * @inheritDoc |
73 | */ |
74 | protected function getExtendedDescription() { |
75 | $limit = $this->getConfig()->get( 'ReadingListsMaxListsPerUser' ); |
76 | return $this->msg( 'apihelp-readinglists+create-extended-description', $limit ); |
77 | } |
78 | |
79 | /** |
80 | * @inheritDoc |
81 | */ |
82 | public function getHelpUrls() { |
83 | return [ |
84 | 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:ReadingLists#API', |
85 | ]; |
86 | } |
87 | |
88 | /** |
89 | * @inheritDoc |
90 | */ |
91 | protected function getExamplesMessages() { |
92 | $batch = wfArrayToCgi( [ 'batch' => json_encode( [ |
93 | [ 'name' => 'dogs', 'description' => 'Woof!' ], |
94 | [ 'name' => 'cats', 'description' => 'Meow' ], |
95 | ] ) ] ); |
96 | return [ |
97 | 'action=readinglists&command=create&name=dogs&description=Woof!&token=123ABC' |
98 | => 'apihelp-readinglists+create-example-1', |
99 | "action=readinglists&command=create&$batch&token=123ABC" |
100 | => 'apihelp-readinglists+create-example-2', |
101 | ]; |
102 | } |
103 | |
104 | // The parent module already enforces these but they make documentation nicer. |
105 | |
106 | /** |
107 | * @inheritDoc |
108 | */ |
109 | public function isWriteMode() { |
110 | return true; |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | */ |
116 | public function mustBePosted() { |
117 | return true; |
118 | } |
119 | |
120 | /** |
121 | * @inheritDoc |
122 | */ |
123 | public function isInternal() { |
124 | // ReadingLists API is still experimental |
125 | return true; |
126 | } |
127 | |
128 | } |