aboutsummaryrefslogtreecommitdiffstats
path: root/includes/diff/TextSlotDiffRenderer.php
diff options
context:
space:
mode:
authorSam Wilson <sam@samwilson.id.au>2023-05-22 13:41:21 +0800
committerTim Starling <tstarling@wikimedia.org>2023-05-31 15:43:28 +1000
commit1eb586013c10811a03a136820abe609bed51f3e0 (patch)
tree9f2aae43efbb0e607abd71c5167973eb2a65cb51 /includes/diff/TextSlotDiffRenderer.php
parent6a3e33f330435d4581777a6a34419b0760a5e057 (diff)
downloadmediawikicore-1eb586013c10811a03a136820abe609bed51f3e0.tar.gz
mediawikicore-1eb586013c10811a03a136820abe609bed51f3e0.zip
diff: Add legend and tooltips to inline diff display
Add a legend at the top of the inline diff display, showing the meanings of the colours of the inserted and deleted highlighting. Also add the same text as tooltips on the highlighted elements. The legend is added as part of a new area above the diff table that can be modified via a new TextSlotDiffRendererTablePrefix hook, so that extensions can add other buttons etc. there as required. This is a follow-up to the previous attempt, which added the legend in DifferenceEngine::showDiff() and was called from too many places. This patch moves it to be called in DifferenceEngine::showDiffPage(). Bug: T324759 Change-Id: I2a3c67bcfa47313dee597e602a62073e4e298cd2 Follow-up: I6de30bf79eb5ac262285951792782b870d075e00
Diffstat (limited to 'includes/diff/TextSlotDiffRenderer.php')
-rw-r--r--includes/diff/TextSlotDiffRenderer.php72
1 files changed, 72 insertions, 0 deletions
diff --git a/includes/diff/TextSlotDiffRenderer.php b/includes/diff/TextSlotDiffRenderer.php
index 9af3a7266b0e..e42ce046f490 100644
--- a/includes/diff/TextSlotDiffRenderer.php
+++ b/includes/diff/TextSlotDiffRenderer.php
@@ -21,6 +21,9 @@
* @ingroup DifferenceEngine
*/
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
+use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Shell\Shell;
use Wikimedia\Assert\Assert;
@@ -49,18 +52,26 @@ class TextSlotDiffRenderer extends SlotDiffRenderer {
/** Use an external executable. */
public const ENGINE_EXTERNAL = 'external';
+ public const INLINE_LEGEND_KEY = '10_mw-diff-inline-legend';
+
/** @var IBufferingStatsdDataFactory|null */
private $statsdDataFactory;
/** @var Language|null The language this content is in. */
private $language;
+ /** @var HookRunner|null */
+ private $hookRunner;
+
/** @var string One of the ENGINE_* constants. */
private $engine = self::ENGINE_PHP;
/** @var string|null Path to an executable to be used as the diff engine. */
private $externalEngine;
+ /** @var string */
+ private $contentModel;
+
/** @inheritDoc */
public function getExtraCacheKeys() {
// Tell DifferenceEngine this is a different variant from the standard wikidiff2 variant
@@ -100,6 +111,22 @@ class TextSlotDiffRenderer extends SlotDiffRenderer {
}
/**
+ * @since 1.41
+ * @param HookContainer $hookContainer
+ */
+ public function setHookContainer( HookContainer $hookContainer ): void {
+ $this->hookRunner = new HookRunner( $hookContainer );
+ }
+
+ /**
+ * @param string $contentModel
+ * @since 1.41
+ */
+ public function setContentModel( string $contentModel ) {
+ $this->contentModel = $contentModel;
+ }
+
+ /**
* Set which diff engine to use.
* @param string $type One of the ENGINE_* constants.
* @param string|null $executable Path to an external executable, only when type is ENGINE_EXTERNAL.
@@ -120,6 +147,16 @@ class TextSlotDiffRenderer extends SlotDiffRenderer {
$this->externalEngine = $executable;
}
+ /**
+ * Get the content model ID that this renderer acts on
+ *
+ * @since 1.41
+ * @return string
+ */
+ public function getContentModel(): string {
+ return $this->contentModel;
+ }
+
/** @inheritDoc */
public function getDiff( Content $oldContent = null, Content $newContent = null ) {
$this->normalizeContents( $oldContent, $newContent, TextContent::class );
@@ -131,6 +168,41 @@ class TextSlotDiffRenderer extends SlotDiffRenderer {
}
/**
+ * @inheritDoc
+ */
+ public function getTablePrefix( IContextSource $context ): string {
+ $legend = null;
+ // wikidiff2 inline type gets a legend to explain the highlighting colours.
+ if ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
+ $ins = Html::element(
+ 'span', [ 'class' => 'mw-diff-inline-legend-ins' ], $context->msg( 'diff-inline-tooltip-ins' )->plain()
+ );
+ $del = Html::element(
+ 'span', [ 'class' => 'mw-diff-inline-legend-del' ], $context->msg( 'diff-inline-tooltip-del' )->plain()
+ );
+ $legend = Html::rawElement( 'div', [ 'class' => 'mw-diff-inline-legend' ], "$ins $del" );
+ }
+ // Allow extensions to add other parts to this area (or modify the legend).
+ // An empty placeholder for the legend is added when it's not in use and other items have been added.
+ $parts = [ self::INLINE_LEGEND_KEY => $legend ];
+
+ $this->hookRunner->onTextSlotDiffRendererTablePrefix( $this, $context, $parts );
+ if ( count( $parts ) > 1 && $parts[self::INLINE_LEGEND_KEY] === null ) {
+ $parts[self::INLINE_LEGEND_KEY] = Html::element( 'div' );
+ }
+ ksort( $parts );
+ if ( count( array_filter( $parts ) ) > 0 ) {
+ $attrs = [
+ 'class' => 'mw-diff-table-prefix',
+ 'dir' => $this->language->getDir(),
+ 'lang' => $this->language->getCode(),
+ ];
+ return Html::rawElement( 'div', $attrs, implode( '', $parts ) );
+ }
+ return '';
+ }
+
+ /**
* Diff the text representations of two content objects (or just two pieces of text in general).
* @param string $oldText
* @param string $newText