Yurik has submitted this change and it was merged.
Change subject: Added marker coutners (integer and alpha)
......................................................................
Added marker coutners (integer and alpha)
GeoJSON can now contain 'counter' and 'letter' properties,
with the value being any group name. This allows
for markers to be auto-numbered with integers (1-99) and letters (A-Z),
and have multiple marker counters at the same time.
Change-Id: I228162bbd73d8779a54e0f15e0cc5de8872533af
---
M Kartographer.hooks.php
1 file changed, 88 insertions(+), 2 deletions(-)
Approvals:
Yurik: Verified; Looks good to me, approved
jenkins-bot: Verified
diff --git a/Kartographer.hooks.php b/Kartographer.hooks.php
index 0719dec..28ad587 100644
--- a/Kartographer.hooks.php
+++ b/Kartographer.hooks.php
@@ -107,12 +107,14 @@
}
// Merge existing data with the new tag's data under the same
group name
+ $counter = false;
if ( $value ) {
if ( !$group ) {
// If it's not mode=data, the tag's data is
private for this tag only
$group = '_' . sha1( FormatJson::encode(
$value, false, FormatJson::ALL_OK ) );
}
$data = $output->getExtensionData( 'kartographer_data'
) ?: new stdClass();
+ $counter = self::processCounters( $value, $data );
if ( isset( $data->$group ) ) {
$data->$group = array_merge( $data->$group,
$value );
} else {
@@ -173,6 +175,10 @@
}
$output->setExtensionData(
'kartographer_interact', true );
$html = Html::rawElement( 'div', $attrs );
+ break;
+
+ case 'data':
+ $html = $counter === false ? '' : $counter;
break;
}
$output->setExtensionData( 'kartographer_valid', true );
@@ -251,8 +257,10 @@
private static function validateContent( $status ) {
$value = $status->getValue();
- // The content must be a non-associative array of values
- if ( !is_array( $value ) && !( $value instanceof stdClass ) ) {
+ // The content must be a non-associative array of values or an
object
+ if ( $value instanceof stdClass ) {
+ $value = array ( $value );
+ } elseif ( !is_array( $value ) ) {
$status->fatal( 'kartographer-error-bad_data' );
return false;
}
@@ -272,4 +280,82 @@
}
return true;
}
+
+ /**
+ *
+ * @param mixed $values
+ * @param stdClass $data
+ * @return bool|int|string
+ */
+ private static function processCounters( $values, $data ) {
+ if ( !property_exists( $data, 'counters' ) ) {
+ $numCounters = new stdClass();
+ $alphaCounters = new stdClass();
+ $data->counters = (object) array( 'numeric' =>
$numCounters, 'alpha' => $alphaCounters );
+ } else {
+ $numCounters = $data->counters->numeric;
+ $alphaCounters = $data->counters->alpha;
+ }
+ return self::processItemList( $values, $numCounters,
$alphaCounters );
+ }
+
+ /**
+ * @param $values
+ * @param $numCounters
+ * @param $alphaCounters
+ * @return bool|string
+ */
+ private static function processItemList( $values, $numCounters,
$alphaCounters ) {
+ $firstMarker = false;
+ if ( !is_array( $values ) ) {
+ return $firstMarker;
+ }
+ foreach ( $values as $item ) {
+ if ( property_exists( $item, 'properties' ) ) {
+ $props = $item->properties;
+ if ( property_exists( $props, 'counter' ) ) {
+ $grp = $props->counter;
+ unset( $props->counter );
+ $count =
+ property_exists( $numCounters,
$grp ) ? min( $numCounters->$grp + 1, 99 )
+ : 1;
+ $marker = strval( $count );
+ $numCounters->$grp = $count;
+ } elseif ( property_exists( $props, 'letter' )
) {
+ $grp = $props->letter;
+ unset( $props->letter );
+ $count =
+ property_exists(
$alphaCounters, $grp ) ? min( $alphaCounters->$grp + 1,
+ 26 ) : 1;
+ $marker = chr( ord( 'a' ) + $count - 1
); // letters a..z
+ $alphaCounters->$grp = $count;
+ } else {
+ $marker = false;
+ }
+
+ if ( $marker !== false ) {
+ $props->{'marker-symbol'} = $marker;
+ if ( $firstMarker === false ) {
+ $firstMarker = $marker;
+ }
+ }
+ }
+ if ( !property_exists( $item, 'type' ) ) {
+ continue;
+ }
+ $type = $item->type;
+ if ( $type === 'FeatureCollection' && property_exists(
$item, 'features' ) ) {
+ $tmp = self::processItemList( $item->features,
$numCounters, $alphaCounters );
+ if ( $firstMarker === false ) {
+ $firstMarker = $tmp;
+ }
+ } elseif ( $type === 'GeometryCollection' &&
property_exists( $item, 'geometries' ) ) {
+ $tmp = self::processItemList(
$item->geometries, $numCounters, $alphaCounters );
+ if ( $firstMarker === false ) {
+ $firstMarker = $tmp;
+ }
+ }
+ }
+ return $firstMarker;
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/262859
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I228162bbd73d8779a54e0f15e0cc5de8872533af
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Kartographer
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits