Yurik has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/276885

Change subject: Add coordinate formatter
......................................................................

Add coordinate formatter

Use it in <maplink> when no text or markers are specified.
Localisation has been discussed with Amir.

Bug: T129210
Change-Id: I51bf65bffef703e58e16adc80bba507d8e862be8
---
M extension.json
M i18n/en.json
M i18n/qqq.json
A includes/CoordFormatter.php
M includes/Tag/MapLink.php
M tests/parserTests.txt
A tests/phpunit/CoordFormatterTest.php
7 files changed, 120 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Kartographer 
refs/changes/85/276885/1

diff --git a/extension.json b/extension.json
index 661a6f9..0f87744 100644
--- a/extension.json
+++ b/extension.json
@@ -13,6 +13,7 @@
                ]
        },
        "AutoloadClasses": {
+               "Kartographer\\CoordFormatter": "includes/CoordFormatter.php",
                "Kartographer\\DataModule": "includes/DataModule.php",
                "Kartographer\\Hooks": "includes/Hooks.php",
                "Kartographer\\SimpleStyleSanitizer": 
"includes/SimpleStyleSanitizer.php",
diff --git a/i18n/en.json b/i18n/en.json
index 9ef0f9d..78e245c 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -15,6 +15,12 @@
        "kartographer-error-bad_data": "The JSON content is not correct",
        "kartographer-tracking-category": "Pages with maps",
        "kartographer-tracking-category-desc": "The page includes a map",
+       "kartographer-coord-combined": "$1 $2",
+       "kartographer-coord-dms": "$1°$2′$3″",
+       "kartographer-coord-lat-positive": "$1N",
+       "kartographer-coord-lat-negative": "$1S",
+       "kartographer-coord-lon-positive": "$1E",
+       "kartographer-coord-lon-negative": "$1W",
        "leafletdraw-draw-handlers-circle-radius": "Radius",
        "leafletdraw-draw-handlers-circle-tooltip-start": "Click and drag to 
draw a circle",
        "leafletdraw-draw-handlers-marker-tooltip-start": "Click map to place a 
marker",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 8795ace..892a1f1 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -17,6 +17,12 @@
        "kartographer-error-bad_data": "This error is shown if the JSON content 
of the tag does not pass validation",
        "kartographer-tracking-category": "Name of the tracking category",
        "kartographer-tracking-category-desc": "Description on 
[[Special:TrackingCategories]] for the 
{{msg-mw|kartographer-tracking-category}} tracking category.",
+       "kartographer-coord-combined": "{{optional}}\nJoins two parts of 
geogrpahical coordinates. $1 and $2 are latitude and longitude, respectively.",
+       "kartographer-coord-dms": "{{optional}}\nFormats geographical latitude 
or longitude into degrees, munutes and seconds.\nParameters:\n* $1 - degrees\n* 
$2 - minutes\n* $3 - seconds",
+       "kartographer-coord-lat-positive": "{{optional}}\nNorth latitude suffix 
for geographical coordinates. $1 is value in degrees/minutes/seconds, see 
{{msg-mw|kartographer-coord-dms}}",
+       "kartographer-coord-lat-negative": "{{optional}}\nSouth latitude suffix 
for geographical coordinates. $1 is value in degrees/minutes/seconds, see 
{{msg-mw|kartographer-coord-dms}}",
+       "kartographer-coord-lon-positive": "{{optional}}\nEast longitude suffix 
for geographical coordinates. $1 is value in degrees/minutes/seconds, see 
{{msg-mw|kartographer-coord-dms}}",
+       "kartographer-coord-lon-negative": "{{optional}}\nWest longitude suffix 
for geographical coordinates. $1 is value in degrees/minutes/seconds, see 
{{msg-mw|kartographer-coord-dms}}",
        "leafletdraw-draw-handlers-circle-radius": "Label for circle 
radius\n{{Identical|Radius}}",
        "leafletdraw-draw-handlers-circle-tooltip-start": "Tooltip for how to 
start drawing a circle",
        "leafletdraw-draw-handlers-marker-tooltip-start": "Tooltip for how to 
place a marker",
diff --git a/includes/CoordFormatter.php b/includes/CoordFormatter.php
new file mode 100644
index 0000000..accd102
--- /dev/null
+++ b/includes/CoordFormatter.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Kartographer;
+
+use Language;
+
+/**
+ * Formats coordinates into human-readable strings
+ */
+class CoordFormatter {
+       /**
+        * Formats coordinates
+        *
+        * @param float $lat
+        * @param float $lon
+        * @param Language $language
+        * @return string
+        */
+       public static function format( $lat, $lon, Language $language ) {
+               $latStr = self::formatOneCoord( $lat, 'lat', $language );
+               $lonStr = self::formatOneCoord( $lon, 'lon', $language );
+
+               return wfMessage( 'kartographer-coord-combined' )
+                       ->params( $latStr, $lonStr )
+                       ->inLanguage( $language )
+                       ->plain();
+       }
+
+       /**
+        * @param float $coord
+        * @param string $latLon 'lat' or 'lon'
+        * @param Language $language
+        * @return string
+        */
+       private static function formatOneCoord( $coord, $latLon, Language 
$language ) {
+               $val = $sign = round( $coord * 3600 );
+               $val = abs( $val );
+               $degrees = floor( $val / 3600 );
+               $minutes = floor( ( $val - $degrees * 3600 ) / 60 );
+               $seconds = $val - $degrees * 3600 - $minutes * 60;
+               $plusMinus = $sign < 0 ? 'negative' : 'positive';
+               $text = wfMessage( 'kartographer-coord-dms' )
+                       ->numParams( $degrees, $minutes, round( $seconds ) )
+                       ->inLanguage( $language )
+                       ->plain();
+
+               return wfMessage( "kartographer-coord-$latLon-$plusMinus" )
+                       ->params( $text )
+                       ->inLanguage( $language )
+                       ->plain();
+       }
+}
diff --git a/includes/Tag/MapLink.php b/includes/Tag/MapLink.php
index d92b679..21793a3 100644
--- a/includes/Tag/MapLink.php
+++ b/includes/Tag/MapLink.php
@@ -4,6 +4,7 @@
 
 use FormatJson;
 use Html;
+use Kartographer\CoordFormatter;
 
 /**
  * The <maplink> tag creates a link that, when clicked,
@@ -20,10 +21,11 @@
                if ( is_numeric( $counter ) ) {
                        $counter = 
$this->parser->getTargetLanguage()->formatNum( $counter );
                }
-               $text = $this->parser->recursiveTagParse(
-                       $this->getText( 'text', $counter, '/\S+/' ),
-                       $this->frame
-               );
+               $text = $this->getText( 'text', null, '/\S+/' );
+               if ( $text === null ) {
+                       $text = $counter ?: CoordFormatter::format( $this->lat, 
$this->lon, $this->language );
+               }
+               $text = $this->parser->recursiveTagParse( $text, $this->frame );
                $attrs = $this->defaultAttributes;
                $attrs['class'] .= ' mw-kartographer-link';
                $attrs['data-style'] = $this->style;
diff --git a/tests/parserTests.txt b/tests/parserTests.txt
index 14de241..486a893 100644
--- a/tests/parserTests.txt
+++ b/tests/parserTests.txt
@@ -79,6 +79,16 @@
 !! end
 
 !! test
+<maplink> - autolabelling with coordinates
+!! input
+<maplink latitude=10 longitude=20 zoom=13 />
+!! result
+<p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" 
data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20">10°0′0″N 
20°0′0″E</a>
+</p>
+!! end
+
+
+!! test
 <mapframe>
 !! input
 <mapframe latitude=10 longitude=20 zoom=13 width=640 height=480/>
diff --git a/tests/phpunit/CoordFormatterTest.php 
b/tests/phpunit/CoordFormatterTest.php
new file mode 100644
index 0000000..d7e52b0
--- /dev/null
+++ b/tests/phpunit/CoordFormatterTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Kartographer\Tests;
+
+use Kartographer\CoordFormatter;
+use Language;
+use MediaWikiTestCase;
+
+/**
+ * @group Kartographer
+ */
+class CoordFormatterTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider provideFormatter
+        *
+        * @param $expected
+        * @param $lat
+        * @param $lon
+        */
+       public function testFormatter( $expected, $lat, $lon ) {
+               $lang = Language::factory( 'en' );
+               $result = CoordFormatter::format( $lat, $lon, $lang );
+               $this->assertEquals( $expected, $result );
+       }
+
+       public function provideFormatter() {
+               return [
+                       [ '0°0′0″N 0°0′0″E', 0, 0 ],
+                       [ '0°0′0″N 0°0′0″E', -0.000000000001, 0.000000000001 ],
+                       [ '1°0′0″S 1°0′0″W', -1, -1 ],
+                       [ '1°0′0″N 11°0′0″W', 0.999999999999, -10.999999999999 
],
+                       [ '10°20′0″N 0°0′0″E', 10.333333333333333333, 0 ],
+                       [ '10°12′0″N 0°0′0″E', 10.2, 0 ], // Was getting 
10°11′60″N 0°0′0″E here
+                       [ '45°30′0″N 20°0′36″E', 45.5, 20.01 ],
+                       [ '27°46′23″N 82°38′24″W', 27.773056, -82.64 ],
+                       [ '29°57′31″N 90°3′54″W', 29.958611, -90.065 ],
+               ];
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/276885
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I51bf65bffef703e58e16adc80bba507d8e862be8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Kartographer
Gerrit-Branch: wmf/1.27.0-wmf.16
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to