r23866 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23865‎ | r23866 | r23867 >
Date:14:50, 8 July 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Central error handling via SMWDataValue. Beautified error output with warning icon+tooltip (no lengthy messages
any more, unless JavaScript is disabled).
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Error.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_URI.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/skins/SMW_tooltip.js (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/skins/images/warning.png (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -53,10 +53,6 @@
5454 * in a strict sense.
5555 */
5656 var $unit;
57 - /**
58 - * Error message, if value could not be intitialised. FALSE otherwise.
59 - */
60 - var $error;
6157
6258 /**
6359 * String identifier that describes which of the returned
@@ -262,7 +258,7 @@
263259 * make a value invalid, preventing it, e.g., from being stored in the database.
264260 */
265261 function setError($message) {
266 - $this->error = $message;
 262+ $this->addError($message);
267263 $this->description = false;
268264 $this->tooltip = false;
269265 }
@@ -292,7 +288,6 @@
293289 $this->unit = '';
294290 $this->input = 'K';
295291 $this->others = array();
296 - $this->error = false;
297292
298293 $this->tooltip = false;
299294 $this->description = false;
@@ -338,7 +333,7 @@
339334 public function getLongWikiText($linked = NULL) {
340335 // copied from deprecated getValueDescription
341336 if ($this->description === false) {
342 - if ($this->error === false) {
 337+ if ($this->isValid()) {
343338 if (count($this->others)>0) {
344339 $sep = '';
345340 foreach ($this->others as $other) {
@@ -347,7 +342,9 @@
348343 }
349344 if (' (' != $sep) $this->description .= ')';
350345 }
351 - } else { $this->description = '<span class="smwwarning">' . $this->error . '</span>'; }
 346+ } else {
 347+ $this->description = $this->getErrorText();
 348+ }
352349 }
353350 return $this->description;
354351 }
@@ -429,13 +426,6 @@
430427 }
431428
432429 /**
433 - * Return error string or false if no error occured.
434 - */
435 - function getError() {
436 - return $this->error;
437 - }
438 -
439 - /**
440430 * Return the type id for this value, or FALSE if no type was given.
441431 */
442432 function getTypeID() {
@@ -464,7 +454,7 @@
465455 function getValueDescription() {
466456 trigger_error("The function getValueDescription() is deprecated. Use getLongWikiText() or getLongHTMLText().", E_USER_NOTICE);
467457 if ($this->description === false) {
468 - if ($this->error === false) {
 458+ if ($this->isValid()) {
469459 if (count($this->others)>0) {
470460 $sep = '';
471461 foreach ($this->others as $other) {
@@ -473,7 +463,7 @@
474464 }
475465 if (' (' != $sep) $this->description .= ')';
476466 }
477 - } else { $this->description = '<span class="smwwarning">' . $this->error . '</span>'; }
 467+ } else { $this->description = $this->getErrorText(); }
478468 }
479469 return $this->description;
480470 }
@@ -487,7 +477,7 @@
488478 */
489479 function getTooltip() {
490480 if ($this->tooltip === false) {
491 - if ($this->error === false) {
 481+ if ($this->isValid()) {
492482 $this->tooltip = '';
493483 $sep = '';
494484 foreach ($this->others as $id => $other) {
@@ -555,14 +545,6 @@
556546 }
557547
558548 /**
559 - * Return TRUE if a value was defined and understood by the given type,
560 - * and false if parsing errors occured or no value was given.
561 - */
562 - function isValid() {
563 - return ( ($this->error === false) && ($this->vuser !== false) );
564 - }
565 -
566 - /**
567549 * Return TRUE if values of the given type generally have a numeric version.
568550 */
569551 function isNumeric() {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -11,6 +11,8 @@
1212
1313 protected $m_attribute = false; /// The text label of the respective attribute or false if none given
1414 protected $m_caption = false; /// The text label to be used for output or false if none given
 15+ protected $m_errors = array(); /// Array of error text messages
 16+ protected $m_isset = false; /// True if a value was set.
1517
1618 ///// Set methods /////
1719
@@ -20,10 +22,12 @@
2123 * label for printout might also be specified.
2224 */
2325 public function setUserValue($value, $caption = false) {
 26+ $this->m_errors = array(); // clear errors
2427 if ($caption !== false) {
2528 $this->m_caption = $caption;
2629 }
2730 $this->parseUserValue($value); // may set caption if not set yet, depending on datavalue
 31+ $this->m_isset = true;
2832 }
2933
3034 /**
@@ -32,8 +36,10 @@
3337 * implementations should support round-tripping).
3438 */
3539 public function setXSDValue($value, $unit) {
 40+ $this->m_errors = array(); // clear errors
3641 $this->m_caption = false;
3742 $this->parseXSDValue($value, $unit);
 43+ $this->m_isset = true;
3844 }
3945
4046 /**
@@ -55,6 +61,14 @@
5662 */
5763 abstract public function setOutputFormat($formatstring);
5864
 65+ /**
 66+ * Add a new error string to the error list. All error string must be wiki and
 67+ * html-safe! No further escaping will happen!
 68+ */
 69+ protected function addError($errorstring) {
 70+ $this->m_errors[] = $errorstring;
 71+ }
 72+
5973 ///// Abstract processing methods /////
6074
6175 /**
@@ -152,8 +166,9 @@
153167
154168 /**
155169 * Return error string or an empty string if no error occured.
 170+ * @DEPRECATED
156171 */
157 - abstract public function getError();
 172+ public function getError() {}
158173
159174 /**
160175 * Return a short string that unambiguously specify the type of this value.
@@ -177,18 +192,39 @@
178193 abstract public function getHash();
179194
180195 /**
 196+ * Return TRUE if values of the given type generally have a numeric version.
 197+ */
 198+ abstract public function isNumeric();
 199+
 200+ /**
181201 * Return TRUE if a value was defined and understood by the given type,
182202 * and false if parsing errors occured or no value was given.
183203 */
184 - abstract public function isValid();
 204+ public function isValid() {
 205+ return ( (count($this->m_errors) == 0) && $this->m_isset );
 206+ }
185207
186208 /**
187 - * Return TRUE if values of the given type generally have a numeric version.
 209+ * Return a string that displays all error messages as a tooltip, or
 210+ * an empty string if no errors happened.
188211 */
189 - abstract public function isNumeric();
 212+ public function getErrorText() {
 213+ if (count($this->m_errors) > 0) {
 214+ $errors = implode(', ', $this->m_errors);
 215+ return '<span class="smwttpersist"><span class="smwtticon">warning.png</span><span class="smwttcontent">' . $errors . '</span></span>';
 216+ } else {
 217+ return '';
 218+ }
 219+ }
190220
 221+ /**
 222+ * Return an array of error messages, or an empty array
 223+ * if no errors occurred.
 224+ */
 225+ public function getErrors() {
 226+ return $this->m_errors;
 227+ }
191228
192 -
193229 /*********************************************************************/
194230 /* Static methods for initialisation */
195231 /*********************************************************************/
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Error.php
@@ -7,13 +7,12 @@
88 */
99 class SMWErrorValue extends SMWDataValue {
1010
11 - private $m_error;
1211 private $m_value;
1312 private $m_infolinks = Array();
1413
1514 public function SMWErrorValue($errormsg = '', $uservalue = '', $caption = false) {
16 - $this->m_error = $errormsg;
1715 $this->setUserValue($uservalue, $caption);
 16+ $this->addError($errormsg);
1817 }
1918
2019 protected function parseUserValue($value) {
@@ -31,9 +30,6 @@
3231 public function setOutputFormat($formatstring){
3332 // no output formats
3433 }
35 - public function setError($errormsg){
36 - $this->m_error = $errormsg;
37 - }
3834
3935 public function getShortWikiText($linked = NULL) {
4036 //TODO: support linking?
@@ -46,11 +42,11 @@
4743
4844 public function getLongWikiText($linked = NULL) {
4945 //TODO: support linking?
50 - return '<span class="smwwarning">'.$this->m_error.'</span>';
 46+ return $this->getErrorText() . '&nbsp;'; // &nbsp; is a hack to get non-empty table rows for better img placement in FF (any maybe elsewhere too); should not hurt
5147 }
5248
5349 public function getLongHTMLText($linker = NULL) {
54 - return $this->getLongWikiText($linker);
 50+ return $this->getErrorText() . '&nbsp;'; // &nbsp; is a hack to get non-empty table rows for better img placement in FF (any maybe elsewhere too); should not hurt
5551 }
5652
5753 public function getXSDValue() {
@@ -69,10 +65,6 @@
7066 return ''; // empty unit
7167 }
7268
73 - public function getError() {
74 - return $this->m_error;
75 - }
76 -
7769 public function getTypeID(){
7870 return 'error';
7971 }
@@ -82,13 +74,9 @@
8375 }
8476
8577 public function getHash() {
86 - return $this->getLongWikiText() . $this->m_value;
 78+ return $this->getLongWikiText(); // use only error for hash so as not to display the same error twice
8779 }
8880
89 - public function isValid() {
90 - return (($this->m_error == '') && ($this->m_value !== '') );
91 - }
92 -
9381 public function isNumeric() {
9482 return false;
9583 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_URI.php
@@ -19,7 +19,6 @@
2020 */
2121 class SMWURIValue extends SMWDataValue {
2222
23 - private $m_error = '';
2423 private $m_value = '';
2524 private $m_xsdvalue = '';
2625 private $m_infolinks = Array();
@@ -53,7 +52,7 @@
5453 foreach ($uri_blacklist as $uri) {
5554 if (' ' == $uri[0]) $uri = mb_substr($uri,1); //tolerate beautification space
5655 if ($uri == mb_substr($value,0,mb_strlen($uri))) { //disallowed URI!
57 - $this->m_error = wfMsgForContent('smw_baduri', $uri);
 56+ $this->addError(wfMsgForContent('smw_baduri', $uri));
5857 return true;
5958 }
6059 }
@@ -63,7 +62,7 @@
6463 $this->m_value = str_replace(array('&','<',' '),array('&amp;','&lt;','_'),$value); // TODO: spaces are just not allowed and should lead to an error
6564 $this->m_infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->m_attribute, $this->m_value);
6665 } else {
67 - $this->m_error = (wfMsgForContent('smw_emptystring'));
 66+ $this->addError(wfMsgForContent('smw_emptystring'));
6867 }
6968 return true;
7069
@@ -78,9 +77,8 @@
7978 }
8079
8180 public function getShortWikiText($linked = NULL) {
82 - //TODO: Support linking
8381 wfDebug("\r\n getShortWikiText: ".$this->m_caption);
84 - return $this->m_caption;
 82+ return $this->m_caption; // TODO: support linking with caption as alternative text, depending on type of DV
8583 }
8684
8785 public function getShortHTMLText($linker = NULL) {
@@ -88,15 +86,19 @@
8987 }
9088
9189 public function getLongWikiText($linked = NULL) {
92 - if (! ($this->m_error === '')){
93 - return ('<span class="smwwarning">' . $this->m_error . '</span>');
94 - } else {
95 - return $this->m_value;
96 - }
 90+ if ($this->isValid()){
 91+ return $this->getErrorText();
 92+ } else {
 93+ return $this->m_value;
 94+ }
9795 }
9896
9997 public function getLongHTMLText($linker = NULL) {
100 - return '<span class="external free">' . htmlspecialchars($this->m_value) . '</span>'; /// TODO support linking
 98+ if ($this->isValid()){
 99+ return $this->getErrorText();
 100+ } else {
 101+ return '<span class="external free">' . htmlspecialchars($this->m_value) . '</span>'; /// TODO support linking
 102+ }
101103 }
102104
103105 public function getXSDValue() {
@@ -115,10 +117,6 @@
116118 return ''; // empty unit
117119 }
118120
119 - public function getError() {
120 - return $this->m_error;
121 - }
122 -
123121 public function getTypeID(){
124122 switch ($this->m_mode) {
125123 case SMW_URI_MODE_URL: return 'url';
@@ -136,10 +134,6 @@
137135 return $this->getShortWikiText(false);
138136 }
139137
140 - public function isValid() {
141 - return (($this->m_error == '') && ($this->m_value !== '') );
142 - }
143 -
144138 public function isNumeric() {
145139 return false;
146140 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php
@@ -12,7 +12,6 @@
1313 class SMWTypesValue extends SMWDataValue {
1414
1515 private $m_typelabels = false;
16 - private $m_error = '';
1716 private $m_xsdvalue = false;
1817
1918 protected function parseUserValue($value) {
@@ -38,16 +37,23 @@
3938 }
4039
4140 public function getShortWikiText($linked = NULL) {
 41+ if ($this->m_caption !== false) {
 42+ return $this->m_caption;
 43+ }
 44+ return $this->getLongWikiText($linked);
 45+ }
 46+
 47+ public function getShortHTMLText($linker = NULL) {
 48+ if ($this->m_caption !== false) {
 49+ return htmlspecialchars($this->m_caption);
 50+ }
 51+ return $this->getLongHTMLText($linker);
 52+ }
 53+
 54+ public function getLongWikiText($linked = NULL) {
4255 if ( ($linked === NULL) || ($linked === false) ) {
43 - if ($this->m_caption === false) {
44 - return str_replace('_',' ',implode(', ', $this->getTypeLabels()));
45 - } else {
46 - return $this->m_caption;
47 - }
 56+ return str_replace('_',' ',implode(', ', $this->getTypeLabels()));
4857 } else {
49 - if ($this->m_caption !== false) { ///TODO: how can we support linking for nary texts?
50 - return $this->m_caption;
51 - }
5258 global $wgContLang;
5359 $result = '';
5460 $typenamespace = $wgContLang->getNsText(SMW_NS_TYPE);
@@ -64,19 +70,11 @@
6571 }
6672 }
6773
68 - public function getShortHTMLText($linker = NULL) {
69 - ///TODO Support linking
 74+ public function getLongHTMLText($linker = NULL) {
 75+ /// TODO: support linking
7076 return implode(', ', $this->m_typelabels);
7177 }
7278
73 - public function getLongWikiText($linked = NULL) {
74 - return $this->getShortWikiText($linked);
75 - }
76 -
77 - public function getLongHTMLText($linker = NULL) {
78 - return $this->getShortHTMLText($linker);
79 - }
80 -
8179 public function getXSDValue() {
8280 if ($this->isValid()) {
8381 if ($this->m_xsdvalue === false) {
@@ -100,10 +98,6 @@
10199 return ''; // empty unit
102100 }
103101
104 - public function getError() {
105 - return $this->m_error;
106 - }
107 -
108102 public function getTypeID() {
109103 return 'types';
110104 }
@@ -116,11 +110,6 @@
117111 return implode('[]', $this->getTypeLabels());
118112 }
119113
120 - public function isValid() {
121 - return ( ($this->m_error == '') &&
122 - ( ($this->m_typelabels !== false) || ($this->m_xsdvalue !== false) ) );
123 - }
124 -
125114 public function isNumeric() {
126115 return false;
127116 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -8,7 +8,6 @@
99 */
1010 class SMWStringValue extends SMWDataValue {
1111
12 - private $m_error = '';
1312 private $m_value = '';
1413 private $m_xsdvalue = '';
1514 private $m_infolinks = Array();
@@ -21,14 +20,14 @@
2221 $this->m_xsdvalue = smwfXMLContentEncode($value);
2322 // 255 below matches smw_attributes.value_xsd definition in smwfMakeSemanticTables()
2423 if (strlen($this->m_xsdvalue) > 255) {
25 - $this->m_error = wfMsgForContent('smw_maxstring', $this->m_xsdvalue);
 24+ $this->addError(wfMsgForContent('smw_maxstring', $this->m_xsdvalue));
2625 $this->m_value = $this->m_xsdvalue;
2726 } else {
2827 $this->m_value = $this->m_xsdvalue;
2928 $this->m_infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->m_attribute, $this->m_value);
3029 }
3130 } else {
32 - $this->m_error = wfMsgForContent('smw_emptystring');
 31+ $this->addError(wfMsgForContent('smw_emptystring'));
3332 }
3433 return true;
3534 }
@@ -52,16 +51,16 @@
5352 }
5453
5554 public function getLongWikiText($linked = NULL) {
56 - if (! ($this->m_error === '')){
57 - return ('<span class="smwwarning">' . $this->m_error . '</span>');
 55+ if (!$this->isValid()) {
 56+ return $this->getErrorText();
5857 } else {
5958 return $this->m_value;
6059 }
6160 }
6261
6362 public function getLongHTMLText($linker = NULL) {
64 - if (! ($this->m_error === '')){
65 - return ('<span class="smwwarning">' . $this->m_error . '</span>');
 63+ if (!$this->isValid()) {
 64+ return $this->getErrorText();
6665 } else {
6766 return htmlspecialchars($this->m_value);
6867 }
@@ -82,10 +81,6 @@
8382 public function getUnit() {
8483 return ''; // empty unit
8584 }
86 -
87 - public function getError() {
88 - return $this->m_error;
89 - }
9085
9186 public function getTypeID(){
9287 return 'string';
@@ -99,10 +94,6 @@
10095 return $this->getLongWikiText(false) . $this->m_xsdvalue ;
10196 }
10297
103 - public function isValid() {
104 - return (($this->m_error == '') && ($this->m_value !== '') );
105 - }
106 -
10798 public function isNumeric() {
10899 return false;
109100 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php
@@ -14,11 +14,6 @@
1515 private $m_values = array();
1616
1717 /**
18 - * variable for representing error messages
19 - */
20 - private $m_error;
21 -
22 - /**
2318 * types as we received them when datafactory called us
2419 */
2520 private $m_type = array();
@@ -182,10 +177,6 @@
183178 return ''; // empty unit
184179 }
185180
186 - public function getError() {
187 - return $this->m_error;
188 - }
189 -
190181 public function getTypeID() {
191182 return 'nary';
192183 }
Index: trunk/extensions/SemanticMediaWiki/skins/SMW_tooltip.js
@@ -40,10 +40,12 @@
4141 if(spans[i].className=="smwtticon"){
4242 img=document.createElement("img");
4343 img.setAttribute("src",imagePath+spans[i].innerHTML);
 44+ img.setAttribute("style","padding-right: 5px; padding-left: 5px;"); // setting a CSS class here fails
4445 a.replaceChild(img, a.firstChild);
4546 }
4647 //make content invisible
4748 //done here and not in the css so that non-js clients can see it
 49+ //TODO: why not delete this span completely?
4850 if(spans[i].className=="smwttcontent"){
4951 spans[i].style.display="none";
5052 }
Index: trunk/extensions/SemanticMediaWiki/skins/images/warning.png
Cannot display: file marked as a binary type.
svn:mime-type = image/png
Property changes on: trunk/extensions/SemanticMediaWiki/skins/images/warning.png
___________________________________________________________________
Added: svn:mime-type
5153 + image/png

Status & tagging log