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