Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/49608
Change subject: Simple diff visualization ...................................................................... Simple diff visualization - todo: make sure all cases are covered, such as reference change - further design improvements - tests - there's opportunity for DiffView and other places to share code Change-Id: Icdf9dfc1264974499b34ff67454b881042509829 --- M lib/WikibaseLib.php A lib/includes/DiffOpValueVisualizer.php M lib/includes/claim/ClaimDifferenceVisualizer.php M lib/includes/entity/EntityDiffVisualizer.php 4 files changed, 274 insertions(+), 62 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/08/49608/1 diff --git a/lib/WikibaseLib.php b/lib/WikibaseLib.php index af38c64..f5db53d 100644 --- a/lib/WikibaseLib.php +++ b/lib/WikibaseLib.php @@ -93,6 +93,7 @@ $wgAutoloadClasses['Wikibase\ChangeNotifier'] = $dir . 'includes/ChangeNotifier.php'; $wgAutoloadClasses['Wikibase\ChangeNotificationJob'] = $dir . 'includes/ChangeNotificationJob.php'; $wgAutoloadClasses['Wikibase\ChangesTable'] = $dir . 'includes/ChangesTable.php'; +$wgAutoloadClasses['Wikibase\DiffOpValueVisualizer'] = $dir . 'includes/DiffOpValueVisualizer.php'; $wgAutoloadClasses['Wikibase\DiffView'] = $dir . 'includes/DiffView.php'; $wgAutoloadClasses['Wikibase\Lib\GuidGenerator'] = $dir . 'includes/GuidGenerator.php'; $wgAutoloadClasses['Wikibase\Lib\V4GuidGenerator'] = $dir . 'includes/GuidGenerator.php'; diff --git a/lib/includes/DiffOpValueVisualizer.php b/lib/includes/DiffOpValueVisualizer.php new file mode 100644 index 0000000..c080f29 --- /dev/null +++ b/lib/includes/DiffOpValueVisualizer.php @@ -0,0 +1,150 @@ +<?php + +namespace Wikibase; + +use Html; +use Diff; + +/** + * Class for generating HTML for Claim Diffs. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @file + * @ingroup WikibaseLib + * + * @licence GNU GPL v2+ + * @author Tobias Gritschacher < [email protected] > + * @author Katie Filbert < [email protected] > + */ +class DiffOpValueVisualizer { + + protected $name; + + protected $oldValue; + + protected $newValue; + + public function __construct( $name, $oldValue, $newValue ) { + $this->name = $name; + $this->oldValue = $oldValue; + $this->newValue = $newValue; + } + + /** + * Generates HTML for the header of the diff operation + * + * @since 0.4 + * + * @return string + */ + protected function generateHeaderHtml() { + $html = Html::openElement( 'tr' ); + $html .= Html::rawElement( 'td', array( 'colspan'=>'2', 'class' => 'diff-lineno' ), $this->name ); + $html .= Html::rawElement( 'td', array( 'colspan'=>'2', 'class' => 'diff-lineno' ), $this->name ); + $html .= Html::closeElement( 'tr' ); + + return $html; + } + + /** + * Generates HTML for an change diffOp + * + * @since 0.4 + * + * @return string + */ + protected function generateChangeOpHtml() { + $html = Html::openElement( 'tr' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '-' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-deletedline' ), + Html::rawElement( 'div', array(), + Html::rawElement( 'del', array( 'class' => 'diffchange diffchange-inline' ), + $this->oldValue ) ) ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '+' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-addedline' ), + Html::rawElement( 'div', array(), + Html::rawElement( 'ins', array( 'class' => 'diffchange diffchange-inline' ), + $this->newValue ) ) ); + $html .= Html::closeElement( 'tr' ); + $html .= Html::closeElement( 'tr' ); + + return $html; + } + + /** + * Generates HTML for an add diffOp + * + * @since 0.4 + * + * @return string + */ + protected function generateAddOpHtml() { + $html = Html::openElement( 'tr' ); + $html .= Html::rawElement( 'td', array( 'colspan'=>'2' ), ' ' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '+' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-addedline' ), + Html::rawElement( 'div', array(), + Html::rawElement( 'ins', array( 'class' => 'diffchange diffchange-inline' ), + $this->newValue ) + ) + ); + $html .= Html::closeElement( 'tr' ); + + return $html; + } + + /** + * Generates HTML for an remove diffOp + * + * @since 0.4 + * + * @return string + */ + protected function generateRemoveOpHtml() { + $html = Html::openElement( 'tr' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '-' ); + $html .= Html::rawElement( 'td', array( 'class' => 'diff-deletedline' ), + Html::rawElement( 'div', array(), + Html::rawElement( 'del', array( 'class' => 'diffchange diffchange-inline' ), + $this->oldValue ) ) ); + $html .= Html::rawElement( 'td', array( 'colspan'=>'2' ), ' ' ); + $html .= Html::closeElement( 'tr' ); + + return $html; + } + + public function generateHtml() { + $html = ''; + + if ( is_string( $this->name ) ) { + $html .= $this->generateHeaderHtml(); + } + + if ( is_string( $this->oldValue ) && is_string( $this->newValue ) ) { + $html .= $this->generateChangeOpHtml(); + } else if ( is_string( $this->newValue ) ) { + $html .= $this->generateAddOpHtml(); + } else if ( is_string( $this->oldValue ) ) { + $html .= $this->generateRemoveOpHtml(); + } + + return $html; + } + +} diff --git a/lib/includes/claim/ClaimDifferenceVisualizer.php b/lib/includes/claim/ClaimDifferenceVisualizer.php index 5404af4..9b81565 100644 --- a/lib/includes/claim/ClaimDifferenceVisualizer.php +++ b/lib/includes/claim/ClaimDifferenceVisualizer.php @@ -58,68 +58,122 @@ * * @return string */ - public function visualizeDiff( ClaimDifference $claimDifference ) { - $html = $this->generateHeaderHtml( 'TODO: claim difference' ); // TODO + public function visualizeDiff( ClaimDifference $claimDifference, $langCode ) { + $html = ''; if ( $claimDifference->getMainSnakChange() !== null ) { $mainSnakChange = $claimDifference->getMainSnakChange(); - $html .= $this->generateChangeOpHtml( $mainSnakChange->getOldValue(), $mainSnakChange->getNewValue() ); + $valueVisualizer = new DiffOpValueVisualizer( + $this->getMainSnakHeader( $mainSnakChange->getNewValue(), $langCode ), + $this->getMainSnakValue( $mainSnakChange->getOldValue(), $langCode ), + $this->getMainSnakValue( $mainSnakChange->getNewValue(), $langCode ) + ); + $html .= $valueVisualizer->generateHtml(); } if ( $claimDifference->getRankChange() !== null ) { $rankChange = $claimDifference->getRankChange(); - $html .= $this->generateChangeOpHtml( $rankChange->getOldValue(), $rankChange->getNewValue() ); + $valueVisualizer = new DiffOpValueVisualizer( + wfMessage( 'wikibase-diffview-rank' ), + $rankChange->getOldValue(), + $rankChange->getNewValue() + ); + $html .= $valueVisualizer->generateHtml(); } // TODO: html for qualifier changes - // TODO: html for reference changes + if ( $claimDifference->getReferenceChanges() !== null ) { + $referenceChanges = $claimDifference->getReferenceChanges(); + + foreach( $referenceChanges as $referenceChange ) { + if ( $referenceChange instanceof \Diff\DiffOpAdd ) { + $html .= $this->getRefHtml( $referenceChange->getNewValue(), $langCode, 'add' ); + } else if ( $referenceChange instanceof \Diff\DiffOpRemove ) { + $html .= $this->getRefHtml( $referenceChange->getOldValue(), $langCode, 'remove' ); + } + // todo reference DiffOpChange + } + } return $html; } - /** - * Generates HTML for the header of the diff operation - * - * @since 0.4 - * - * @param string $name - * - * @return string - */ - protected function generateHeaderHtml( $name ) { - $html = Html::openElement( 'tr' ); - $html .= Html::rawElement( 'td', array( 'colspan'=>'2', 'class' => 'diff-lineno' ), $name ); - $html .= Html::rawElement( 'td', array( 'colspan'=>'2', 'class' => 'diff-lineno' ), $name ); - $html .= Html::closeElement( 'tr' ); + public function getSnakHtml( Snak $mainSnak, $langCode, $type, $prependHeader = null ) { + $snakHeader = ''; + if ( $prependHeader !== null ) { + $snakHeader = $prependHeader; + } + $snakHeader .= $this->getMainSnakHeader( $mainSnak, $langCode ); - return $html; + $snakValue = $this->getMainSnakValue( $mainSnak, $langCode ); + + if ( $type === 'add' ) { + $valueVisualizer = new DiffOpValueVisualizer( $snakHeader, null, $snakValue ); + } else if ( $type === 'remove' ) { + $valueVisualizer = new DiffOpValueVisualizer( $snakHeader, $snakValue, null ); + } else { + throw new \MWException( 'Unknown diffop type' ); + } + + return $valueVisualizer->generateHtml(); } - /** - * Generates HTML for an change diffOp - * - * @since 0.4 - * - * @param string $oldValue - * @param string $newValue - * - * @return string - */ - protected function generateChangeOpHtml( $oldValue, $newValue ) { - $html = Html::openElement( 'tr' ); - $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '-' ); - $html .= Html::rawElement( 'td', array( 'class' => 'diff-deletedline' ), - Html::rawElement( 'div', array(), - Html::rawElement( 'del', array( 'class' => 'diffchange diffchange-inline' ), - $oldValue ) ) ); - $html .= Html::rawElement( 'td', array( 'class' => 'diff-marker' ), '+' ); - $html .= Html::rawElement( 'td', array( 'class' => 'diff-addedline' ), - Html::rawElement( 'div', array(), - Html::rawElement( 'ins', array( 'class' => 'diffchange diffchange-inline' ), - $newValue ) ) ); - $html .= Html::closeElement( 'tr' ); - $html .= Html::closeElement( 'tr' ); + protected function getMainSnakHeader( Snak $mainSnak, $langCode ) { + $propertyId = $mainSnak->getPropertyId(); + $property = $this->entityLookup->getEntity( $propertyId ); + $dataTypeLabel = $property->getDataType()->getLabel( $langCode ); + + $label = $property->getLabel( $langCode ); + $propertyLabel = $label !== false ? $label : $property->getPrefixedId(); + + $headerText = wfMessage( 'wikibase-entity-property' ) . ' / ' . $dataTypeLabel . ' / ' . $label; + + return $headerText; + } + + protected function getMainSnakValue( Snak $snak, $langCode ) { + $snakType = $snak->getType(); + + if ( $snakType === 'value' ) { + $dataValue = $snak->getDataValue(); + + // FIXME! + if ( $dataValue instanceof EntityId ) { + $diffValueString = $this->getEntityLabel( $dataValue, $langCode ); + } else { + $diffValueString = $dataValue->getValue(); + } + + return $diffValueString; + } + + return null; + } + + protected function getEntityLabel( EntityId $entityId , $langCode ) { + $label = $entityId->getPrefixedId(); + + $lookedUpLabel = $this->entityLookup->getEntity( $entityId )->getLabel( $langCode ); + + if ( $lookedUpLabel !== false ) { + $label = $lookedUpLabel; + } + + return $label; + } + + protected function getRefHtml( Reference $ref, $langCode, $opType ) { + $html = ''; + + $refSnakList = $ref->getSnaks(); + foreach ( $refSnakList as $snak ) { + if ( $html !== '' ) { + $html .= Html::rawElement( 'br', array(), '' ); + } + $html .= $this->getSnakHtml( $snak, $langCode, $opType, + wfMessage( 'wikibase-diffview-reference' ) . ' / ' ); + } return $html; } diff --git a/lib/includes/entity/EntityDiffVisualizer.php b/lib/includes/entity/EntityDiffVisualizer.php index e29ebe0..4eb7e4f 100644 --- a/lib/includes/entity/EntityDiffVisualizer.php +++ b/lib/includes/entity/EntityDiffVisualizer.php @@ -70,7 +70,8 @@ * @param ClaimDiffer $claimDiffer * @param ClaimDifferenceVisualizer $claimDiffView */ - public function __construct( IContextSource $contextSource, ClaimDiffer $claimDiffer, ClaimDifferenceVisualizer $claimDiffView ) { + public function __construct( IContextSource $contextSource, ClaimDiffer $claimDiffer, + ClaimDifferenceVisualizer $claimDiffView ) { $this->context = $contextSource; $this->claimDiffer = $claimDiffer; $this->claimDiffVisualizer = $claimDiffView; @@ -132,29 +133,35 @@ */ protected function getClaimDiffHtml( DiffOp $claimDiffOp ) { if ( $claimDiffOp instanceof DiffOpChange ) { - $claimDifference = $this->claimDiffer->diffClaims( $claimDiffOp->getOldValue(), $claimDiffOp->getNewValue() ); - return $this->claimDiffVisualizer->visualizeDiff( $claimDifference ); + $claimDifference = $this->claimDiffer->diffClaims( + $claimDiffOp->getOldValue(), + $claimDiffOp->getNewValue() + ); + return $this->claimDiffVisualizer->visualizeDiff( + $claimDifference, + $this->context->getLanguage()->getCode() + ); } if ( $claimDiffOp instanceof DiffOpAdd || $claimDiffOp instanceof DiffOpRemove ) { - $claim = $claimDiffOp instanceof DiffOpAdd ? $claimDiffOp->getNewValue() : $claimDiffOp->getOldValue(); - return $this->getClaimHtml( $claim ); + if ( $claimDiffOp instanceof DiffOpAdd ) { + $claim = $claimDiffOp->getNewValue(); + $opType = 'add'; + } else { + $claim = $claimDiffOp->getOldValue(); + $opType = 'remove'; + } + + $mainSnak = $claim->getMainSnak(); + + return $this->claimDiffVisualizer->getSnakHtml( + $mainSnak, + $this->context->getLanguage()->getCode(), + $opType + ); } throw new MWException( 'Encountered an unexpected diff operation type for a claim' ); - } - - /** - * Returns the HTML for a single Claim. - * - * @since 0.4 - * - * @param Claim $claim - * - * @return string - */ - protected function getClaimHtml( Claim $claim ) { - return 'TODO'; // TODO } } -- To view, visit https://gerrit.wikimedia.org/r/49608 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Icdf9dfc1264974499b34ff67454b881042509829 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
