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