r86271 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86270‎ | r86271 | r86272 >
Date:18:55, 17 April 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
extend Turtle serializer with mode for SPARQL query serialization (hold back namespace declarations for separate retrieval)
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer_Turtle.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer_Turtle.php
@@ -22,12 +22,66 @@
2323 * serializing some SMWExpData. The elements of the array are serialized
2424 * later during the same serialization step (so this is not like another
2525 * queue for declarations or the like; it just unfolds an SMWExpData
26 - * object).
 26+ * object).
 27+ *
 28+ * @var array of SMWExpData
2729 */
2830 protected $subexpdata;
2931
 32+ /**
 33+ * If true, do not serialize namespace declarations and record them in
 34+ * $sparql_namespaces instead for later retrieval.
 35+ * @var boolean
 36+ */
 37+ protected $sparqlmode;
 38+
 39+ /**
 40+ * Array of retrieved namespaces (abbreviation => URI) for later use.
 41+ * @var array of string
 42+ */
 43+ protected $sparql_namespaces;
 44+
 45+ public function __construct( $sparqlMode = false ) {
 46+ parent::__construct();
 47+ $this->sparqlmode = $sparqlMode;
 48+ }
 49+
 50+ public function clear() {
 51+ parent::clear();
 52+ $this->sparql_namespaces = array();
 53+ }
 54+
 55+ /**
 56+ * Namespaces are not serialized among triples in SPARQL mode but are
 57+ * collected separately. This method serializes them as SPARQL prefix
 58+ * declarations and empties the collected list afterwards.
 59+ *
 60+ * @return string
 61+ */
 62+ public function flushSparqlPrefixes() {
 63+ $result = '';
 64+ foreach ( $this->sparql_namespaces as $shortname => $uri ) {
 65+ $result .= "PREFIX $shortname: <$uri>\n";
 66+ }
 67+ $this->sparql_namespaces = array();
 68+ return $result;
 69+ }
 70+
3071 protected function serializeHeader() {
31 - $this->pre_ns_buffer =
 72+ if ( $this->sparqlmode ) {
 73+ $this->pre_ns_buffer = '';
 74+ $this->sparql_namespaces = array(
 75+ "rdf" => SMWExporter::expandURI( '&rdf;' ),
 76+ "rdfs" => SMWExporter::expandURI( '&rdfs;' ),
 77+ "owl" => SMWExporter::expandURI( '&owl;' ),
 78+ "swivt" => SMWExporter::expandURI( '&swivt;' ),
 79+ "wiki" => SMWExporter::expandURI( '&wiki;' ),
 80+ "property" => SMWExporter::expandURI( '&property;' ),
 81+ "xsd" => "http://www.w3.org/2001/XMLSchema#" ,
 82+ "wikiurl" => SMWExporter::expandURI( '&wikiurl;' )
 83+ );
 84+ } else {
 85+ $this->pre_ns_buffer =
3286 "@prefix rdf: <" . SMWExporter::expandURI( '&rdf;' ) . "> .\n" .
3387 "@prefix rdfs: <" . SMWExporter::expandURI( '&rdfs;' ) . "> .\n" .
3488 "@prefix owl: <" . SMWExporter::expandURI( '&owl;' ) . "> .\n" .
@@ -38,12 +92,15 @@
3993 "@prefix property: <" . SMWExporter::expandURI( '&property;' ) . "> .\n" .
4094 "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n" . // note that this XSD URI is hardcoded below (its unlikely to change, of course)
4195 "@prefix wikiurl: <" . SMWExporter::expandURI( '&wikiurl;' ) . "> .\n";
 96+ }
4297 $this->global_namespaces = array( 'rdf' => true, 'rdfs' => true, 'owl' => true, 'swivt' => true, 'wiki' => true, 'property' => true );
4398 $this->post_ns_buffer = "\n";
4499 }
45100
46101 protected function serializeFooter() {
47 - $this->post_ns_buffer .= "\n# Created by Semantic MediaWiki, http://semantic-mediawiki.org/\n";
 102+ if ( !$this->sparqlmode ) {
 103+ $this->post_ns_buffer .= "\n# Created by Semantic MediaWiki, http://semantic-mediawiki.org/\n";
 104+ }
48105 }
49106
50107 public function serializeDeclaration( $uri, $typename ) {
@@ -56,12 +113,15 @@
57114 $this->serializeNestedExpData( array_pop( $this->subexpdata ), '' );
58115 }
59116 $this->serializeNamespaces();
60 -
61117 }
62118
63119 protected function serializeNamespace( $shortname, $uri ) {
64120 $this->global_namespaces[$shortname] = true;
65 - $this->pre_ns_buffer .= "@prefix $shortname: <$uri> .\n";
 121+ if ( $this->sparqlmode ) {
 122+ $this->sparql_namespaces[$shortname] = $uri;
 123+ } else {
 124+ $this->pre_ns_buffer .= "@prefix $shortname: <$uri> .\n";
 125+ }
66126 }
67127
68128 /**
Index: trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer.php
@@ -40,10 +40,12 @@
4141 * that one can append additional namespace declarations to $pre_ns_buffer
4242 * so that they affect all current elements. The buffers are flushed during
4343 * output in order to achieve "streaming" RDF export for larger files.
 44+ * @var string
4445 */
4546 protected $pre_ns_buffer;
4647 /**
4748 * See documentation for $pre_ns_buffer.
 49+ * @var string
4850 */
4951 protected $post_ns_buffer;
5052 /**
@@ -51,6 +53,7 @@
5254 * resourcename => decl-flag, where decl-flag is a sum of flags
5355 * SMW_SERIALIZER_DECL_CLASS, SMW_SERIALIZER_DECL_OPROP,
5456 * SMW_SERIALIZER_DECL_APROP.
 57+ * @var array of integer
5558 */
5659 protected $decl_todo;
5760 /**
@@ -58,6 +61,7 @@
5962 * resourcename => decl-flag, where decl-flag is a sum of flags
6063 * SMW_SERIALIZER_DECL_CLASS, SMW_SERIALIZER_DECL_OPROP,
6164 * SMW_SERIALIZER_DECL_APROP.
 65+ * @var array of integer
6266 */
6367 protected $decl_done;
6468 /**
@@ -68,12 +72,14 @@
6973 * the client already. But we wait with printing the current block so that
7074 * extra namespaces from this array can still be printed (note that one
7175 * never know which extra namespaces you encounter during export).
 76+ * @var array of string
7277 */
7378 protected $extra_namespaces;
7479 /**
7580 * Array of namespaces that have been declared globally already. Contains
7681 * entries of format 'namespace abbreviation' => true, assuming that the
7782 * same abbreviation always refers to the same URI.
 83+ * @var array of string
7884 */
7985 protected $global_namespaces;
8086
@@ -150,8 +156,8 @@
151157 /**
152158 * Serialize a single declaration for the given $uri (expanded) and type
153159 * (given as a QName).
154 - * @param string $uri of the thing to declare
155 - * @param string $typename one of owl:Class, owl:ObjectProperty, and
 160+ * @param $uri string URI of the thing to declare
 161+ * @param $typename string one of owl:Class, owl:ObjectProperty, and
156162 * owl:datatypeProperty
157163 */
158164 abstract public function serializeDeclaration( $uri, $typename );
@@ -195,8 +201,8 @@
196202 * Namespaces that were serialized in such a way that they remain
197203 * available for all following output should be added to
198204 * $global_namespaces.
199 - * @param string $shortname the abbreviation/prefix to declare
200 - * @param string $uri the URI prefix that the namespace encodes
 205+ * @param $shortname string abbreviation/prefix to declare
 206+ * @param $uri string URI prefix that the namespace encodes
201207 */
202208 abstract protected function serializeNamespace( $shortname, $uri );
203209

Status & tagging log