r24199 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24198‎ | r24199 | r24200 >
Date:15:11, 17 July 2007
Author:vrandezo
Status:old
Tags:
Comment:
Extended export for attributes with units
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_OldDataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/specials/ExportRDF/SMW_SpecialExportRDF.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/ExportRDF/SMW_SpecialExportRDF.php
@@ -68,8 +68,11 @@
6969 } elseif ( ($bl == '0') || ( '' == $bl && $postform) ) {
7070 $backlinks = false; //everybody can explicitly switch off backlinks
7171 }
 72+ $date = $wgRequest->getText( 'date' );
 73+ if ('' == $date) $date = $wgRequest->getVal( 'date' );
7274
7375 $exRDF = new ExportRDF();
 76+ if ('' !== $date) $exRDF->setDate($date);
7477 $exRDF->printPages($pages,$recursive,$backlinks);
7578 return;
7679 }
@@ -142,7 +145,7 @@
143146 $this->modifier = $modifier;
144147 if ($modifier != '') $modifier = '#' . $modifier;
145148
146 - $this->is_individual = ( ($this->title_namespace !== SMW_NS_RELATION) && ($this->title_namespace !== SMW_NS_ATTRIBUTE) && ($this->title_namespace !== NS_CATEGORY) );
 149+ $this->is_individual = ( ($this->title_namespace !== SMW_NS_PROPERTY) && ($this->title_namespace !== NS_CATEGORY) );
147150 $this->exists = $title->exists();
148151
149152 if ($this->exists) {
@@ -169,14 +172,10 @@
170173 $this->ns_uri = ExportRDF::makeXMLExportId(urlencode(str_replace(' ', '_', $ns_text)));
171174 $baseXML = ExportRDF::makeXMLExportId(urlencode(str_replace(' ', '_', $this->title_text . $modifier)));
172175 switch ($this->title_namespace) {
173 - case SMW_NS_RELATION:
174 - $xmlprefix = 'relation:';
175 - $xmlent = '&relation;';
 176+ case SMW_NS_PROPERTY:
 177+ $xmlprefix = 'property:';
 178+ $xmlent = '&property;';
176179 break;
177 - case SMW_NS_ATTRIBUTE:
178 - $xmlprefix = 'attribute:';
179 - $xmlent = '&attribute;';
180 - break;
181180 default:
182181 $xmlprefix = 'wiki:';
183182 $xmlent = '&wiki;';
@@ -194,7 +193,11 @@
195194 $this->long_uri = $this->ns_uri . $this->ext_section;
196195 $this->short_uri = $this->ext_nsid . ':' . $this->ext_section;
197196 }
198 - $this->label = $ns_text . $this->title_text;
 197+ if ($this->is_individual) {
 198+ $this->label = $ns_text . $this->title_text;
 199+ } else {
 200+ $this->label = $this->title_text;
 201+ }
199202 //$this->label = $this->title_text; // we show the namespace prefixes in most specials in our wiki, so this should also be done by external (re)users (mak)
200203 //TODO: should we make an exception for schema elements (Category, Attriubte, ...) where the prefix is clear from the context? At least namespaces like User: seem to be essential for understanding.
201204 if ($this->modifier != '') $this->label .= " ($this->modifier)";
@@ -248,6 +251,12 @@
249252 var $store;
250253
251254 /**
 255+ * Date used to filter the export. If a page has not been changed since that
 256+ * date it will not be exported
 257+ */
 258+ var $date;
 259+
 260+ /**
252261 * Array of additional namespaces (abbreviation => URI), flushed on
253262 * closing the current namespace tag. Since we export RDF in a streamed
254263 * way, it is not always possible to embed additional namespaces into
@@ -318,9 +327,20 @@
319328
320329 $this->element_queue = array();
321330 $this->element_done = array();
 331+ $this->date = '';
322332 }
323333
324334 /**
 335+ * Sets a date as a filter. Any page that has not been changed since that date
 336+ * will not be exported. The date has to be a string in XSD format.
 337+ */
 338+ public function setDate($date) {
 339+ $timeint = strtotime($date);
 340+ $stamp = date("YmdHis", $timeint);
 341+ $this->date = $stamp;
 342+ }
 343+
 344+ /**
325345 * This function prints all selected pages. The parameter $recursion determines
326346 * how referenced ressources are treated:
327347 * '0' : add brief declarations for each
@@ -390,13 +410,14 @@
391411 }
392412
393413 // if pages are not processed recursively, print mentioned declarations
394 - if (count($this->element_queue) > 0) {
 414+ if (!empty($this->element_queue)) {
395415 if ( '' != $this->pre_ns_buffer ) {
396416 $this->post_ns_buffer .= "\t<!-- auxilliary definitions -->\n";
397417 } else {
398418 print "\t<!-- auxilliary definitions -->\n"; // just print this comment, so that later outputs still find the empty pre_ns_buffer!
399419 }
400 - foreach ( $this->element_queue as $et ) {
 420+ while (!empty($this->element_queue)) {
 421+ $et = array_pop($this->element_queue);
401422 $this->printTriples($et,false);
402423 }
403424 }
@@ -513,10 +534,8 @@
514535 "\t<!ENTITY smwdt 'http://smw.ontoware.org/2005/smw-datatype#'>\n" .
515536 // A note on "wiki": this namespace is crucial as a fallback when it would be illegal to start e.g. with a number. In this case, one can always use wiki:... followed by "_" and possibly some namespace, since _ is legal as a first character.
516537 "\t<!ENTITY wiki '" . $this->wiki_xmlns_xml . "'>\n" .
517 - "\t<!ENTITY relation '" . $this->wiki_xmlns_xml .
518 - $this->makeXMLExportId(urlencode(str_replace(' ', '_', $wgContLang->getNsText(SMW_NS_RELATION) . ':'))) . "'>\n" .
519 - "\t<!ENTITY attribute '" . $this->wiki_xmlns_xml .
520 - $this->makeXMLExportId(urlencode(str_replace(' ', '_', $wgContLang->getNsText(SMW_NS_ATTRIBUTE) . ':'))) . "'>\n" .
 538+ "\t<!ENTITY property '" . $this->wiki_xmlns_xml .
 539+ $this->makeXMLExportId(urlencode(str_replace(' ', '_', $wgContLang->getNsText(SMW_NS_PROPERTY) . ':'))) . "'>\n" .
521540 "\t<!ENTITY wikiurl '" . $this->wiki_xmlns_url . "'>\n" .
522541 "]>\n\n" .
523542 "<rdf:RDF\n" .
@@ -525,9 +544,8 @@
526545 "\txmlns:owl =\"&owl;\"\n" .
527546 "\txmlns:smw=\"&smw;\"\n" .
528547 "\txmlns:wiki=\"&wiki;\"\n" .
529 - "\txmlns:relation=\"&relation;\"\n" .
530 - "\txmlns:attribute=\"&attribute;\"";
531 - $this->global_namespaces = array('rdf'=>true, 'rdfs'=>true, 'owl'=>true, 'smw'=>true, 'wiki'=>true, 'relation'=>true, 'attribute'=>true);
 548+ "\txmlns:property=\"&property;\"";
 549+ $this->global_namespaces = array('rdf'=>true, 'rdfs'=>true, 'owl'=>true, 'smw'=>true, 'wiki'=>true, 'property'=>true);
532550
533551 $this->post_ns_buffer .=
534552 ">\n\t<!-- reference to the Semantic MediaWiki schema -->\n" .
@@ -540,9 +558,21 @@
541559 "\t<owl:AnnotationProperty rdf:about=\"&smw;subPropertyOf\">\n" .
542560 "\t\t<rdfs:isDefinedBy rdf:resource=\"http://smw.ontoware.org/2005/smw\"/>\n" .
543561 "\t</owl:AnnotationProperty>\n" .
 562+ "\t<owl:AnnotationProperty rdf:about=\"&smw;exportDate\">\n" .
 563+ "\t\t<rdfs:isDefinedBy rdf:resource=\"http://smw.ontoware.org/2005/smw\"/>\n" .
 564+ "\t</owl:AnnotationProperty>\n" .
 565+ "\t<owl:AnnotationProperty rdf:about=\"&smw;hasModifier\">\n" .
 566+ "\t\t<rdfs:isDefinedBy rdf:resource=\"http://smw.ontoware.org/2005/smw\"/>\n" .
 567+ "\t</owl:AnnotationProperty>\n" .
 568+ "\t<owl:AnnotationProperty rdf:about=\"&smw;baseProperty\">\n" .
 569+ "\t\t<rdfs:isDefinedBy rdf:resource=\"http://smw.ontoware.org/2005/smw\"/>\n" .
 570+ "\t</owl:AnnotationProperty>\n" .
544571 "\t<owl:Class rdf:about=\"&smw;Thing\">\n" .
545572 "\t\t<rdfs:isDefinedBy rdf:resource=\"http://smw.ontoware.org/2005/smw\"/>\n" .
546573 "\t</owl:Class>\n" .
 574+ "\t<owl:Ontology rdf:about=\"\">\n" .
 575+ "\t\t<smw:exportDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" . date(DATE_W3C) . "</smw:exportDate>\n" .
 576+ "\t</owl:Ontology>" .
547577 "\t<!-- exported page data -->\n";
548578 }
549579
@@ -551,16 +581,37 @@
552582 }
553583
554584 function printTriples($et, $fullexport=true) {
 585+ // return if younger than the filtered date
 586+ if ($et->exists && ($this->date !== '')) {
 587+ $rev = Revision::getTimeStampFromID($et->title->getLatestRevID());
 588+ if ($rev < $this->date) return;
 589+ }
 590+
555591 $datatype_rel = false;
556592 $category_rel = false;
557593 $equality_rel = false;
558594 $subrel_rel = false;
559595 $subatt_rel = false;
 596+
 597+ // if this was already exported, don't do it again
 598+ if (array_key_exists($et->hashkey, $this->element_done)) return;
560599
561600 // Set parameters for export
562601 switch ($et->title_namespace) {
563 - case SMW_NS_RELATION:
564 - $type = 'owl:ObjectProperty';
 602+ case SMW_NS_PROPERTY:
 603+ if ( $et->has_type ) {
 604+ if ( ('annouri' == $et->has_type->getTypeID()) || ('annostring' == $et->has_type->getTypeID()) ) {
 605+ $type = 'owl:AnnotationProperty';
 606+ //$subrel_rel = "smw:subPropertyOf";
 607+ //TODO can it be equivalent? cannot be a subproperty
 608+ } elseif ('_wpg' == $et->has_type->getTypeID()) {
 609+ $type = 'owl:ObjectProperty';
 610+ } else {
 611+ $type = 'owl:DatatypeProperty';
 612+ }
 613+ } else {
 614+ $type = 'owl:ObjectProperty';
 615+ }
565616 $equality_rel = "owl:equivalentProperty";
566617 global $smwgExportSemanticRelationHierarchy;
567618 if ($smwgExportSemanticRelationHierarchy) {
@@ -569,25 +620,6 @@
570621 $subrel_rel = "smw:subPropertyOf";
571622 }
572623 break;
573 - case SMW_NS_ATTRIBUTE:
574 - if ( $et->has_type === false ) return; //attributes w/o type not exportable, TODO: is this what we want?
575 - if ( ('annouri' == $et->has_type->getTypeID()) || ('annostring' == $et->has_type->getTypeID()) ) {
576 - $type = 'owl:AnnotationProperty'; // cannot be a subproperty, etc.
577 - } else {
578 - if ('' != SMWTypeHandlerFactory::getXSDTypeByID($et->has_type->getTypeID())) {
579 - $type = 'owl:DatatypeProperty';
580 - } else { // no xsd-type -> treat as object property
581 - $type = 'owl:ObjectProperty';
582 - }
583 - $equality_rel = "owl:equivalentProperty";
584 - }
585 - global $smwgExportSemanticRelationHierarchy;
586 - if ($smwgExportSemanticRelationHierarchy) {
587 - $subatt_rel = "rdfs:subPropertyOf";
588 - } else {
589 - $subatt_rel = "smw:subPropertyOf";
590 - }
591 - break;
592624 case NS_CATEGORY:
593625 $type = 'owl:Class';
594626 $category_rel = "rdfs:subClassOf";
@@ -612,6 +644,18 @@
613645 $et->title_prefurl . "\"/>\n" .
614646 "\t\t<rdfs:isDefinedBy rdf:resource=\"" .
615647 $this->special_url . '/' . $et->title_prefurl . "\"/>\n";
 648+ // If the property is modified by a unit, export the modifier
 649+ // and the base relation explicitly
 650+ if ( $et->has_type && $et->modifier ) {
 651+ $this->post_ns_buffer .= "\t\t<smw:hasModifier rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" .
 652+ $et->modifier .
 653+ "</smw:hasModifier>\n";
 654+ $baseprop = $this->getExportTitle($et->title_text, SMW_NS_PROPERTY);
 655+ $this->post_ns_buffer .= "\t\t<smw:baseProperty rdf:resource=\"" . $baseprop->long_uri . "\"/>\n";
 656+ if (!array_key_exists($baseprop->hashkey, $this->element_queue)) {
 657+ $this->element_queue[$baseprop->hashkey] = $baseprop;
 658+ }
 659+ }
616660
617661 if ( ($fullexport) && ($et->exists) ) {
618662 // add statements about categories
@@ -647,7 +691,7 @@
648692 foreach ($relations as $relation) {
649693 // TODO in future, check type safety relations <-> attributes
650694 // TODO check also the type of what I am pointing to (is it a relation or sth else?)
651 - $suprel = $this->getExportTitle($relation, SMW_NS_RELATION);
 695+ $suprel = $this->getExportTitle($relation, SMW_NS_PROPERTY);
652696 $this->post_ns_buffer .= "\t\t<$subrel_rel rdf:resource=\"" . $suprel->long_uri . "\"/>\n";
653697 if (!array_key_exists($suprel->hashkey, $this->element_queue)) {
654698 $this->element_queue[$suprel->hashkey] = $suprel;
@@ -662,7 +706,7 @@
663707 // TODO check the type of the attribute pointed to, does it match?
664708 // Could lead to inconsistencies in the output -- and in the wiki? But this will
665709 // need to be dealt with as soon as people add subattribute semantics to the wiki
666 - $supatt = $this->getExportTitle($attribute, SMW_NS_ATTRIBUTE);
 710+ $supatt = $this->getExportTitle($attribute, SMW_NS_PROPERTY);
667711 $this->post_ns_buffer .= "\t\t<$subatt_rel rdf:resource=\"" . $supatt->long_uri . "\"/>\n";
668712 if (!array_key_exists($supatt->hashkey, $this->element_queue)) {
669713 $this->element_queue[$supatt->hashkey] = $supatt;
@@ -678,30 +722,10 @@
679723 foreach ( $values as $value ) {
680724 $pt = $this->getExportTitleFromTitle( $prop, $value->getUnit() );
681725 $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 -// }
 726+ // TODO check OWL Fullness in Wikipage
686727 }
687728 }
688 - // print all attributes
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 - }
703 - }
704 - }
705 -*/ }
 729+ }
706730 }
707731
708732 $this->post_ns_buffer .= "\t</" . $type . ">\n";
@@ -762,7 +786,7 @@
763787 */
764788 public function getURI(Title $title, $unit = '') {
765789 $et = $this->getExportTitleFromTitle( $title, $unit );
766 - return $et->short_uri;
 790+ return $et->long_uri;
767791 }
768792
769793 /** Fetch SMWExportTitle for a given article. The inputs are
@@ -842,8 +866,7 @@
843867 static function fitsNsRestriction($res, $ns) {
844868 if ($res === false) return true;
845869 if ($res >= 0) return ( $res == $ns );
846 - return ( ($res != NS_CATEGORY) && ($res != SMW_NS_RELATION)
847 - && ($res != SMW_NS_ATTRIBUTE) && ($res != SMW_NS_TYPE) );
 870+ return ( ($res != NS_CATEGORY) && ($res != SMW_NS_PROPERTY) && ($res != SMW_NS_TYPE) );
848871 }
849872
850873 /** This function transforms a valid url-encoded URI into a string
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -542,6 +542,18 @@
543543 } else { return false; }
544544 }
545545
 546+ /**
 547+ * Creates the export line for the RDF export
 548+ *
 549+ * @param string $QName The element name of this datavalue
 550+ * @param ExportRDF $exporter the exporter calling this function
 551+ * @return the line to be exported
 552+ */
 553+ public function exportToRDF($QName, ExportRDF $exporter) {
 554+ $type = $this->type_handler->getXSDType();
 555+ $value = $this->getXSDValue();
 556+ return "\t\t<$QName rdf:datatype=\"$type\">$value</$QName>\n";
 557+ }
546558 }
547559
548560
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_WikiPage.php
@@ -128,9 +128,16 @@
129129 return false;
130130 }
131131
 132+ /**
 133+ * Creates the export line for the RDF export
 134+ *
 135+ * @param string $QName The element name of this datavalue
 136+ * @param ExportRDF $exporter the exporter calling this function
 137+ * @return the line to be exported
 138+ */
132139 public function exportToRDF($QName, ExportRDF $exporter) {
133140 $obj = $exporter->getURI( $this->getTitle() );
134 - return "\t\t<$QName rdf:resource=\"$obj\" />\n";
 141+ return "\t\t<$QName rdf:resource=\"$obj\"/>\n";
135142 }
136143
137144 ///// special interface for wiki page values
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -92,6 +92,13 @@
9393 return $this->m_infolinks;
9494 }
9595
 96+ /**
 97+ * Creates the export line for the RDF export
 98+ *
 99+ * @param string $QName The element name of this datavalue
 100+ * @param ExportRDF $exporter the exporter calling this function
 101+ * @return the line to be exported
 102+ */
96103 public function exportToRDF($QName, ExportRDF $exporter) {
97104 return "\t\t<$QName rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">$this->m_xsdvalue</$QName>\n";
98105 }

Status & tagging log