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