Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
7 / 9
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockActionInfo
77.78% covered (warning)
77.78%
7 / 9
50.00% covered (danger)
50.00%
2 / 4
6.40
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllBlockActions
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 getActionFromId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdFromAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Block;
22
23use MediaWiki\HookContainer\HookContainer;
24use MediaWiki\HookContainer\HookRunner;
25use UnexpectedValueException;
26
27/**
28 * Defines the actions that can be blocked by a partial block. They are
29 * always blocked by a sitewide block.
30 *
31 * NB: The terms "right" and "action" are often used to describe the same
32 * string, depending on the context. E.g. a querystring might contain
33 * 'action=edit', but the PermissionManager will refer to the 'edit' right.
34 *
35 * Here, we use "action", since a user may be in a group that has a
36 * certain right, while also being blocked from performing that action.
37 *
38 * @since 1.37
39 */
40class BlockActionInfo {
41    private HookRunner $hookRunner;
42
43    /** @internal Public for testing only -- use getIdFromAction() */
44    public const ACTION_UPLOAD = 1;
45
46    /** @internal Public for testing only -- use getIdFromAction() */
47    public const ACTION_MOVE = 2;
48
49    /** @internal Public for testing only -- use getIdFromAction() */
50    public const ACTION_CREATE = 3;
51
52    /**
53     * Core block actions.
54     *
55     * Each key is an action string passed to PermissionManager::checkUserBlock
56     * Each value is a class constant for that action
57     *
58     * Each key has a corresponding message with key "ipb-action-$key"
59     *
60     * Core messages:
61     * ipb-action-upload
62     * ipb-action-move
63     * ipb-action-create
64     */
65    private const CORE_BLOCK_ACTIONS = [
66        'upload' => self::ACTION_UPLOAD,
67        'move' => self::ACTION_MOVE,
68        'create' => self::ACTION_CREATE,
69    ];
70
71    public function __construct( HookContainer $hookContainer ) {
72        $this->hookRunner = new HookRunner( $hookContainer );
73    }
74
75    /**
76     * Cache the array of actions
77     * @var int[]|null
78     */
79    private $allBlockActions = null;
80
81    /**
82     * @return int[]
83     */
84    public function getAllBlockActions(): array {
85        // Don't run the hook multiple times in the same request
86        if ( !$this->allBlockActions ) {
87            $this->allBlockActions = self::CORE_BLOCK_ACTIONS;
88            $this->hookRunner->onGetAllBlockActions( $this->allBlockActions );
89        }
90        if ( count( $this->allBlockActions ) !== count( array_unique( $this->allBlockActions ) ) ) {
91            throw new UnexpectedValueException( 'Blockable action IDs not unique' );
92        }
93        return $this->allBlockActions;
94    }
95
96    /**
97     * @param int $actionId
98     * @return string|false
99     */
100    public function getActionFromId( int $actionId ) {
101        return array_search( $actionId, $this->getAllBlockActions() );
102    }
103
104    /**
105     * @param string $action
106     * @return int|bool False if the action is not in the list of blockable actions
107     */
108    public function getIdFromAction( string $action ) {
109        return $this->getAllBlockActions()[$action] ?? false;
110    }
111
112}