Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
34.69% |
17 / 49 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
OATHAuthModuleRegistry | |
34.69% |
17 / 49 |
|
14.29% |
1 / 7 |
97.49 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getModuleByKey | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getAllModules | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getModuleId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getModuleIds | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
getModuleIdsFromDatabase | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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\Extension\OATHAuth; |
22 | |
23 | use InvalidArgumentException; |
24 | use Wikimedia\Rdbms\IConnectionProvider; |
25 | |
26 | class OATHAuthModuleRegistry { |
27 | |
28 | private IConnectionProvider $dbProvider; |
29 | |
30 | /** @var array */ |
31 | private $modules; |
32 | |
33 | /** @var array|null */ |
34 | private $moduleIds; |
35 | |
36 | public function __construct( |
37 | IConnectionProvider $dbProvider, |
38 | array $modules |
39 | ) { |
40 | $this->dbProvider = $dbProvider; |
41 | $this->modules = $modules; |
42 | } |
43 | |
44 | public function getModuleByKey( string $key ): ?IModule { |
45 | if ( isset( $this->getModules()[$key] ) ) { |
46 | $module = call_user_func_array( $this->getModules()[$key], [] ); |
47 | if ( !$module instanceof IModule ) { |
48 | return null; |
49 | } |
50 | return $module; |
51 | } |
52 | |
53 | return null; |
54 | } |
55 | |
56 | /** |
57 | * Get all modules registered on the wiki |
58 | * |
59 | * @return IModule[] |
60 | */ |
61 | public function getAllModules(): array { |
62 | $modules = []; |
63 | foreach ( $this->getModules() as $key => $callback ) { |
64 | $module = $this->getModuleByKey( $key ); |
65 | if ( !( $module instanceof IModule ) ) { |
66 | continue; |
67 | } |
68 | $modules[$key] = $module; |
69 | } |
70 | return $modules; |
71 | } |
72 | |
73 | /** |
74 | * Returns the numerical ID for the module with the specified key. |
75 | * |
76 | * @param string $key |
77 | * @return int |
78 | */ |
79 | public function getModuleId( string $key ): int { |
80 | $ids = $this->getModuleIds(); |
81 | if ( isset( $ids[$key] ) ) { |
82 | return $ids[$key]; |
83 | } |
84 | |
85 | throw new InvalidArgumentException( "Module $key does not seem to exist" ); |
86 | } |
87 | |
88 | /** |
89 | * @return array |
90 | */ |
91 | public function getModuleIds(): array { |
92 | if ( $this->moduleIds === null ) { |
93 | $this->moduleIds = $this->getModuleIdsFromDatabase( false ); |
94 | } |
95 | |
96 | $missing = array_diff( |
97 | array_keys( $this->getModules() ), |
98 | array_keys( $this->moduleIds ) |
99 | ); |
100 | |
101 | if ( $missing ) { |
102 | $insert = $this->dbProvider |
103 | ->getPrimaryDatabase( 'virtual-oathauth' ) |
104 | ->newInsertQueryBuilder() |
105 | ->insertInto( 'oathauth_types' ) |
106 | ->caller( __METHOD__ ); |
107 | |
108 | foreach ( $missing as $name ) { |
109 | $insert->row( [ 'oat_name' => $name ] ); |
110 | } |
111 | |
112 | $insert->execute(); |
113 | $this->moduleIds = $this->getModuleIdsFromDatabase( true ); |
114 | } |
115 | |
116 | return $this->moduleIds; |
117 | } |
118 | |
119 | private function getModuleIdsFromDatabase( bool $fromPrimary ): array { |
120 | $ids = []; |
121 | |
122 | if ( $fromPrimary ) { |
123 | $dbr = $this->dbProvider->getPrimaryDatabase( 'virtual-oathauth' ); |
124 | } else { |
125 | $dbr = $this->dbProvider->getReplicaDatabase( 'virtual-oathauth' ); |
126 | } |
127 | |
128 | $rows = $dbr->newSelectQueryBuilder() |
129 | ->select( [ 'oat_id', 'oat_name' ] ) |
130 | ->from( 'oathauth_types' ) |
131 | ->caller( __METHOD__ ) |
132 | ->fetchResultSet(); |
133 | |
134 | foreach ( $rows as $row ) { |
135 | $ids[$row->oat_name] = (int)$row->oat_id; |
136 | } |
137 | |
138 | return $ids; |
139 | } |
140 | |
141 | private function getModules(): array { |
142 | return $this->modules; |
143 | } |
144 | } |