r109924 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109923‎ | r109924 | r109925 >
Date:11:34, 24 January 2012
Author:maxsem
Status:ok
Tags:geodata 
Comment:
Moving API modules to their own subdirectory in preparation to adding more
Modified paths:
  • /trunk/extensions/GeoData/ApiQueryCoordinates.php (deleted) (history)
  • /trunk/extensions/GeoData/ApiQueryGeoSearch.php (deleted) (history)
  • /trunk/extensions/GeoData/GeoData.php (modified) (history)
  • /trunk/extensions/GeoData/api (added) (history)
  • /trunk/extensions/GeoData/api/ApiQueryCoordinates.php (added) (history)
  • /trunk/extensions/GeoData/api/ApiQueryGeoSearch.php (added) (history)

Diff [purge]

Index: trunk/extensions/GeoData/ApiQueryCoordinates.php
@@ -1,135 +0,0 @@
2 -<?php
3 -/**
4 - * This query adds an <coordinates> subelement to all pages with the list of coordinated present on those pages.
5 - */
6 -class ApiQueryCoordinates extends ApiQueryBase {
7 -
8 - public function __construct( $query, $moduleName ) {
9 - parent::__construct( $query, $moduleName, 'co' );
10 - }
11 -
12 - public function execute() {
13 - $titles = $this->getPageSet()->getGoodTitles();
14 - if ( count( $titles ) == 0 ) {
15 - return;
16 - }
17 -
18 - $params = $this->extractRequestParams();
19 - $this->addTables( 'geo_tags' );
20 - $this->addFields( array( 'gt_id', 'gt_page_id', 'gt_lat', 'gt_lon', 'gt_primary' ) );
21 - foreach( $params['prop'] as $prop ) {
22 - if ( isset( Coord::$fieldMapping[$prop] ) ) {
23 - $this->addFields( Coord::$fieldMapping[$prop] );
24 - }
25 - }
26 - $this->addWhereFld( 'gt_page_id', array_keys( $titles ) );
27 - $primary = array_flip( $params['primary'] );
28 - $this->addWhereIf( array( 'gt_primary' => 1 ), isset( $primary['yes'] ) && !isset( $primary['no'] ) );
29 - $this->addWhereIf( array( 'gt_primary' => 0 ), !isset( $primary['yes'] ) && isset( $primary['no'] ) );
30 -
31 - if ( isset( $params['continue'] ) ) {
32 - $parts = explode( '|', $params['continue'] );
33 - if ( count( $parts ) != 2 || !is_numeric( $parts[0] ) || !is_numeric( $parts[0] ) ) {
34 - $this->dieUsage( "Invalid continue parameter. You should pass the " .
35 - "original value returned by the previous query", "_badcontinue" );
36 - }
37 - $parts[0] = intval( $parts[0] );
38 - $parts[1] = intval( $parts[1] );
39 - $this->addWhere( "gt_page_id > {$parts[0]} OR ( gt_page_id = {$parts[0]} AND gt_id > {$parts[1]} )" );
40 - }
41 -
42 - $this->addOption( 'ORDER BY', array( 'gt_page_id', 'gt_id' ) );
43 - $this->addOption( 'LIMIT', $params['limit'] + 1 );
44 -
45 - $res = $this->select( __METHOD__ );
46 -
47 - $count = 0;
48 - foreach ( $res as $row ) {
49 - if ( ++$count > $params['limit'] ) {
50 - $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
51 - break;
52 - }
53 - $vals = array(
54 - 'lat' => floatval( $row->gt_lat ),
55 - 'lon' => floatval( $row->gt_lon ),
56 - );
57 - if ( $row->gt_primary ) {
58 - $vals['primary'] = '';
59 - }
60 - foreach( $params['prop'] as $prop ) {
61 - if ( isset( Coord::$fieldMapping[$prop] ) && isset( $row->{Coord::$fieldMapping[$prop]} ) ) {
62 - $field = Coord::$fieldMapping[$prop];
63 - $vals[$prop] = $row->$field;
64 - }
65 - }
66 - $fit = $this->addPageSubItem( $row->gt_page_id, $vals );
67 - if ( !$fit ) {
68 - $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
69 - }
70 - }
71 - }
72 -
73 - public function getCacheMode( $params ) {
74 - return 'public';
75 - }
76 -
77 - public function getAllowedParams() {
78 - return array(
79 - 'limit' => array(
80 - ApiBase::PARAM_DFLT => 10,
81 - ApiBase::PARAM_TYPE => 'limit',
82 - ApiBase::PARAM_MIN => 1,
83 - ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
84 - ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
85 - ),
86 - 'continue' => array(
87 - ApiBase::PARAM_TYPE => 'string',
88 - ),
89 - 'prop' => array(
90 - ApiBase::PARAM_TYPE => array( 'type', 'name', 'dim', 'country', 'region' ),
91 - ApiBase::PARAM_DFLT => '',
92 - ApiBase::PARAM_ISMULTI => true,
93 - ),
94 - 'primary' => array(
95 - ApiBase::PARAM_TYPE => array( 'yes', 'no' ),
96 - ApiBase::PARAM_ISMULTI => true,
97 - ApiBase::PARAM_DFLT => 'yes',
98 - ),
99 - );
100 - }
101 -
102 - public function getParamDescription() {
103 - return array(
104 - 'limit' => 'How many coordinates to return',
105 - 'continue' => 'When more results are available, use this to continue',
106 - 'prop' => 'What additional coordinate properties to return',
107 - 'primary' => "Whether to return only primary coordinates (``yes''), secondary (``no'') or both (``yes|no'')",
108 - );
109 - }
110 -
111 - public function getDescription() {
112 - return 'Returns coordinates of the given page(s)';
113 - }
114 -
115 - public function getPossibleErrors() {
116 - return array_merge( parent::getPossibleErrors(), array(
117 - array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
118 - ) );
119 - }
120 -
121 - public function getExamples() {
122 - return array(
123 - 'Get a list of coordinates of the [[Main Page]]:',
124 - ' api.php?action=query&prop=coordinates&titles=Main%20Page',
125 - );
126 - }
127 -
128 -
129 - public function getHelpUrls() {
130 - return 'https://www.mediawiki.org/wiki/Extension:GeoData#prop.3Dcoordinates';
131 - }
132 -
133 - public function getVersion() {
134 - return __CLASS__ . ': $Id$';
135 - }
136 -}
Index: trunk/extensions/GeoData/ApiQueryGeoSearch.php
@@ -1,221 +0,0 @@
2 -<?php
3 -
4 -class ApiQueryGeoSearch extends ApiQueryGeneratorBase {
5 - const MIN_RADIUS = 10;
6 -
7 - public function __construct( $query, $moduleName ) {
8 - parent::__construct( $query, $moduleName, 'gs' );
9 - }
10 -
11 - public function execute() {
12 - $this->run();
13 - }
14 -
15 - public function getCacheMode( $params ) {
16 - return 'public';
17 - }
18 -
19 - public function executeGenerator( $resultPageSet ) {
20 - $this->run( $resultPageSet );
21 - }
22 -
23 - /**
24 - * @param $resultPageSet ApiPageSet
25 - * @return
26 - */
27 - private function run( $resultPageSet = null ) {
28 - $params = $this->extractRequestParams();
29 - $exclude = false;
30 -
31 - $this->requireOnlyOneParameter( $params, 'coord', 'page' );
32 - if ( isset( $params['coord'] ) ) {
33 - $arr = explode( '|', $params['coord'] );
34 - if ( count( $arr ) != 2 || !GeoData::validateCoord( $arr[0], $arr[1], $params['globe'] ) ) {
35 - $this->dieUsage( 'Invalid coordinate provided', '_invalid-coord' );
36 - }
37 - $lat = $arr[0];
38 - $lon = $arr[1];
39 - } elseif ( isset( $params['page'] ) ) {
40 - $t = Title::newFromText( $params['page'] );
41 - if ( !$t || !$t->canExist() ) {
42 - $this->dieUsage( "Invalid page title ``{$params['page']}'' provided", '_invalid-page' );
43 - }
44 - if ( !$t->exists() ) {
45 - $this->dieUsage( "Page ``{$params['page']}'' does not exist", '_nonexistent-page' );
46 - }
47 - $coord = GeoData::getPageCoordinates( $t );
48 - if ( !$coord ) {
49 - $this->dieUsage( 'Page coordinates unknown', '_no-coordinates' );
50 - }
51 - $lat = $coord->lat;
52 - $lon = $coord->lon;
53 - $exclude = $t->getArticleID();
54 - }
55 - $lat = floatval( $lat );
56 - $lon = floatval( $lon );
57 - $radius = intval( $params['radius'] );
58 - $rect = GeoMath::rectAround( $lat, $lon, $radius );
59 -
60 - $dbr = wfGetDB( DB_SLAVE );
61 - $this->addTables( array( 'geo_tags', 'page' ) );
62 - $this->addFields( array( 'gt_lat', 'gt_lon', 'gt_primary',
63 - "{$dbr->tablePrefix()}gd_distance( {$lat}, {$lon}, gt_lat, gt_lon ) AS dist" )
64 - );
65 - // retrieve some fields only if page set needs them
66 - if ( is_null( $resultPageSet ) ) {
67 - $this->addFields( array( 'page_id', 'page_namespace', 'page_title' ) );
68 - } else {
69 - $this->addFields( array( "{$dbr->tableName( 'page' )}.*" ) );
70 - }
71 - foreach( $params['prop'] as $prop ) {
72 - if ( isset( Coord::$fieldMapping[$prop] ) ) {
73 - $this->addFields( Coord::$fieldMapping[$prop] );
74 - }
75 - }
76 - $this->addWhereFld( 'gt_globe', $params['globe'] );
77 - $this->addWhereRange( 'gt_lat', 'newer', $rect["minLat"], $rect["maxLat"], false );
78 - $this->addWhereRange( 'gt_lon', 'newer', $rect["minLon"], $rect["maxLon"], false );
79 - //$this->addWhere( 'dist < ' . intval( $radius ) ); hasta be in HAVING, not WHERE
80 - $this->addWhereFld( 'page_namespace', $params['namespace'] );
81 - $this->addWhere( 'gt_page_id = page_id' );
82 - if ( $exclude ) {
83 - $this->addWhere( "gt_page_id <> {$exclude}" );
84 - }
85 - if ( isset( $params['maxdim'] ) ) {
86 - $this->addWhere( 'gt_dim < ' . intval( $params['maxdim'] ) );
87 - }
88 - $primary = array_flip( $params['primary'] );
89 - $this->addWhereIf( array( 'gt_primary' => 1 ), isset( $primary['yes'] ) && !isset( $primary['no'] ) );
90 - $this->addWhereIf( array( 'gt_primary' => 0 ), !isset( $primary['yes'] ) && isset( $primary['no'] ) );
91 - $this->addOption( 'ORDER BY', 'dist' );
92 -
93 - $limit = $params['limit'];
94 - $this->addOption( 'LIMIT', $limit );
95 -
96 - $res = $this->select( __METHOD__ );
97 -
98 - $result = $this->getResult();
99 - foreach ( $res as $row ) {
100 - if ( is_null( $resultPageSet ) ) {
101 - $title = Title::newFromRow( $row );
102 - $vals = array(
103 - 'pageid' => intval( $row->page_id ),
104 - 'ns' => intval( $title->getNamespace() ),
105 - 'title' => $title->getPrefixedText(),
106 - 'lat' => floatval( $row->gt_lat ),
107 - 'lon' => floatval( $row->gt_lon ),
108 - 'dist' => round( $row->dist, 1 ),
109 - );
110 - if ( $row->gt_primary ) {
111 - $vals['primary'] = '';
112 - }
113 - foreach( $params['prop'] as $prop ) {
114 - if ( isset( Coord::$fieldMapping[$prop] ) && isset( $row->{Coord::$fieldMapping[$prop]} ) ) {
115 - $field = Coord::$fieldMapping[$prop];
116 - $vals[$prop] = $row->$field;
117 - }
118 - }
119 - $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
120 - if ( !$fit ) {
121 - break;
122 - }
123 - } else {
124 - $resultPageSet->processDbRow( $row );
125 - }
126 - }
127 - if ( is_null( $resultPageSet ) ) {
128 - $result->setIndexedTagName_internal(
129 - array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
130 - }
131 - }
132 -
133 - public function getAllowedParams() {
134 - global $wgMaxGeoSearchRadius, $wgDefaultGlobe;
135 - return array (
136 - 'coord' => array(
137 - ApiBase::PARAM_TYPE => 'string',
138 - ),
139 - 'page' => array(
140 - ApiBase::PARAM_TYPE => 'string',
141 - ),
142 - 'radius' => array(
143 - ApiBase::PARAM_TYPE => 'integer',
144 - ApiBase::PARAM_REQUIRED => true,
145 - ApiBase::PARAM_MIN => self::MIN_RADIUS,
146 - ApiBase::PARAM_MAX => $wgMaxGeoSearchRadius,
147 - ApiBase::PARAM_RANGE_ENFORCE => true,
148 - ),
149 - 'maxdim' => array(
150 - ApiBase::PARAM_TYPE => 'integer',
151 - ),
152 - 'limit' => array(
153 - ApiBase::PARAM_DFLT => 10,
154 - ApiBase::PARAM_TYPE => 'limit',
155 - ApiBase::PARAM_MIN => 1,
156 - ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
157 - ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
158 - ),
159 - 'globe' => array(
160 - ApiBase::PARAM_TYPE => 'string',
161 - ApiBase::PARAM_DFLT => $wgDefaultGlobe,
162 - ),
163 - 'namespace' => array(
164 - ApiBase::PARAM_TYPE => 'namespace',
165 - ApiBase::PARAM_DFLT => NS_MAIN,
166 - ApiBase::PARAM_ISMULTI => true,
167 - ),
168 - 'prop' => array(
169 - ApiBase::PARAM_TYPE => array( 'type', 'name', 'dim', 'country', 'region' ),
170 - ApiBase::PARAM_DFLT => '',
171 - ApiBase::PARAM_ISMULTI => true,
172 - ),
173 - 'primary' => array(
174 - ApiBase::PARAM_TYPE => array( 'yes', 'no' ),
175 - ApiBase::PARAM_ISMULTI => true,
176 - ApiBase::PARAM_DFLT => 'yes',
177 - ),
178 - );
179 - }
180 -
181 - public function getParamDescription() {
182 - global $wgDefaultGlobe;
183 - return array(
184 - 'coord' => 'Coordinate around which to search: two floating-point values separated by pipe (|)',
185 - 'page' => 'Title of page around which to search',
186 - 'radius' => 'Search radius in meters',
187 - 'maxdim' => 'Restrict search to objects no larger than this, in meters',
188 - 'limit' => 'Maximum number of pages to return',
189 - 'globe' => "Globe to search on (by default ``{$wgDefaultGlobe}'')",
190 - 'namespace' => 'Namespace(s) to search',
191 - 'prop' => 'What additional coordinate properties to return',
192 - 'primary' => "Whether to return only primary coordinates (``yes''), secondary (``no'') or both (``yes|no'')",
193 - );
194 - }
195 -
196 - public function getDescription() {
197 - return 'Returns pages around the given point';
198 - }
199 -
200 - public function getPossibleErrors() {
201 - return array_merge( parent::getPossibleErrors(), array(
202 - array( 'code' => '_invalid-page', 'info' => "Invalid page title provided" ),
203 - array( 'code' => '_nonexistent-page', 'info' => "Page does not exist" ),
204 - array( 'code' => '_no-coordinates', 'info' => 'Page coordinates unknown' ),
205 - ) );//@todo:
206 - }
207 -
208 - public function getExamples() {
209 - return array(
210 - "api.php?action=query&list=geosearch&gsradius=10000&gscoord=37.786971|-122.399677" =>
211 - "Search around the point with coordinates 37° 47′ 13.1″ N, 122° 23′ 58.84″ W",
212 - );
213 - }
214 -
215 - public function getHelpUrls() {
216 - return 'https://www.mediawiki.org/wiki/Extension:GeoData#list.3Dgeosearch';
217 - }
218 -
219 - public function getVersion() {
220 - return __CLASS__ . ': $Id$';
221 - }
222 -}
Index: trunk/extensions/GeoData/api/ApiQueryCoordinates.php
@@ -0,0 +1,135 @@
 2+<?php
 3+/**
 4+ * This query adds an <coordinates> subelement to all pages with the list of coordinated present on those pages.
 5+ */
 6+class ApiQueryCoordinates extends ApiQueryBase {
 7+
 8+ public function __construct( $query, $moduleName ) {
 9+ parent::__construct( $query, $moduleName, 'co' );
 10+ }
 11+
 12+ public function execute() {
 13+ $titles = $this->getPageSet()->getGoodTitles();
 14+ if ( count( $titles ) == 0 ) {
 15+ return;
 16+ }
 17+
 18+ $params = $this->extractRequestParams();
 19+ $this->addTables( 'geo_tags' );
 20+ $this->addFields( array( 'gt_id', 'gt_page_id', 'gt_lat', 'gt_lon', 'gt_primary' ) );
 21+ foreach( $params['prop'] as $prop ) {
 22+ if ( isset( Coord::$fieldMapping[$prop] ) ) {
 23+ $this->addFields( Coord::$fieldMapping[$prop] );
 24+ }
 25+ }
 26+ $this->addWhereFld( 'gt_page_id', array_keys( $titles ) );
 27+ $primary = array_flip( $params['primary'] );
 28+ $this->addWhereIf( array( 'gt_primary' => 1 ), isset( $primary['yes'] ) && !isset( $primary['no'] ) );
 29+ $this->addWhereIf( array( 'gt_primary' => 0 ), !isset( $primary['yes'] ) && isset( $primary['no'] ) );
 30+
 31+ if ( isset( $params['continue'] ) ) {
 32+ $parts = explode( '|', $params['continue'] );
 33+ if ( count( $parts ) != 2 || !is_numeric( $parts[0] ) || !is_numeric( $parts[0] ) ) {
 34+ $this->dieUsage( "Invalid continue parameter. You should pass the " .
 35+ "original value returned by the previous query", "_badcontinue" );
 36+ }
 37+ $parts[0] = intval( $parts[0] );
 38+ $parts[1] = intval( $parts[1] );
 39+ $this->addWhere( "gt_page_id > {$parts[0]} OR ( gt_page_id = {$parts[0]} AND gt_id > {$parts[1]} )" );
 40+ }
 41+
 42+ $this->addOption( 'ORDER BY', array( 'gt_page_id', 'gt_id' ) );
 43+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 44+
 45+ $res = $this->select( __METHOD__ );
 46+
 47+ $count = 0;
 48+ foreach ( $res as $row ) {
 49+ if ( ++$count > $params['limit'] ) {
 50+ $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
 51+ break;
 52+ }
 53+ $vals = array(
 54+ 'lat' => floatval( $row->gt_lat ),
 55+ 'lon' => floatval( $row->gt_lon ),
 56+ );
 57+ if ( $row->gt_primary ) {
 58+ $vals['primary'] = '';
 59+ }
 60+ foreach( $params['prop'] as $prop ) {
 61+ if ( isset( Coord::$fieldMapping[$prop] ) && isset( $row->{Coord::$fieldMapping[$prop]} ) ) {
 62+ $field = Coord::$fieldMapping[$prop];
 63+ $vals[$prop] = $row->$field;
 64+ }
 65+ }
 66+ $fit = $this->addPageSubItem( $row->gt_page_id, $vals );
 67+ if ( !$fit ) {
 68+ $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
 69+ }
 70+ }
 71+ }
 72+
 73+ public function getCacheMode( $params ) {
 74+ return 'public';
 75+ }
 76+
 77+ public function getAllowedParams() {
 78+ return array(
 79+ 'limit' => array(
 80+ ApiBase::PARAM_DFLT => 10,
 81+ ApiBase::PARAM_TYPE => 'limit',
 82+ ApiBase::PARAM_MIN => 1,
 83+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 84+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 85+ ),
 86+ 'continue' => array(
 87+ ApiBase::PARAM_TYPE => 'string',
 88+ ),
 89+ 'prop' => array(
 90+ ApiBase::PARAM_TYPE => array( 'type', 'name', 'dim', 'country', 'region' ),
 91+ ApiBase::PARAM_DFLT => '',
 92+ ApiBase::PARAM_ISMULTI => true,
 93+ ),
 94+ 'primary' => array(
 95+ ApiBase::PARAM_TYPE => array( 'yes', 'no' ),
 96+ ApiBase::PARAM_ISMULTI => true,
 97+ ApiBase::PARAM_DFLT => 'yes',
 98+ ),
 99+ );
 100+ }
 101+
 102+ public function getParamDescription() {
 103+ return array(
 104+ 'limit' => 'How many coordinates to return',
 105+ 'continue' => 'When more results are available, use this to continue',
 106+ 'prop' => 'What additional coordinate properties to return',
 107+ 'primary' => "Whether to return only primary coordinates (``yes''), secondary (``no'') or both (``yes|no'')",
 108+ );
 109+ }
 110+
 111+ public function getDescription() {
 112+ return 'Returns coordinates of the given page(s)';
 113+ }
 114+
 115+ public function getPossibleErrors() {
 116+ return array_merge( parent::getPossibleErrors(), array(
 117+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 118+ ) );
 119+ }
 120+
 121+ public function getExamples() {
 122+ return array(
 123+ 'Get a list of coordinates of the [[Main Page]]:',
 124+ ' api.php?action=query&prop=coordinates&titles=Main%20Page',
 125+ );
 126+ }
 127+
 128+
 129+ public function getHelpUrls() {
 130+ return 'https://www.mediawiki.org/wiki/Extension:GeoData#prop.3Dcoordinates';
 131+ }
 132+
 133+ public function getVersion() {
 134+ return __CLASS__ . ': $Id$';
 135+ }
 136+}
Property changes on: trunk/extensions/GeoData/api/ApiQueryCoordinates.php
___________________________________________________________________
Added: svn:keywords
1137 + Id
Added: svn:eol-style
2138 + native
Index: trunk/extensions/GeoData/api/ApiQueryGeoSearch.php
@@ -0,0 +1,221 @@
 2+<?php
 3+
 4+class ApiQueryGeoSearch extends ApiQueryGeneratorBase {
 5+ const MIN_RADIUS = 10;
 6+
 7+ public function __construct( $query, $moduleName ) {
 8+ parent::__construct( $query, $moduleName, 'gs' );
 9+ }
 10+
 11+ public function execute() {
 12+ $this->run();
 13+ }
 14+
 15+ public function getCacheMode( $params ) {
 16+ return 'public';
 17+ }
 18+
 19+ public function executeGenerator( $resultPageSet ) {
 20+ $this->run( $resultPageSet );
 21+ }
 22+
 23+ /**
 24+ * @param $resultPageSet ApiPageSet
 25+ * @return
 26+ */
 27+ private function run( $resultPageSet = null ) {
 28+ $params = $this->extractRequestParams();
 29+ $exclude = false;
 30+
 31+ $this->requireOnlyOneParameter( $params, 'coord', 'page' );
 32+ if ( isset( $params['coord'] ) ) {
 33+ $arr = explode( '|', $params['coord'] );
 34+ if ( count( $arr ) != 2 || !GeoData::validateCoord( $arr[0], $arr[1], $params['globe'] ) ) {
 35+ $this->dieUsage( 'Invalid coordinate provided', '_invalid-coord' );
 36+ }
 37+ $lat = $arr[0];
 38+ $lon = $arr[1];
 39+ } elseif ( isset( $params['page'] ) ) {
 40+ $t = Title::newFromText( $params['page'] );
 41+ if ( !$t || !$t->canExist() ) {
 42+ $this->dieUsage( "Invalid page title ``{$params['page']}'' provided", '_invalid-page' );
 43+ }
 44+ if ( !$t->exists() ) {
 45+ $this->dieUsage( "Page ``{$params['page']}'' does not exist", '_nonexistent-page' );
 46+ }
 47+ $coord = GeoData::getPageCoordinates( $t );
 48+ if ( !$coord ) {
 49+ $this->dieUsage( 'Page coordinates unknown', '_no-coordinates' );
 50+ }
 51+ $lat = $coord->lat;
 52+ $lon = $coord->lon;
 53+ $exclude = $t->getArticleID();
 54+ }
 55+ $lat = floatval( $lat );
 56+ $lon = floatval( $lon );
 57+ $radius = intval( $params['radius'] );
 58+ $rect = GeoMath::rectAround( $lat, $lon, $radius );
 59+
 60+ $dbr = wfGetDB( DB_SLAVE );
 61+ $this->addTables( array( 'geo_tags', 'page' ) );
 62+ $this->addFields( array( 'gt_lat', 'gt_lon', 'gt_primary',
 63+ "{$dbr->tablePrefix()}gd_distance( {$lat}, {$lon}, gt_lat, gt_lon ) AS dist" )
 64+ );
 65+ // retrieve some fields only if page set needs them
 66+ if ( is_null( $resultPageSet ) ) {
 67+ $this->addFields( array( 'page_id', 'page_namespace', 'page_title' ) );
 68+ } else {
 69+ $this->addFields( array( "{$dbr->tableName( 'page' )}.*" ) );
 70+ }
 71+ foreach( $params['prop'] as $prop ) {
 72+ if ( isset( Coord::$fieldMapping[$prop] ) ) {
 73+ $this->addFields( Coord::$fieldMapping[$prop] );
 74+ }
 75+ }
 76+ $this->addWhereFld( 'gt_globe', $params['globe'] );
 77+ $this->addWhereRange( 'gt_lat', 'newer', $rect["minLat"], $rect["maxLat"], false );
 78+ $this->addWhereRange( 'gt_lon', 'newer', $rect["minLon"], $rect["maxLon"], false );
 79+ //$this->addWhere( 'dist < ' . intval( $radius ) ); hasta be in HAVING, not WHERE
 80+ $this->addWhereFld( 'page_namespace', $params['namespace'] );
 81+ $this->addWhere( 'gt_page_id = page_id' );
 82+ if ( $exclude ) {
 83+ $this->addWhere( "gt_page_id <> {$exclude}" );
 84+ }
 85+ if ( isset( $params['maxdim'] ) ) {
 86+ $this->addWhere( 'gt_dim < ' . intval( $params['maxdim'] ) );
 87+ }
 88+ $primary = array_flip( $params['primary'] );
 89+ $this->addWhereIf( array( 'gt_primary' => 1 ), isset( $primary['yes'] ) && !isset( $primary['no'] ) );
 90+ $this->addWhereIf( array( 'gt_primary' => 0 ), !isset( $primary['yes'] ) && isset( $primary['no'] ) );
 91+ $this->addOption( 'ORDER BY', 'dist' );
 92+
 93+ $limit = $params['limit'];
 94+ $this->addOption( 'LIMIT', $limit );
 95+
 96+ $res = $this->select( __METHOD__ );
 97+
 98+ $result = $this->getResult();
 99+ foreach ( $res as $row ) {
 100+ if ( is_null( $resultPageSet ) ) {
 101+ $title = Title::newFromRow( $row );
 102+ $vals = array(
 103+ 'pageid' => intval( $row->page_id ),
 104+ 'ns' => intval( $title->getNamespace() ),
 105+ 'title' => $title->getPrefixedText(),
 106+ 'lat' => floatval( $row->gt_lat ),
 107+ 'lon' => floatval( $row->gt_lon ),
 108+ 'dist' => round( $row->dist, 1 ),
 109+ );
 110+ if ( $row->gt_primary ) {
 111+ $vals['primary'] = '';
 112+ }
 113+ foreach( $params['prop'] as $prop ) {
 114+ if ( isset( Coord::$fieldMapping[$prop] ) && isset( $row->{Coord::$fieldMapping[$prop]} ) ) {
 115+ $field = Coord::$fieldMapping[$prop];
 116+ $vals[$prop] = $row->$field;
 117+ }
 118+ }
 119+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
 120+ if ( !$fit ) {
 121+ break;
 122+ }
 123+ } else {
 124+ $resultPageSet->processDbRow( $row );
 125+ }
 126+ }
 127+ if ( is_null( $resultPageSet ) ) {
 128+ $result->setIndexedTagName_internal(
 129+ array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
 130+ }
 131+ }
 132+
 133+ public function getAllowedParams() {
 134+ global $wgMaxGeoSearchRadius, $wgDefaultGlobe;
 135+ return array (
 136+ 'coord' => array(
 137+ ApiBase::PARAM_TYPE => 'string',
 138+ ),
 139+ 'page' => array(
 140+ ApiBase::PARAM_TYPE => 'string',
 141+ ),
 142+ 'radius' => array(
 143+ ApiBase::PARAM_TYPE => 'integer',
 144+ ApiBase::PARAM_REQUIRED => true,
 145+ ApiBase::PARAM_MIN => self::MIN_RADIUS,
 146+ ApiBase::PARAM_MAX => $wgMaxGeoSearchRadius,
 147+ ApiBase::PARAM_RANGE_ENFORCE => true,
 148+ ),
 149+ 'maxdim' => array(
 150+ ApiBase::PARAM_TYPE => 'integer',
 151+ ),
 152+ 'limit' => array(
 153+ ApiBase::PARAM_DFLT => 10,
 154+ ApiBase::PARAM_TYPE => 'limit',
 155+ ApiBase::PARAM_MIN => 1,
 156+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 157+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 158+ ),
 159+ 'globe' => array(
 160+ ApiBase::PARAM_TYPE => 'string',
 161+ ApiBase::PARAM_DFLT => $wgDefaultGlobe,
 162+ ),
 163+ 'namespace' => array(
 164+ ApiBase::PARAM_TYPE => 'namespace',
 165+ ApiBase::PARAM_DFLT => NS_MAIN,
 166+ ApiBase::PARAM_ISMULTI => true,
 167+ ),
 168+ 'prop' => array(
 169+ ApiBase::PARAM_TYPE => array( 'type', 'name', 'dim', 'country', 'region' ),
 170+ ApiBase::PARAM_DFLT => '',
 171+ ApiBase::PARAM_ISMULTI => true,
 172+ ),
 173+ 'primary' => array(
 174+ ApiBase::PARAM_TYPE => array( 'yes', 'no' ),
 175+ ApiBase::PARAM_ISMULTI => true,
 176+ ApiBase::PARAM_DFLT => 'yes',
 177+ ),
 178+ );
 179+ }
 180+
 181+ public function getParamDescription() {
 182+ global $wgDefaultGlobe;
 183+ return array(
 184+ 'coord' => 'Coordinate around which to search: two floating-point values separated by pipe (|)',
 185+ 'page' => 'Title of page around which to search',
 186+ 'radius' => 'Search radius in meters',
 187+ 'maxdim' => 'Restrict search to objects no larger than this, in meters',
 188+ 'limit' => 'Maximum number of pages to return',
 189+ 'globe' => "Globe to search on (by default ``{$wgDefaultGlobe}'')",
 190+ 'namespace' => 'Namespace(s) to search',
 191+ 'prop' => 'What additional coordinate properties to return',
 192+ 'primary' => "Whether to return only primary coordinates (``yes''), secondary (``no'') or both (``yes|no'')",
 193+ );
 194+ }
 195+
 196+ public function getDescription() {
 197+ return 'Returns pages around the given point';
 198+ }
 199+
 200+ public function getPossibleErrors() {
 201+ return array_merge( parent::getPossibleErrors(), array(
 202+ array( 'code' => '_invalid-page', 'info' => "Invalid page title provided" ),
 203+ array( 'code' => '_nonexistent-page', 'info' => "Page does not exist" ),
 204+ array( 'code' => '_no-coordinates', 'info' => 'Page coordinates unknown' ),
 205+ ) );//@todo:
 206+ }
 207+
 208+ public function getExamples() {
 209+ return array(
 210+ "api.php?action=query&list=geosearch&gsradius=10000&gscoord=37.786971|-122.399677" =>
 211+ "Search around the point with coordinates 37° 47′ 13.1″ N, 122° 23′ 58.84″ W",
 212+ );
 213+ }
 214+
 215+ public function getHelpUrls() {
 216+ return 'https://www.mediawiki.org/wiki/Extension:GeoData#list.3Dgeosearch';
 217+ }
 218+
 219+ public function getVersion() {
 220+ return __CLASS__ . ': $Id$';
 221+ }
 222+}
Property changes on: trunk/extensions/GeoData/api/ApiQueryGeoSearch.php
___________________________________________________________________
Added: svn:keywords
1223 + Id
Added: svn:eol-style
2224 + native
Index: trunk/extensions/GeoData/GeoData.php
@@ -13,8 +13,8 @@
1414
1515 $dir = dirname( __FILE__ );
1616
17 -$wgAutoloadClasses['ApiQueryCoordinates'] = "$dir/ApiQueryCoordinates.php";
18 -$wgAutoloadClasses['ApiQueryGeoSearch'] = "$dir/ApiQueryGeoSearch.php";
 17+$wgAutoloadClasses['ApiQueryCoordinates'] = "$dir/api/ApiQueryCoordinates.php";
 18+$wgAutoloadClasses['ApiQueryGeoSearch'] = "$dir/api/ApiQueryGeoSearch.php";
1919 $wgAutoloadClasses['Coord'] = "$dir/GeoData.body.php";
2020 $wgAutoloadClasses['CoordinatesParserFunction'] = "$dir/CoordinatesParserFunction.php";
2121 $wgAutoloadClasses['GeoData'] = "$dir/GeoData.body.php";

Status & tagging log