r24149 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24148‎ | r24149 | r24150 >
Date:07:51, 16 July 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Support for reading nary values
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -190,7 +190,7 @@
191191
192192 $id = SMWDataValueFactory::getPropertyObjectTypeID($property);
193193 switch ($id) {
194 - case '_txt': // long text property
 194+ case '_txt':
195195 $res = $db->select( $db->tableName('smw_longstrings'),
196196 'value_blob',
197197 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
@@ -204,7 +204,7 @@
205205 }
206206 $db->freeResult($res);
207207 break;
208 - case '_wpg': // wiki page
 208+ case '_wpg':
209209 $res = $db->select( $db->tableName('smw_relations'),
210210 'object_title, object_namespace',
211211 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
@@ -219,7 +219,77 @@
220220 }
221221 $db->freeResult($res);
222222 break;
223 - default: // all others
 223+ case '__nry':
 224+ $type = SMWDataValueFactory::getPropertyObjectTypeValue($property);
 225+ $subtypes = $type->getTypeValues();
 226+ $res = $db->select( $db->tableName('smw_nary'),
 227+ 'nary_key',
 228+ 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
 229+ ' AND attribute_title=' . $db->addQuotes($property->getDBkey()),
 230+ 'SMW::getPropertyValues', $this->getSQLOptions($requestoptions) );
 231+ ///TODO: presumably slow. Try to do less SQL queries by making a join with smw_nary
 232+ while($row = $db->fetchObject($res)) {
 233+ $res2 = $db->select( $db->tableName('smw_nary_attributes'),
 234+ 'nary_pos, value_unit, value_xsd',
 235+ 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
 236+ ' AND nary_key=' . $db->addQuotes($row->nary_key),
 237+ 'SMW::getPropertyValues');
 238+ while($row2 = $db->fetchObject($res2)) {
 239+ if ($row2->nary_pos < count($subtypes)) {
 240+ $dv = SMWDataValueFactory::newTypeObjectValue($subtypes[$row2->nary_pos]);
 241+ $dv->setXSDValue($row2->value_xsd, $row2->value_unit);
 242+ $values[$row2->nary_pos] = $dv;
 243+ }
 244+ }
 245+ $res2 = $db->select( $db->tableName('smw_nary_longstrings'),
 246+ 'nary_pos, value_blob',
 247+ 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
 248+ ' AND nary_key=' . $db->addQuotes($row->nary_key),
 249+ 'SMW::getPropertyValues');
 250+ while($row2 = $db->fetchObject($res2)) {
 251+ if ( $row2->nary_pos < count($subtypes) ) {
 252+ $dv = SMWDataValueFactory::newTypeObjectValue($subtypes[$row2->nary_pos]);
 253+ $dv->setXSDValue($row2->value_blob, '');
 254+ $values[$row2->nary_pos] = $dv;
 255+ }
 256+ }
 257+ $res2 = $db->select( $db->tableName('smw_nary_relations'),
 258+ 'nary_pos, object_title, object_namespace',
 259+ 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
 260+ ' AND nary_key=' . $db->addQuotes($row->nary_key),
 261+ 'SMW::getPropertyValues');
 262+ while($row2 = $db->fetchObject($res2)) {
 263+ if ( ($row2->nary_pos < count($subtypes)) &&
 264+ ($subtypes[$row2->nary_pos]->getXSDValue() == '_wpg') ) {
 265+ $dv = SMWDataValueFactory::newTypeIDValue('_wpg');
 266+ $dv->setValues($row2->object_title, $row2->object_namespace);
 267+ $values[$row2->nary_pos] = $dv;
 268+ }
 269+ }
 270+ $db->freeResult($res2);
 271+ $uservalue = ''; /// hack hack hack
 272+ $first = true;
 273+ for ($i=0; $i<count($subtypes); $i++) {
 274+// if (!array_key_exists($i, $values) {
 275+// $values[$i] = NULL;
 276+// }
 277+ if ($first) {
 278+ $first = false;
 279+ } else {
 280+ $uservalue .= ';';
 281+ }
 282+ if (array_key_exists($i, $values)) {
 283+ $uservalue .= $values[$i]->getWikiValue();
 284+ }
 285+ }
 286+ $dv = SMWDataValueFactory::newPropertyObjectValue($property);
 287+ $dv->setOutputFormat($outputformat);
 288+ $dv->setUserValue($uservalue);
 289+ $result[] = $dv;
 290+ }
 291+ $db->freeResult($res);
 292+ break;
 293+ default:
224294 if ( ($requestoptions !== NULL) && ($requestoptions->boundary !== NULL) &&
225295 ($requestoptions->boundary->isNumeric()) ) {
226296 $value_column = 'value_num';
@@ -288,48 +358,39 @@
289359
290360 function getAllPropertySubjects(Title $property, $requestoptions = NULL) {
291361 $db =& wfGetDB( DB_SLAVE );
292 - $sql = 'attribute_title=' . $db->addQuotes($property->getDBkey()) .
293 - $this->getSQLConditions($requestoptions,'subject_title','subject_title');
294 -
295 - $result = array();
296362 $id = SMWDataValueFactory::getPropertyObjectTypeID($property);
297363 switch ($id) {
298364 case '_txt':
299 - $res = $db->select( $db->tableName('smw_longstrings'),
300 - 'DISTINCT subject_id',
301 - $sql, 'SMW::getAllPropertySubjects',
302 - $this->getSQLOptions($requestoptions,'subject_title') );
303 - if($db->numRows( $res ) > 0) {
304 - while($row = $db->fetchObject($res)) {
305 - $result[] = Title::newFromId($row->subject_id);
306 - }
307 - }
308 - $db->freeResult($res);
 365+ $tablename = 'smw_longstrings';
 366+ $pcolumn = 'attribute_title';
 367+ $extraconds = '';
309368 break;
310369 case '_wpg':
311 - $sql = 'relation_title=' . $db->addQuotes($property->getDBkey()) .
312 - $this->getSQLConditions($requestoptions,'subject_title','subject_title');
313 - $res = $db->select( $db->tableName('smw_relations'),
314 - 'DISTINCT subject_id',
315 - $sql, 'SMW::getAllPropertySubjects',
316 - $this->getSQLOptions($requestoptions,'subject_title') );
317 - while($row = $db->fetchObject($res)) {
318 - $result[] = Title::newFromId($row->subject_id);
319 - }
320 - $db->freeResult($res);
 370+ $tablename = 'smw_relations';
 371+ $pcolumn = 'relation_title';
 372+ $extraconds = $this->getSQLConditions($requestoptions,'subject_title','subject_title');
321373 break;
 374+ case '__nry':
 375+ $tablename = 'smw_nary';
 376+ $pcolumn = 'attribute_title';
 377+ $extraconds = $this->getSQLConditions($requestoptions,'subject_title','subject_title');
 378+ break;
322379 default:
323 - $res = $db->select( $db->tableName('smw_attributes'),
324 - 'DISTINCT subject_id',
325 - $sql, 'SMW::getAllPropertySubjects',
326 - $this->getSQLOptions($requestoptions,'subject_title') );
327 - if($db->numRows( $res ) > 0) {
328 - while($row = $db->fetchObject($res)) {
329 - $result[] = Title::newFromId($row->subject_id);
330 - }
331 - }
332 - $db->freeResult($res);
 380+ $tablename = 'smw_attributes';
 381+ $pcolumn = 'attribute_title';
 382+ $extraconds = $this->getSQLConditions($requestoptions,'subject_title','subject_title');
333383 }
 384+
 385+ $res = $db->select( $db->tableName($tablename),
 386+ 'DISTINCT subject_id',
 387+ $pcolumn .'=' . $db->addQuotes($property->getDBkey()) . $extraconds,
 388+ 'SMW::getAllPropertySubjects',
 389+ $this->getSQLOptions($requestoptions,'subject_title') );
 390+ $result = array();
 391+ while($row = $db->fetchObject($res)) {
 392+ $result[] = Title::newFromId($row->subject_id);
 393+ }
 394+ $db->freeResult($res);
334395 return $result;
335396 }
336397
@@ -368,6 +429,15 @@
369430 }
370431 }
371432 $db->freeResult($res);
 433+ $res = $db->select( $db->tableName('smw_nary'),
 434+ 'DISTINCT attribute_title',
 435+ $sql, 'SMW::getProperties', $this->getSQLOptions($requestoptions,'attribute_title') );
 436+ if ($db->numRows( $res ) > 0) {
 437+ while($row = $db->fetchObject($res)) {
 438+ $result[] = Title::newFromText($row->attribute_title, SMW_NS_PROPERTY);
 439+ }
 440+ }
 441+ $db->freeResult($res);
372442
373443 return $result;
374444 }
@@ -410,17 +480,19 @@
411481 $db->delete('smw_nary',
412482 array('subject_id' => $subject->getArticleID()),
413483 'SMW::deleteSubject::NAry');
 484+ if ($db->affectedRows() != 0) {
414485 ///FIXME: check there were entries in smw_nary before continuing!
415486 /// "Affected rows" should be easy to get
416 - $db->delete('smw_nary_relations',
417 - array('subject_id' => $subject->getArticleID()),
418 - 'SMW::deleteSubject::NAryRelations');
419 - $db->delete('smw_nary_attributes',
420 - array('subject_id' => $subject->getArticleID()),
421 - 'SMW::deleteSubject::NAryAttributes');
422 - $db->delete('smw_nary_longstrings',
423 - array('subject_id' => $subject->getArticleID()),
424 - 'SMW::deleteSubject::NaryLongstrings');
 487+ $db->delete('smw_nary_relations',
 488+ array('subject_id' => $subject->getArticleID()),
 489+ 'SMW::deleteSubject::NAryRelations');
 490+ $db->delete('smw_nary_attributes',
 491+ array('subject_id' => $subject->getArticleID()),
 492+ 'SMW::deleteSubject::NAryAttributes');
 493+ $db->delete('smw_nary_longstrings',
 494+ array('subject_id' => $subject->getArticleID()),
 495+ 'SMW::deleteSubject::NaryLongstrings');
 496+ }
425497 if ( $subject->getNamespace() == SMW_NS_PROPERTY ) {
426498 $db->delete('smw_subprops',
427499 array('subject_title' => $subject->getDBKey()),
@@ -475,30 +547,32 @@
476548 'nary_key' => $nkey );
477549 $npos = 0;
478550 foreach ($value->getDVs() as $dv) {
479 - switch ($dv->getTypeID()) {
480 - case '_wpg':
481 - $up_nary_relations[] =
482 - array( 'subject_id' => $subject->getArticleID(),
483 - 'nary_key' => $nkey,
484 - 'nary_pos' => $npos,
485 - 'object_namespace' => $dv->getNamespace(),
486 - 'object_title' => $dv->getDBkey() );
487 - break;
488 - case '_txt':
489 - $up_nary_longstrings[] =
490 - array( 'subject_id' => $subject->getArticleID(),
491 - 'nary_key' => $nkey,
492 - 'nary_pos' => $npos,
493 - 'value_blob' => $dv->getXSDValue() );
494 - break;
495 - default:
496 - $up_nary_attributes[] =
497 - array( 'subject_id' => $subject->getArticleID(),
498 - 'nary_key' => $nkey,
499 - 'nary_pos' => $npos,
500 - 'value_unit' => $dv->getUnit(),
501 - 'value_xsd' => $dv->getXSDValue(),
502 - 'value_num' => $dv->getNumericValue() );
 551+ if ( ($dv !== NULL) && ($dv->isValid()) ) {
 552+ switch ($dv->getTypeID()) {
 553+ case '_wpg':
 554+ $up_nary_relations[] =
 555+ array( 'subject_id' => $subject->getArticleID(),
 556+ 'nary_key' => $nkey,
 557+ 'nary_pos' => $npos,
 558+ 'object_namespace' => $dv->getNamespace(),
 559+ 'object_title' => $dv->getDBkey() );
 560+ break;
 561+ case '_txt':
 562+ $up_nary_longstrings[] =
 563+ array( 'subject_id' => $subject->getArticleID(),
 564+ 'nary_key' => $nkey,
 565+ 'nary_pos' => $npos,
 566+ 'value_blob' => $dv->getXSDValue() );
 567+ break;
 568+ default:
 569+ $up_nary_attributes[] =
 570+ array( 'subject_id' => $subject->getArticleID(),
 571+ 'nary_key' => $nkey,
 572+ 'nary_pos' => $npos,
 573+ 'value_unit' => $dv->getUnit(),
 574+ 'value_xsd' => $dv->getXSDValue(),
 575+ 'value_num' => $dv->getNumericValue() );
 576+ }
503577 }
504578 $npos++;
505579 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php
@@ -185,8 +185,10 @@
186186 */
187187 public function getTypeValues() {
188188 $result = array();
 189+ $i = 0;
189190 foreach ($this->getTypeLabels() as $tl) {
190 - $result[] = SMWDataValueFactory::newSpecialValue(SMW_SP_HAS_TYPE, $tl);
 191+ $result[$i] = SMWDataValueFactory::newSpecialValue(SMW_SP_HAS_TYPE, $tl);
 192+ $i++;
191193 }
192194 return $result;
193195 }

Status & tagging log