Skip to content

Commit

Permalink
Merge pull request #1568 from SemanticMediaWiki/invalid
Browse files Browse the repository at this point in the history
Remove/strip invalid characters/tags from property name, refs #1567
  • Loading branch information
mwjames committed May 12, 2016
2 parents a2dec45 + 6c4781f commit 6cbf2b7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
1 change: 1 addition & 0 deletions i18n/en.json
Expand Up @@ -341,6 +341,7 @@
"smw-pa-property-predefined_errp": "\"$1\" is a predefined property to track input errors for irregular value annotations that was likely caused by type or [[Property:Allows value|allowed value]] restrictions and is provided by [https://www.semantic-mediawiki.org/wiki/Help:Special_properties Semantic MediaWiki].",
"smw-pa-property-predefined_pval": "[https://www.semantic-mediawiki.org/wiki/Help:Special_property_Allows_value \"$1\"] is a predefined property that can define a list of permissible values to restrict value assignments for a property and is provided by [https://www.semantic-mediawiki.org/wiki/Help:Special_properties Semantic MediaWiki].",
"smw-datavalue-property-restricted-use": "Property \"$1\" has been marked for restricted use.",
"smw-datavalue-property-invalid-name": "Property name contains invalid characters (e.g. $1) .",
"smw-datavalue-restricted-use": "Datavalue \"$1\" has been marked for restricted use.",
"smw-datavalue-invalid-number": "\"$1\" can not be interpreted as a number.",
"smw-query-condition-circular": "A possible circular condition has been detected in \"$1\".",
Expand Down
38 changes: 29 additions & 9 deletions includes/datavalues/SMW_DV_Property.php
Expand Up @@ -133,17 +133,13 @@ protected function parseUserValue( $value ) {
if ( $this->m_caption === false ) { // always use this as caption
$this->m_caption = $value;
}
$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); // slightly normalise label
$inverse = false;
if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse
$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) );
/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
$inverse = true;
}

list( $propertyName, $inverse ) = $this->doNormalizeUserValue(
$value
);

try {
$this->m_dataitem = DIProperty::newFromUserLabel( $propertyName, $inverse, $this->m_typeid );
$this->m_dataitem = DIProperty::newFromUserLabel( $propertyName, $inverse );
} catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-"
$this->addError( wfMessage( 'smw_noproperty', $value )->inContentLanguage()->text() );
$this->m_dataitem = new DIProperty( 'ERROR', false ); // just to have something
Expand Down Expand Up @@ -444,4 +440,28 @@ public function getText() {
return $this->m_dataitem->getLabel();
}

private function doNormalizeUserValue( $value ) {

// #1567
if ( strpos( $value, '#' ) !== false ) {
$this->addErrorMsg( array( 'smw-datavalue-property-invalid-name', '#' ) );
$this->m_dataitem = new DIProperty( 'ERROR', false );
}

$value = strip_tags( $value );
$inverse = false;

// slightly normalise label
$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) );

if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse
$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) );
/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
$inverse = true;
}

return array( $propertyName, $inverse );
}

}
69 changes: 69 additions & 0 deletions tests/phpunit/Integration/ByJsonScript/Fixtures/p-0102.json
@@ -0,0 +1,69 @@
{
"description": "Test in-text annotation on properties with invalid names/charaters (#1567, `wgContLang=en`)",
"properties": [
{
"name": "Has #",
"contents": "[[Has type::Date]]"
}
],
"subjects": [
{
"name": "Example/P0102/1",
"contents": "[[| ]]</td><::123]]"
},
{
"name": "Example/P0102/2",
"contents": "[[<code>Has property a</code>::ABC]] {{#set: <code>Has property b</code>=DEF }}"
},
{
"name": "Example/P0102/3",
"contents": "[[<code>Has property e::ABC]] {{#set: <code>Has property f=DEF }}"
}
],
"parser-testcases": [
{
"about": "#0 do not allow #",
"subject": "Example/P0102/1",
"store": {
"semantic-data": {
"strict-mode-valuematch": false,
"propertyCount": 3,
"propertyKeys": [ "_ERRC", "_MDAT", "_SKEY" ]
}
}
},
{
"about": "#1 strip tags",
"subject": "Example/P0102/2",
"store": {
"semantic-data": {
"strict-mode-valuematch": false,
"propertyCount": 4,
"propertyKeys": [ "Has_property_a", "Has_property_b", "_MDAT", "_SKEY" ],
"propertyValues": [ "ABC", "DEF" ]
}
}
},
{
"about": "#2 remove broken tags",
"subject": "Example/P0102/3",
"store": {
"semantic-data": {
"strict-mode-valuematch": false,
"propertyCount": 4,
"propertyKeys": [ "Has_property_e", "Has_property_f", "_MDAT", "_SKEY" ],
"propertyValues": [ "ABC", "DEF" ]
}
}
}
],
"settings": {
"wgContLang": "en",
"smwgPageSpecialProperties": [ "_MDAT" ]
},
"meta": {
"version": "0.1",
"is-incomplete": false,
"debug": false
}
}

0 comments on commit 6cbf2b7

Please sign in to comment.