Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/353974 )

Change subject: Add useful methods to ConstraintParameterRenderer
......................................................................

Add useful methods to ConstraintParameterRenderer

These methods can be used by individual checkers to render constraint
parameters into their messages (the intention is to inject a
ConstraintParameterRenderer into them instead of the current
EntityIdFormatter).

The implode( '</li><li>', … ) trick is taken from Ie3a3e1af93.

Change-Id: I5666f3b062a37793a5f5f7e88912aa2d478a6eb7
---
M includes/ConstraintParameterRenderer.php
1 file changed, 109 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/74/353974/1

diff --git a/includes/ConstraintParameterRenderer.php 
b/includes/ConstraintParameterRenderer.php
index b876042..f07bfe1 100644
--- a/includes/ConstraintParameterRenderer.php
+++ b/includes/ConstraintParameterRenderer.php
@@ -2,6 +2,7 @@
 namespace WikibaseQuality\ConstraintReport;
 
 use DataValues\DataValue;
+use InvalidArgumentException;
 use ValueFormatters\ValueFormatter;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\ItemId;
@@ -39,8 +40,8 @@
        private $dataValueFormatter;
 
        /**
-        * @param EntityIdFormatter $entityIdFormatter
-        * @param ValueFormatter $dataValueFormatter
+        * @param EntityIdFormatter $entityIdFormatter should return HTML
+        * @param ValueFormatter $dataValueFormatter should return HTML
         */
        public function __construct(
                EntityIdFormatter $entityIdFormatter,
@@ -64,10 +65,10 @@
                        return htmlspecialchars( $value );
                } elseif ( $value instanceof EntityId ) {
                        // Cases like 'Conflicts with' 'property', to which we 
can link
-                       return $this->entityIdLabelFormatter->formatEntityId( 
$value );
+                       return $this->formatEntityId( $value );
                } else {
                        // Cases where we format a DataValue
-                       return $this->dataValueFormatter->format( $value );
+                       return $this->formatDataValue( $value );
                }
        }
 
@@ -113,4 +114,108 @@
                return $array;
        }
 
+       /**
+        * @param DataValue $value
+        * @return string HTML
+        */
+       public function formatDataValue( DataValue $value ) {
+               return $this->dataValueFormatter->format( $value );
+       }
+
+       /**
+        * @param EntityId $entityId
+        * @return string HTML
+        */
+       public function formatEntityId( EntityId $entityId ) {
+               return $this->entityIdLabelFormatter->formatEntityId( $entityId 
);
+       }
+
+       /**
+        * Format a property ID parameter, potentially unparsed (string).
+        *
+        * If you know that your property ID is already parsed, use {@see 
formatEntityId}.
+        *
+        * @param PropertyId|string $propertyId
+        * @return string HTML
+        */
+       public function formatPropertyId( $propertyId ) {
+               if ( $propertyId instanceof PropertyId ) {
+                       return $this->formatEntityId( $propertyId );
+               } else if ( is_string( $propertyId ) ) {
+                       try {
+                               return $this->formatEntityId( new PropertyId( 
$propertyId ) );
+                       } catch ( InvalidArgumentException $e ) {
+                               return htmlspecialchars( $propertyId );
+                       }
+               } else {
+                       throw new InvalidArgumentException( '$propertyId must 
be either PropertyId or string' );
+               }
+       }
+
+       /**
+        * Format an item ID parameter, potentially unparsed (string).
+        *
+        * If you know that your item ID is already parsed, use {@see 
formatEntityId}.
+        *
+        * @param ItemId|string $itemId
+        * @return string HTML
+        */
+       public function formatItemId( $itemId ) {
+               if ( $itemId instanceof ItemId ) {
+                       return $this->formatEntityId( $itemId );
+               } else if ( is_string( $itemId ) ) {
+                       try {
+                               return $this->formatEntityId( new ItemId( 
$itemId ) );
+                       } catch ( InvalidArgumentException $e ) {
+                               return htmlspecialchars( $itemId );
+                       }
+               } else {
+                       throw new InvalidArgumentException( '$itemId must be 
either ItemId or string' );
+               }
+       }
+
+       /**
+        * Format a list of (potentially unparsed) property IDs.
+        *
+        * The returned array begins with an HTML list of the formatted 
property IDs
+        * and then contains all the individual formatted property IDs.
+        *
+        * @param PropertyId[]|string[] $propertyIds
+        * @return string[] HTML
+        */
+       public function formatPropertyIdList( array $propertyIds ) {
+               if ( empty( $propertyIds ) ) {
+                       return [ '<ul></ul>' ];
+               }
+               $propertyIds = $this->limitArrayLength( $propertyIds );
+               $formattedPropertyIds = array_map( [ $this, "formatPropertyId" 
], $propertyIds );
+               array_unshift(
+                       $formattedPropertyIds,
+                       '<ul><li>' . implode( '</li><li>', 
$formattedPropertyIds ) . '</li></ul>'
+               );
+               return $formattedPropertyIds;
+       }
+
+       /**
+        * Format a list of (potentially unparsed) item IDs.
+        *
+        * The returned array begins with an HTML list of the formatted item IDs
+        * and then contains all the individual formatted item IDs.
+        *
+        * @param ItemId[]|string[] $itemIds
+        * @return string[] HTML
+        */
+       public function formatItemIdList( array $itemIds ) {
+               if ( empty( $itemIds ) ) {
+                       return [ '<ul></ul>' ];
+               }
+               $itemIds = $this->limitArrayLength( $itemIds );
+               $formattedItemIds = array_map( [ $this, "formatItemId" ], 
$itemIds );
+               array_unshift(
+                       $formattedItemIds,
+                       '<ul><li>' . implode( '</li><li>', $formattedItemIds ) 
. '</li></ul>'
+               );
+               return $formattedItemIds;
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5666f3b062a37793a5f5f7e88912aa2d478a6eb7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>

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

Reply via email to