MaxSem has uploaded a new change for review. https://gerrit.wikimedia.org/r/306315
Change subject: WIP: static map page ...................................................................... WIP: static map page Change-Id: I90fcd2fb0e458c9d5a805b441f5114157ea470a4 --- A Kartographer.alias.php M extension.json A includes/SpecialMap.php M includes/Tag/MapLink.php A styles/images/COPYING A styles/images/earth.jpg A styles/specialMap.less A tests/phpunit/SpecialMapTest.php 8 files changed, 189 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Kartographer refs/changes/15/306315/1 diff --git a/Kartographer.alias.php b/Kartographer.alias.php new file mode 100644 index 0000000..0a6b39d --- /dev/null +++ b/Kartographer.alias.php @@ -0,0 +1,15 @@ +<?php +/** + * Aliases for special pages for extension Echo + * + * @file + * @ingroup Extensions + */ +// @codingStandardsIgnoreFile + +$specialPageAliases = array(); + +/** English (English) */ +$specialPageAliases['en'] = [ + 'Map' => 'Map', +]; diff --git a/extension.json b/extension.json index b978281..da1fc58 100644 --- a/extension.json +++ b/extension.json @@ -16,6 +16,12 @@ "modules/wikivoyage/i18n" ] }, + "ExtensionMessagesFiles": { + "KartogrpaherAliases": "Kartographer.alias.php" + }, + "SpecialPages": { + "Map": "Kartographer\\SpecialMap" + }, "AutoloadClasses": { "Kartographer\\ApiQueryMapData": "includes/ApiQueryMapData.php", "Kartographer\\ApiSanitizeMapData": "includes/ApiSanitizeMapData.php", @@ -23,6 +29,7 @@ "Kartographer\\DataModule": "includes/DataModule.php", "Kartographer\\Hooks": "includes/Hooks.php", "Kartographer\\SimpleStyleParser": "includes/SimpleStyleParser.php", + "Kartographer\\SpecialMap": "includes/SpecialMap.php", "Kartographer\\State": "includes/State.php", "Kartographer\\Tag\\MapFrame": "includes/Tag/MapFrame.php", "Kartographer\\Tag\\MapLink": "includes/Tag/MapLink.php", @@ -323,6 +330,15 @@ "mobile", "desktop" ] + }, + "ext.kartographer.specialMap": { + "styles": [ + "styles/specialMap.less" + ], + "targets": [ + "mobile", + "desktop" + ] } }, "ResourceFileModulePaths": { diff --git a/includes/SpecialMap.php b/includes/SpecialMap.php new file mode 100644 index 0000000..4ec69e1 --- /dev/null +++ b/includes/SpecialMap.php @@ -0,0 +1,66 @@ +<?php + +namespace Kartographer; + +use GeoData\Globe; +use Html; +use SpecialPage; + +class SpecialMap extends SpecialPage { + public function __construct( $name = 'Map' ) { + parent::__construct( $name, /* $restriction */ '', /* $listed */ false ); + } + + public function execute( $par ) { + $this->setHeaders(); + $this->getOutput()->addModuleStyles( 'ext.kartographer.specialMap' ); + + $coord = self::parseSubpage( $par ); + if ( !$coord ) { + $coordText = 'Bogus coordinates!'; + $markerHtml = ''; + } else { + list( , $lat, $lon ) = $coord; + $coordText = CoordFormatter::format( $lat, $lon, $this->getLanguage() ); + $hLat = intval( $lat * 2 ) + 180; + $hLon = intval( $lon * 2 ) + 360; + $markerHtml = Html::element( 'div', + [ + 'id' => 'mw-specialMap-marker', + 'style' => "position: absolute; left:{$hLat}px; top:{$hLon}px; font-size: 20px; color: yellow;" + ], + '●' + ); + } + + $this->getOutput()->addHTML( + Html::openElement( 'div', [ 'id' => 'mw-specialMap-outer' ] ) + . Html::element( 'span', [], $coordText ) + . Html::openElement( 'div', [ 'id' => 'mw-specialMap-inner' ] ) + . Html::element( 'div', [ 'id' => 'mw-specialMap-map' ] ) + . $markerHtml + . Html::closeElement( 'div' ) + . Html::closeElement( 'div' ) + ); + } + + public static function parseSubpage( $par ) { + if ( !preg_match( '#^(?<zoom>\d+)/(?<lat>-?\d+(\.\d+)?)/(?<lon>-?\d+(\.\d+)?)$#', $par, $matches ) ) { + return false; + } + + if ( class_exists( Globe::class ) ) { + $globe = new Globe( 'earth' ); + + if ( !$globe->coordinatesAreValid( $matches['lat'], $matches['lon'] ) ) { + return false; + } + } + + return [ (int)$matches['zoom'], (float)$matches['lat'], (float)$matches['lon'] ]; + } + + public static function link( $lat, $lon, $zoom ) { + return SpecialPage::getTitleFor( 'Map', "$zoom/$lat/$lon" ); + } +} diff --git a/includes/Tag/MapLink.php b/includes/Tag/MapLink.php index f11c667..a55a5ca 100644 --- a/includes/Tag/MapLink.php +++ b/includes/Tag/MapLink.php @@ -5,6 +5,7 @@ use FormatJson; use Html; use Kartographer\CoordFormatter; +use Kartographer\SpecialMap; /** * The <maplink> tag creates a link that, when clicked, @@ -35,6 +36,7 @@ 'class' => 'mw-kartographer-maplink', 'mw-data' => 'interface', 'data-style' => $this->mapStyle, + 'href' => SpecialMap::link( $this->lat, $this->lon, $this->zoom )->getLocalURL() ]; if ( $this->zoom !== null ) { $attrs['data-zoom'] = $this->zoom; diff --git a/styles/images/COPYING b/styles/images/COPYING new file mode 100644 index 0000000..4496b87 --- /dev/null +++ b/styles/images/COPYING @@ -0,0 +1,2 @@ +== earth.jpg == +Public domain image from https://commons.wikimedia.org/wiki/File:Earthmap1000x500.jpg diff --git a/styles/images/earth.jpg b/styles/images/earth.jpg new file mode 100644 index 0000000..2c2a476 --- /dev/null +++ b/styles/images/earth.jpg Binary files differ diff --git a/styles/specialMap.less b/styles/specialMap.less new file mode 100644 index 0000000..72f9dfd --- /dev/null +++ b/styles/specialMap.less @@ -0,0 +1,26 @@ +div#mw-specialMap-outer { + display: block; + margin-left: auto; + margin-right: auto; + + span { + text-align: center; + } +} + +div#mw-specialMap-inner { + width: 720px; + height: 360px; + position: absolute; +} + +div#mw-specialMap-map { + position: absolute; + width: 720px; + height: 360px; + background-image: url(images/earth.jpg); +} + +div#mw-specialMap-marker { + position: relative; +} diff --git a/tests/phpunit/SpecialMapTest.php b/tests/phpunit/SpecialMapTest.php new file mode 100644 index 0000000..1675086 --- /dev/null +++ b/tests/phpunit/SpecialMapTest.php @@ -0,0 +1,62 @@ +<?php + +namespace Kartographer\Tests; + +use Kartographer\SpecialMap; +use MediaWikiTestCase; +use Title; + +/** + * @group Kartographer + */ +class SpecialMapTest extends MediaWikiTestCase { + + /** + * @dataProvider provideParseSubpage + * + * @param string $par + * @param float $expectedLat + * @param float $expectedLon + */ + public function testParseSubpage( $par, $expectedLat = null, $expectedLon = null ) { + $res = SpecialMap::parseSubpage( $par ); + if ( $expectedLat === null || $expectedLon === null ) { + $this->assertFalse( $res, 'Parsing is expected to fail' ); + } else { + list( , $lat, $lon ) = $res; + $this->assertSame( $expectedLat, $lat, 'Comparing latitudes' ); + $this->assertSame( $expectedLon, $lon, 'Comparing longitudes' ); + } + } + + public function provideParseSubpage() { + return [ + [ '' ], + [ 'foo' ], + [ 'foo/bar/baz' ], + [ '123' ], + [ '1/2' ], + [ '1/2/3/4' ], + [ '1.0/2/3' ], + [ '-1/2/3' ], + [ '1/2/.3' ], + [ '1/2/3.45e+6' ], + [ '1/2,3/4,5' ], + // Invalid coordinates + [ '0/90.000001/10' ], + [ '0/10/180.000000001' ], + + [ '0/0/-0', 0.0, 0.0 ], + [ '12/-34.56/0.78', -34.56, 0.78 ], + [ '18/89.9/179.9', 89.9, 179.9 ], + [ '18/-89.9/-179.9', -89.9, -179.9 ], + [ '18/90/-180', 90.0, -180.0 ], + ]; + } + + public function testLink() { + $title = SpecialMap::link( 12, -34.5, 6 ); + $this->assertType( Title::class, $title ); + $this->assertEquals( '/wiki/Special:Map/6/12/-34.5', $title->getLocalURL() ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/306315 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I90fcd2fb0e458c9d5a805b441f5114157ea470a4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Kartographer Gerrit-Branch: master Gerrit-Owner: MaxSem <maxsem.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits