Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| GeoData | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| getPageCoordinates | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getAllCoordinates | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| getDB | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace GeoData; |
| 4 | |
| 5 | use MediaWiki\MediaWikiServices; |
| 6 | use Wikimedia\Rdbms\IReadableDatabase; |
| 7 | |
| 8 | class GeoData { |
| 9 | /** |
| 10 | * Returns primary coordinates of the given page, if any |
| 11 | */ |
| 12 | public static function getPageCoordinates( int $pageId ): ?Coord { |
| 13 | $coords = self::getAllCoordinates( $pageId, [ 'gt_primary' => 1 ] ); |
| 14 | return $coords[0] ?? null; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Retrieves all coordinates for the given page id |
| 19 | * |
| 20 | * @param int $pageId ID of the page |
| 21 | * @param array $conds Conditions for {@see IReadableDatabase::select} |
| 22 | * @param int $dbType Database to select from DB_PRIMARY or DB_REPLICA |
| 23 | * @return Coord[] |
| 24 | */ |
| 25 | public static function getAllCoordinates( int $pageId, array $conds = [], int $dbType = DB_REPLICA ): array { |
| 26 | $db = self::getDB( $dbType ); |
| 27 | $conds['gt_page_id'] = $pageId; |
| 28 | $columns = array_values( Coord::FIELD_MAPPING ); |
| 29 | $res = $db->newSelectQueryBuilder() |
| 30 | ->select( $columns ) |
| 31 | ->from( 'geo_tags' ) |
| 32 | ->where( $conds ) |
| 33 | ->caller( __METHOD__ ) |
| 34 | ->fetchResultSet(); |
| 35 | $coords = []; |
| 36 | foreach ( $res as $row ) { |
| 37 | $coords[] = Coord::newFromRow( $row ); |
| 38 | } |
| 39 | return $coords; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param int $dbType DB_PRIMARY or DB_REPLICA |
| 44 | * @return IReadableDatabase |
| 45 | */ |
| 46 | private static function getDB( int $dbType ): IReadableDatabase { |
| 47 | return MediaWikiServices::getInstance() |
| 48 | ->getDBLoadBalancer() |
| 49 | ->getConnection( $dbType ); |
| 50 | } |
| 51 | } |