Skip to content

Commit

Permalink
Support for DISPLAYTITLE caption
Browse files Browse the repository at this point in the history
Adds general support for subjects that contain a `{{DISPLAYTITLE:Foo}}`.
The pre-defined property `_DTITLE` is unrestricted therefore manually
annotation via `Display title of` are allowed.
  • Loading branch information
mwjames committed Feb 29, 2016
1 parent 2eb2c05 commit 583085c
Show file tree
Hide file tree
Showing 50 changed files with 667 additions and 100 deletions.
13 changes: 12 additions & 1 deletion SemanticMediaWiki.settings.php
Expand Up @@ -964,6 +964,17 @@
# - SMW_DV_PVAP (Allows pattern) to allow regular expression pattern matching
# when `Allows pattern` property is assigned to user-defined property
#
# - SMW_DV_WPV_DTITLE (WikiPageValue) is to allow requesting a lookup for a display
# title and if present will be used as caption for the invoked subject
#
# - SMW_DV_PROV_DTITLE (PropertyValue) in combination with SMW_DV_WPV_DTITLE; If
# enabled it will attempt to resolve a property label by matching it against a
# possible assigned property "Display title of" value. For example, property
# "Foo" has "Display title of" "hasFoolishFoo" where "hasFoolishFoo" is being
# resolved as "Foo" when creating annotations. Currently, this uses an
# uncached lookup and therefore is disabled by default to avoid a possible
# performance impact (which has not been established or analyzed).
#
# @since 2.4
##
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE | SMW_DV_PVAP;
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE | SMW_DV_PVAP | SMW_DV_WPV_DTITLE;
3 changes: 2 additions & 1 deletion i18n/en.json
Expand Up @@ -382,5 +382,6 @@
"smw-datavalue-allows-pattern-mismatch": "\"$1\" was classified as invalid by the \"$2\" regular expression.",
"smw-datavalue-allows-pattern-reference-unknown": "The \"$1\" pattern reference could not be match to an entry in [[MediaWiki:Smw allows pattern]].",
"smw-datavalue-feature-not-supported": "The \"$1\" feature is not supported or was disabled on this wiki.",
"smw-pa-property-predefined_pvap": "\"$1\" is a predefined property that can specify a [[MediaWiki:Smw allows pattern|pattern reference]] to apply [https://en.wikipedia.org/wiki/Regular_expression regular expression] matching and is provided by [https://www.semantic-mediawiki.org/wiki/Help:Special_properties Semantic MediaWiki]."
"smw-pa-property-predefined_pvap": "\"$1\" is a predefined property that can specify a [[MediaWiki:Smw allows pattern|pattern reference]] to apply [https://en.wikipedia.org/wiki/Regular_expression regular expression] matching and is provided by [https://www.semantic-mediawiki.org/wiki/Help:Special_properties Semantic MediaWiki].",
"smw-pa-property-predefined_dtitle": "\"$1\" is a predefined property that can assign a distinct display title to an entity and is provided by [https://www.semantic-mediawiki.org/wiki/Help:Special_properties Semantic MediaWiki]."
}
2 changes: 2 additions & 0 deletions includes/Defines.php
Expand Up @@ -142,4 +142,6 @@
define( 'SMW_DV_PROV_REDI', 2 ); // PropertyValue to follow a property redirect target
define( 'SMW_DV_MLTV_LCODE', 4 ); // MonolingualTextValue requires language code
define( 'SMW_DV_PVAP', 16 ); // Allows pattern
define( 'SMW_DV_WPV_DTITLE', 32 ); // WikiPageValue to use an explicit display title
define( 'SMW_DV_PROV_DTITLE', 64 ); // PropertyValue allow to find a property using the display title
/**@}*/
9 changes: 1 addition & 8 deletions includes/SMW_Infolink.php
Expand Up @@ -461,14 +461,7 @@ static public function decodeParameters( $titleParam = '', $allParams = false )

foreach ( $ps as $p ) {
if ( $p !== '' ) {
$p = rawurldecode( str_replace( '-', '%', $p ) );
$parts = explode( '=', $p, 2 );

if ( count( $parts ) > 1 ) {
$result[$parts[0]] = $parts[1];
} else {
$result[] = $p;
}
$result[] = rawurldecode( str_replace( '-', '%', $p ) );
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions includes/articlepages/SMW_PropertyPage.php
Expand Up @@ -153,10 +153,15 @@ protected function getPropertyValueList() {

if ( count( $diWikiPages ) > 0 ) {
$pageLister = new SMWPageLister( $diWikiPages, null, $this->limit, $this->from, $this->until );

$this->mTitle->setFragment( '#SMWResults' ); // Make navigation point to the result list.
$navigation = $pageLister->getNavigationLinks( $this->mTitle );

$titleText = htmlspecialchars( $this->mTitle->getText() );
$dvWikiPage = DataValueFactory::getInstance()->newDataItemValue(
$this->mProperty
);

$titleText = htmlspecialchars( $dvWikiPage->getWikiValue() );
$resultNumber = min( $this->limit, count( $diWikiPages ) );

$result .= "<a name=\"SMWResults\"></a><div id=\"mw-pages\">\n" .
Expand Down Expand Up @@ -210,7 +215,7 @@ protected function subjectObjectList( array $diWikiPages ) {
}

// Property name
$searchlink = SMWInfolink::newBrowsingLink( '+', $dvWikiPage->getShortHTMLText() );
$searchlink = SMWInfolink::newBrowsingLink( '+', $dvWikiPage->getWikiValue() );
$r .= '<tr><td class="smwpropname">' . $dvWikiPage->getShortHTMLText( smwfGetLinker() ) .
'&#160;' . $searchlink->getHTML( smwfGetLinker() ) . '</td><td class="smwprops">';

Expand Down
51 changes: 40 additions & 11 deletions includes/datavalues/SMW_DV_Property.php
Expand Up @@ -57,15 +57,6 @@ public function __construct( $typeid ) {
parent::__construct( $typeid );
}

/**
* @since 2.4
*
* @return boolean
*/
public function isToFindPropertyRedirect() {
return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_PROV_REDI ) != 0;
}

/**
* Static function for creating a new property object from a
* propertyname (string) as a user might enter it.
Expand Down Expand Up @@ -152,9 +143,18 @@ protected function parseUserValue( $value ) {
$this->m_dataitem = new SMWDIProperty( 'ERROR', false ); // just to have something
}

// @see the SMW_DV_PROV_DTITLE explanation
if ( $this->canFindPropertyByDisplayTitle() ) {
$dataItem = $this->getPropertySpecificationLookup()->getPropertyFromDisplayTitle(
$value
);

$this->m_dataitem = $dataItem ? $dataItem : $this->m_dataitem;
}

$this->inceptiveProperty = $this->m_dataitem;

if ( $this->isToFindPropertyRedirect() ) {
if ( $this->canFindPropertyRedirect() ) {
$this->m_dataitem = $this->m_dataitem->getRedirectTarget();
}
}
Expand Down Expand Up @@ -217,6 +217,7 @@ public function getWikiPageValue() {
$this->m_wikipage = null;
}
}

return $this->m_wikipage;
}

Expand Down Expand Up @@ -279,7 +280,16 @@ public function getLongHTMLText( $linked = null ) {
}

public function getWikiValue() {
return $this->isVisible() ? $this->m_dataitem->getLabel() : '';

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

if ( $this->getWikiPageValue() !== null && $this->getWikiPageValue()->getDisplayTitle() !== '' ) {
return $this->getWikiPageValue()->getDisplayTitle();
}

return $this->m_dataitem->getLabel();
}

/**
Expand Down Expand Up @@ -341,6 +351,25 @@ protected function highlightText( $text, $linker = null ) {
return $text;
}


/**
* @since 2.4
*
* @return boolean
*/
protected function canFindPropertyRedirect() {
return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_PROV_REDI ) != 0;
}

/**
* @since 2.4
*
* @return boolean
*/
protected function canFindPropertyByDisplayTitle() {
return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_PROV_DTITLE ) != 0;
}

/**
* A function for registering/overwriting predefined properties for SMW. Should be called from
* within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid
Expand Down
101 changes: 76 additions & 25 deletions includes/datavalues/SMW_DV_WikiPage.php
@@ -1,4 +1,8 @@
<?php

use SMW\ApplicationFactory;
use SMW\DIProperty;

/**
* @ingroup SMWDataValues
*/
Expand Down Expand Up @@ -143,24 +147,25 @@ protected function loadDataItem( SMWDataItem $dataItem ) {
$dataItem = $dataItem->getSemanticData()->getSubject();
}

if ( $dataItem->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) {
$this->m_dataitem = $dataItem;
$this->m_id = -1;
$this->m_title = null;
$this->m_fragment = $dataItem->getSubobjectName();
$this->m_prefixedtext = '';
$this->m_caption = false; // this class can handle this

if ( ( $this->m_fixNamespace != NS_MAIN ) &&
( $this->m_fixNamespace != $dataItem->getNamespace() ) ) {
global $wgContLang;
$this->addError( wfMessage( 'smw_wrong_namespace',
$wgContLang->getNsText( $this->m_fixNamespace ) )->inContentLanguage()->text() );
}
return true;
} else {
if ( $dataItem->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) {
return false;
}

$this->m_dataitem = $dataItem;
$this->m_id = -1;
$this->m_title = null;
$this->m_fragment = $dataItem->getSubobjectName();
$this->m_prefixedtext = '';
$this->m_caption = false; // this class can handle this

if ( ( $this->m_fixNamespace != NS_MAIN ) &&
( $this->m_fixNamespace != $dataItem->getNamespace() ) ) {
global $wgContLang;
$this->addError( wfMessage( 'smw_wrong_namespace',
$wgContLang->getNsText( $this->m_fixNamespace ) )->inContentLanguage()->text() );
}

return true;
}

/**
Expand Down Expand Up @@ -241,6 +246,12 @@ public function getShortHTMLText( $linker = null ) {
$this->getTitle();
}

$displayTitle = $this->getDisplayTitle();

if ( $displayTitle !== '' && $linker === null ) {
return htmlspecialchars( $displayTitle );
}

if ( is_null( $linker ) || $linker === false || !$this->isValid() ||
$this->m_outformat == '-' || $this->m_caption === '' ) {

Expand Down Expand Up @@ -489,7 +500,14 @@ protected function getShortCaptionText() {
} else {
$fragmentText = '';
}
return $this->getText() . $fragmentText;

$displayTitle = $this->getDisplayTitle();

if ( $displayTitle === '' ) {
$displayTitle = $this->getText();
}

return $displayTitle . $fragmentText;
}

/**
Expand All @@ -508,7 +526,14 @@ protected function getLongCaptionText() {
} else {
$fragmentText = '';
}
return ( $this->m_fixNamespace == NS_MAIN ? $this->getPrefixedText() : $this->getText() ) . $fragmentText;

$displayTitle = $this->getDisplayTitle();

if ( $displayTitle === '' ) {
$displayTitle = $this->m_fixNamespace == NS_MAIN ? $this->getPrefixedText() : $this->getText();
}

return $displayTitle . $fragmentText;
}

/**
Expand All @@ -535,6 +560,20 @@ public function getSortKey() {
return \SMW\StoreFactory::getStore()->getWikiPageSortKey( $this->m_dataitem );
}

/**
* @since 2.4
*
* @return string
*/
public function getDisplayTitle() {

if ( $this->m_dataitem === null || ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_WPV_DTITLE ) == 0 ) {
return '';
}

return $this->findDisplayTitleFor( $this->m_dataitem );
}

/**
* Static function for creating a new wikipage object from
* data as it is typically stored internally. In particular,
Expand Down Expand Up @@ -571,11 +610,23 @@ static public function makePageFromTitle( Title $title ) {
return $dvWikiPage;
}

}
private function findDisplayTitleFor( $subject ) {

/**
* SMW\WikiPageValue
*
* @since 1.9
*/
class_alias( 'SMWWikiPageValue', 'SMW\WikiPageValue' );
$displayTitle = '';

$dataItems = ApplicationFactory::getInstance()->getCachedPropertyValuesPrefetcher()->getPropertyValues(
$subject,
new DIProperty( '_DTITLE' )
);

if ( $dataItems !== null && $dataItems !== array() ) {
$displayTitle = end( $dataItems )->getString();
} elseif ( $subject->getSubobjectName() !== '' ) {
// Check whether the base subject has a DISPLAYTITLE
return $this->findDisplayTitleFor( $subject->asBase() );
}

return $displayTitle;
}

}
5 changes: 4 additions & 1 deletion includes/storage/SMW_QueryResult.php
Expand Up @@ -304,6 +304,8 @@ public function serializeToArray() {
*/
public function toArray() {

$time = microtime( true );

// @note micro optimization: We call getSerializedQueryResult()
// only once and create the hash here instead of calling getHash()
// to avoid getSerializedQueryResult() being called again
Expand All @@ -315,7 +317,8 @@ public function toArray() {
'hash' => HashBuilder::createHashIdForContent( $serializeArray ),
'count' => $this->getCount(),
'offset' => $this->mQuery->getOffset(),
'source' => $this->mQuery->getQuerySource()
'source' => $this->mQuery->getQuerySource(),
'time' => number_format( ( microtime( true ) - $time ), 6, '.', '' )
)
)
);
Expand Down
1 change: 1 addition & 0 deletions includes/storage/SQLStore/SMW_Sql3SmwIds.php
Expand Up @@ -182,6 +182,7 @@ class SMWSql3SmwIds {
'_SERV' => 13,
'_PVAL' => 14,
'_REDI' => 15,
'_DTITLE' => 16,
'_SUBP' => 17,
'_SUBC' => 18,
'_CONC' => 19,
Expand Down
3 changes: 2 additions & 1 deletion languages/SMW_Language.php
Expand Up @@ -101,7 +101,8 @@ abstract class SMWLanguage {
'Language code' => '_LCODE',
'Text' => '_TEXT',
'Has property description' => '_PDESC',
'Allows pattern' => '_PVAP'
'Allows pattern' => '_PVAP',
'Display title of' => '_DTITLE'
);

public function __construct() {
Expand Down
3 changes: 2 additions & 1 deletion languages/SMW_LanguageAr.php
Expand Up @@ -82,7 +82,8 @@ class SMWLanguageAr extends SMWLanguage {
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
'_PVAP' => 'Allows pattern',
'_DTITLE' => 'Display title of'
);

protected $m_SpecialPropertyAliases = array(
Expand Down
3 changes: 2 additions & 1 deletion languages/SMW_LanguageArz.php
Expand Up @@ -82,7 +82,8 @@ class SMWLanguageArz extends SMWLanguage {
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
'_PVAP' => 'Allows pattern',
'_DTITLE' => 'Display title of'
);

protected $m_SpecialPropertyAliases = array(
Expand Down
3 changes: 2 additions & 1 deletion languages/SMW_LanguageCa.php
Expand Up @@ -86,7 +86,8 @@ class SMWLanguageCa extends SMWLanguage {
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
'_PVAP' => 'Allows pattern',
'_DTITLE' => 'Display title of'
);

protected $m_SpecialPropertyAliases = array(
Expand Down
3 changes: 2 additions & 1 deletion languages/SMW_LanguageDe.php
Expand Up @@ -92,7 +92,8 @@ class SMWLanguageDe extends SMWLanguage {
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
'_PVAP' => 'Allows pattern',
'_DTITLE' => 'Display title of'
);

protected $m_SpecialPropertyAliases = array(
Expand Down

0 comments on commit 583085c

Please sign in to comment.