Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.54% |
8 / 13 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Utils | |
61.54% |
8 / 13 |
|
25.00% |
1 / 4 |
6.42 | |
0.00% |
0 / 1 |
| getCentralId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getEnabledCounters | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getUserCountsDB | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getDB | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 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 | * |
| 17 | * @file |
| 18 | */ |
| 19 | |
| 20 | namespace MediaWiki\Extension\WikimediaEditorTasks; |
| 21 | |
| 22 | use MediaWiki\Config\ConfigException; |
| 23 | use MediaWiki\MediaWikiServices; |
| 24 | use MediaWiki\User\CentralId\CentralIdLookup; |
| 25 | use MediaWiki\User\UserIdentity; |
| 26 | use Wikimedia\Rdbms\IDatabase; |
| 27 | |
| 28 | class Utils { |
| 29 | |
| 30 | /** |
| 31 | * Get the central user ID for $user |
| 32 | * @param UserIdentity $user local User |
| 33 | * @return int central user ID |
| 34 | */ |
| 35 | public static function getCentralId( UserIdentity $user ) { |
| 36 | return MediaWikiServices::getInstance()->getCentralIdLookupFactory()->getLookup() |
| 37 | ->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the enabled counter definitions from the extension configuration |
| 42 | * @return array enabled counter config |
| 43 | */ |
| 44 | public static function getEnabledCounters() { |
| 45 | $config = WikimediaEditorTasksServices::getInstance()->getExtensionConfig(); |
| 46 | return $config->get( 'WikimediaEditorTasksEnabledCounters' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a database connection to the user counts database. |
| 51 | * @param int $db Type of the connection to get, e.g. DB_PRIMARY or DB_REPLICA. |
| 52 | * @param MediaWikiServices $services |
| 53 | * @param array $groups Query groups [optional] |
| 54 | * @return IDatabase |
| 55 | * @throws ConfigException |
| 56 | */ |
| 57 | public static function getUserCountsDB( $db, $services, $groups = [] ) { |
| 58 | $wetServices = WikimediaEditorTasksServices::wrap( $services ); |
| 59 | $database = $wetServices->getExtensionConfig()->get( 'WikimediaEditorTasksUserCountsDatabase' ); |
| 60 | $cluster = $wetServices->getExtensionConfig()->get( 'WikimediaEditorTasksUserCountsCluster' ); |
| 61 | return self::getDB( $db, $services, $database, $cluster, $groups ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get a database connection. |
| 66 | * @param int $db Type of the connection to get, e.g. DB_PRIMARY or DB_REPLICA. |
| 67 | * @param MediaWikiServices $services |
| 68 | * @param string $database DB name from extension config |
| 69 | * @param string $cluster cluster name from extension config |
| 70 | * @param array $groups Query groups [optional] |
| 71 | * @return IDatabase |
| 72 | * @throws ConfigException |
| 73 | */ |
| 74 | private static function getDB( $db, $services, $database, $cluster, $groups = [] ) { |
| 75 | $loadBalancerFactory = $services->getDBLoadBalancerFactory(); |
| 76 | $loadBalancer = $cluster |
| 77 | ? $loadBalancerFactory->getExternalLB( $cluster ) |
| 78 | : $loadBalancerFactory->getMainLB( $database ); |
| 79 | return $loadBalancer->getConnection( $db, $groups, $database ); |
| 80 | } |
| 81 | |
| 82 | } |