Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.78% covered (warning)
83.78%
31 / 37
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiReadingLists
83.78% covered (warning)
83.78%
31 / 37
57.14% covered (warning)
57.14%
4 / 7
10.43
0.00% covered (danger)
0.00%
0 / 1
 execute
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
3.04
 getModuleManager
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
6 / 6
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
 isWriteMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 ApiModuleManager;
7use MediaWiki\Extension\ReadingLists\ReadingListRepositoryException;
8use MediaWiki\MediaWikiServices;
9use Wikimedia\ParamValidator\ParamValidator;
10
11/**
12 * API parent module for all write operations.
13 * Each operation (command) is implemented as a submodule. This module just performs some
14 * basic checks and dispatches the execute() call.
15 */
16class ApiReadingLists extends ApiBase {
17
18    /** @var array Module name => module class */
19    private static $submodules = [
20        'setup' => ApiReadingListsSetup::class,
21        'teardown' => ApiReadingListsTeardown::class,
22        'create' => ApiReadingListsCreate::class,
23        'update' => ApiReadingListsUpdate::class,
24        'delete' => ApiReadingListsDelete::class,
25        'createentry' => ApiReadingListsCreateEntry::class,
26        'deleteentry' => ApiReadingListsDeleteEntry::class,
27    ];
28
29    /** @var ApiModuleManager */
30    private $moduleManager;
31
32    /**
33     * Entry point for executing the module
34     * @inheritDoc
35     */
36    public function execute() {
37        if ( !$this->getUser()->isNamed() ) {
38            $this->dieWithError( [ 'apierror-mustbeloggedin',
39                $this->msg( 'action-editmyprivateinfo' ) ], 'notloggedin' );
40        }
41        $this->checkUserRightsAny( 'editmyprivateinfo' );
42
43        $command = $this->getParameter( 'command' );
44        $module = $this->moduleManager->getModule( $command, 'command' );
45        $module->extractRequestParams();
46        try {
47            $module->execute();
48            $module->getResult()->addValue( null, $module->getModuleName(), [ 'result' => 'Success' ] );
49        } catch ( ReadingListRepositoryException $e ) {
50            $module->getResult()->addValue( null, $module->getModuleName(), [ 'result' => 'Failure' ] );
51            $this->dieWithException( $e );
52        }
53    }
54
55    /**
56     * @inheritDoc
57     */
58    public function getModuleManager() {
59        if ( !$this->moduleManager ) {
60            $modules = array_map( static function ( $class ) {
61                return [
62                    'class' => $class,
63                    'factory' => "$class::factory",
64                ];
65            }, self::$submodules );
66            $this->moduleManager = new ApiModuleManager(
67                $this,
68                MediaWikiServices::getInstance()->getObjectFactory()
69            );
70            $this->moduleManager->addModules( $modules, 'command' );
71        }
72        return $this->moduleManager;
73    }
74
75    /**
76     * @inheritDoc
77     */
78    protected function getAllowedParams() {
79        return [
80            'command' => [
81                ParamValidator::PARAM_TYPE => 'submodule',
82                ParamValidator::PARAM_REQUIRED => true,
83            ],
84        ];
85    }
86
87    /**
88     * @inheritDoc
89     */
90    public function getHelpUrls() {
91        return [
92            'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:ReadingLists#API',
93        ];
94    }
95
96    /**
97     * @inheritDoc
98     */
99    public function isWriteMode() {
100        return true;
101    }
102
103    /**
104     * @inheritDoc
105     */
106    public function needsToken() {
107        return 'csrf';
108    }
109
110    /**
111     * @inheritDoc
112     */
113    public function isInternal() {
114        // ReadingLists API is still experimental
115        return true;
116    }
117
118}