r24138 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24137‎ | r24138 | r24139 >
Date:23:27, 15 July 2007
Author:vrandezo
Status:old
Tags:
Comment:
Switched to the SAL for the export.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_WikiPage.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/specials/ExportRDF/SMW_SpecialExportRDF.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/ExportRDF/SMW_SpecialExportRDF.php
@@ -1,6 +1,7 @@
22 <?php
33 /**
44 * @author Markus Krötzsch
 5+ * @author Denny Vrandecic
56 *
67 * This special page for MediaWiki implements an RDF-export of
78 * semantic data, gathered both from the annotations in articles,
@@ -100,7 +101,7 @@
101102 // Values for special properties, see SMW_Settings.php for documentation.
102103 // Each value may be missing if not set or not relevant
103104 public $has_type = false;
104 - public $has_uri = false;
 105+ public $has_uri = false; // TODO not used right now
105106 public $ext_nsid = false;
106107 public $ext_section = false;
107108 // relevant title values, mandatory
@@ -112,6 +113,7 @@
113114 public $title_prefurl;
114115 public $title_dbkey;
115116 public $modifier = '';
 117+ public $value; // the title as a datavalue
116118 // details about URIs for export
117119 public $ns_uri = false;
118120 public $short_uri;
@@ -135,6 +137,7 @@
136138 $this->title_fragment = $title->getFragment();
137139 $this->title_prefurl = $title->getPrefixedURL();
138140 $this->title_dbkey = $title->getDBKey();
 141+ $this->value = SMWDataValueFactory::newTypeIDValue('_wpg', $title->getPrefixedText());
139142 $this->hashkey = $this->title_prefurl . ' ' . $modifier; // must agree with keys generated elsewhere in this code!
140143 $this->modifier = $modifier;
141144 if ($modifier != '') $modifier = '#' . $modifier;
@@ -143,30 +146,16 @@
144147 $this->exists = $title->exists();
145148
146149 if ($this->exists) {
147 - $res = $export->db->select($export->db->tableName('smw_specialprops'),
148 - 'property_id, value_string',
149 - 'subject_id =' . $export->db->addQuotes($this->title_id),
150 - 'SMWExportTitle::newFromID');
151 - while($row = $export->db->fetchObject($res)) { // read out relevant special props
152 - switch ($row->property_id) {
153 - case SMW_SP_HAS_TYPE:
154 - $this->has_type = $row->value_string;
155 - break;
156 - case SMW_SP_HAS_URI:
157 - $this->has_uri = $row->value_string;
158 - break;
159 - case SMW_SP_EXT_BASEURI:
160 - $this->ns_uri = $row->value_string;
161 - break;
162 - case SMW_SP_EXT_NSID:
163 - $this->ext_nsid = $row->value_string;
164 - break;
165 - case SMW_SP_EXT_SECTION:
166 - $this->ext_section = $row->value_string;
167 - break;
168 - }
 150+ if ($title->getNamespace() == SMW_NS_ATTRIBUTE) {
 151+ $a = $export->store->getSpecialValues( $title, SMW_SP_HAS_TYPE );
 152+ if (count($a)>0) $this->has_type = $a[0];
169153 }
170 - $export->db->freeResult($res);
 154+ $a = $export->store->getSpecialValues( $title, SMW_SP_EXT_BASEURI );
 155+ if (count($a)>0) $this->ns_uri = $a[0];
 156+ $a = $export->store->getSpecialValues( $title, SMW_SP_EXT_NSID );
 157+ if (count($a)>0) $this->ext_nsid = $a[0];
 158+ $a = $export->store->getSpecialValues( $title, SMW_SP_EXT_SECTION );
 159+ if (count($a)>0) $this->ext_section = $a[0];
171160 }
172161
173162 // Calculate URIs and label
@@ -254,9 +243,9 @@
255244 var $special_url;
256245
257246 /**
258 - * Database handler -- needed multiple times, so "cache" it
 247+ * Store handler -- needed multiple times, so "cache" it
259248 */
260 - var $db;
 249+ var $store;
261250
262251 /**
263252 * Array of additional namespaces (abbreviation => URI), flushed on
@@ -313,8 +302,6 @@
314303 global $wgServer; // actual server address (with http://)
315304 global $wgScript; // "/subdirectory/of/wiki/index.php"
316305 global $wgArticlePath;
317 - //@TODO: generate this properly, using $wgArticlePath DONE, please check
318 - //$this->wiki_xmlns_url = $wgServer . $wgScript . '/';
319306 $this->wiki_xmlns_url = $wgServer . str_replace('$1', '', $wgArticlePath);
320307 if (''==$smwgNamespace) {
321308 $resolver = Title::makeTitle( NS_SPECIAL, 'URIResolver');
@@ -344,7 +331,7 @@
345332 * properties are exported as well. Enables "browsable RDF."
346333 */
347334 public function printPages($pages, $recursion = 1, $backlinks = true) {
348 - $this->db = & wfGetDB( DB_MASTER );
 335+ $this->store = &smwfGetStore();
349336 $this->pre_ns_buffer = '';
350337 $this->post_ns_buffer = '';
351338 $this->first_flush = true;
@@ -376,24 +363,20 @@
377364 }
378365 // possibly add backlinks
379366 if ($backlinks === true) {
380 - $res = $this->db->query(
381 - 'SELECT DISTINCT subject_id FROM ' . $this->db->tableName('smw_relations') .
382 - ' WHERE object_title=' . $this->db->addQuotes($et->title_dbkey) .
383 - ' AND object_namespace=' . $this->db->addQuotes($et->title_namespace),
384 - 'SMWExportRDF::printPages');
385 - while($row = $this->db->fetchObject($res)) {
386 - $st = $this->getExportTitleFromID($row->subject_id);
387 - if (!array_key_exists($st->hashkey, $this->element_done)) {
388 - $cur_queue[] = $st;
 367+ $inRels = $this->store->getInProperties( $et->value );
 368+ foreach ($inRels as $inRel) {
 369+ $inSubs = $this->store->getPropertySubjects( $inRel, $et->value );
 370+ foreach($inSubs as $inSub) {
 371+ $st = $this->getExportTitleFromTitle( $inSub );
 372+ if (!array_key_exists($st->hashkey, $this->element_done)) {
 373+ $cur_queue[] = $st;
 374+ }
389375 }
390376 }
391377 if ( NS_CATEGORY === $et->title_namespace ) { // also print elements of categories
392 - $res = $this->db->query(
393 - 'SELECT cl_from FROM ' . $this->db->tableName('categorylinks') .
394 - ' WHERE cl_to=' . $this->db->addQuotes($et->title_dbkey),
395 - 'SMWExportRDF::printPages');
396 - while($row = $this->db->fetchObject($res)) {
397 - $st = $this->getExportTitleFromID($row->cl_from);
 378+ $instances = $this->store->getSpecialSubjects( SMW_SP_HAS_CATEGORY, $et->title );
 379+ foreach($instances as $instance) {
 380+ $st = $this->getExportTitleFromTitle( $instance );
398381 if (!array_key_exists($st->hashkey, $this->element_done)) {
399382 $cur_queue[] = $st;
400383 }
@@ -430,7 +413,8 @@
431414 global $smwgNamespacesWithSemanticLinks;
432415 $linkCache =& LinkCache::singleton();
433416
434 - $this->db = & wfGetDB( DB_MASTER );
 417+ $db = & wfGetDB( DB_MASTER );
 418+ $this->store = & smwfGetStore();
435419 $this->pre_ns_buffer = '';
436420 $this->post_ns_buffer = '';
437421 $this->first_flush = true;
@@ -450,7 +434,7 @@
451435 $this->printHeader(); // also inits global namespaces
452436
453437 $start = 1;
454 - $end = $this->db->selectField( 'page', 'max(page_id)', false, $fname );
 438+ $end = $db->selectField( 'page', 'max(page_id)', false, $outfile );
455439
456440 $a_count = 0; $d_count = 0; //DEBUG
457441
@@ -587,11 +571,10 @@
588572 break;
589573 case SMW_NS_ATTRIBUTE:
590574 if ( $et->has_type === false ) return; //attributes w/o type not exportable, TODO: is this what we want?
591 - $datatype_handler = SMWTypeHandlerFactory::getTypeHandlerByLabel($et->has_type);
592 - if ( ('annouri' == $datatype_handler->getID()) || ('annostring' == $datatype_handler->getID()) ) {
 575+ if ( ('annouri' == $et->has_type->getTypeID()) || ('annostring' == $et->has_type->getTypeID()) ) {
593576 $type = 'owl:AnnotationProperty'; // cannot be a subproperty, etc.
594577 } else {
595 - if ('' != $datatype_handler->getXSDType()) {
 578+ if ('' != SMWTypeHandlerFactory::getXSDTypeByID($et->has_type->getTypeID())) {
596579 $type = 'owl:DatatypeProperty';
597580 } else { // no xsd-type -> treat as object property
598581 $type = 'owl:ObjectProperty';
@@ -633,12 +616,11 @@
634617 if ( ($fullexport) && ($et->exists) ) {
635618 // add statements about categories
636619 if ($category_rel) {
637 - $res = $this->db->query( 'SELECT cl_to FROM ' . $this->db->tableName('categorylinks') . ' WHERE cl_from=' . $et->title_id, 'SMWExportRDF::printTriples');
638 - while($row = $this->db->fetchObject($res)) {
639 - $ct = $this->getExportTitle($row->cl_to, NS_CATEGORY);
 620+ $cats = $this->store->getSpecialValues( $et->title, SMW_SP_HAS_CATEGORY );
 621+ foreach ($cats as $cat) {
 622+ $ct = $this->getExportTitleFromTitle( $cat );
640623 $this->post_ns_buffer .= "\t\t<" . $category_rel . ' rdf:resource="' . $ct->long_uri . "\"/>\n";
641624 }
642 - $this->db->freeResult($res);
643625 }
644626
645627 // TODO: this is not convincing, esp. now that we have custom types
@@ -661,7 +643,7 @@
662644
663645 // add rdfs:subPropertyOf statements
664646 if ($subrel_rel) {
665 - $relations = &smwfGetStore()->getSpecialValues($et->title, SMW_SP_SUBPROPERTY_OF);
 647+ $relations = &smwfGetStore()->getSpecialValues($et->title, SMW_SP_SUBPROPERTY_OF);
666648 foreach ($relations as $relation) {
667649 // TODO in future, check type safety relations <-> attributes
668650 // TODO check also the type of what I am pointing to (is it a relation or sth else?)
@@ -673,7 +655,7 @@
674656 }
675657 }
676658 if ($subatt_rel) {
677 - $attributes = &smwfGetStore()->getSpecialValues($et->title, SMW_SP_SUBPROPERTY_OF);
 659+ $attributes = &smwfGetStore()->getSpecialValues($et->title, SMW_SP_SUBPROPERTY_OF);
678660 foreach ($attributes as $attribute) {
679661 // TODO in future, check type safety relations <-> attributes
680662 // TODO check also the type of what I am pointing to (is it an atrribute or sth else?)
@@ -690,32 +672,36 @@
691673
692674 if ($et->is_individual) { // do not print relations for schema elements, stay in OWL DL
693675 // print all relations
694 - $res = $this->db->query( 'SELECT relation_title, object_namespace, object_title FROM ' . $this->db->tableName('smw_relations') . ' WHERE subject_id=' . $et->title_id, 'SMWExportRDF::printTriples');
695 - while($row = $this->db->fetchObject($res)) {
696 - $pt = $this->getExportTitle($row->relation_title, SMW_NS_RELATION);
697 - $ot = $this->getExportTitle($row->object_title, $row->object_namespace);
698 - if ($ot->is_individual) { // no OWL Full
699 - $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:resource="' . $ot->long_uri . "\"/>\n";
 676+ $props = $this->store->getProperties( $et->title );
 677+ foreach ( $props as $prop ) {
 678+ $values = $this->store->getPropertyValues( $et->title, $prop );
 679+ foreach ( $values as $value ) {
 680+ $pt = $this->getExportTitleFromTitle( $prop, $value->getUnit() );
 681+ $this-> post_ns_buffer .= $value->exportToRDF( $pt->short_uri, $this );
 682+// $ot = $this->getExportTitleFromTitle( $obj );
 683+// if ($ot->is_individual) { // check OWL Fullness
 684+// $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:resource="' . $ot->long_uri . "\"/>\n";
 685+// }
700686 }
701687 }
702688 // print all attributes
703 - $res = $this->db->query( 'SELECT attribute_title, value_unit, value_datatype, value_xsd FROM ' . $this->db->tableName('smw_attributes') . ' WHERE subject_id=' . $et->title_id, 'SMWExportRDF::printTriples');
704 - while($row = $this->db->fetchObject($res)) {
705 - $pt = $this->getExportTitle($row->attribute_title, SMW_NS_ATTRIBUTE, $row->value_unit);
706 - // check whether attribute type has changed since data was stored:
707 - if ( ($pt->has_type !== false) &&
708 - ($row->value_datatype !== SMWTypeHandlerFactory::getTypeHandlerByLabel($pt->has_type)->getID() ) )
709 - continue;
710 - $xsdtype = SMWTypeHandlerFactory::getXSDTypeByID($row->value_datatype);
711 - if ( ($xsdtype !== '') && ($xsdtype !== NULL) ) {
712 - $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:datatype="' . $xsdtype . '">' . $row->value_xsd . '</' . $pt->short_uri . ">\n";
713 - } elseif ($xsdtype === '') { // no xsd-type -> export as object property
714 - $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:resource="' . $row->value_xsd . "\"/>\n";
715 - } else {
716 - $this->post_ns_buffer .= "<!-- Sorry, type '$row->value_datatype' of attribute '$pt->label' could not be resolved to XSD. Probably an upgrade issue. Try saving the respective article again. -->";
 689+/* $atts = $this->store->getAttributes( $et->title );
 690+ foreach ( $atts as $att ) {
 691+ $dvs = $this->store->getAttributeValues( $et->title, $att );
 692+ foreach ( $dvs as $dv ) {
 693+ $pt = $this->getExportTitleFromTitle( $att, $dv->getUnit() );
 694+ $xsdtype = SMWTypeHandlerFactory::getXSDTypeByID( $dv->getTypeID() );
 695+ $xsdvalue = $dv->getXSDValue();
 696+ if ( ($xsdtype !== '') && ($xsdtype !== NULL) && ($xsdvalue != '') ) {
 697+ $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:datatype="' . $xsdtype . '">' . $xsdvalue . '</' . $pt->short_uri . ">\n";
 698+ } elseif ($xsdtype === '') { // no xsd-type -> export as object property
 699+ $this->post_ns_buffer .= "\t\t<" . $pt->short_uri . ' rdf:resource="' . $xsdvalue . "\"/>\n";
 700+ } else {
 701+ $this->post_ns_buffer .= "\t\t<!-- Sorry, type '$xsdtype' of attribute '$pt->label' could not be resolved to XSD. Probably an upgrade issue. Try saving the respective article again. -->\n";
 702+ }
717703 }
718704 }
719 - }
 705+*/ }
720706 }
721707
722708 $this->post_ns_buffer .= "\t</" . $type . ">\n";
@@ -762,7 +748,23 @@
763749 $this->extra_namespaces[$nsshort] = $nsuri;
764750 }
765751 }
 752+
 753+ /**
 754+ * Returns the exportable QName of a page.
 755+ */
 756+ public function getQName(Title $title, $unit = '') {
 757+ $et = $this->getExportTitleFromTitle( $title, $unit );
 758+ return $et->long_uri;
 759+ }
766760
 761+ /**
 762+ * Returns the exportable URL of a page.
 763+ */
 764+ public function getURI(Title $title, $unit = '') {
 765+ $et = $this->getExportTitleFromTitle( $title, $unit );
 766+ return $et->short_uri;
 767+ }
 768+
767769 /** Fetch SMWExportTitle for a given article. The inputs are
768770 * $text -- standard text form of the main part of the article name
769771 * (no namespace, no urlencode)
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -70,8 +70,8 @@
7171 /**
7272 * Define a particular output format. Output formats are user-supplied strings
7373 * that the datavalue may (or may not) use to customise its return value. For
74 - * example, quantities with units of measurement may interpret the string as
75 - * a desired output unit. In other cases, the output format might be built-in
 74+ * example, quantities with units of measurement may interpret the string as
 75+ * a desired output unit. In other cases, the output format might be built-in
7676 * and subject to internationalisation (which the datavalue has to implement).
7777 * In any case, an empty string resets the output format to the default.
7878 */
@@ -96,7 +96,7 @@
9797
9898 /**
9999 * Initialise the datavalue from the given value string and unit.
100 - * The format of both strings strictly corresponds to the output
 100+ * The format of both strings strictly corresponds to the output
101101 * of this implementation for getXSDValue() and getUnit().
102102 */
103103 abstract protected function parseXSDValue($value, $unit);
@@ -158,7 +158,7 @@
159159 /**
160160 * Return the plain wiki version of the value, or
161161 * FALSE if no such version is available. The returned
162 - * string suffices to reobtain the same DataValue
 162+ * string suffices to reobtain the same DataValue
163163 * when passing it as an input string to setUserValue().
164164 * Thus it also includes units, if any.
165165 */
@@ -237,6 +237,20 @@
238238 public function getErrors() {
239239 return $this->m_errors;
240240 }
 241+
 242+ /**
 243+ * Exports the datavalue to RDF (i.e. it returns a string that consists
 244+ * of the lines that, in RDF/XML, can be fitted between the object-tags.
 245+ * This should be overwritten.
 246+ * QName -- the qualified name that the data value should use for exporting,
 247+ * since it may be an imported name.
 248+ * Exporter -- the exporting object
 249+ * TODO make it an abstract function? Not sure.
 250+ */
 251+ public function exportToRDF($QName, ExportRDF $exporter) {
 252+ $type = $this->getTypeID();
 253+ return "\t\t<!-- Sorry, unknown how to export type '$type'. -->\n";
 254+ }
241255
242256 /*********************************************************************/
243257 /* Static methods for initialisation */
@@ -246,7 +260,7 @@
247261 * Create a value from a string supplied by a user for a given attribute.
248262 * If no value is given, an empty container is created, the value of which
249263 * can be set later on.
250 - *
 264+ *
251265 * @DEPRECATED
252266 */
253267 static function newAttributeValue($attribute, $value=false) {
@@ -256,7 +270,7 @@
257271
258272 /**
259273 * Create a value from a string supplied by a user for a given special
260 - * property, encoded as a numeric constant.
 274+ * property, encoded as a numeric constant.
261275 * If no value is given, an empty container is created, the value of which
262276 * can be set later on.
263277 *
@@ -271,14 +285,14 @@
272286 * Create a value from a user-supplied string for which a type handler is known
273287 * If no value is given, an empty container is created, the value of which
274288 * can be set later on.
275 - *
 289+ *
276290 * @DEPRECATED
277291 */
278292 static function newTypedValue(SMWTypeHandler $type, $value=false) {
279293 trigger_error("The function SMWDataValue::newTypedValue() is deprecated.", E_USER_NOTICE);
280294 return SMWDataValueFactory::newTypeHandlerValue($type, $value);
281295 }
282 -
 296+
283297 /*********************************************************************/
284298 /* Legacy methods for compatiblity */
285299 /*********************************************************************/
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_WikiPage.php
@@ -1,7 +1,7 @@
22 <?php
33 /**
44 * This datavalue implements special processing suitable for defining
5 - * wikipages as values of properties. This value container currently
 5+ * wikipages as values of properties. This value container currently
66 * behaves somewhat special in that its xsdvalue is not containint all
77 * relevant information (it just gives the DB-Key, not the namespace).
88 * TODO: This should change, but is not really critical now.
@@ -128,6 +128,11 @@
129129 return false;
130130 }
131131
 132+ public function exportToRDF($QName, ExportRDF $exporter) {
 133+ $obj = $exporter->getURI( $this->getTitle() );
 134+ return "\t\t<$QName rdf:resource=\"$obj\" />\n";
 135+ }
 136+
132137 ///// special interface for wiki page values
133138
134139 /**
@@ -204,4 +209,4 @@
205210 }
206211 }
207212
208 -?>
\ No newline at end of file
 213+?>
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -3,7 +3,7 @@
44 /**
55 * This datavalue implements String-Datavalues suitable for defining
66 * String-types of properties.
7 - *
 7+ *
88 * @author: Nikolas Iwan
99 */
1010 class SMWStringValue extends SMWDataValue {
@@ -67,11 +67,11 @@
6868 public function getXSDValue() {
6969 return $this->m_xsdvalue;
7070 }
71 -
 71+
7272 public function getWikiValue(){
7373 return $this->m_value;
7474 }
75 -
 75+
7676 public function getNumericValue() {
7777 return NULL;
7878 }
@@ -88,5 +88,8 @@
8989 return false;
9090 }
9191
 92+ public function exportToRDF($QName, ExportRDF $exporter) {
 93+ return "\t\t<$QName rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">$this->m_xsdvalue</$QName>\n";
 94+ }
9295
9396 }

Status & tagging log