New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define value constraint using regular expressions #1417

Merged
merged 1 commit into from Feb 29, 2016
Jump to file or symbol
Failed to load files and symbols.
+1,223 −164
Diff settings

Always

Just for now

Define value constraint using regular expressions

Similar to `Allows value`, the `Allows pattern` property is to specify a
regular expression to match permissible values for an assigned property.
If a value cannot be matched then a general purpose message is being
displayed.

To avoid any issues with expressions that include | or [] it is suggested
to use #set (`|+pipe` indicator ensures that MW doesn't interfere with the
expression):

```
Whitelist

{{#set:
 |Allows pattern=^(Foo|Foo bar|Bar)$|+pipe
}}
```
```
IPv4

{{#set:
 |Al1lows pattern=(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|+pipe
}}
```

`$GLOBALS['smwgDVFeatures'] = SMW_DV_PAVP` is enabled by default but if removed
will disable the functionality.
  • Loading branch information...
mwjames committed Feb 29, 2016
commit 4d0b9f9108c409fc5d8e32568aec21db0f5ae97e
@@ -514,7 +514,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' );
$GLOBALS['smwgDeclarationProperties'] = array( '_PVAL', '_LIST', '_PVAP' );
##
// some default settings which usually need no modification
@@ -953,15 +953,17 @@
# - SMW_DV_PROV_REDI (PropertyValue) If a property is redirected to a different
# target (Foo -> Bar) then follow it by default in order to allow query results
# to be displayed equivalent for both queries without having to adjust
# (or change) a query.
#
# This flag is mainly provided to restore backwards compatibility where
# behaviour is not expected to be altered, nevertheless it is recommended that
# the setting is enabled to improve user friendliness in terms of query execution.
# (or change) a query. This flag is mainly provided to restore backwards
# compatibility where behaviour is not expected to be altered, nevertheless it is
# recommended that the setting is enabled to improve user friendliness in terms
# of query execution.
#
# - SMW_DV_MLTV_LCODE (MonolingualTextValue) is to require a language code in order
# for a DV to be completed otherwise a MLTV can operate without a language code
#
# - SMW_DV_PVAP (Allows pattern) to allow regular expression pattern matching
# when `Allows pattern` property is assigned to user-defined property
#
# @since 2.4
##
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE;
$GLOBALS['smwgDVFeatures'] = SMW_DV_PROV_REDI | SMW_DV_MLTV_LCODE | SMW_DV_PVAP;
View
@@ -200,7 +200,7 @@
"smw-ask-sorting": "Sorting",
"smw-ask-search": "Search",
"smw-ask-format-selection-help": "For a detailed description, please visit the $1 help page.",
"smw-ask-query-search-info":"The query <code><nowiki>$1</nowiki></code> was answered by the $2 in $3 sec.",
"smw-ask-query-search-info":"The query <code><nowiki>$1</nowiki></code> was answered by the <code>$2</code> in $3 sec.",
"searchbyproperty": "Search by property",
"smw_sbv_docu": "Search for all pages that have a given property and value.",
"smw_sbv_novalue": "Enter a valid value for the property, or view all property values for \"$1\".",
@@ -288,9 +288,14 @@
"smw_conceptarticlecount": "Showing below $1 {{PLURAL:$1|page|pages}}.",
"smw-qp-empty-data": "Requested data could not be displayed due to some insufficient selection criteria.",
"right-smw-admin": "Access administration tasks (Semantic MediaWiki)",
"group-smwadministrator": "Semantic MediaWiki administrators",
"group-smwadministrator-member": "{{GENDER:$1|administrator (SMW)}}",
"grouppage-smwadministrator": "{{ns:project}}:SMW_administrators",
"right-smw-patternedit": "Edit access to maintain allowed regular expressions and patterns (Semantic MediaWiki)",
"action-smw-patternedit": "edit regular expressions used by Semantic MediaWiki",
"group-smwadministrator": "Administrators (Semantic MediaWiki)",
"group-smwadministrator-member": "{{GENDER:$1|administrator (Semantic MediaWiki)}}",
"grouppage-smwadministrator": "{{ns:project}}:Administrators (Semantic MediaWiki)",
"group-smwcurator": "Curators (Semantic MediaWiki)",
"group-smwcurator-member": "{{GENDER:$1|curator (Semantic MediaWiki)}}",
"grouppage-smwcurator": "{{ns:project}}:Curators (Semantic MediaWiki)",
"action-smw-admin": "access Semantic MediaWiki administration tasks",
"smw-pa-property-predefined-default": "\"$1\" is a predefined property.",
"smw-pa-property-predefined-common": "This property is pre-deployed (also known as [https://www.semantic-mediawiki.org/wiki/Help:Special_properties special property]) and comes with additional administrative privileges but can be used just like any other [https://www.semantic-mediawiki.org/wiki/Property user-defined property].",
@@ -372,5 +377,10 @@
"smw-limitreport-intext-parsertime": "[SMW] In-text annotation parser time",
"smw-limitreport-intext-parsertime-value" : "$1 {{PLURAL:$1|second|seconds}}",
"smw-limitreport-pagepurge-storeupdatetime": "[SMW] Store update time (on page purge)",
"smw-limitreport-pagepurge-storeupdatetime-value" : "$1 {{PLURAL:$1|second|seconds}}"
"smw-limitreport-pagepurge-storeupdatetime-value" : "$1 {{PLURAL:$1|second|seconds}}",
"smw_allows_pattern": "This page is expected to contain a list of references (followed by [https://en.wikipedia.org/wiki/Regular_expression regular expressions]) and to be made available using the [[Property:Allows pattern|Allows pattern]] property.",
"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]."
}
View
@@ -295,6 +295,11 @@
"group-smwadministrator-member": "{{doc-group|smwadministrator|member}}",
"grouppage-smwadministrator": "{{doc-group|smwadministrator|page}}",
"action-smw-admin": "{{doc-action|smw-admin}}",
"right-smw-patternedit": "{{doc-right|smw-patternedit}}",
"action-smw-patternedit": "{{doc-action|smw-patternedit}}",
"group-smwcurator": "{{doc-group|smwcurator|group}}",
"group-smwcurator-member": "{{doc-group|smwcurator|member}}",
"grouppage-smwcurator": "{{doc-group|smwcurator|page}}",
"smw-pa-property-predefined-default": "This message is the default introductory text for [https://semantic-mediawiki.org/wiki/Help:Special_properties special (predefined) properties].\n\nParameter:\n* $1 - Name of the invoked predefined property",
"smw-pa-property-predefined-common": "This informatory message explains what [https://semantic-mediawiki.org/wiki/Help:Special_properties special property] is.",
"smw-pa-property-predefined_ask": "This informatory message describes a [https://semantic-mediawiki.org/wiki/Help:Special_properties special property].\n\nParameter:\n* $1 - name of predefined property",
@@ -373,5 +378,10 @@
"smw-limitreport-intext-parsertime": "This is an informatory message that extends the \"NewPP limit report\" generated by MediaWiki shown when viewing the raw HTML source of a generated page, e.g. [[Media:NewPP limit report swm.png|NewPP limit report swm]].\n\n{{doc-important|Do not translate: [SMW]}}",
"smw-limitreport-intext-parsertime-value": "This is an informatory message that extends the \"NewPP limit report\" generated by MediaWiki shown when viewing the raw HTML source of a generated page, e.g. [[Media:NewPP limit report swm.png|NewPP limit report swm]].\n\n* $1 holds the value of the seconds elapsed.\n\n{{doc-important|Do not translate: [SMW]}}\n{{Identical|Second}}",
"smw-limitreport-pagepurge-storeupdatetime": "This is an informatory message that extends the \"NewPP limit report\" generated by MediaWiki shown when viewing the raw HTML source of a generated page, e.g. [[Media:NewPP limit report swm.png|NewPP limit report swm]]. Store refers to the type of database backend used by the software, i.e. a SQL-store or RDF-store.\n\n{{doc-important|Do not translate: [SMW]}}",
"smw-limitreport-pagepurge-storeupdatetime-value": "This is an informatory message that extends the \"NewPP limit report\" generated by MediaWiki shown when viewing the raw HTML source of a generated page, e.g. [[Media:NewPP limit report swm.png|NewPP limit report swm]].\n\n* $1 holds the value of the seconds elapsed (always plural).\n\n{{doc-important|Do not translate: [SMW]}}\n{{Identical|Second}}"
"smw-limitreport-pagepurge-storeupdatetime-value": "This is an informatory message that extends the \"NewPP limit report\" generated by MediaWiki shown when viewing the raw HTML source of a generated page, e.g. [[Media:NewPP limit report swm.png|NewPP limit report swm]].\n\n* $1 holds the value of the seconds elapsed (always plural).\n\n{{doc-important|Do not translate: [SMW]}}\n{{Identical|Second}}",
"smw_allows_pattern": "Modifications are expected to occur on a local wiki only as this page represents a placeholder.",
"smw-datavalue-allows-pattern-mismatch": "This is an error message.",
"smw-datavalue-allows-pattern-reference-unknown": "This is an error message.",
"smw-datavalue-feature-not-supported": "This is an error message.",
"smw-pa-property-predefined_pvap": "This informatory message describes the [https://semantic-mediawiki.org/wiki/Help:Special_properties special property].\n\n* $1 holds the name of the special property."
}
View
@@ -141,4 +141,5 @@
define( 'SMW_DV_NONE', 0 );
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
/**@}*/
View
@@ -200,12 +200,17 @@ private function registerPermissions() {
// Rights
$this->globalVars['wgAvailableRights'][] = 'smw-admin';
$this->globalVars['wgAvailableRights'][] = 'smw-patternedit';
// User group rights
if ( !isset( $this->globalVars['wgGroupPermissions']['sysop']['smw-admin'] ) ) {
$this->globalVars['wgGroupPermissions']['sysop']['smw-admin'] = true;
}
if ( !isset( $this->globalVars['wgGroupPermissions']['smwcurator']['smw-patternedit'] ) ) {
$this->globalVars['wgGroupPermissions']['smwcurator']['smw-patternedit'] = true;
}
if ( !isset( $this->globalVars['wgGroupPermissions']['smwadministrator']['smw-admin'] ) ) {
$this->globalVars['wgGroupPermissions']['smwadministrator']['smw-admin'] = true;
}
@@ -12,12 +12,18 @@
*/
class SMWStringValue extends SMWDataValue {
/**
* @see DataValue::parseUserValue
*
* @param string $value
*/
protected function parseUserValue( $value ) {
if ( $value === '' ) {
$this->addError( wfMessage( 'smw_emptystring' )->inContentLanguage()->text() );
$this->addErrorMsg( 'smw_emptystring' );
}
$this->m_dataitem = new SMWDIBlob( $value, $this->m_typeid );
$this->m_dataitem = new SMWDIBlob( $value );
}
/**
@@ -158,6 +158,11 @@
*/
protected $approximateValue = false;
/**
* @var ValueConstraintValidator
*/
private $valueConstraintValidator = null;
/**
* Constructor.
*
@@ -439,18 +444,21 @@ public function addError( $error ) {
}
}
/**
* @since 2.4
*
* @param $parameters
* @param integer|null $type
* @param integer|null $language
*/
protected function addErrorMsg( $parameters, $type = null, $language = null ) {
$this->addError( Message::get( $parameters, $type, $language ) );
}
/**
* Clear error messages. This function is provided temporarily to allow
* n-ary to do this.
* properly so that this function will vanish again.
* @note Do not use this function in external code.
* @todo Check if we can remove this function again.
* @since 2.4
*/
protected function clearErrors() {
public function clearErrors() {
$this->mErrors = array();
$this->mHasErrors = false;
}
@@ -873,29 +881,7 @@ public function getErrors() {
* Creates an error if the value is illegal.
*/
protected function checkAllowedValues() {
// If no property known the no data to check
if ( $this->m_property === null || !isset( $this->m_dataitem ) ) {
return;
}
$this->checkForAllowedValueList();
}
private function checkForAllowedValueList() {
$allowsValue = DataValueFactory::getInstance()->newPropertyObjectValue(
new DIProperty( '_PVAL' ) ,
false,
false,
$this->getContextPage()
);
$allowsValue->isAllowedValueFor(
$this
);
$this->addError( $allowsValue->getErrors() );
DataValueFactory::getInstance()->getValueConstraintValidator()->doValidate( $this );
}
}
@@ -574,7 +574,7 @@ protected static function getSortingOption ( $params ) {
$result .= "</div>\n";
}
$result .= '<div id="sorting_starter" style="display: none">' . wfMessage( 'smw_ask_sortby' )->escaped() . ' <input type="text" name="sort_num" size="35" />' . "\n";
$result .= '<div id="sorting_starter" style="display: none">' . wfMessage( 'smw_ask_sortby' )->escaped() . ' <input type="text" name="sort_num" size="35" class="smw-property-input" />' . "\n";
$result .= ' <select name="order_num">' . "\n";
$result .= ' <option value="ASC">' . wfMessage( 'smw_ask_ascorder' )->escaped() . "</option>\n";
$result .= ' <option value="DESC">' . wfMessage( 'smw_ask_descorder' )->escaped() . "</option>\n</select>\n";
@@ -100,7 +100,8 @@
'Display precision of' => '_PREC',
'Language code' => '_LCODE',
'Text' => '_TEXT',
'Has property description' => '_PDESC'
'Has property description' => '_PDESC',
'Allows pattern' => '_PVAP'
);
public function __construct() {
@@ -81,7 +81,8 @@ class SMWLanguageAr extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -81,7 +81,8 @@ class SMWLanguageArz extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -85,7 +85,8 @@ class SMWLanguageCa extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -91,7 +91,8 @@ class SMWLanguageDe extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -89,13 +89,15 @@ class SMWLanguageEn extends SMWLanguage {
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
'Display unit' => '_UNIT',
'Display precision' => '_PREC',
'Property description' => '_PDESC'
'Property description' => '_PDESC',
'Allows pattern' => '_PVAP'
);
protected $m_Namespaces = array(
@@ -82,7 +82,8 @@ class SMWLanguageEs extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -78,7 +78,8 @@ class SMWLanguageFi extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_Namespaces = array(
@@ -82,7 +82,8 @@ class SMWLanguageFr extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -81,7 +81,8 @@ class SMWLanguageHe extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -86,7 +86,8 @@ class SMWLanguageHu extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -82,7 +82,8 @@ class SMWLanguageId extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -84,7 +84,8 @@ class SMWLanguageIt extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -91,7 +91,8 @@ class SMWLanguageNb extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -84,7 +84,8 @@ class SMWLanguageNl extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -100,7 +100,8 @@ class SMWLanguagePl extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
@@ -89,7 +89,8 @@ class SMWLanguagePt extends SMWLanguage {
'_PREC' => 'Display precision of',
'_LCODE' => 'Language code',
'_TEXT' => 'Text',
'_PDESC' => 'Has property description'
'_PDESC' => 'Has property description',
'_PVAP' => 'Allows pattern'
);
protected $m_SpecialPropertyAliases = array(
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.