r108819 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108818‎ | r108819 | r108820 >
Date:17:00, 13 January 2012
Author:maxsem
Status:deferred (Comments)
Tags:geodata 
Comment:
Fixed dim handling, made it fall back to scale and type
Modified paths:
  • /trunk/extensions/GeoData/CoordinatesParserFunction.php (modified) (history)
  • /trunk/extensions/GeoData/GeoData.php (modified) (history)
  • /trunk/extensions/GeoData/tests/TagTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GeoData/tests/TagTest.php
@@ -4,6 +4,10 @@
55 * @group GeoData
66 */
77 class TagTest extends MediaWikiTestCase {
 8+ public function setUp() {
 9+ $GLOBALS['wgDefaultDim'] = 1000; // reset to default
 10+ }
 11+
812 /**
913 * @dataProvider getData
1014 */
@@ -27,6 +31,7 @@
2832
2933 public function getData() {
3034 return array(
 35+ // Basics
3136 array(
3237 '{{#coordinates: 10|20|primary}}',
3338 array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'primary' => true ),
@@ -39,6 +44,7 @@
4045 '{{#coordinates: primary|10|20}}',
4146 array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'primary' => true ),
4247 ),
 48+ // type
4349 array(
4450 '{{#coordinates: 10|20|type:city}}',
4551 array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'type' => 'city' ),
@@ -47,26 +53,56 @@
4854 '{{#coordinates: 10|20|type:city(666)}}',
4955 array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'type' => 'city' ),
5056 ),
 57+ // Other geohack params
 58+ array(
 59+ '{{#coordinates: 10|20}}',
 60+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 1000 ),
 61+ ),
5162 array(
5263 '{{#coordinates:10|20|globe:Moon dim:10_region:RU-mos}}',
53 - array( 'lat' => 10, 'lon' => 20, 'globe' => 'moon', 'country' => 'RU', 'region' => 'MOS' ),
 64+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'moon', 'country' => 'RU', 'region' => 'MOS', 'dim' => 10 ),
5465 ),
5566 array(
5667 '{{#coordinates:10|20|globe:Moon dim:10_region:RU}}',
57 - array( 'lat' => 10, 'lon' => 20, 'globe' => 'moon', 'country' => 'RU' ),
 68+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'moon', 'country' => 'RU', 'dim' => 10 ),
5869 ),
5970 array(
60 - '{{#coordinates: 10|20|primary|_dim:3Km_}}',
61 - array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'primary' => true, 'dim' => 3000 ),
 71+ '{{#coordinates: 10|20|_dim:3Km_}}',
 72+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 3000 ),
6273 ),
6374 array(
64 - '{{#coordinates: 10|20|primary|foo:bar dim:100m}}',
 75+ '{{#coordinates: 10|20|foo:bar dim:100m}}',
6576 array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 100 ),
6677 ),
6778 array(
68 - '{{#coordinates: 10|20|primary|dim:1L}}',
69 - array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth' ),
 79+ '{{#coordinates: 10|20|dim:-300}}',
 80+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 1000 ),
7081 ),
 82+ array(
 83+ '{{#coordinates: 10|20|dim:-10km}}',
 84+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 1000 ),
 85+ ),
 86+ array(
 87+ '{{#coordinates: 10|20|dim:1L}}',
 88+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 1000 ),
 89+ ),
 90+ // dim fallbacks
 91+ array(
 92+ '{{#coordinates: 10|20|type:city}}',
 93+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'type' => 'city', 'dim' => 10000 ),
 94+ ),
 95+ array(
 96+ '{{#coordinates: 10|20|type:city(2000)}}',
 97+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'type' => 'city', 'dim' => 10000 ),
 98+ ),
 99+ array(
 100+ '{{#coordinates: 10|20|type:lulz}}',
 101+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'type' => 'lulz', 'dim' => 1000 ),
 102+ ),
 103+ array(
 104+ '{{#coordinates: 10|20|scale:50000}}',
 105+ array( 'lat' => 10, 'lon' => 20, 'globe' => 'earth', 'dim' => 5000 ),
 106+ ),
71107 );
72108 }
73109 }
\ No newline at end of file
Index: trunk/extensions/GeoData/CoordinatesParserFunction.php
@@ -129,7 +129,7 @@
130130 * @param Coord $coord
131131 */
132132 private function parseTagArgs( Coord $coord ) {
133 - global $wgDefaultGlobe, $wgContLang;
 133+ global $wgDefaultGlobe, $wgContLang, $wgTypeToDim, $wgDefaultDim;
134134 $result = Status::newGood();
135135 $args = $this->named;
136136 // fear not of overwriting the stuff we've just received from the geohack param, it has minimum precedence
@@ -138,15 +138,20 @@
139139 }
140140 $coord->primary = isset( $args['primary'] );
141141 $coord->globe = isset( $args['globe'] ) ? $wgContLang->lc( $args['globe'] ) : $wgDefaultGlobe;
 142+ $coord->dim = $wgDefaultDim;
 143+ if ( isset( $args['type'] ) ) {
 144+ $coord->type = preg_replace( '/\(.*?\).*$/', '', $args['type'] );
 145+ $coord->dim = isset( $wgTypeToDim[$coord->type] ) ? isset( $wgTypeToDim[$coord->type] ) : $wgDefaultDim;
 146+ }
 147+ if ( isset( $args['scale'] ) ) {
 148+ $coord->dim = $args['scale'] / 10;
 149+ }
142150 if ( isset( $args['dim'] ) ) {
143151 $dim = $this->parseDim( $args['dim'] );
144 - if ( $dim !== '' ) {
 152+ if ( $dim !== false ) {
145153 $coord->dim = $dim;
146154 }
147155 }
148 - if ( isset( $args['type'] ) ) {
149 - $coord->type = preg_replace( '/\(.*?\).*$/', '', $args['type'] );
150 - }
151156 $coord->name = isset( $args['name'] ) ? $args['name'] : null;
152157 if ( isset( $args['region'] ) ) {
153158 $code = strtoupper( $args['region'] );
@@ -176,7 +181,7 @@
177182
178183 private function parseDim( $str ) {
179184 if ( is_numeric( $str ) ) {
180 - return $str > 0;
 185+ return $str > 0 ? $str : false;
181186 }
182187 if ( !preg_match( '/^(\d+)(km|m)$/i', $str, $m ) ) {
183188 return false;
Index: trunk/extensions/GeoData/GeoData.php
@@ -51,3 +51,34 @@
5252 * Maximum number of coordinates per page, -1 means no limit
5353 */
5454 $wgMaxCoordinatesPerPage = 500;
 55+
 56+/**
 57+ * Conversion table type --> dim
 58+ */
 59+$wgTypeToDim = array(
 60+ 'country' => 1000000,
 61+ 'satellite' => 1000000,
 62+ 'state' => 300000,
 63+ 'adm1st' => 100000,
 64+ 'adm2nd' => 30000,
 65+ 'adm3rd' => 10000,
 66+ 'city' => 10000,
 67+ 'isle' => 10000,
 68+ 'mountain' => 10000,
 69+ 'river' => 10000,
 70+ 'waterbody' => 10000,
 71+ 'event' => 5000,
 72+ 'forest' => 5000,
 73+ 'glacier' => 5000,
 74+ 'airport' => 3000,
 75+ 'railwaystation' => 1000,
 76+ 'edu' => 1000,
 77+ 'pass' => 1000,
 78+ 'camera' => 1000,
 79+ 'landmark' => 1000,
 80+);
 81+
 82+/**
 83+ * Default value of dim if it is unknown
 84+ */
 85+$wgDefaultDim = 1000;

Comments

#Comment by Nikerabbit (talk | contribs)   17:04, 13 January 2012

Are these dimensions translatable?

#Comment by MaxSem (talk | contribs)   17:06, 13 January 2012

This parser function reuses stuff intended for geohack, and geohack doesn't localise anything - thus, parameters will not be localised unless we write an in-MediaWiki replacement for geohack.

Status & tagging log