jenkins-bot has submitted this change and it was merged. Change subject: Groups support, link layer fix ......................................................................
Groups support, link layer fix * Implemented groups as described in https://www.mediawiki.org/w/index.php?title=Extension:Kartographer&oldid=2070870#Groups * <maplink> now includes extra layers Change-Id: I9d1060fe6b88421f640bf4ebd17770d3821ebccc --- M includes/Tag/MapFrame.php M includes/Tag/MapLink.php M includes/Tag/TagHandler.php M modules/kartographer.js M tests/parserTests.txt M tests/phpunit/KartographerTest.php 6 files changed, 55 insertions(+), 41 deletions(-) Approvals: MaxSem: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/Tag/MapFrame.php b/includes/Tag/MapFrame.php index e00727a..09da3ca 100644 --- a/includes/Tag/MapFrame.php +++ b/includes/Tag/MapFrame.php @@ -51,10 +51,10 @@ ); $dataParam = ''; - $groups = $this->groups; - if ( $groups ) { - array_unshift( $groups, $this->parser->getTitle()->getPrefixedDBkey() ); - $dataParam = '?data=' . implode( '|', array_map( 'rawurlencode', $groups ) ); + $showGroups = $this->showGroups; + if ( $showGroups ) { + array_unshift( $showGroups, $this->parser->getTitle()->getPrefixedDBkey() ); + $dataParam = '?data=' . implode( '|', array_map( 'rawurlencode', $showGroups ) ); } $imgAttrs = array( 'src' => $statParams . '.jpeg' . $dataParam, @@ -86,8 +86,8 @@ $attrs['data-zoom'] = $this->zoom; $attrs['data-lat'] = $this->lat; $attrs['data-lon'] = $this->lon; - if ( $this->groups ) { - $attrs['data-overlays'] = FormatJson::encode( $this->groups, false, + if ( $this->showGroups ) { + $attrs['data-overlays'] = FormatJson::encode( $this->showGroups, false, FormatJson::ALL_OK ); } $this->parser->getOutput()->setExtensionData( 'kartographer_interact', true ); diff --git a/includes/Tag/MapLink.php b/includes/Tag/MapLink.php index 834365a..d92b679 100644 --- a/includes/Tag/MapLink.php +++ b/includes/Tag/MapLink.php @@ -2,6 +2,7 @@ namespace Kartographer\Tag; +use FormatJson; use Html; /** @@ -29,6 +30,10 @@ $attrs['data-zoom'] = $this->zoom; $attrs['data-lat'] = $this->lat; $attrs['data-lon'] = $this->lon; + if ( $this->showGroups ) { + $attrs['data-overlays'] = FormatJson::encode( $this->showGroups, false, + FormatJson::ALL_OK ); + } return Html::rawElement( 'a', $attrs, $text ); } diff --git a/includes/Tag/TagHandler.php b/includes/Tag/TagHandler.php index 1d2d976..2e75022 100644 --- a/includes/Tag/TagHandler.php +++ b/includes/Tag/TagHandler.php @@ -48,8 +48,11 @@ /** @var string */ protected $style; - /** @var string[] */ - protected $groups; + /** @var string name of the group, or null for private */ + protected $groupName; + + /** @var string[] list of groups to show */ + protected $showGroups = []; /** @var string[] */ protected $defaultAttributes; @@ -167,8 +170,21 @@ } private function parseGroups() { - $text = $this->getText( 'group', '*', '/^[a-zA-Z0-9]+(\s*,\s*[a-zA-Z0-9]+)*$/' ); - $this->groups = array_map( 'trim', explode( ',', $text ) ); + $this->groupName = $this->getText( 'group', null, '/^[a-zA-Z0-9]+$/' ); + + $text = $this->getText( 'show', null, '/^[a-zA-Z0-9]+(\s*,\s*[a-zA-Z0-9]+)*$/' ); + if ( $text !== null ) { + $this->showGroups = array_map( 'trim', explode( ',', $text ) ); + } + + // Make sure the current group is shown for this map, even if there is no geojson + // Private group will be added during the save, as it requires hash calculation + if ( $this->groupName !== null ) { + $this->showGroups[] = $this->groupName; + } + + // Make sure there are no group name duplicates + $this->showGroups = array_unique( $this->showGroups ); } protected function getInt( $name, $default = false ) { @@ -232,9 +248,15 @@ $this->counter = $this->doCountersRecursive( $this->geometries, $counters ); $output->setExtensionData( 'kartographer_counters', $counters ); + if ( $this->groupName === null ) { + $group = '_' . sha1( FormatJson::encode( $this->geometries, false, FormatJson::ALL_OK ) ); + $this->groupName = $group; + $this->showGroups[] = $group; + // no need to array_unique() because it's impossible to manually add a private group + } else { + $group = $this->groupName; + } - //$group = '_' . sha1( FormatJson::encode( $this->geometries, false, FormatJson::ALL_OK ) ); - $group = '*'; $data = $output->getExtensionData( 'kartographer_data' ) ?: new stdClass(); if ( isset( $data->$group ) ) { $data->$group = array_merge( $data->$group, $this->geometries ); diff --git a/modules/kartographer.js b/modules/kartographer.js index 5c6deee..a0eb7b7 100644 --- a/modules/kartographer.js +++ b/modules/kartographer.js @@ -29,10 +29,6 @@ return brackets[ brackets.length - 1 ]; } - function isPrivateGroup( groupName ) { - return groupName[ 0 ] === '_'; - } - scale = bracketDevicePixelRatio(); scale = ( scale === 1 ) ? '' : ( '@' + scale + 'x' ); urlFormat = '/{z}/{x}/{y}' + scale + '.png'; @@ -108,19 +104,10 @@ if ( data.overlays ) { $.each( data.overlays, function ( index, group ) { - if ( group === '*' ) { - $.each( mapData, function ( k, d ) { - if ( !isPrivateGroup( k ) ) { - mw.kartographer.addDataLayer( map, d ); - } - } ); - } else if ( mapData.hasOwnProperty( group ) ) { - if ( index + 1 === data.overlays.length ) { - map.kartographerLayer = - mw.kartographer.addDataLayer( map, mapData[ group ] ); - } else { - mw.kartographer.addDataLayer( map, mapData[ group ] ); - } + if ( mapData.hasOwnProperty( group ) ) { + mw.kartographer.addDataLayer( map, mapData[ group ] ); + } else { + mw.log( 'Layer not found "' + group + '"' ); } } ); } diff --git a/tests/parserTests.txt b/tests/parserTests.txt index 8d1713f..2d886ac 100644 --- a/tests/parserTests.txt +++ b/tests/parserTests.txt @@ -71,10 +71,10 @@ } </maplink> !! 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">Foo</a> -</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20">2</a> -</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20">a</a> -</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20">b</a> +<p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["_b34053f88dea58fe70a7da09c558f792394f6e11"]">Foo</a> +</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["_2e4133dc429eb3c3abf4e45916c073e841f9d193"]">2</a> +</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["_dd3c7bae539f7c3747c64d043c97e1a8834fbc8f"]">a</a> +</p><p><a class="mw-kartographer mw-kartographer-link" mw-data="interface" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["_8ce73f7395e427bd6f462824717c0a214b3ca564"]">b</a> </p> !! end @@ -86,10 +86,10 @@ <mapframe latitude=10 longitude=20 zoom=13 width=640 height=480 align=center/> <mapframe latitude=10 longitude=20 zoom=13 width=640 height=480 align=right/> !! result -<div class="mw-kartographer mw-kartographer-interactive floatright" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["*"]"></div> -<div class="mw-kartographer mw-kartographer-interactive floatleft" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["*"]"></div> -<div class="mw-kartographer mw-kartographer-interactive center" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["*"]"></div> -<div class="mw-kartographer mw-kartographer-interactive floatright" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20" data-overlays="["*"]"></div> +<div class="mw-kartographer mw-kartographer-interactive floatright" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20"></div> +<div class="mw-kartographer mw-kartographer-interactive floatleft" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20"></div> +<div class="mw-kartographer mw-kartographer-interactive center" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20"></div> +<div class="mw-kartographer mw-kartographer-interactive floatright" mw-data="interface" style="width:640px; height:480px;" data-style="osm-intl" data-zoom="13" data-lat="10" data-lon="20"></div> !! end @@ -111,4 +111,4 @@ <li> Attribute "align" has an invalid value</li></ul> </div> -!! end \ No newline at end of file +!! end diff --git a/tests/phpunit/KartographerTest.php b/tests/phpunit/KartographerTest.php index 90301b3..d5f5e10 100644 --- a/tests/phpunit/KartographerTest.php +++ b/tests/phpunit/KartographerTest.php @@ -68,7 +68,7 @@ "marker-color": "0050d0" } }'; - $wikitextJsonParsed = '{"*":[ + $wikitextJsonParsed = '{"_b3a06246589b01ce9e9c2ba3dc97e265f7ea0308":[ {"type":"Feature","geometry":{"type":"Point","coordinates":[-122.3988,37.8013]}, "properties":{"title":"<script>alert(document.cookie);<\/script>", "description":"<a href=\"\/w\/index.php?title=Link_to_nowhere&action=edit&redlink=1\" class=\"new\" title=\"Link to nowhere (page does not exist)\">Link to nowhere<\/a>"}} @@ -78,8 +78,8 @@ [ 'null', '<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013></mapframe>', '<mapframe> without JSON 2' ], //[ false, '<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013 mode=interactive>123</mapframe>', 'Invalid JSON' ], //[ false, '<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013 mode=interactive>{{"":""}}</maps>', 'Invalid JSON 3' ], - [ "{\"*\":[$validJson]}", "<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013>$validJson</mapframe>", '<mapframe> with GeoJSON' ], - [ "{\"*\":[$validJson]}", "<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013>[$validJson]</mapframe>", '<mapframe> with GeoJSON array' ], + [ "{\"_bc2671e0e7a829e9d19c743d6701fa410dd04827\":[$validJson]}", "<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013>$validJson</mapframe>", '<mapframe> with GeoJSON' ], + [ "{\"_bc2671e0e7a829e9d19c743d6701fa410dd04827\":[$validJson]}", "<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013>[$validJson]</mapframe>", '<mapframe> with GeoJSON array' ], [ $wikitextJsonParsed, "<mapframe width=700 height=400 zoom=13 longitude=-122.3988 latitude=37.8013>[{$this->wikitextJson}]</mapframe>", '<mapframe> with parsable text and description' ], [ $wikitextJsonParsed, "<maplink zoom=13 longitude=-122.3988 latitude=37.8013>[{$this->wikitextJson}]</maplink>", '<maplink> with parsable text and description' ], // Bugs -- To view, visit https://gerrit.wikimedia.org/r/275205 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9d1060fe6b88421f640bf4ebd17770d3821ebccc Gerrit-PatchSet: 7 Gerrit-Project: mediawiki/extensions/Kartographer Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits