jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/362222 )

Change subject: Add link to constraint definition in gadget
......................................................................


Add link to constraint definition in gadget

Two new fields are added to the API description of a constraint, the
label of its type and the link to its definition. The gadget uses these
fields to render the title as a link instead of as plain text. The API
module gets three additional services injected so it can produce those
parameters.

The link to the definition is the link to the constraint statement
(group) for a constraint that was imported from a statement, and
otherwise the link to the property talk page, where the constraint
template is expected to be defined.

Bug: T167399
Change-Id: I9e2080950fc558e6083b5d69c33f86c4196e9caf
---
M api/CheckConstraints.php
M modules/gadget.js
M tests/phpunit/Api/CheckConstraintsTest.php
3 files changed, 71 insertions(+), 8 deletions(-)

Approvals:
  Jonas Kress (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/api/CheckConstraints.php b/api/CheckConstraints.php
index 3bb2f1f..15288ec 100644
--- a/api/CheckConstraints.php
+++ b/api/CheckConstraints.php
@@ -5,18 +5,24 @@
 use ApiBase;
 use ApiMain;
 use ApiResult;
+use Config;
 use MediaWiki\MediaWikiServices;
 use RequestContext;
 use ValueFormatters\FormatterOptions;
 use Wikibase\ChangeOp\StatementChangeOpFactory;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\EntityIdParsingException;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
 use Wikibase\DataModel\Services\Statement\StatementGuidParser;
+use Wikibase\DataModel\Services\Statement\StatementGuidParsingException;
 use Wikibase\DataModel\Services\Statement\StatementGuidValidator;
 use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\EntityTitleLookup;
 use Wikibase\Repo\Api\ApiErrorReporter;
 use Wikibase\Repo\Api\ApiHelperFactory;
 use Wikibase\Repo\Api\ResultBuilder;
+use Wikibase\Repo\EntityIdLabelFormatterFactory;
 use Wikibase\Repo\WikibaseRepo;
 use 
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
 use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
@@ -84,6 +90,21 @@
        private $constraintParameterRenderer;
 
        /**
+        * @var EntityTitleLookup
+        */
+       private $entityTitleLookup;
+
+       /**
+        * @var EntityIdFormatter
+        */
+       private $entityIdLabelFormatter;
+
+       /**
+        * @var Config
+        */
+       private $config;
+
+       /**
         * Creates new instance from global state.
         *
         * @param ApiMain $main
@@ -104,9 +125,11 @@
                $languageFallbackLabelDescriptionLookupFactory = 
$repo->getLanguageFallbackLabelDescriptionLookupFactory();
                $labelDescriptionLookup = 
$languageFallbackLabelDescriptionLookupFactory->newLabelDescriptionLookup( 
$language );
                $entityIdHtmlLinkFormatterFactory = 
$repo->getEntityIdHtmlLinkFormatterFactory();
-               $entityIdFormatter = 
$entityIdHtmlLinkFormatterFactory->getEntityIdFormatter( 
$labelDescriptionLookup );
+               $entityIdHtmlLinkFormatter = 
$entityIdHtmlLinkFormatterFactory->getEntityIdFormatter( 
$labelDescriptionLookup );
+               $entityIdLabelFormatterFactory = new 
EntityIdLabelFormatterFactory();
+               $entityIdLabelFormatter = 
$entityIdLabelFormatterFactory->getEntityIdFormatter( $labelDescriptionLookup );
                $statementGuidParser = $repo->getStatementGuidParser();
-               $constraintParameterRenderer = new ConstraintParameterRenderer( 
$entityIdFormatter, $valueFormatter );
+               $constraintParameterRenderer = new ConstraintParameterRenderer( 
$entityIdHtmlLinkFormatter, $valueFormatter );
                $config = MediaWikiServices::getInstance()->getMainConfig();
                $titleParser = 
MediaWikiServices::getInstance()->getTitleParser();
                $constraintReportFactory = new ConstraintReportFactory(
@@ -127,7 +150,10 @@
                return new CheckConstraints( $main, $name, $prefix, 
$repo->getEntityIdParser(),
                        $repo->getStatementGuidValidator(), 
$statementGuidParser, $constraintReportFactory->getConstraintChecker(),
                        $constraintParameterRenderer,
-                       $repo->getApiHelperFactory( RequestContext::getMain() ) 
);
+                       $repo->getApiHelperFactory( RequestContext::getMain() ),
+                       $repo->getEntityTitleLookup(),
+                       $entityIdLabelFormatter,
+                       $config );
        }
 
        /**
@@ -140,13 +166,20 @@
         * @param DelegatingConstraintChecker $delegatingConstraintChecker
         * @param ConstraintParameterRenderer $constraintParameterRenderer
         * @param ApiHelperFactory $apiHelperFactory
+        * @param EntityTitleLookup $entityTitleLookup
+        * @param EntityIdFormatter $entityIdLabelFormatter
+        * @param Config $config
         */
        public function __construct( ApiMain $main, $name, $prefix = '', 
EntityIdParser $entityIdParser,
                StatementGuidValidator $statementGuidValidator,
                StatementGuidParser $statementGuidParser,
                DelegatingConstraintChecker $delegatingConstraintChecker,
                ConstraintParameterRenderer $constraintParameterRenderer,
-               ApiHelperFactory $apiHelperFactory ) {
+               ApiHelperFactory $apiHelperFactory,
+               EntityTitleLookup $entityTitleLookup,
+               EntityIdFormatter $entityIdLabelFormatter,
+               Config $config
+       ) {
                parent::__construct( $main, $name, $prefix );
 
                $repo = WikibaseRepo::getDefaultInstance();
@@ -162,6 +195,9 @@
                $this->errorReporter = $apiHelperFactory->getErrorReporter( 
$this );
 
                $this->constraintParameterRenderer = 
$constraintParameterRenderer;
+               $this->entityTitleLookup = $entityTitleLookup;
+               $this->entityIdLabelFormatter = $entityIdLabelFormatter;
+               $this->config = $config;
        }
 
        /**
@@ -299,6 +335,21 @@
                        $entityId = 
$checkResult->getEntityId()->getSerialization();
                        $propertyId = 
$checkResult->getPropertyId()->getSerialization();
                        $claimId = $statement->getGuid();
+                       $constraintId = 
$checkResult->getConstraint()->getConstraintId();
+                       $typeItemId = 
$checkResult->getConstraint()->getConstraintTypeItemId();
+
+                       $title = $this->entityTitleLookup->getTitleForId( 
$checkResult->getPropertyId() );
+                       try {
+                               $statementGuid = 
$this->statementGuidParser->parse( $constraintId );
+                               // constraint statement
+                               $typeLabel = 
$this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) );
+                               // TODO link to the statement when possible 
(T169224)
+                               $link = $title->getFullUrl() . '#' . 
$this->config->get( 'WBQualityConstraintsPropertyConstraintId' );
+                       } catch ( StatementGuidParsingException $e ) {
+                               // constraint template on talk page
+                               $typeLabel = htmlspecialchars( $typeItemId );
+                               $link = $title->getTalkPage()->getFullUrl();
+                       }
 
                        $result = [
                                'status' => $checkResult->getStatus(),
@@ -306,7 +357,9 @@
                                'claim' => 
$checkResult->getStatement()->getGuid(),
                                'constraint' => [
                                        'id' => $checkResult->getConstraintId(),
-                                       'type' => 
$checkResult->getConstraintName(),
+                                       'type' => $typeItemId,
+                                       'typeLabel' => $typeLabel,
+                                       'link' => $link,
                                        'detail' => 
$checkResult->getParameters(),
                                        'detailHTML' => 
$this->constraintParameterRenderer->formatParameters( 
$checkResult->getParameters() )
                                ]
diff --git a/modules/gadget.js b/modules/gadget.js
index adbc77e..b6ac5e4 100644
--- a/modules/gadget.js
+++ b/modules/gadget.js
@@ -32,7 +32,12 @@
 
                if ( result.status === 'violation' ) {
                        $report = $( '<div>' ).addClass( 'wbqc-report' );
-                       $heading = $( '<h4>' ).text( result.constraint.type );
+                       $heading = $( '<h4>' ).append(
+                               $( '<a>' )
+                                       .text( result.constraint.typeLabel )
+                                       .attr( 'href', result.constraint.link )
+                                       .attr( 'target', '_blank' )
+                       );
                        $helpButton = new OO.ui.ButtonWidget( {
                                icon: 'help',
                                framed: false,
diff --git a/tests/phpunit/Api/CheckConstraintsTest.php 
b/tests/phpunit/Api/CheckConstraintsTest.php
index 68f541a..14f82d0 100644
--- a/tests/phpunit/Api/CheckConstraintsTest.php
+++ b/tests/phpunit/Api/CheckConstraintsTest.php
@@ -4,6 +4,7 @@
 
 use ApiTestCase;
 use DataValues\UnknownValue;
+use HashConfig;
 use RequestContext;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
@@ -84,6 +85,7 @@
                        $fallbackLabelDescLookupFactory = new 
LanguageFallbackLabelDescriptionLookupFactory( $languageFallbackChainFactory, 
$termLookup, $termBuffer );
                        $language = new Language();
                        $labelLookup = 
$fallbackLabelDescLookupFactory->newLabelDescriptionLookup( $language );
+                       $entityIdFormatter = $factory->getEntityIdFormatter( 
$labelLookup );
 
                        $formatterOptions = new FormatterOptions();
                        $factoryFunctions = [];
@@ -111,8 +113,11 @@
                                new StatementGuidValidator( $entityIdParser ),
                                new StatementGuidParser( $entityIdParser ),
                                $constraintChecker,
-                               new ConstraintParameterRenderer( 
$factory->getEntityIdFormatter( $labelLookup ), $valueFormatter ),
-                               $repo->getApiHelperFactory( 
RequestContext::getMain() )
+                               new ConstraintParameterRenderer( 
$entityIdFormatter, $valueFormatter ),
+                               $repo->getApiHelperFactory( 
RequestContext::getMain() ),
+                               $repo->getEntityTitleLookup(),
+                               $entityIdFormatter,
+                               new HashConfig( [ 
'WBQualityConstraintsPropertyConstraintId' => 'P1' ] )
                        );
                };
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9e2080950fc558e6083b5d69c33f86c4196e9caf
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to