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