Index: trunk/extensions/GeoData/api/GeoDataQueryExtender.php |
— | — | @@ -0,0 +1,67 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Common code for API modules that enumerate stuff and we add filter by coordinates to them |
| 6 | + */ |
| 7 | +class GeoDataQueryExtender { |
| 8 | + /** |
| 9 | + * Prepares modifications to the query |
| 10 | + * |
| 11 | + * @param Array $params: Request parameters |
| 12 | + * @param String $joinField: Field to use for joins |
| 13 | + * @param String $useIndex: Index to force for query |
| 14 | + * @return Array |
| 15 | + */ |
| 16 | + public static function alterQuery( $params, $joinField, $useIndex ) { |
| 17 | + $tables = array(); |
| 18 | + $fields = array(); |
| 19 | + $joins = array(); |
| 20 | + $options = array(); |
| 21 | + $where = array(); |
| 22 | + |
| 23 | + if ( isset( $params['withcoordinates'] ) || $params['withoutcoordinates'] ) { |
| 24 | + $tables[] = 'geo_tags'; |
| 25 | + $joins['geo_tags'] = array( 'LEFT JOIN', "$joinField = gt_page_id" ); |
| 26 | + if ( isset( $params['withcoordinates'] ) ) { |
| 27 | + switch ( $params['withcoordinates'] ) { |
| 28 | + case 'primary': |
| 29 | + $where[] = 'gt_primary = 1'; |
| 30 | + break; |
| 31 | + case 'secondary': |
| 32 | + $where[] = 'gt_primary = 0'; |
| 33 | + $options['GROUP BY'] = 'page_id'; |
| 34 | + break; |
| 35 | + case 'any': |
| 36 | + $where[] = 'gt_primary IS NOT NULL'; |
| 37 | + $options['GROUP BY'] = 'page_id'; |
| 38 | + break; |
| 39 | + } |
| 40 | + } else { |
| 41 | + $where[] = 'gt_primary IS NULL'; |
| 42 | + } |
| 43 | + } elseif ( $useIndex ) { |
| 44 | + $options['USE INDEX'] = $useIndex; |
| 45 | + } |
| 46 | + return array( $tables, $fields, $joins, $options, $where ); |
| 47 | + } |
| 48 | + |
| 49 | + public static function getAllowedParams() { |
| 50 | + return array( |
| 51 | + 'withcoordinates' => array( |
| 52 | + ApiBase::PARAM_TYPE => array( 'primary', 'secondary', 'any' ), |
| 53 | + ), |
| 54 | + 'withoutcoordinates' => false, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function getParamDescription() { |
| 59 | + return array( |
| 60 | + 'withcoordinates' => 'Whether to return pages with primary, secondary or either coordinates', |
| 61 | + 'withoutcoordinates' => 'Return only pages without coordinates', |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public static function getDescription() { |
| 66 | + return ' (extended by the GeoData extension)'; |
| 67 | + } |
| 68 | +} |
Property changes on: trunk/extensions/GeoData/api/GeoDataQueryExtender.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 69 | + native |
Index: trunk/extensions/GeoData/api/ApiQueryAllPages_GeoData.php |
— | — | @@ -0,0 +1,52 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Overrides and extends core's list=allpages query module |
| 6 | + */ |
| 7 | +class ApiQueryAllPages_GeoData extends ApiQueryAllPages { |
| 8 | + private $useIndex, $alreadyAltered; |
| 9 | + |
| 10 | + public function __construct( $query, $moduleName ) { |
| 11 | + parent::__construct( $query, $moduleName ); |
| 12 | + } |
| 13 | + |
| 14 | + protected function select( $method, $extraQuery = array() ) { |
| 15 | + if ( !$this->alreadyAltered ) { |
| 16 | + $params = $this->extractRequestParams(); |
| 17 | + |
| 18 | + $this->requireMaxOneParameter( $params, 'withcoordinates', 'withoutcoordinates' ); |
| 19 | + |
| 20 | + list( $tables, $fields, $joins, $options, $where ) = |
| 21 | + GeoDataQueryExtender::alterQuery( $params, 'page_id', $this->useIndex ); |
| 22 | + $this->addTables( $tables ); |
| 23 | + $this->addFields( $fields ); |
| 24 | + $this->addJoinConds( $joins ); |
| 25 | + foreach ( $options as $name => $value ) { |
| 26 | + $this->addOption( $name, $value ); |
| 27 | + } |
| 28 | + $this->addWhere( $where ); |
| 29 | + $this->alreadyAltered = true; |
| 30 | + } |
| 31 | + |
| 32 | + return parent::select( __METHOD__, $extraQuery ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Only allow USE INDEX if not joining, otherwise it errors out |
| 37 | + */ |
| 38 | + protected function addOption( $name, $value = null ) { |
| 39 | + if ( $name == 'USE INDEX' ) { |
| 40 | + $this->useIndex = $value; |
| 41 | + } else { |
| 42 | + parent::addOption( $name, $value ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function getDescription() { |
| 47 | + return parent::getDescription() . GeoDataQueryExtender::getDescription(); |
| 48 | + } |
| 49 | + |
| 50 | + public function getAllowedParams() { |
| 51 | + return array_merge( parent::getAllowedParams(), GeoDataQueryExtender::getAllowedParams() ); |
| 52 | + } |
| 53 | +} |
Property changes on: trunk/extensions/GeoData/api/ApiQueryAllPages_GeoData.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 54 | + native |
Index: trunk/extensions/GeoData/api/ApiQueryCategoryMembers_GeoData.php |
— | — | @@ -0,0 +1,52 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Overrides and extends core's list=categorymembers query module |
| 6 | + */ |
| 7 | +class ApiQueryCategoryMembers_GeoData extends ApiQueryCategoryMembers { |
| 8 | + private $useIndex, $alreadyAltered; |
| 9 | + |
| 10 | + public function __construct( $query, $moduleName ) { |
| 11 | + parent::__construct( $query, $moduleName ); |
| 12 | + } |
| 13 | + |
| 14 | + protected function select( $method, $extraQuery = array() ) { |
| 15 | + if ( !$this->alreadyAltered ) { |
| 16 | + $params = $this->extractRequestParams(); |
| 17 | + |
| 18 | + $this->requireMaxOneParameter( $params, 'withcoordinates', 'withoutcoordinates' ); |
| 19 | + |
| 20 | + list( $tables, $fields, $joins, $options, $where ) = |
| 21 | + GeoDataQueryExtender::alterQuery( $params, 'cl_from', $this->useIndex ); |
| 22 | + $this->addTables( $tables ); |
| 23 | + $this->addFields( $fields ); |
| 24 | + $this->addJoinConds( $joins ); |
| 25 | + foreach ( $options as $name => $value ) { |
| 26 | + $this->addOption( $name, $value ); |
| 27 | + } |
| 28 | + $this->addWhere( $where ); |
| 29 | + $this->alreadyAltered = true; |
| 30 | + } |
| 31 | + |
| 32 | + return parent::select( __METHOD__, $extraQuery ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Only allow USE INDEX if not joining, otherwise it errors out |
| 37 | + */ |
| 38 | + protected function addOption( $name, $value = null ) { |
| 39 | + if ( $name == 'USE INDEX' ) { |
| 40 | + $this->useIndex = $value; |
| 41 | + } else { |
| 42 | + parent::addOption( $name, $value ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function getDescription() { |
| 47 | + return parent::getDescription() . GeoDataQueryExtender::getDescription(); |
| 48 | + } |
| 49 | + |
| 50 | + public function getAllowedParams() { |
| 51 | + return array_merge( parent::getAllowedParams(), GeoDataQueryExtender::getAllowedParams() ); |
| 52 | + } |
| 53 | +} |
Property changes on: trunk/extensions/GeoData/api/ApiQueryCategoryMembers_GeoData.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 54 | + native |
Index: trunk/extensions/GeoData/GeoData.php |
— | — | @@ -15,6 +15,9 @@ |
16 | 16 | |
17 | 17 | $wgAutoloadClasses['ApiQueryCoordinates'] = "$dir/api/ApiQueryCoordinates.php"; |
18 | 18 | $wgAutoloadClasses['ApiQueryGeoSearch'] = "$dir/api/ApiQueryGeoSearch.php"; |
| 19 | +$wgAutoloadClasses['ApiQueryAllPages_GeoData'] = "$dir/api/ApiQueryAllPages_GeoData.php"; |
| 20 | +$wgAutoloadClasses['ApiQueryCategoryMembers_GeoData'] = "$dir/api/ApiQueryCategoryMembers_GeoData.php"; |
| 21 | +$wgAutoloadClasses['GeoDataQueryExtender'] = "$dir/api/GeoDataQueryExtender.php"; |
19 | 22 | $wgAutoloadClasses['Coord'] = "$dir/GeoData.body.php"; |
20 | 23 | $wgAutoloadClasses['CoordinatesParserFunction'] = "$dir/CoordinatesParserFunction.php"; |
21 | 24 | $wgAutoloadClasses['GeoData'] = "$dir/GeoData.body.php"; |
— | — | @@ -28,6 +31,10 @@ |
29 | 32 | $wgAPIListModules['geosearch'] = 'ApiQueryGeoSearch'; |
30 | 33 | $wgAPIPropModules['coordinates'] = 'ApiQueryCoordinates'; |
31 | 34 | |
| 35 | +// overriding core |
| 36 | +$wgAPIListModules['allpages'] = 'ApiQueryAllPages_GeoData'; |
| 37 | +$wgAPIListModules['categorymembers'] = 'ApiQueryCategoryMembers_GeoData'; |
| 38 | + |
32 | 39 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'GeoDataHooks::onLoadExtensionSchemaUpdates'; |
33 | 40 | $wgHooks['ParserFirstCallInit'][] = 'GeoDataHooks::onParserFirstCallInit'; |
34 | 41 | $wgHooks['UnitTestsList'][] = 'GeoDataHooks::onUnitTestsList'; |