Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.35% |
41 / 43 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
ListsUpdateHandler | |
95.35% |
41 / 43 |
|
83.33% |
5 / 6 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
postInitSetup | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
run | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
2.03 | |||
getParamSettings | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
getBodyParamSettings | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\ReadingLists\Rest; |
4 | |
5 | use MediaWiki\Config\Config; |
6 | use MediaWiki\Extension\ReadingLists\ReadingListRepository; |
7 | use MediaWiki\Extension\ReadingLists\ReadingListRepositoryException; |
8 | use MediaWiki\Logger\LoggerFactory; |
9 | use MediaWiki\Rest\Handler; |
10 | use MediaWiki\Rest\LocalizedHttpException; |
11 | use MediaWiki\Rest\SimpleHandler; |
12 | use MediaWiki\Rest\Validator\Validator; |
13 | use MediaWiki\User\CentralId\CentralIdLookup; |
14 | use Psr\Log\LoggerInterface; |
15 | use Wikimedia\ParamValidator\ParamValidator; |
16 | use Wikimedia\ParamValidator\TypeDef\NumericDef; |
17 | use Wikimedia\ParamValidator\TypeDef\StringDef; |
18 | use Wikimedia\Rdbms\LBFactory; |
19 | |
20 | /** |
21 | * Handle PUT requests to /{module}/lists/{id} |
22 | * |
23 | * Updates reading lists |
24 | */ |
25 | class ListsUpdateHandler extends SimpleHandler { |
26 | use ReadingListsHandlerTrait; |
27 | use ReadingListsTokenAwareHandlerTrait; |
28 | |
29 | private LBFactory $dbProvider; |
30 | |
31 | private Config $config; |
32 | |
33 | private CentralIdLookup $centralIdLookup; |
34 | |
35 | private LoggerInterface $logger; |
36 | |
37 | /** |
38 | * @param LBFactory $dbProvider |
39 | * @param Config $config |
40 | * @param CentralIdLookup $centralIdLookup |
41 | */ |
42 | public function __construct( |
43 | LBFactory $dbProvider, |
44 | Config $config, |
45 | CentralIdLookup $centralIdLookup |
46 | ) { |
47 | $this->dbProvider = $dbProvider; |
48 | $this->config = $config; |
49 | $this->centralIdLookup = $centralIdLookup; |
50 | $this->logger = LoggerFactory::getInstance( 'readinglists' ); |
51 | } |
52 | |
53 | /** |
54 | * Create the repository data access object instance. |
55 | * |
56 | * @return void |
57 | */ |
58 | public function postInitSetup() { |
59 | $this->repository = $this->createRepository( |
60 | $this->getAuthority()->getUser(), $this->dbProvider, $this->config, $this->centralIdLookup, $this->logger |
61 | ); |
62 | } |
63 | |
64 | /** |
65 | * @inheritDoc |
66 | */ |
67 | public function validate( Validator $restValidator ) { |
68 | try { |
69 | parent::validate( $restValidator ); |
70 | $this->validateToken(); |
71 | } catch ( LocalizedHttpException $e ) { |
72 | // Add fields expected by WMF mobile apps |
73 | $this->die( $e->getMessageValue(), [], $e->getCode(), $e->getErrorData() ); |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * @param int $id the list to update |
79 | * @return array |
80 | */ |
81 | public function run( int $id ) { |
82 | $this->checkAuthority( $this->getAuthority() ); |
83 | |
84 | $params = $this->getValidatedBody() ?? []; |
85 | $this->requireAtLeastOneParameter( $params, 'name', 'description' ); |
86 | try { |
87 | $list = $this->getRepository()->updateList( $id, $params['name'], $params['description'] ); |
88 | } catch ( ReadingListRepositoryException $e ) { |
89 | $this->die( $e->getMessageObject() ); |
90 | } |
91 | |
92 | $listData = $this->getListFromRow( $list ); |
93 | return [ |
94 | 'list' => $listData, |
95 | ]; |
96 | } |
97 | |
98 | /** |
99 | * @return array[] |
100 | */ |
101 | public function getParamSettings() { |
102 | return [ |
103 | 'id' => [ |
104 | ParamValidator::PARAM_TYPE => 'integer', |
105 | ParamValidator::PARAM_REQUIRED => true, |
106 | NumericDef::PARAM_MIN => 1, |
107 | Handler::PARAM_SOURCE => 'path', |
108 | ] |
109 | ] + $this->getReadingListsTokenParamDefinition(); |
110 | } |
111 | |
112 | /** |
113 | * @return array[] |
114 | */ |
115 | public function getBodyParamSettings(): array { |
116 | return [ |
117 | 'name' => [ |
118 | self::PARAM_SOURCE => 'body', |
119 | ParamValidator::PARAM_TYPE => 'string', |
120 | ParamValidator::PARAM_REQUIRED => false, |
121 | StringDef::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_name'], |
122 | ], |
123 | 'description' => [ |
124 | self::PARAM_SOURCE => 'body', |
125 | ParamValidator::PARAM_TYPE => 'string', |
126 | ParamValidator::PARAM_REQUIRED => false, |
127 | StringDef::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_description'], |
128 | ] |
129 | ] + $this->getTokenParamDefinition(); |
130 | } |
131 | |
132 | } |