r23214 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23213‎ | r23214 | r23215 >
Date:13:03, 22 June 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Support output format modifier in printing data values. Enables user specified unit conversion before
query output ("find the ten longest rivers and return their length in km and in miles").
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Store.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -115,6 +115,8 @@
116116 var $attribute; // wiki name (without namespace) of the attribute that this value was
117117 // assigned to, or FALSE if no attribute was given.
118118 /**#@-*/
 119+
 120+ private $byxsd = false; // hack hack hack (temporary to ease DV transition)
119121
120122 /**
121123 * Just initialise variables. To create value objects, use one of the
@@ -139,6 +141,7 @@
140142 */
141143 function setUserValue($value) {
142144 $this->clear();
 145+ $this->byxsd = false; // DV transition hack to distinguish this case and to use others[0] for XSD-values
143146 $this->vuser = $value;
144147 //this is needed since typehandlers are not strictly required to
145148 //set the user value, especially if errors are reported.
@@ -154,6 +157,7 @@
155158 * Set the xsd value (and compute other representations if possible)
156159 */
157160 function setXSDValue($value, $unit) {
 161+ $this->byxsd = true; // DV transition hack to distinguish this case and to use others[0] for XSD-values
158162 $this->vxsd = $value;
159163 if ($this->type_handler === NULL) {
160164 return false;
@@ -173,7 +177,7 @@
174178 * @param unit - see $unit
175179 */
176180 function setProcessedValues($user, $xsd, $num=NULL, $unit='') {
177 - $this->vuser = $user;
 181+ $this->vuser = $user;
178182 $this->vxsd = $xsd;
179183 $this->vnum = $num;
180184 $this->unit = $unit;
@@ -296,13 +300,29 @@
297301 $this->infolinks = array();
298302 }
299303
 304+ public function setOutputFormat($formatstring) {
 305+ // interpret new output format as the desired unit for united quantities
 306+ $this->setDesiredUnits(array($formatstring));
 307+ }
300308
 309+
301310 /*********************************************************************/
302311 /* Get methods */
303312 /*********************************************************************/
304313
305314 public function getShortWikiText($linked = NULL) {
306 - return $this->vuser;
 315+ // behave like old getUserValue if DV was initialised from user value,
 316+ // else behave like old getStringValue. Transition hack.
 317+ if ($this->byxsd) {
 318+ if ( count($this->others) > 0 ) {
 319+ reset($this->others);
 320+ return current($this->others); // return first element
 321+ } else {
 322+ return $this->vuser;
 323+ }
 324+ } else {
 325+ return $this->vuser;
 326+ }
307327 }
308328
309329 public function getShortHTMLText($linker = NULL) {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -101,6 +101,16 @@
102102 */
103103 abstract public function setAttribute($attribute);
104104
 105+ /**
 106+ * Define a particular output format. Output formats are user-supplied strings
 107+ * that the datavalue may (or may not) use to customise its return value. For
 108+ * example, quantities with units of measurement may interpret the string as
 109+ * a desired output unit. In other cases, the output format might be built-in
 110+ * and subject to internationalisation (which the datavalue has to implement).
 111+ * In any case, an empty string resets the output format to the default.
 112+ */
 113+ abstract public function setOutputFormat($formatstring);
 114+
105115 /*********************************************************************/
106116 /* Get methods */
107117 /*********************************************************************/
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php
@@ -25,10 +25,11 @@
2626 * obtain additional information for the retrieved results.
2727 */
2828 class SMWPrintRequest {
29 - protected $m_mode;
30 - protected $m_label;
31 - protected $m_title;
32 - protected $m_datavalue;
 29+ protected $m_mode; // type of print request
 30+ protected $m_label; // string for labelling results, contains no markup
 31+ protected $m_title; // title object to which print request refers (if any)
 32+ protected $m_typeid = false; // id of the datatype of the printed objects, if applicable
 33+ protected $m_outputformat; // output format string for formatting results, if applicable
3334
3435 /**
3536 * Create a print request.
@@ -37,11 +38,11 @@
3839 * @param $title optional Title object that specifies the request (usually a relation or attribute)
3940 * @param $datavalue optional SMWDataValue container that sets parameters for printing data values (e.g. the unit)
4041 */
41 - public function SMWPrintRequest($mode, $label, Title $title = NULL, $datavalue = NULL) {
 42+ public function SMWPrintRequest($mode, $label, Title $title = NULL, $outputformat = '') {
4243 $this->m_mode = $mode;
4344 $this->m_label = $label;
4445 $this->m_title = $title;
45 - $this->m_datavalue = $datavalue;
 46+ $this->m_outputformat = $outputformat;
4647 }
4748
4849 public function getMode() {
@@ -91,10 +92,18 @@
9293 return $this->m_title;
9394 }
9495
95 - public function getDatavalue() {
96 - return $this->m_datavalue;
 96+ public function getOutputFormat() {
 97+ return $this->m_outputformat;
9798 }
9899
 100+ public function getTypeID() {
 101+ if ($this->m_typeid === false) {
 102+ $dv = SMWDataValueFactory::newAttributeValue($this->m_title->getText());
 103+ $this->m_typeid = $dv->getTypeID();
 104+ }
 105+ return $this->m_typeid;
 106+ }
 107+
99108 /**
100109 * Return a hash string that is used to eliminate duplicate
101110 * print requests.
@@ -104,11 +113,7 @@
105114 if ($this->m_title !== NULL) {
106115 $hash .= $this->m_title->getPrefixedText() . ':';
107116 }
108 - if ($this->m_datavalue !== NULL) {
109 - foreach ($this->m_datavalue->getDesiredUnits() as $unit) {
110 - $hash .= $unit . ':';
111 - }
112 - }
 117+ $hash .= $this->m_outputformat . ':';
113118 return $hash;
114119 }
115120 }
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -168,7 +168,7 @@
169169 }
170170
171171
172 - function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL) {
 172+ function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL, $outputformat = '') {
173173 $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
174174 $result = array();
175175
@@ -187,6 +187,7 @@
188188 while($row = $db->fetchObject($res)) {
189189 $dv = SMWDataValueFactory::newTypehandlerValue(SMWTypeHandlerFactory::getTypeHandlerByID($row->value_datatype));
190190 $dv->setAttribute($attribute->getText());
 191+ $dv->setOutputFormat($outputformat);
191192 $dv->setXSDValue($row->value_xsd, $row->value_unit);
192193 $result[] = $dv;
193194 }
@@ -203,6 +204,7 @@
204205 while($row = $db->fetchObject($res)) {
205206 $dv = SMWDataValueFactory::newTypehandlerValue(SMWTypeHandlerFactory::getTypeHandlerByID('text'));
206207 $dv->setAttribute($attribute->getText());
 208+ $dv->setOutputFormat($outputformat);
207209 $dv->setXSDValue($row->value_blob, '');
208210 $result[] = $dv;
209211 }
@@ -607,8 +609,7 @@
608610 $row[] = new SMWResultArray($this->getSpecialValues($qt,SMW_SP_HAS_CATEGORY), $pr);
609611 break;
610612 case SMW_PRINT_ATTS:
611 - ///TODO: respect given datavalue (desired unit), needs extension of getAttributeValues()
612 - $row[] = new SMWResultArray($this->getAttributeValues($qt,$pr->getTitle()), $pr);
 613+ $row[] = new SMWResultArray($this->getAttributeValues($qt,$pr->getTitle(), NULL, $pr->getOutputFormat()), $pr);
613614 break;
614615 }
615616 }
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Store.php
@@ -130,9 +130,11 @@
131131
132132 /**
133133 * Get an array of all attribute values stored for the given subject and atttribute. The result
134 - * is an array of SMWDataValue objects.
 134+ * is an array of SMWDataValue objects. The provided outputformat is a string identifier that
 135+ * may be used by the datavalues to modify their output behaviour, e.g. when interpreted as a
 136+ * desierd unit to convert the output to.
135137 */
136 - abstract function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL);
 138+ abstract function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL, $outputformat = '');
137139
138140 /**
139141 * Get an array of all subjects that have the given value for the given attribute. The
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php
@@ -33,6 +33,10 @@
3434 public function setAttribute($attribute) { // ignore
3535 }
3636
 37+ public function setOutputFormat($formatstring) {
 38+ // no output formats supported, ignore
 39+ }
 40+
3741 /*********************************************************************/
3842 /* Get methods */
3943 /*********************************************************************/

Status & tagging log