Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.78% |
7 / 9 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
BlockActionInfo | |
77.78% |
7 / 9 |
|
50.00% |
2 / 4 |
6.40 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllBlockActions | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getActionFromId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIdFromAction | |
100.00% |
1 / 1 |
|
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 | |
21 | namespace MediaWiki\Block; |
22 | |
23 | use MediaWiki\HookContainer\HookContainer; |
24 | use MediaWiki\HookContainer\HookRunner; |
25 | use 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 | */ |
40 | class BlockActionInfo { |
41 | /** @var HookRunner */ |
42 | private $hookRunner; |
43 | |
44 | /** @internal Public for testing only -- use getIdFromAction() */ |
45 | public const ACTION_UPLOAD = 1; |
46 | |
47 | /** @internal Public for testing only -- use getIdFromAction() */ |
48 | public const ACTION_MOVE = 2; |
49 | |
50 | /** @internal Public for testing only -- use getIdFromAction() */ |
51 | public const ACTION_CREATE = 3; |
52 | |
53 | /** |
54 | * Core block actions. |
55 | * |
56 | * Each key is an action string passed to PermissionManager::checkUserBlock |
57 | * Each value is a class constant for that action |
58 | * |
59 | * Each key has a corresponding message with key "ipb-action-$key" |
60 | * |
61 | * Core messages: |
62 | * ipb-action-upload |
63 | * ipb-action-move |
64 | * ipb-action-create |
65 | * |
66 | * @var int[] |
67 | */ |
68 | private const CORE_BLOCK_ACTIONS = [ |
69 | 'upload' => self::ACTION_UPLOAD, |
70 | 'move' => self::ACTION_MOVE, |
71 | 'create' => self::ACTION_CREATE, |
72 | ]; |
73 | |
74 | /** |
75 | * @param HookContainer $hookContainer |
76 | */ |
77 | public function __construct( HookContainer $hookContainer ) { |
78 | $this->hookRunner = new HookRunner( $hookContainer ); |
79 | } |
80 | |
81 | /** |
82 | * Cache the array of actions |
83 | * @var int[]|null |
84 | */ |
85 | private $allBlockActions = null; |
86 | |
87 | /** |
88 | * @return int[] |
89 | */ |
90 | public function getAllBlockActions(): array { |
91 | // Don't run the hook multiple times in the same request |
92 | if ( !$this->allBlockActions ) { |
93 | $this->allBlockActions = self::CORE_BLOCK_ACTIONS; |
94 | $this->hookRunner->onGetAllBlockActions( $this->allBlockActions ); |
95 | } |
96 | if ( count( $this->allBlockActions ) !== count( array_unique( $this->allBlockActions ) ) ) { |
97 | throw new UnexpectedValueException( 'Blockable action IDs not unique' ); |
98 | } |
99 | return $this->allBlockActions; |
100 | } |
101 | |
102 | /** |
103 | * @param int $actionId |
104 | * @return string|false |
105 | */ |
106 | public function getActionFromId( int $actionId ) { |
107 | return array_search( $actionId, $this->getAllBlockActions() ); |
108 | } |
109 | |
110 | /** |
111 | * @param string $action |
112 | * @return int|bool False if the action is not in the list of blockable actions |
113 | */ |
114 | public function getIdFromAction( string $action ) { |
115 | return $this->getAllBlockActions()[$action] ?? false; |
116 | } |
117 | |
118 | } |