Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsEntriesCreateBatchHandler
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 postInitSetup
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getParamSettings
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\ReadingLists\Rest;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\Rest\Handler;
8use MediaWiki\Rest\SimpleHandler;
9use MediaWiki\Rest\Validator\Validator;
10use MediaWiki\User\CentralId\CentralIdLookup;
11use Psr\Log\LoggerInterface;
12use Wikimedia\ParamValidator\ParamValidator;
13use Wikimedia\ParamValidator\TypeDef\NumericDef;
14use Wikimedia\Rdbms\LBFactory;
15
16/**
17 * Handle POST requests to /readinglists/v0/lists/{id}/entries/batch
18 *
19 * Gets reading list entries
20 */
21class ListsEntriesCreateBatchHandler extends SimpleHandler {
22    use ReadingListsHandlerTrait;
23    use ReadingListsTokenAwareHandlerTrait;
24
25    private LBFactory $dbProvider;
26
27    private Config $config;
28
29    private CentralIdLookup $centralIdLookup;
30
31    private LoggerInterface $logger;
32
33    /**
34     * @param LBFactory $dbProvider
35     * @param Config $config
36     * @param CentralIdLookup $centralIdLookup
37     */
38    public function __construct(
39        LBFactory $dbProvider,
40        Config $config,
41        CentralIdLookup $centralIdLookup
42    ) {
43        $this->dbProvider = $dbProvider;
44        $this->config = $config;
45        $this->centralIdLookup = $centralIdLookup;
46        $this->logger = LoggerFactory::getInstance( 'readinglists' );
47    }
48
49    /**
50     * Create the repository data access object instance.
51     *
52     * @return void
53     */
54    public function postInitSetup() {
55        $this->repository = $this->createRepository(
56            $this->getAuthority()->getUser(), $this->dbProvider, $this->config, $this->centralIdLookup, $this->logger
57        );
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function validate( Validator $restValidator ) {
64        parent::validate( $restValidator );
65        $this->validateToken();
66    }
67
68    /**
69     * @param int $id the list to update
70     * @return array
71     */
72    public function run( int $id ) {
73        $this->checkAuthority( $this->getAuthority() );
74
75        $validatedBody = $this->getValidatedBody() ?? [];
76        $batch = $validatedBody['batch'];
77
78        $result = [];
79        $result['entries'] = [];
80        $result['batch'] = [];
81        foreach ( $this->getBatchOps( $batch ) as $op ) {
82            $this->requireAtLeastOneBatchParameter( $op, 'project' );
83            $this->requireAtLeastOneBatchParameter( $op, 'title' );
84            $res = $this->createListEntry( $id, $op['project'], $op['title'], $this->getRepository() );
85            $result['batch'][] = [ 'id' => $res['id'] ];
86            $result['entries'][] = $res['entry'];
87        }
88
89        return $result;
90    }
91
92    /**
93     * @return array[]
94     */
95    public function getParamSettings() {
96        return [
97            'id' => [
98                ParamValidator::PARAM_TYPE => 'integer',
99                ParamValidator::PARAM_REQUIRED => true,
100                Handler::PARAM_SOURCE => 'path',
101                NumericDef::PARAM_MIN => 1,
102            ],
103            // TODO: consider additional validation on "batch", once we have that capability.
104            'batch' => [
105                self::PARAM_SOURCE => 'body',
106                ParamValidator::PARAM_TYPE => 'array',
107                ParamValidator::PARAM_REQUIRED => true,
108            ],
109        ] + $this->getTokenParamDefinition() + $this->getReadingListsTokenParamDefinition();
110    }
111}