r24040 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24039‎ | r24040 | r24041 >
Date:17:19, 12 July 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Storage and retrieval of "subproperty of" in dedicated table
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -50,33 +50,39 @@
5151 $result = array();
5252 if ($specialprop === SMW_SP_HAS_CATEGORY) { // category membership
5353 $sql = 'cl_from=' . $db->addQuotes($subject->getArticleID());
54 - $res = $db->select( $db->tableName('categorylinks'),
 54+ $res = $db->select( 'categorylinks',
5555 'DISTINCT cl_to',
5656 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
5757 // rewrite result as array
58 - if($db->numRows( $res ) > 0) {
59 - while($row = $db->fetchObject($res)) {
60 - $result[] = Title::newFromText($row->cl_to, NS_CATEGORY);
61 - }
 58+ while($row = $db->fetchObject($res)) {
 59+ $result[] = Title::newFromText($row->cl_to, NS_CATEGORY);
6260 }
6361 $db->freeResult($res);
6462 } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { // redirections
6563 $sql = 'rd_from=' . $db->addQuotes($subject->getArticleID());
66 - $res = $db->select( $db->tableName('redirect'),
 64+ $res = $db->select( 'redirect',
6765 'rd_namespace,rd_title',
6866 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
69 -
70 - // reqrite results as array
71 - if($db->numRows( $res ) > 0) {
72 - while($row = $db->fetchObject($res)) {
73 - $result[] = Title::makeTitle($row->rd_namespace, $row->rd_title);
74 - }
 67+ // rewrite results as array
 68+ while($row = $db->fetchObject($res)) {
 69+ $result[] = Title::makeTitle($row->rd_namespace, $row->rd_title);
7570 }
7671 $db->freeResult($res);
 72+ } elseif ($specialprop === SMW_SP_SUBPROPERTY_OF) { // subproperty
 73+ $sql = 'subject_title=' . $db->addQuotes($subject->getDBKey()) .
 74+ 'AND namespace=' . $db->addQuotes($subject->getNamespace());
 75+ $res = $db->select( 'smw_subprops',
 76+ 'object_title',
 77+ $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
 78+ // rewrite results as array
 79+ while($row = $db->fetchObject($res)) {
 80+ $result[] = Title::makeTitle($subject->namespace, $row->object_title);
 81+ }
 82+ $db->freeResult($res);
7783 } else { // "normal" special property
7884 $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
7985 'AND property_id=' . $db->addQuotes($specialprop);
80 - $res = $db->select( $db->tableName('smw_specialprops'),
 86+ $res = $db->select( 'smw_specialprops',
8187 'value_string',
8288 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
8389 // rewrite result as array
@@ -107,34 +113,36 @@
108114 return array();
109115 }
110116 $sql = 'cl_to=' . $db->addQuotes($value->getDBKey());
111 - $res = $db->select( $db->tableName('categorylinks'),
 117+ $res = $db->select( 'categorylinks',
112118 'DISTINCT cl_from',
113119 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
114 -
115120 // rewrite result as array
116 - if($db->numRows( $res ) > 0) {
117 - while($row = $db->fetchObject($res)) {
118 - $result[] = Title::newFromID($row->cl_from);
119 - }
 121+ while($row = $db->fetchObject($res)) {
 122+ $result[] = Title::newFromID($row->cl_from);
120123 }
121124 $db->freeResult($res);
122 -
123125 } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { // redirections
124 -
125126 $sql = 'rd_title=' . $db->addQuotes($value->getDBKey())
126127 . ' AND rd_namespace=' . $db->addQuotes($value->getNamespace());
127 - $res = $db->select( $db->tableName('redirect'),
 128+ $res = $db->select( 'redirect',
128129 'rd_from',
129130 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
130 -
131131 // reqrite results as array
132 - if($db->numRows( $res ) > 0) {
133 - while($row = $db->fetchObject($res)) {
134 - $result[] = Title::newFromID($row->rd_from);
135 - }
 132+ while($row = $db->fetchObject($res)) {
 133+ $result[] = Title::newFromID($row->rd_from);
136134 }
137135 $db->freeResult($res);
138 -
 136+ } elseif ($specialprop === SMW_SP_SUBPROPERTY_OF) { // subproperties
 137+ $sql = 'object_title=' . $db->addQuotes($value->getDBKey()) .
 138+ 'AND namespace=' . $db->addQuotes($value->getNamespace());
 139+ $res = $db->select( 'smw_subprops',
 140+ 'subject_title',
 141+ $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
 142+ // reqrite results as array
 143+ while($row = $db->fetchObject($res)) {
 144+ $result[] = Title::makeTitle($value->namespace, $row->subject_title);
 145+ }
 146+ $db->freeResult($res);
139147 } else {
140148
141149 if ($value instanceof SMWDataValue) {
@@ -157,15 +165,13 @@
158166 ' AND value_string=' . $db->addQuotes($stringvalue) .
159167 $this->getSQLConditions($requestoptions,'subject_title','subject_title');
160168
161 - $res = $db->select( $db->tableName('smw_specialprops'),
 169+ $res = $db->select( 'smw_specialprops',
162170 'DISTINCT subject_id',
163171 $sql, 'SMW::getSpecialSubjects', $this->getSQLOptions($requestoptions,'subject_title') );
164172
165173 // rewrite result as array
166 - if($db->numRows( $res ) > 0) {
167 - while($row = $db->fetchObject($res)) {
168 - $result[] = Title::newFromID($row->subject_id);
169 - }
 174+ while($row = $db->fetchObject($res)) {
 175+ $result[] = Title::newFromID($row->subject_id);
170176 }
171177 $db->freeResult($res);
172178 }
@@ -418,18 +424,23 @@
419425
420426 function deleteSubject(Title $subject) {
421427 $db =& wfGetDB( DB_MASTER );
422 - $db->delete($db->tableName('smw_relations'),
 428+ $db->delete('smw_relations',
423429 array('subject_id' => $subject->getArticleID()),
424430 'SMW::deleteSubject::Relations');
425 - $db->delete($db->tableName('smw_attributes'),
 431+ $db->delete('smw_attributes',
426432 array('subject_id' => $subject->getArticleID()),
427433 'SMW::deleteSubject::Attributes');
428 - $db->delete($db->tableName('smw_longstrings'),
 434+ $db->delete('smw_longstrings',
429435 array('subject_id' => $subject->getArticleID()),
430436 'SMW::deleteSubject::Longstrings');
431 - $db->delete($db->tableName('smw_specialprops'),
 437+ $db->delete('smw_specialprops',
432438 array('subject_id' => $subject->getArticleID()),
433439 'SMW::deleteSubject::Specialprops');
 440+ if ( ($subject->getNamespace() == SMW_NS_ATTRIBUTE) || ($subject->getNamespace() == SMW_NS_RELATION) ) {
 441+ $db->delete('smw_subprops',
 442+ array('subject_title' => $subject->getDBKey(), 'namespace' => $subject->getNamespace()),
 443+ 'SMW::deleteSubject::Subprops');
 444+ }
434445 }
435446
436447 function updateData(SMWSemanticData $data) {
@@ -442,6 +453,7 @@
443454 $up_attributes = array();
444455 $up_longstrings = array();
445456 $up_specials = array();
 457+ $up_subprops = array();
446458
447459 // relations
448460 foreach($data->getRelations() as $relation) {
@@ -485,46 +497,69 @@
486498
487499 //special properties
488500 foreach ($data->getSpecialProperties() as $special) {
489 - if ($special == SMW_SP_IMPORTED_FROM) { // don't store this, just used for display; TODO: filtering it here is bad
490 - continue;
491 - }
492 - $valueArray = $data->getSpecialValues($special);
493 - foreach($valueArray as $value) {
494 - if ($value instanceof SMWDataValue) {
495 - if ($value->getXSDValue() !== false) { // filters out error-values etc.
496 - $stringvalue = $value->getXSDValue();
 501+ switch ($special) {
 502+ case SMW_SP_IMPORTED_FROM: case SMW_SP_HAS_CATEGORY: case SMW_SP_REDIRECTS_TO:
 503+ // don't store this, just used for display;
 504+ // TODO: filtering here is bad for fully neglected properties (IMPORTED FROM)
 505+ break;
 506+ case SMW_SP_SUBPROPERTY_OF:
 507+ if ( ($subject->getNamespace() != SMW_NS_RELATION) &&
 508+ ($subject->getNamespace() != SMW_NS_ATTRIBUTE) ) {
 509+ break;
497510 }
498 - } elseif ($value instanceof Title) {
499 - if ( $special == SMW_SP_HAS_TYPE ) { // special handling, TODO: change this to use type ids
500 - $stringvalue = $value->getText();
501 - } else {
502 - $stringvalue = $value->getPrefixedText();
 511+ $valueArray = $data->getSpecialValues($special);
 512+ foreach($valueArray as $value) {
 513+ if ( $subject->getNamespace() == $value->getNamespace()) {
 514+ $up_subprops[] =
 515+ array('subject_title' => $subject->getDBkey(),
 516+ 'namespace' => $subject->getNamespace(),
 517+ 'object_title' => $value->getDBKey());
 518+ }
503519 }
504 - } else {
505 - $stringvalue = $value;
506 - }
507 - $up_specials[] =
508 - array('subject_id' => $subject->getArticleID(),
509 - 'subject_namespace' => $subject->getNamespace(),
510 - 'subject_title' => $subject->getDBkey(),
511 - 'property_id' => $special,
512 - 'value_string' => $stringvalue);
 520+ break;
 521+ default: // normal special value
 522+ $valueArray = $data->getSpecialValues($special);
 523+ foreach($valueArray as $value) {
 524+ if ($value instanceof SMWDataValue) {
 525+ if ($value->getXSDValue() !== false) { // filters out error-values etc.
 526+ $stringvalue = $value->getXSDValue();
 527+ }
 528+ } elseif ($value instanceof Title) {
 529+ if ( $special == SMW_SP_HAS_TYPE ) { /// TODO: ensure that all types are given as DVs!
 530+ $stringvalue = $value->getDBKey();
 531+ } else {
 532+ $stringvalue = $value->getPrefixedText();
 533+ }
 534+ } else {
 535+ $stringvalue = $value;
 536+ }
 537+ $up_specials[] =
 538+ array('subject_id' => $subject->getArticleID(),
 539+ 'subject_namespace' => $subject->getNamespace(),
 540+ 'subject_title' => $subject->getDBkey(),
 541+ 'property_id' => $special,
 542+ 'value_string' => $stringvalue);
 543+ }
 544+ break;
513545 }
514546 }
515547
516548 // write to DB:
517549 if (count($up_relations) > 0) {
518 - $db->insert( $db->tableName('smw_relations'), $up_relations, 'SMW::updateRelData');
 550+ $db->insert( 'smw_relations', $up_relations, 'SMW::updateRelData');
519551 }
520552 if (count($up_attributes) > 0) {
521 - $db->insert( $db->tableName('smw_attributes'), $up_attributes, 'SMW::updateAttData');
 553+ $db->insert( 'smw_attributes', $up_attributes, 'SMW::updateAttData');
522554 }
523555 if (count($up_longstrings) > 0) {
524 - $db->insert( $db->tableName('smw_longstrings'), $up_longstrings, 'SMW::updateLongData');
 556+ $db->insert( 'smw_longstrings', $up_longstrings, 'SMW::updateLongData');
525557 }
526558 if (count($up_specials) > 0) {
527 - $db->insert( $db->tableName('smw_specialprops'), $up_specials, 'SMW::updateSpecData');
 559+ $db->insert( 'smw_specialprops', $up_specials, 'SMW::updateSpecData');
528560 }
 561+ if (count($up_subprops) > 0) {
 562+ $db->insert( 'smw_subprops', $up_subprops, 'SMW::updateSubPropData');
 563+ }
529564 }
530565
531566 function changeTitle(Title $oldtitle, Title $newtitle, $keepid = true) {
@@ -534,7 +569,7 @@
535570 'subject_namespace' => $oldtitle->getNamespace() );
536571 $val_array = array( 'subject_title' => $newtitle->getDBkey(),
537572 'subject_namespace' => $newtitle->getNamespace() );
538 -
 573+
539574 // don't do this by default, since the ids you get when moving articles
540575 // are not the ones from the old article and the new one (in reality, the
541576 // $old_title refers to the newly generated redirect article, which does
@@ -550,10 +585,18 @@
551586 }
552587 }
553588
554 - $db->update($db->tableName('smw_relations'), $val_array, $cond_array, 'SMW::changeTitle');
555 - $db->update($db->tableName('smw_attributes'), $val_array, $cond_array, 'SMW::changeTitle');
556 - $db->update($db->tableName('smw_longstrings'), $val_array, $cond_array, 'SMW::changeTitle');
557 - $db->update($db->tableName('smw_specialprops'), $val_array, $cond_array, 'SMW::changeTitle');
 589+ $db->update('smw_relations', $val_array, $cond_array, 'SMW::changeTitle');
 590+ $db->update('smw_attributes', $val_array, $cond_array, 'SMW::changeTitle');
 591+ $db->update('smw_longstrings', $val_array, $cond_array, 'SMW::changeTitle');
 592+ $db->update('smw_specialprops', $val_array, $cond_array, 'SMW::changeTitle');
 593+
 594+ if ( ($oldtitle->getNamespace() == SMW_NS_ATTRIBUTE) || ($oldtitle->getNamespace() == SMW_NS_RELATION) ) {
 595+ if ( $oldtitle->getNamespace() == $newtitle->getNamespace() ) {
 596+ $db->update('smw_subprops', array('subject_title' => $newtitle->getDBkey()), array('subject_title' => $oldtitle->getDBkey()), 'SMW::changeTitle');
 597+ } else {
 598+ $db->delete('smw_subprops', array('subject_title' => $oldtitle->getDBKey()), 'SMW::changeTitle');
 599+ }
 600+ }
558601 }
559602
560603 ///// Query answering /////
@@ -688,52 +731,57 @@
689732 function setup($verbose = true) {
690733 global $wgDBname;
691734 $this->reportProgress("Setting up standard database configuration for SMW ...\n\n",$verbose);
692 -
693 - $fname = 'SMW::setupDatabase';
694735 $db =& wfGetDB( DB_MASTER );
695736
696 - extract( $db->tableNames('smw_relations','smw_attributes','smw_longstrings','smw_specialprops') );
 737+ extract( $db->tableNames('smw_relations','smw_attributes','smw_longstrings','smw_specialprops','smw_subprops') );
697738
698739 // create relation table
699740 $this->setupTable($smw_relations,
700741 array('subject_id' => 'INT(8) UNSIGNED NOT NULL',
701 - 'subject_namespace' => 'INT(11) NOT NULL',
702 - 'subject_title' => 'VARCHAR(255) NOT NULL',
703 - 'relation_title' => 'VARCHAR(255) NOT NULL',
704 - 'object_namespace' => 'INT(11) NOT NULL',
705 - 'object_title' => 'VARCHAR(255) NOT NULL'), $db, $verbose);
 742+ 'subject_namespace' => 'INT(11) NOT NULL',
 743+ 'subject_title' => 'VARCHAR(255) NOT NULL',
 744+ 'relation_title' => 'VARCHAR(255) NOT NULL',
 745+ 'object_namespace' => 'INT(11) NOT NULL',
 746+ 'object_title' => 'VARCHAR(255) NOT NULL'), $db, $verbose);
706747 $this->setupIndex($smw_relations, array('subject_id','relation_title','object_title,object_namespace'), $db);
707748
708749 // create attribute table
709750 $this->setupTable($smw_attributes,
710751 array('subject_id' => 'INT(8) UNSIGNED NOT NULL',
711 - 'subject_namespace' => 'INT(11) NOT NULL',
712 - 'subject_title' => 'VARCHAR(255) NOT NULL',
713 - 'attribute_title' => 'VARCHAR(255) NOT NULL',
714 - 'value_unit' => 'VARCHAR(63)',
715 - 'value_datatype' => 'VARCHAR(31) NOT NULL', /// TODO: remove value_datatype column
716 - 'value_xsd' => 'VARCHAR(255) NOT NULL',
717 - 'value_num' => 'DOUBLE'), $db, $verbose);
 752+ 'subject_namespace' => 'INT(11) NOT NULL',
 753+ 'subject_title' => 'VARCHAR(255) NOT NULL',
 754+ 'attribute_title' => 'VARCHAR(255) NOT NULL',
 755+ 'value_unit' => 'VARCHAR(63)',
 756+ 'value_datatype' => 'VARCHAR(31) NOT NULL', /// TODO: remove value_datatype column
 757+ 'value_xsd' => 'VARCHAR(255) NOT NULL',
 758+ 'value_num' => 'DOUBLE'), $db, $verbose);
718759 $this->setupIndex($smw_attributes, array('subject_id','attribute_title','value_num','value_xsd'), $db);
719760
720761 // create table for long string attributes
721762 $this->setupTable($smw_longstrings,
722763 array('subject_id' => 'INT(8) UNSIGNED NOT NULL',
723 - 'subject_namespace' => 'INT(11) NOT NULL',
724 - 'subject_title' => 'VARCHAR(255) NOT NULL',
725 - 'attribute_title' => 'VARCHAR(255) NOT NULL',
726 - 'value_blob' => 'MEDIUMBLOB'), $db, $verbose);
 764+ 'subject_namespace' => 'INT(11) NOT NULL',
 765+ 'subject_title' => 'VARCHAR(255) NOT NULL',
 766+ 'attribute_title' => 'VARCHAR(255) NOT NULL',
 767+ 'value_blob' => 'MEDIUMBLOB'), $db, $verbose);
727768 $this->setupIndex($smw_longstrings, array('subject_id','attribute_title'), $db);
728769
729770 // create table for special properties
730771 $this->setupTable($smw_specialprops,
731772 array('subject_id' => 'INT(8) UNSIGNED NOT NULL',
732 - 'subject_namespace' => 'INT(11) NOT NULL',
733 - 'subject_title' => 'VARCHAR(255) NOT NULL',
734 - 'property_id' => 'SMALLINT(6) NOT NULL',
735 - 'value_string' => 'VARCHAR(255) NOT NULL'), $db, $verbose);
 773+ 'subject_namespace' => 'INT(11) NOT NULL',
 774+ 'subject_title' => 'VARCHAR(255) NOT NULL',
 775+ 'property_id' => 'SMALLINT(6) NOT NULL',
 776+ 'value_string' => 'VARCHAR(255) NOT NULL'), $db, $verbose);
736777 $this->setupIndex($smw_specialprops, array('subject_id', 'property_id'), $db);
737778
 779+ // create table for subproperty relationships
 780+ $this->setupTable($smw_subprops,
 781+ array('subject_title' => 'VARCHAR(255) NOT NULL',
 782+ 'namespace' => 'INT(11) NOT NULL', // will be obsolete when collapsing attribs+rels
 783+ 'object_title' => 'VARCHAR(255) NOT NULL'), $db, $verbose);
 784+ $this->setupIndex($smw_subprops, array('subject_title', 'namespace', 'object_title'), $db);
 785+
738786 $this->reportProgress("Database initialised successfully.\n",$verbose);
739787 return true;
740788 }
@@ -1235,8 +1283,8 @@
12361284 * in the given DB table.
12371285 */
12381286 protected function setupIndex($table, $columns, $db) {
1239 - $fname = 'SMW::SetupIndex';
1240 - $res = $db->query( 'SHOW INDEX FROM ' . $table , $fname);
 1287+ $table = $db->tableName($table);
 1288+ $res = $db->query( 'SHOW INDEX FROM ' . $table , 'SMW::SetupIndex');
12411289 if ( !$res ) {
12421290 return false;
12431291 }
@@ -1252,13 +1300,13 @@
12531301 if ( $id !== false ) {
12541302 $columns[$id] = false;
12551303 } else { // duplicate or unrequired index
1256 - $db->query( 'DROP INDEX ' . $key . ' ON ' . $table);
 1304+ $db->query( 'DROP INDEX ' . $key . ' ON ' . $table, 'SMW::SetupIndex');
12571305 }
12581306 }
12591307
12601308 foreach ($columns as $column) { // add remaining indexes
12611309 if ($column != false) {
1262 - $db->query( "ALTER TABLE $table ADD INDEX ( $column )", $fname );
 1310+ $db->query( "ALTER TABLE $table ADD INDEX ( $column )", 'SMW::SetupIndex');
12631311 }
12641312 }
12651313 return true;

Status & tagging log