Ladsgroup has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/313629

Change subject: [WIP] Add EditActionHookHandler in clinet to inject entity 
usage data
......................................................................

[WIP] Add EditActionHookHandler in clinet to inject entity usage data

What needs to be done:
- Get javascript working
- CI tests

Bug: T144921
Change-Id: I1f1fad785545be475f1db8c12d1647ec449a29d7
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
M client/i18n/en.json
M client/i18n/qqq.json
A client/includes/Hooks/EditActionHookHandler.php
5 files changed, 184 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/29/313629/1

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 1a5ee70..abe1b52 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -23,6 +23,7 @@
 use Wikibase\Client\Hooks\DeletePageNoticeCreator;
 use Wikibase\Client\Hooks\EchoNotificationsHandlers;
 use Wikibase\Client\Hooks\InfoActionHookHandler;
+use Wikibase\Client\Hooks\EditActionHookHandler;
 use Wikibase\Client\RecentChanges\ChangeLineFormatter;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
 use Wikibase\Client\Specials\SpecialPagesWithBadges;
@@ -438,6 +439,40 @@
        }
 
        /**
+        * Adds the Entity usage data in ActionEdit
+        *
+        * @param EditPage $editor
+        *
+        * @return bool
+        */
+       public static function onEditAction( EditPage &$editor, &$checkboxes, 
&$tabindex ) {
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+
+               if ( $editor->preview || $editor->section != '' ) {
+                       // Shorten out, like template translusion in core
+                       return true;
+               }
+               $usageLookup = $wikibaseClient->getStore()->getUsageLookup();
+               $labelDescriptionLookupFactory = new 
LanguageFallbackLabelDescriptionLookupFactory(
+                       $wikibaseClient->getLanguageFallbackChainFactory(),
+                       $wikibaseClient->getTermLookup(),
+                       $wikibaseClient->getTermBuffer()
+               );
+               $idParser = $wikibaseClient->getEntityIdParser();
+
+               $editActionHookHandler = new EditActionHookHandler(
+                       $wikibaseClient->newRepoLinker(),
+                       $usageLookup,
+                       $labelDescriptionLookupFactory,
+                       $idParser
+               );
+
+               $editActionHookHandler->handle( $editor );
+
+               return true;
+       }
+
+       /**
         * Notify the user that we have automatically updated the repo or that 
they
         * need to do that per hand.
         *
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 7b34a44..6df21ae 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -122,6 +122,7 @@
        $wgHooks['BeforePageDisplay'][] = 
'\Wikibase\ClientHooks::onBeforePageDisplayAddJsConfig';
        $wgHooks['ScribuntoExternalLibraries'][] = 
'\Wikibase\ClientHooks::onScribuntoExternalLibraries';
        $wgHooks['InfoAction'][] = '\Wikibase\ClientHooks::onInfoAction';
+       $wgHooks['EditPageBeforeEditChecks'][] = 
'\Wikibase\ClientHooks::onEditAction';
        $wgHooks['BaseTemplateAfterPortlet'][] = 
'\Wikibase\ClientHooks::onBaseTemplateAfterPortlet';
        $wgHooks['GetBetaFeaturePreferences'][] = 
'\Wikibase\ClientHooks::onGetBetaFeaturePreferences';
        $wgHooks['ArticleDeleteAfterSuccess'][] = 
'\Wikibase\ClientHooks::onArticleDeleteAfterSuccess';
diff --git a/client/i18n/en.json b/client/i18n/en.json
index 0e5f82f..0c6654d 100644
--- a/client/i18n/en.json
+++ b/client/i18n/en.json
@@ -105,6 +105,7 @@
        "wikibase-pageinfo-entity-usage-T": "Title",
        "wikibase-pageinfo-entity-usage-X": "All entity data",
        "wikibase-pageinfo-entity-usage-O": "Other (Statements)",
+       "wikibase-entityusage-explanation": "{{WBREPONAME}} entities used in 
this page:",
        "wikibase-property-render-error": "Failed to render property $1: $2",
        "wikibase-otherprojects": "In other projects",
        "wikibase-otherprojects-beta-message": "Other projects sidebar",
diff --git a/client/i18n/qqq.json b/client/i18n/qqq.json
index 012b245..24d1786 100644
--- a/client/i18n/qqq.json
+++ b/client/i18n/qqq.json
@@ -116,6 +116,7 @@
        "wikibase-pageinfo-entity-usage-T": "Name for ''title'' entity 
usage\n{{Identical|Title}}",
        "wikibase-pageinfo-entity-usage-X": "Name for ''all'' entity usage",
        "wikibase-pageinfo-entity-usage-O": "Name for ''other'' entity usage",
+       "wikibase-entityusage-explanation": "Explanation on bottom of edti page 
for entity usage",
        "wikibase-property-render-error": "Error message shown when the 
#property parser function fails to render a property value.\n\nParameters:\n* 
$1 - the property ID or name\n* $2 - the original error message (this is 
typically in English and may be rather technical)",
        "wikibase-otherprojects": "Label of the sidebar section containing 
links to other projects.\n\nAlso used in 
{{msg-mw|Wikibase-otherprojects-beta-description}}\n{{Identical|Other 
project}}\n{{related|Wikibase-otherprojects}}",
        "wikibase-otherprojects-beta-message": "Used as checkbox label for the 
\"other projects\" beta feature.\n\nThe description for this label is 
{{msg-mw|wikibase-otherprojects-beta-description}}\n{{related|Wikibase-otherprojects}}",
diff --git a/client/includes/Hooks/EditActionHookHandler.php 
b/client/includes/Hooks/EditActionHookHandler.php
new file mode 100644
index 0000000..d1a28c4
--- /dev/null
+++ b/client/includes/Hooks/EditActionHookHandler.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use EditPage;
+use Html;
+use IContextSource;
+use Wikibase\Client\RepoLinker;
+use Wikibase\Client\Usage\EntityUsage;
+use Wikibase\Client\Usage\UsageLookup;
+use Wikibase\DataModel\Entity\EntityIdParser;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
+
+/**
+ * @since 0.5
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani < [email protected] >
+ */
+class EditActionHookHandler {
+
+       /**
+        * @var UsageLookup
+        */
+       private $usageLookup;
+
+       /**
+        * @var LanguageFallbackLabelDescriptionLookupFactory
+        */
+       private $labelDescriptionLookupFactory;
+
+       /**
+        * @var EntityIdParser
+        */
+       private $idParser;
+
+       public function __construct(
+               RepoLinker $repoLinker,
+               UsageLookup $usageLookup,
+               LanguageFallbackLabelDescriptionLookupFactory 
$labelDescriptionLookupFactory,
+               EntityIdParser $idParser
+       ) {
+               $this->repoLinker = $repoLinker;
+               $this->usageLookup = $usageLookup;
+               $this->labelDescriptionLookupFactory = 
$labelDescriptionLookupFactory;
+               $this->idParser = $idParser;
+       }
+
+       /**
+        * @param EditPage $editor
+        */
+       public function handle( EditPage &$editor ) {
+               // Check if there is usage to show
+               $title = $editor->getTitle();
+               $usage = $this->usageLookup->getUsagesForPage( 
$title->getArticleID() );
+
+               if ( $usage ) {
+                       $header = $this->getHeader( $editor );
+                       $usageOutput = $this->formatEntityUsage( $editor, 
$usage );
+                       $output = Html::rawElement(
+                               'div', [ 'class' => 'wikibase-entity-usage' ],
+                               $header . "\n" . $usageOutput
+                       );
+                       $editor->editFormTextAfterTools .= $output;
+               }
+       }
+
+       /**
+        * @param string[] $rowAspects
+        * @param IContextSource $context
+        *
+        * @return string HTML
+        */
+       private function formatAspects( $rowAspects, IContextSource $context ) {
+               $aspects = [];
+
+               foreach ( $rowAspects as $aspect ) {
+                       $aspects[] = $context->msg(
+                               'wikibase-pageinfo-entity-usage-' . $aspect[0], 
$aspect[1] )->parse();
+               }
+
+               return $context->getLanguage()->commaList( $aspects );
+       }
+
+       /**
+        * @param EditPage $editor
+        * @param EntityUsage[] $usage
+        */
+       private function formatEntityUsage( EditPage $editor, array $usage ) {
+               $usageAspectsByEntity = [];
+               $entities = [];
+               foreach ( $usage as $key => $entityUsage ) {
+                       $entityId = 
$entityUsage->getEntityId()->getSerialization();
+                       $entities[$entityId] = $entityUsage->getEntityId();
+                       if ( !isset( $usageAspectsByEntity[$entityId] ) ) {
+                               $usageAspectsByEntity[$entityId] = [];
+                       }
+                       $usageAspectsByEntity[$entityId][] = [
+                               $entityUsage->getAspect(),
+                               $entityUsage->getModifier()
+                       ];
+               }
+               $output = '';
+               $entityIds = array_map(
+                       function( $entityId ) {
+                               return $this->idParser->parse( $entityId );
+                       },
+                       array_keys( $usageAspectsByEntity )
+               );
+               $labelLookup = 
$this->labelDescriptionLookupFactory->newLabelDescriptionLookup(
+                       $editor->getContext()->getLanguage(),
+                       $entityIds
+               );
+               foreach ( $usageAspectsByEntity as $entityId => $aspects ) {
+                       $label = $labelLookup->getLabel( 
$this->idParser->parse( $entityId ) );
+                       $text = $label === null ? $entityId : $label->getText();
+
+                       $aspectContent = $this->formatAspects( $aspects, 
$editor->getContext() );
+
+                       $output .= Html::rawElement( 'li', [],
+                               $this->repoLinker->buildEntityLink(
+                                       $entities[$entityId],
+                                       [ 'external' ],
+                                       $text
+                               ) . ': ' . $aspectContent
+                       );
+               }
+               $output = Html::rawElement( 'ul', [ 'class' => 
'mw-editfooter-list mw-collapsible'],
+                       $output );
+               return $output;
+       }
+
+       /**
+        * @param EditPage $editor
+        * @return string
+        */
+       private function getHeader( EditPage $editor ) {
+               $element = Html::rawelement( 'div', [
+                       'class' => 'wikibase-entityusage-explanation ' .
+                                    'mw-editfooter-toggler'
+               ], $editor->getContext()->msg( 
'wikibase-entityusage-explanation' )->parse()
+               );
+               return $element;
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1f1fad785545be475f1db8c12d1647ec449a29d7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>

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

Reply via email to