Merge "Update searchdisabled message with docs and better link target"
[mediawiki.git] / includes / block / BlockActionInfo.php
blob25a7eb5cb198cb2d0b45bee1e8a5e324bb381db2
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.
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.
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
18 * @file
21 namespace MediaWiki\Block;
23 use MediaWiki\HookContainer\HookContainer;
24 use MediaWiki\HookContainer\HookRunner;
25 use UnexpectedValueException;
27 /**
28 * Defines the actions that can be blocked by a partial block. They are
29 * always blocked by a sitewide block.
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.
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.
38 * @since 1.37
40 class BlockActionInfo {
41 private HookRunner $hookRunner;
43 /** @internal Public for testing only -- use getIdFromAction() */
44 public const ACTION_UPLOAD = 1;
46 /** @internal Public for testing only -- use getIdFromAction() */
47 public const ACTION_MOVE = 2;
49 /** @internal Public for testing only -- use getIdFromAction() */
50 public const ACTION_CREATE = 3;
52 /**
53 * Core block actions.
55 * Each key is an action string passed to PermissionManager::checkUserBlock
56 * Each value is a class constant for that action
58 * Each key has a corresponding message with key "ipb-action-$key"
60 * Core messages:
61 * ipb-action-upload
62 * ipb-action-move
63 * ipb-action-create
65 private const CORE_BLOCK_ACTIONS = [
66 'upload' => self::ACTION_UPLOAD,
67 'move' => self::ACTION_MOVE,
68 'create' => self::ACTION_CREATE,
71 public function __construct( HookContainer $hookContainer ) {
72 $this->hookRunner = new HookRunner( $hookContainer );
75 /**
76 * Cache the array of actions
77 * @var int[]|null
79 private $allBlockActions = null;
81 /**
82 * @return int[]
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 );
90 if ( count( $this->allBlockActions ) !== count( array_unique( $this->allBlockActions ) ) ) {
91 throw new UnexpectedValueException( 'Blockable action IDs not unique' );
93 return $this->allBlockActions;
96 /**
97 * @param int $actionId
98 * @return string|false
100 public function getActionFromId( int $actionId ) {
101 return array_search( $actionId, $this->getAllBlockActions() );
105 * @param string $action
106 * @return int|bool False if the action is not in the list of blockable actions
108 public function getIdFromAction( string $action ) {
109 return $this->getAllBlockActions()[$action] ?? false;