Skip to content

Commit

Permalink
Preferred property label, refs 1865
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Oct 5, 2016
1 parent f00fcfe commit 00123c7
Show file tree
Hide file tree
Showing 32 changed files with 751 additions and 92 deletions.
9 changes: 7 additions & 2 deletions DefaultSettings.php
Expand Up @@ -523,7 +523,7 @@
# setting is not normally changed by users but by extensions that add new
# types that have their own additional declaration properties.
##
$GLOBALS['smwgDeclarationProperties'] = array( '_PVAL', '_LIST', '_PVAP', '_PVUC' );
$GLOBALS['smwgDeclarationProperties'] = array( '_PVAL', '_LIST', '_PVAP', '_PVUC', '_PDESC', '_PPLB' );
##

###
Expand Down Expand Up @@ -1006,9 +1006,14 @@
# - SMW_DV_NUMV_USPACE (Number/QuantityValue) to preserve spaces within
# unit labels
#
# - SMW_DV_PPLB to support the use of preferred property labels
#
# - SMW_DV_PROV_LHNT (PropertyValue) to output a <sup>p</sup> hint marker on
# properties that use a preferred label
#
# @since 2.4
##
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE | SMW_DV_PVAP | SMW_DV_WPV_DTITLE | SMW_DV_TIMEV_CM;
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE | SMW_DV_PVAP | SMW_DV_WPV_DTITLE | SMW_DV_TIMEV_CM | SMW_DV_PPLB | SMW_DV_PROV_LHNT;

##
# Fulltext search support
Expand Down
4 changes: 3 additions & 1 deletion i18n/en.json
Expand Up @@ -445,5 +445,7 @@
"smw-sp-types_ref_rec": "\"$1\" is a [https://www.semantic-mediawiki.org/wiki/Container container] type that allows to record additional information (e.g. provenance data) about a value assignment.",
"smw-datavalue-reference-outputformat": "$1: $2",
"smw-datavalue-reference-invalid-fields-definition": "The [[Special:Types/Reference|Reference]] type expects a list of properties to be declared using the [https://www.semantic-mediawiki.org/wiki/Help:Special_property_Has_fields Has fields] property.",
"smw-parser-invalid-json-format": "The JSON parser returned with a \"$1\"."
"smw-parser-invalid-json-format": "The JSON parser returned with a \"$1\".",
"smw-property-preferred-title-format":"$1 ($2)",
"smw-property-preferred-label-language-combination-exists": "\"$1\" cannot be used as preferred label because the language \"$2\" is already assigned to the \"$3\" label."
}
9 changes: 8 additions & 1 deletion includes/articlepages/SMW_PropertyPage.php
Expand Up @@ -29,6 +29,7 @@ protected function initParameters() {
$this->limit = $smwgPropertyPagingLimit;
$this->mProperty = DIProperty::newFromUserLabel( $this->mTitle->getText() );
$this->store = ApplicationFactory::getInstance()->getStore();
$this->propertyValue = DataValueFactory::getInstance()->newDataItemValue( $this->mProperty );
return true;
}

Expand All @@ -43,6 +44,12 @@ protected function getHtml() {
return '';
}

if ( $this->propertyValue->getDataItem()->getPreferredLabel() !== '' && $this->mTitle->getText() !== $this->propertyValue->getDataItem()->getPreferredLabel() ) {
$this->getContext()->getOutput()->setPageTitle(
wfMessage( 'smw-property-preferred-title-format', $this->mTitle->getPrefixedText(), $this->propertyValue->getWikiValue() )->text()
);
}

$list = $this->getSubpropertyList() . $this->getPropertyValueList();
$result = ( $list !== '' ? Html::element( 'div', array( 'id' => 'smwfootbr' ) ) . $list : '' );

Expand Down Expand Up @@ -77,7 +84,7 @@ protected function getIntroductoryText() {
$messageKeyLong = 'smw-pa-property-predefined-long' . strtolower( $key );
}

$message .= wfMessage( $messageKey )->exists() ? wfMessage( $messageKey, $propertyName )->parse() : wfMessage( 'smw-pa-property-predefined-default' )->parse();
$message .= wfMessage( $messageKey )->exists() ? wfMessage( $messageKey, $propertyName )->parse() : wfMessage( 'smw-pa-property-predefined-default', $propertyName )->parse();
$message .= wfMessage( $messageKeyLong )->exists() ? ' ' . wfMessage( $messageKeyLong )->parse() : '';
$message .= ' ' . wfMessage( 'smw-pa-property-predefined-common' )->parse();

Expand Down
25 changes: 25 additions & 0 deletions includes/dataitems/SMW_DI_Property.php
Expand Up @@ -208,6 +208,31 @@ public function getCanonicalLabel() {
return $prefix . PropertyRegistry::getInstance()->findCanonicalPropertyLabelById( $this->m_key );
}

/**
* Borrowing the skos:prefLabel definition where a preferred label is expected
* to have only one label per given language (skos:altLabel can have many
* alternative labels)
*
* An empty string signals that no preferred label is available in the current
* user language.
*
* @since 2.5
*
* @param string $languageCode
*
* @return string
*/
public function getPreferredLabel( $languageCode = '' ) {

$label = PropertyRegistry::getInstance()->findPreferredPropertyLabelById( $this->m_key, $languageCode );

if ( $label !== '' ) {
return ( $this->m_inverse ? '-' : '' ) . $label;
}

return '';
}

/**
* @since 2.4
*
Expand Down
143 changes: 118 additions & 25 deletions includes/datavalues/SMW_DV_Property.php
Expand Up @@ -49,6 +49,11 @@ class SMWPropertyValue extends SMWDataValue {
*/
protected $linkAttributes = array();

/**
* @var string
*/
private $preferredLabel = '';

/**
* Cache for type value of this property, or null if not calculated yet.
* @var SMWTypesValue
Expand Down Expand Up @@ -136,10 +141,6 @@ protected function parseUserValue( $value ) {
$this->mPropTypeValue = null;
$this->m_wikipage = null;

if ( $this->m_caption === false ) { // always use this as caption
$this->m_caption = $value;
}

list( $propertyName, $inverse ) = $this->doNormalizeUserValue(
$value
);
Expand All @@ -162,11 +163,24 @@ protected function parseUserValue( $value ) {
$this->m_dataitem = $dataItem ? $dataItem : $this->m_dataitem;
}

// Copy the original DI to ensure we can compare it against a possible redirect
$this->inceptiveProperty = $this->m_dataitem;

if ( $this->isEnabledFeature( SMW_DV_PROV_REDI ) ) {
$this->m_dataitem = $this->m_dataitem->getRedirectTarget();
}

// If not external caption has been invoked, try to replace it with a preferred label
if ( $this->m_caption === false || $this->m_caption === '' ) {
$this->preferredLabel = $this->m_dataitem->getPreferredLabel( $this->getOptionBy( self::OPT_USER_LANGUAGE ) );
}

// Use the preferred label as eligible caption, if available
if ( $this->preferredLabel !== '' ) {
$this->m_caption = $this->preferredLabel;
} elseif ( $this->m_caption === false ) {
$this->m_caption = $value;
}
}

/**
Expand All @@ -182,12 +196,17 @@ protected function loadDataItem( SMWDataItem $dataItem ) {

$this->inceptiveProperty = $dataItem;
$this->m_dataitem = $dataItem;
$this->preferredLabel = $this->m_dataitem->getPreferredLabel();

$this->mPropTypeValue = null;
unset( $this->m_wikipage );
$this->m_caption = false;
$this->linkAttributes = array();

if ( $this->preferredLabel !== '' ) {
$this->m_caption = $this->preferredLabel;
}

return true;
}

Expand Down Expand Up @@ -255,7 +274,7 @@ public function getWikiPageValue() {
* @note Every user defined property is necessarily visible.
*/
public function isVisible() {
return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->getLabel() !== '' );
return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->getCanonicalLabel() !== '' );
}

/**
Expand All @@ -267,44 +286,96 @@ public function canUse() {
return $this->isValid() && $this->m_dataitem->isUnrestricted();
}

public function getShortWikiText( $linked = null ) {
public function getShortWikiText( $linker = null ) {

if ( !$this->isVisible() ) {
return '';
}

$wikiPageValue = $this->getWikiPageValue();

if ( $wikiPageValue === null ) {
return '';
}

if ( $this->isVisible() ) {
$wikiPageValue = $this->getWikiPageValue();
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortWikiText( $linked ) );
$label = $this->m_dataitem->getLabel();

if ( $this->m_caption !== false && $this->m_caption !== '' ) {
$wikiPageValue->setCaption( $this->m_caption );
} elseif ( $this->preferredLabel !== '' ) {
$wikiPageValue->setCaption( $this->preferredLabel );
} else {
$wikiPageValue->setCaption( $label );
}

return '';
return $this->highlightText( $wikiPageValue->getShortWikiText( $linker ) ) . $this->hintPreferredLabelUse( $label );
}

public function getShortHTMLText( $linked = null ) {
public function getShortHTMLText( $linker = null ) {

if ( !$this->isVisible() ) {
return '';
}

$wikiPageValue = $this->getWikiPageValue();

if ( $wikiPageValue === null ) {
return '';
}

$label = $this->m_dataitem->getLabel();

if ( $this->isVisible() ) {
$wikiPageValue = $this->getWikiPageValue();
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortHTMLText( $linked ), $linked );
if ( $this->m_caption !== false && $this->m_caption !== '' ) {
$wikiPageValue->setCaption( $this->m_caption );
} elseif ( $this->preferredLabel !== '' ) {
$wikiPageValue->setCaption( $this->preferredLabel );
} else {
$wikiPageValue->setCaption( $label );
}

return '';
return $this->highlightText( $wikiPageValue->getShortHTMLText( $linker ), $linker ) . $this->hintPreferredLabelUse( $label );
}

public function getLongWikiText( $linked = null ) {
public function getLongWikiText( $linker = null ) {

if ( $this->isVisible() ) {
$wikiPageValue = $this->getWikiPageValue();
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongWikiText( $linked ) );
if ( !$this->isVisible() ) {
return '';
}

return '';
$wikiPageValue = $this->getWikiPageValue();

if ( $wikiPageValue === null ) {
return '';
}

if ( $this->m_caption !== false && $this->m_caption !== '' ) {
$wikiPageValue->setCaption( $this->m_caption );
} elseif ( $this->preferredLabel !== '' ) {
$wikiPageValue->setCaption( $this->preferredLabel );
}

return $this->highlightText( $wikiPageValue->getLongWikiText( $linker ) );
}

public function getLongHTMLText( $linked = null ) {
public function getLongHTMLText( $linker = null ) {

if ( !$this->isVisible() ) {
return '';
}

$wikiPageValue = $this->getWikiPageValue();

if ( $wikiPageValue === null ) {
return '';
}

if ( $this->isVisible() ) {
$wikiPageValue = $this->getWikiPageValue();
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongHTMLText( $linked ), $linked );
if ( $this->m_caption !== false && $this->m_caption !== '' ) {
$wikiPageValue->setCaption( $this->m_caption );
} elseif ( $this->preferredLabel !== '' ) {
$wikiPageValue->setCaption( $this->preferredLabel );
}

return '';
return $this->highlightText( $wikiPageValue->getLongHTMLText( $linker ), $linker );
}

public function getWikiValue() {
Expand All @@ -313,6 +384,10 @@ public function getWikiValue() {
return '';
}

if ( $this->preferredLabel !== '' ) {
return $this->preferredLabel;
}

if ( $this->getWikiPageValue() !== null && $this->getWikiPageValue()->getDisplayTitle() !== '' ) {
return $this->getWikiPageValue()->getDisplayTitle();
}
Expand Down Expand Up @@ -488,4 +563,22 @@ private function doNormalizeUserValue( $value ) {
return array( $propertyName, $inverse );
}

private function hintPreferredLabelUse( $label ) {

if ( !$this->isEnabledFeature( SMW_DV_PROV_LHNT ) ||
$this->preferredLabel === $this->m_dataitem->getCanonicalLabel() ||
$this->preferredLabel === '' ) {
return '';
}

$preferredLabelMarker = '';

// if ( $this->preferredLabel !== '' && $this->preferredLabel !== $label && $this->preferredLabel !== $canonicalLabel || $canonicalLabel !== $label ) {
if ( $this->preferredLabel !== $label ) {
$preferredLabelMarker = '&nbsp;' . \Html::rawElement( 'span', array( 'title' => $label ), '<sup>ᵖ</sup>' );
}

return $preferredLabelMarker;
}

}
2 changes: 1 addition & 1 deletion includes/export/SMW_ExportController.php
Expand Up @@ -191,7 +191,7 @@ protected function serializePage( SMWDIWikiPage $diWikiPage, $recursiondepth = 1
$inprops = \SMW\StoreFactory::getStore()->getInProperties( $diWikiPage );

foreach ( $inprops as $inprop ) {
$propWikiPage = $inprop->getDiWikiPage();
$propWikiPage = $inprop->getCanonicalDiWikiPage();

if ( !is_null( $propWikiPage ) ) {
$this->queuePage( $propWikiPage, 0 ); // no real recursion along properties
Expand Down
10 changes: 7 additions & 3 deletions includes/storage/SQLStore/SMW_DIHandler_WikiPage.php
@@ -1,4 +1,8 @@
<?php

use SMW\DIProperty;
use SMW\DIWikiPage;

/**
* @author Nischay Nahata
* @author Markus Kroetzsch
Expand Down Expand Up @@ -111,8 +115,8 @@ public function dataItemFromDBKeys( $dbkeys ) {
if ( $namespace == SMW_NS_PROPERTY && $dbkeys[0] != '' &&
$dbkeys[0]{0} == '_' && $dbkeys[2] == '' ) {
// Correctly interpret internal property keys
$property = new SMW\DIProperty( $dbkeys[0] );
$wikipage = $property->getDiWikiPage( $dbkeys[4] );
$property = new DIProperty( $dbkeys[0] );
$wikipage = $property->getCanonicalDiWikiPage( $dbkeys[4] );
if ( !is_null( $wikipage ) ) {
return $wikipage;
}
Expand All @@ -126,7 +130,7 @@ public function dataItemFromDBKeys( $dbkeys ) {

private function newDiWikiPage( $dbkeys ) {

$diWikiPage = new SMWDIWikiPage(
$diWikiPage = new DIWikiPage(
$dbkeys[0],
intval( $dbkeys[1] ),
$dbkeys[2],
Expand Down
1 change: 1 addition & 0 deletions includes/storage/SQLStore/SMW_Sql3SmwIds.php
Expand Up @@ -179,6 +179,7 @@ class SMWSql3SmwIds {
'_INST' => 4,
'_UNIT' => 7,
'_IMPO' => 8,
'_PPLB' => 9,
'_PDESC' => 10,
'_PREC' => 11,
'_CONV' => 12,
Expand Down
6 changes: 4 additions & 2 deletions languages/i18n/en.json
Expand Up @@ -87,7 +87,8 @@
"_DTITLE": "Display title of",
"_PVUC": "Has uniqueness constraint",
"_PEID": "External identifier",
"_PEFU": "External formatter uri"
"_PEFU": "External formatter uri",
"_PPLB": "Has preferred property label"
},
"propertyAliases": {
"Display unit": "_UNIT",
Expand Down Expand Up @@ -126,7 +127,8 @@
"Has display title of": "_DTITLE",
"Has uniqueness constraint": "_PVUC",
"External identifier": "_PEID",
"External formatter uri": "_PEFU"
"External formatter uri": "_PEFU",
"Preferred property label": "_PPLB"
},
"namespaces":{
"SMW_NS_PROPERTY": "Property",
Expand Down

0 comments on commit 00123c7

Please sign in to comment.