r109940 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109939‎ | r109940 | r109941 >
Date:19:16, 24 January 2012
Author:maxsem
Status:resolved
Tags:api 
Comment:
Support for enumerating pages with or without coordinates, in specific category or not. Can of worms: it overides core API modules:)
Modified paths:
  • /trunk/extensions/GeoData/GeoData.php (modified) (history)
  • /trunk/extensions/GeoData/api/ApiQueryAllPages_GeoData.php (added) (history)
  • /trunk/extensions/GeoData/api/ApiQueryCategoryMembers_GeoData.php (added) (history)
  • /trunk/extensions/GeoData/api/GeoDataQueryExtender.php (added) (history)

Diff [purge]

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
169 + 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
154 + 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
154 + native
Index: trunk/extensions/GeoData/GeoData.php
@@ -15,6 +15,9 @@
1616
1717 $wgAutoloadClasses['ApiQueryCoordinates'] = "$dir/api/ApiQueryCoordinates.php";
1818 $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";
1922 $wgAutoloadClasses['Coord'] = "$dir/GeoData.body.php";
2023 $wgAutoloadClasses['CoordinatesParserFunction'] = "$dir/CoordinatesParserFunction.php";
2124 $wgAutoloadClasses['GeoData'] = "$dir/GeoData.body.php";
@@ -28,6 +31,10 @@
2932 $wgAPIListModules['geosearch'] = 'ApiQueryGeoSearch';
3033 $wgAPIPropModules['coordinates'] = 'ApiQueryCoordinates';
3134
 35+// overriding core
 36+$wgAPIListModules['allpages'] = 'ApiQueryAllPages_GeoData';
 37+$wgAPIListModules['categorymembers'] = 'ApiQueryCategoryMembers_GeoData';
 38+
3239 $wgHooks['LoadExtensionSchemaUpdates'][] = 'GeoDataHooks::onLoadExtensionSchemaUpdates';
3340 $wgHooks['ParserFirstCallInit'][] = 'GeoDataHooks::onParserFirstCallInit';
3441 $wgHooks['UnitTestsList'][] = 'GeoDataHooks::onUnitTestsList';

Follow-up revisions

RevisionCommit summaryAuthorDate
r109947Follow-up r109940: instead of overriding core modules, add the extended modul...maxsem20:24, 24 January 2012

Status & tagging log