r23843 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23842‎ | r23843 | r23844 >
Date:16:05, 7 July 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
New/updated datavalues.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Error.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_URI.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Error.php
@@ -0,0 +1,108 @@
 2+<?php
 3+
 4+/**
 5+ * This datavalue implements Error-Datavalues.
 6+ *
 7+ * @author: Nikolas Iwan
 8+ */
 9+class SMWErrorValue extends SMWDataValue {
 10+
 11+ private $m_attribute = null;
 12+ private $m_error;
 13+ private $m_value;
 14+ private $m_infolinks = Array();
 15+
 16+ public function SMWErrorValue($errormsg = '', $uservalue = '') {
 17+ $this->m_error = $errormsg;
 18+ $this->m_value = $uservalue;
 19+ }
 20+
 21+ /*********************************************************************/
 22+ /* Set methods */
 23+ /*********************************************************************/
 24+
 25+ public function setUserValue($value) {
 26+ $this->m_value = $value;
 27+ return true;
 28+ }
 29+
 30+ public function setXSDValue($value, $unit) {
 31+ $this->setUserValue($value); // no units, compatible syntax
 32+ }
 33+
 34+ public function setAttribute($attribute) {
 35+ $this->m_attribute = $attribute;
 36+ }
 37+
 38+ public function setOutputFormat($formatstring){
 39+ //do nothing
 40+ }
 41+ public function setError($errormsg){
 42+ $this->m_error = $errormsg;
 43+ }
 44+
 45+ /*********************************************************************/
 46+ /* Get methods */
 47+ /*********************************************************************/
 48+
 49+ public function getShortWikiText($linked = NULL) {
 50+ //TODO: support linking
 51+ return $this->m_value;
 52+ }
 53+
 54+ public function getShortHTMLText($linker = NULL) {
 55+ return $this->getShortWikiText($linker);
 56+ }
 57+
 58+ public function getLongWikiText($linked = NULL) {
 59+ //TODO: support linking
 60+ return '<span class="smwwarning">'.$this->m_error.'</span>';
 61+
 62+ }
 63+
 64+ public function getLongHTMLText($linker = NULL) {
 65+ return $this->getLongWikiText($linker);
 66+ }
 67+
 68+ public function getXSDValue() {
 69+ return $this->getShortWikiText(false);
 70+ }
 71+
 72+ public function getWikiValue(){
 73+ return $this->getShortWikiText(false);
 74+ }
 75+
 76+ public function getNumericValue() {
 77+ return NULL;
 78+ }
 79+
 80+ public function getUnit() {
 81+ return array('STDUNIT'=>false, 'ALLUNITS'=>array());
 82+ }
 83+
 84+ public function getError() {
 85+ return $this->m_error;
 86+ }
 87+
 88+ public function getTypeID(){
 89+ return 'error';
 90+ }
 91+
 92+ public function getInfolinks() {
 93+ return $this->m_infolinks;
 94+ }
 95+
 96+ public function getHash() {
 97+ return $this->getLongWikiText() . $this->m_value;
 98+ }
 99+
 100+ public function isValid() {
 101+ return (($this->m_error == '') && ($this->m_value !== '') );
 102+ }
 103+
 104+ public function isNumeric() {
 105+ return false;
 106+ }
 107+}
 108+
 109+?>
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Error.php
___________________________________________________________________
Added: svn:eol-style
1110 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_URI.php
@@ -0,0 +1,153 @@
 2+<?php
 3+
 4+/**
 5+ * This datavalue implements URI-Datavalues suitable for defining
 6+ * URI-types of properties.
 7+ *
 8+ * @author: Nikolas Iwan
 9+ */
 10+
 11+
 12+define('SMW_URI_MODE_URL',1);
 13+define('SMW_URI_MODE_URI',2);
 14+define('SMW_URI_MODE_ANNOURI',3);
 15+
 16+class SMWURIValue extends SMWDataValue {
 17+
 18+ private $m_attribute = null;
 19+ private $m_error = '';
 20+ private $m_value = '';
 21+ private $m_xsdvalue = '';
 22+ private $m_infolinks = Array();
 23+ private $m_mode = '';
 24+
 25+ /*********************************************************************/
 26+ /* Set methods */
 27+ /*********************************************************************/
 28+
 29+ function SMWURIValue($mode) {
 30+ switch ($mode) {
 31+ default: case 'url':
 32+ $this->m_mode = SMW_URI_MODE_URL;
 33+ break;
 34+ case 'uri':
 35+ $this->m_mode = SMW_URI_MODE_URI;
 36+ break;
 37+ case 'annouri':
 38+ $this->m_mode = SMW_URI_MODE_ANNOURI;
 39+ break;
 40+ }
 41+ }
 42+
 43+ public function setUserValue($value) {
 44+ if ($value!='') { //do not accept empty strings
 45+ switch ($this->m_mode) {
 46+ case SMW_URI_MODE_URL:
 47+ $this->m_value = $value;
 48+ break;
 49+ case SMW_URI_MODE_URI: case SMW_URI_MODE_ANNOURI:
 50+ $uri_blacklist = explode("\n",wfMsgForContent('smw_uri_blacklist'));
 51+ foreach ($uri_blacklist as $uri) {
 52+ if (' ' == $uri[0]) $uri = mb_substr($uri,1); //tolerate beautification space
 53+ if ($uri == mb_substr($value,0,mb_strlen($uri))) { //disallowed URI!
 54+ $this->m_error = wfMsgForContent('smw_baduri', $uri);
 55+ return true;
 56+ }
 57+ }
 58+ $this->m_value = $value;
 59+ break;
 60+ }
 61+ $this->m_value = str_replace(array('&','<',' '),array('&amp;','&lt;','_'),$value); // TODO: spaces are just not allowed and should lead to an error
 62+ $this->m_infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->m_attribute, $this->m_value);
 63+ } else {
 64+ $this->m_error = (wfMsgForContent('smw_emptystring'));
 65+ }
 66+
 67+ return true;
 68+
 69+ }
 70+
 71+ public function setXSDValue($value, $unit) {
 72+ $this-> setUserValue($value);
 73+ }
 74+
 75+ public function setAttribute($attribute) {
 76+ $this->m_attribute = $attribute;
 77+ }
 78+
 79+ public function setOutputFormat($formatstring){
 80+ //TODO
 81+ }
 82+ /*********************************************************************/
 83+ /* Get methods */
 84+ /*********************************************************************/
 85+
 86+ public function getShortWikiText($linked = NULL) {
 87+ //TODO: Support linking
 88+ wfDebug("\r\n getShortWikiText: ".$this->m_value);
 89+ return $this->m_value;
 90+ }
 91+
 92+ public function getShortHTMLText($linker = NULL) {
 93+ return $this->getShortWikiText($linker);
 94+ }
 95+
 96+ public function getLongWikiText($linked = NULL) {
 97+ if (! ($this->m_error === '')){
 98+ return ('<span class="smwwarning">' . $this->m_error . '</span>');
 99+ }else {
 100+ return $this->getShortWikiText($linked);
 101+ }
 102+ }
 103+
 104+ public function getLongHTMLText($linker = NULL) {
 105+ return '<span class="external free">'.$this->m_value.'</span>';
 106+ }
 107+
 108+ public function getXSDValue() {
 109+ return $this->getShortWikiText(false);
 110+ }
 111+
 112+ public function getWikiValue(){
 113+ return $this->getShortWikiText(false);
 114+ }
 115+
 116+ public function getNumericValue() {
 117+ return NULL;
 118+ }
 119+
 120+ public function getUnit() {
 121+ return array('STDUNIT'=>false, 'ALLUNITS'=>array());
 122+ }
 123+
 124+ public function getError() {
 125+ return $this->m_error;
 126+ }
 127+
 128+ public function getTypeID(){
 129+ switch ($this->m_mode) {
 130+ case SMW_URI_MODE_URL: return 'url';
 131+ case SMW_URI_MODE_URI: return 'uri';
 132+ case SMW_URI_MODE_ANNOURI: return 'annouri';
 133+ }
 134+ return 'uri';
 135+ }
 136+
 137+ public function getInfolinks() {
 138+ return $this->m_infolinks;
 139+ }
 140+
 141+ public function getHash() {
 142+ return $this->getShortWikiText(false);
 143+ }
 144+
 145+ public function isValid() {
 146+ return (($this->m_error == '') && ($this->m_value !== '') );
 147+ }
 148+
 149+ public function isNumeric() {
 150+ return false;
 151+ }
 152+}
 153+
 154+?>
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_URI.php
___________________________________________________________________
Added: svn:eol-style
1155 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
@@ -1,7 +1,9 @@
22 <?php
33
4 -require_once('SMW_DataValue.php');
5 -require_once('SMW_OldDataValue.php');
 4+global $smwgIP;
 5+require_once($smwgIP . '/includes/SMW_DV_Error.php');
 6+require_once($smwgIP . '/includes/SMW_DataValue.php');
 7+require_once($smwgIP . '/includes/SMW_OldDataValue.php');
68
79 /**
810 * Factory class for creating SMWDataValue objects for supplied types or attributes
@@ -65,11 +67,9 @@
6668 SMWDataValueFactory::$m_typeids[$attstring] = $result->getTypeID(); // also cache typeid
6769 return $result;
6870 } elseif (count($typearray)==0) {
69 - ///TODO
70 - return new SMWOldDataValue(new SMWErrorTypeHandler(wfMsgForContent('smw_notype')));
 71+ return new SMWErrorValue(wfMsgForContent('smw_notype'), $value);
7172 } else {
72 - ///TODO
73 - return new SMWOldDataValue(new SMWErrorTypeHandler(wfMsgForContent('smw_manytypes')));
 73+ return new SMWErrorValue(wfMsgForContent('smw_manytypes'), $value);
7474 }
7575 }
7676 return SMWDataValueFactory::newTypedValue(SMWDataValueFactory::$m_typelabels[$attstring], $value);
@@ -192,3 +192,6 @@
193193
194194 }
195195
 196+SMWDataValueFactory::registerDataValueClass('String','String','SMWStringValue');
 197+SMWDataValueFactory::registerDataValueClass('URI','URI','SMWURIValue', 'uri');
 198+//SMWDataValueFactory::registerDataValueClass('WikiPage','WikiPage','SMWWikiPageValue');
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -0,0 +1,118 @@
 2+<?php
 3+
 4+/**
 5+ * This datavalue implements String-Datavalues suitable for defining
 6+ * String-types of properties.
 7+ *
 8+ * @author: Nikolas Iwan
 9+ */
 10+class SMWStringValue extends SMWDataValue {
 11+
 12+ private $m_attribute = null;
 13+ private $m_error = '';
 14+ private $m_value = '';
 15+ private $m_xsdvalue = '';
 16+ private $m_infolinks = Array();
 17+
 18+ /*********************************************************************/
 19+ /* Set methods */
 20+ /*********************************************************************/
 21+
 22+ public function setUserValue($value) {
 23+ if ($value!='') {
 24+ $this->m_xsdvalue = smwfXMLContentEncode($value);
 25+ // 255 below matches smw_attributes.value_xsd definition in smwfMakeSemanticTables()
 26+ if (strlen($this->m_xsdvalue) > 255) {
 27+ $this->m_error = wfMsgForContent('smw_maxstring', $this->m_xsdvalue);
 28+ $this->m_value = $this->m_xsdvalue;
 29+ } else {
 30+ $this->m_value = $this->m_xsdvalue;
 31+ $this->m_infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->m_attribute, $this->m_value);
 32+ }
 33+ } else {
 34+ $this->m_error = wfMsgForContent('smw_emptystring');
 35+ }
 36+ return true;
 37+ }
 38+
 39+ public function setXSDValue($value, $unit) {
 40+ $this->setUserValue($value); // no units, XML compatible syntax
 41+ }
 42+
 43+ public function setAttribute($attribute) {
 44+ $this->m_attribute = $attribute;
 45+ }
 46+
 47+ public function setOutputFormat($formatstring){
 48+ //ToDo
 49+ }
 50+
 51+ /*********************************************************************/
 52+ /* Get methods */
 53+ /*********************************************************************/
 54+
 55+ public function getShortWikiText($linked = NULL) {
 56+ //TODO: Support linking
 57+ return $this->m_value;
 58+ }
 59+
 60+ public function getShortHTMLText($linker = NULL) {
 61+ return $this->getShortWikiText($linker);
 62+ }
 63+
 64+ public function getLongWikiText($linked = NULL) {
 65+ if (! ($this->m_error === '')){
 66+ return ('<span class="smwwarning">' . $this->m_error . '</span>');
 67+ }else {
 68+ return $this->getShortWikiText($linked);
 69+ }
 70+ }
 71+
 72+ public function getLongHTMLText($linker = NULL) {
 73+ return $this->getLongWikiText($linker);
 74+ }
 75+
 76+ public function getXSDValue() {
 77+ return $this->getShortWikiText();
 78+ }
 79+
 80+ public function getWikiValue(){
 81+ return $this->getShortWikiText(false);
 82+ }
 83+
 84+ public function getNumericValue() {
 85+ return NULL;
 86+ }
 87+
 88+ public function getUnit() {
 89+ return array('STDUNIT'=>false, 'ALLUNITS'=>array());
 90+ }
 91+
 92+ public function getError() {
 93+ return $this->m_error;
 94+ }
 95+
 96+ public function getTypeID(){
 97+ return 'string';
 98+ }
 99+
 100+ public function getInfolinks() {
 101+ return $this->m_infolinks;
 102+ }
 103+
 104+ public function getHash() {
 105+ return $this->getLongWikiText(false) . $this->m_xsdvalue ;
 106+ }
 107+
 108+ public function isValid() {
 109+ return (($this->m_error == '') && ($this->m_value !== '') );
 110+ }
 111+
 112+ public function isNumeric() {
 113+ return false;
 114+ }
 115+
 116+
 117+}
 118+
 119+?>
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
___________________________________________________________________
Added: svn:eol-style
1120 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php
@@ -9,21 +9,6 @@
1010 class SMWNAryValue extends SMWDataValue {
1111
1212 /**
13 - * The original string as specified by a user, if provided to
14 - * initialise this object. Otherwise a generated user-friendly string
15 - * (no xsd). Wikitext.
16 - */
17 - private $uvalue;
18 -
19 - /**
20 - * XML Schema representation of single data value as stored in the DB.
21 - * This value is important for processing, but might be completely different
22 - * from the representations used for printout.
23 - * Plain xml-compatible text. FALSE if value could not be determined.
24 - */
25 - private $vxsd;
26 -
27 - /**
2813 * The array of the data values within this container value
2914 */
3015 private $m_values = array();
@@ -34,16 +19,22 @@
3520 private $m_error;
3621
3722 /**
 23+ * types as we received them when datafactory called us
 24+ */
 25+ private $m_type = array();
 26+
 27+ /**
3828 * constructor to create n-ary data value types and set their initial
3929 * value appropriately.
4030 */
4131 function SMWNAryValue($type, $value) {
42 - $values = explode(';', $value);
 32+ $this->m_type = $type;
 33+
4334 $types = $type->getTypeValues();
44 - if (sizeof($types) > sizeof($values)) {
45 - $this->m_error = "[relation only supports " . (sizeof($types) . " parameters. However, you supplied " . sizeof($values)) . " parameters]"; /// TODO: internationalise
46 - } else if (sizeof($types) < sizeof($values)) {
 35+ $values = $this->parseValues($value);
 36+ if (sizeof($values) < sizeof($types)) {
4737 ///TODO - use default values for types that were not explicitly specified?
 38+
4839 } else {
4940 // everything specified and passed correctly
5041 for ($i = 0; $i < sizeof($types); $i++) {
@@ -52,29 +43,57 @@
5344 }
5445 }
5546
 47+ private function parseValues($commaValues) {
 48+ return preg_split('/[\s]*;[\s]*/', $commaValues, sizeof($this->m_type->getTypeLabels()));
 49+ }
 50+
 51+ //
 52+ // Methods derived from abstract class
 53+ //
 54+
5655 public function setUserValue($value) {
57 - $this->uvalue = $value;
58 - /// TODO: acutally *set* the value, including $this->m_values!
 56+ // get DVtypes
 57+ $types = $this->m_type->getTypeValues();
 58+ // get values supplied by user
 59+ $values = $this->parseValues($value);
 60+ // create user specified DVs
 61+ if (sizeof($values) < sizeof($types)) {
 62+ /// TODO: Actually handle this case!
 63+ } else {
 64+ // everything specified and passed correctly - build DV containers.
 65+ for ($i = 0; $i < sizeof($types); $i++) {
 66+ $this->m_values[$i] = SMWDataValueFactory::newTypedValue($types[$i], $values[$i]);
 67+ }
 68+ }
5969 }
6070
6171 public function setXSDValue($value, $unit) {
62 - // ignore parameter $unit
63 - $this->vxsd = $value;
64 - /// TODO: acutally *set* the value, including $this->m_values!
 72+ // get DVtypes
 73+ $types = $this->m_type->getTypeValues();
 74+ // get values supplied by user
 75+ $values = $this->parseValues($value);
 76+ $units = $this->parseValues($unit);
 77+
 78+ // create user specified DVs
 79+ if (sizeof($values) < sizeof($types)) {
 80+ /// TODO: Actually handle this case!
 81+ } else {
 82+ // everything specified and passed correctly - set XSDValue of DV containers.
 83+ for ($i = 0; $i < sizeof($types); $i++) {
 84+ $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$i] : null));
 85+ }
 86+ }
6587 }
6688
6789 public function setAttribute($attribute) {
6890 /// TODO
6991 }
70 -
 92+
7193 public function setOutputFormat($formatstring) {
7294 /// TODO
7395 }
7496
7597 public function getShortWikiText($linked = NULL) {
76 - if ($this->uvalue) {
77 - return $this->uvalue;
78 - }
7998 $result = '';
8099 $first = true;
81100 foreach ($this->m_values as $value) {
@@ -138,18 +157,11 @@
139158 }
140159
141160 public function getXSDValue() {
142 - $result = '';
143 - $first = true;
144 - $second = true;
145 - foreach ($this->m_values as $value) {
146 - if ($first) {
147 - $first = false;
148 - } else {
149 - $result .= "; ";
150 - }
151 - $result .= $value->getXSDValue();
 161+ $xsdvals = array();
 162+ for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) {
 163+ $xsdvals[$i] = $this->m_values[$i]->getXSDValue();
152164 }
153 - return $result;
 165+ return implode(';', $xsdvals);
154166 }
155167
156168 public function getWikiValue() {
@@ -183,7 +195,7 @@
184196 $hash .= $value->getHash();
185197 /// FIXME: this is wrong (different value combinations yield same hash)
186198 }
187 - return md5($hash);
 199+ return $hash;
188200 }
189201
190202 public function isValid() {
@@ -193,5 +205,22 @@
194206 public function isNumeric() {
195207 return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers)
196208 }
 209+
 210+ //
 211+ // custom functions for n-ary attributes
 212+ //
 213+
 214+ public function getDVTypeIDs() {
 215+ return implode(';', $this->m_type->getTypeLabels());
 216+ }
 217+
 218+ public function getDVs() {
 219+ return $this->isValid() ? $this->m_values : null;
 220+ }
 221+
 222+
 223+
 224+
 225+
197226 }
198227

Status & tagging log