Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.31% covered (warning)
79.31%
23 / 29
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsChangesSinceHandler
79.31% covered (warning)
79.31%
23 / 29
50.00% covered (danger)
50.00%
2 / 4
6.32
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
2.50
 doGetLists
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 getParamSettings
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\ReadingLists\Rest;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Extension\ReadingLists\Doc\ReadingListRow;
7use MediaWiki\Extension\ReadingLists\ReadingListRepositoryException;
8use MediaWiki\Extension\ReadingLists\Utils;
9use MediaWiki\User\CentralId\CentralIdLookup;
10use Wikimedia\ParamValidator\ParamValidator;
11use Wikimedia\Rdbms\IResultWrapper;
12use Wikimedia\Rdbms\LBFactory;
13
14/**
15 * Handle GET requests to /readinglists/v0/lists/changes/since.
16 * This endpoint is for getting lists that have recently changed.
17 */
18class ListsChangesSinceHandler extends ListsHandler {
19    // Temporarily limit paging sizes per T164990#3264314 / T168984#3659998
20    private const MAX_LIMIT = 10;
21
22    /**
23     * @param LBFactory $dbProvider
24     * @param Config $config
25     * @param CentralIdLookup $centralIdLookup
26     */
27    public function __construct(
28        LBFactory $dbProvider,
29        Config $config,
30        CentralIdLookup $centralIdLookup
31    ) {
32        parent::__construct( $dbProvider, $config, $centralIdLookup );
33        $this->allowDeletedRowsInResponse = true;
34    }
35
36    /**
37     * @return array
38     */
39    public function execute() {
40        $expiry = strtotime( Utils::getDeletedExpiry() );
41        $date = $this->getValidatedParams()['date']::time();
42        if ( $date < $expiry ) {
43            $this->die(
44                'apierror-readinglists-too-old',
45                [ '', wfTimestamp( TS_ISO_8601, $expiry ) ]
46            );
47        }
48
49        return $this->getLists( $this->getValidatedParams() );
50    }
51
52    /**
53     * Worker function for getting Reading Lists.
54     *
55     * @param array $params all parameters (path and query)
56     * @return IResultWrapper<ReadingListRow>
57     */
58    protected function doGetLists( array $params ) {
59        $repository = $this->getRepository();
60
61        try {
62            return $repository->getListsByDateUpdated(
63                $params['date'], $params['sort'], $params['dir'], $params['limit'] + 1, $params['next']
64            );
65        } catch ( ReadingListRepositoryException $e ) {
66            $this->die( $e->getMessageObject() );
67        }
68    }
69
70    /**
71     * @return array[]
72     */
73    public function getParamSettings() {
74        return [
75            'date' => [
76                self::PARAM_SOURCE => 'path',
77                ParamValidator::PARAM_TYPE => 'timestamp',
78                ParamValidator::PARAM_REQUIRED => true,
79            ],
80            'next' => [
81                self::PARAM_SOURCE => 'query',
82                ParamValidator::PARAM_TYPE => 'string',
83                ParamValidator::PARAM_REQUIRED => false,
84                ParamValidator::PARAM_DEFAULT => '',
85            ]
86        ] + $this->getSortParamSettings( self::MAX_LIMIT, 'updated' );
87    }
88}