Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
Database | |
0.00% |
0 / 63 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDB | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getForUser | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
getForRemoteUser | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
setForUser | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
72 |
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 | declare( strict_types = 1 ); |
22 | |
23 | namespace MediaWiki\Babel; |
24 | |
25 | use MediaWiki\MediaWikiServices; |
26 | use Wikimedia\Rdbms\IDatabase; |
27 | use Wikimedia\Rdbms\LBFactory; |
28 | |
29 | class Database { |
30 | |
31 | /** |
32 | * @var LBFactory |
33 | */ |
34 | private $loadBalancerFactory; |
35 | |
36 | public function __construct() { |
37 | $this->loadBalancerFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
38 | } |
39 | |
40 | /** |
41 | * @param int $index |
42 | * @param string|bool $wiki Database name if querying a different wiki |
43 | * @return IDatabase |
44 | */ |
45 | protected function getDB( int $index, $wiki = false ): IDatabase { |
46 | return $this->loadBalancerFactory->getMainLB( $wiki ) |
47 | ->getConnection( $index, [], $wiki ); |
48 | } |
49 | |
50 | /** |
51 | * @param int $id user id |
52 | * @return string[] [ lang => level ] |
53 | */ |
54 | public function getForUser( int $id ): array { |
55 | $rows = $this->getDB( DB_REPLICA )->select( |
56 | 'babel', |
57 | [ 'babel_lang', 'babel_level' ], |
58 | [ 'babel_user' => $id ], |
59 | __METHOD__ |
60 | ); |
61 | |
62 | $return = []; |
63 | foreach ( $rows as $row ) { |
64 | $return[$row->babel_lang] = $row->babel_level; |
65 | } |
66 | |
67 | return $return; |
68 | } |
69 | |
70 | /** |
71 | * @param string $wiki Database name |
72 | * @param string $username |
73 | * @return string[] [ lang => level ] |
74 | */ |
75 | public function getForRemoteUser( string $wiki, string $username ): array { |
76 | $rows = $this->getDB( DB_REPLICA, $wiki )->select( |
77 | [ 'babel', 'user' ], |
78 | [ 'babel_lang', 'babel_level' ], |
79 | [ |
80 | 'user_name' => $username |
81 | ], |
82 | __METHOD__, |
83 | [], |
84 | [ |
85 | 'user' => [ 'INNER JOIN', 'babel_user=user_id' ] |
86 | ] |
87 | ); |
88 | |
89 | $return = []; |
90 | foreach ( $rows as $row ) { |
91 | $return[$row->babel_lang] = $row->babel_level; |
92 | } |
93 | |
94 | return $return; |
95 | } |
96 | |
97 | /** |
98 | * @param int $id |
99 | * @param string[] $data [ lang => level ] |
100 | * @return bool true if changes to the db were made |
101 | */ |
102 | public function setForUser( int $id, array $data ): bool { |
103 | $dbw = $this->getDB( DB_PRIMARY ); |
104 | |
105 | $newRows = []; |
106 | foreach ( $data as $lang => $level ) { |
107 | $newRows[$lang] = [ |
108 | 'babel_lang' => $lang, |
109 | 'babel_level' => $level, |
110 | 'babel_user' => $id |
111 | ]; |
112 | } |
113 | |
114 | $rowsDelete = []; |
115 | $res = $dbw->select( |
116 | 'babel', |
117 | [ 'babel_lang', 'babel_level' ], |
118 | [ 'babel_user' => $id ], |
119 | __METHOD__ |
120 | ); |
121 | foreach ( $res as $row ) { |
122 | if ( isset( $newRows[$row->babel_lang] ) ) { |
123 | if ( $newRows[$row->babel_lang]['babel_level'] === $row->babel_level ) { |
124 | // Matching row already exists |
125 | unset( $newRows[$row->babel_lang] ); |
126 | } |
127 | } else { |
128 | $rowsDelete[] = $row->babel_lang; |
129 | } |
130 | } |
131 | |
132 | if ( $rowsDelete ) { |
133 | $dbw->delete( |
134 | 'babel', |
135 | [ 'babel_user' => $id, 'babel_lang' => $rowsDelete ], |
136 | __METHOD__ |
137 | ); |
138 | } |
139 | if ( $newRows ) { |
140 | $dbw->replace( |
141 | 'babel', |
142 | [ [ 'babel_user', 'babel_lang' ] ], |
143 | array_values( $newRows ), |
144 | __METHOD__ |
145 | ); |
146 | } |
147 | |
148 | return $rowsDelete || $newRows; |
149 | } |
150 | } |