Index: trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer_Turtle.php |
— | — | @@ -22,12 +22,66 @@ |
23 | 23 | * serializing some SMWExpData. The elements of the array are serialized |
24 | 24 | * later during the same serialization step (so this is not like another |
25 | 25 | * queue for declarations or the like; it just unfolds an SMWExpData |
26 | | - * object). |
| 26 | + * object). |
| 27 | + * |
| 28 | + * @var array of SMWExpData |
27 | 29 | */ |
28 | 30 | protected $subexpdata; |
29 | 31 | |
| 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 | + |
30 | 71 | 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 = |
32 | 86 | "@prefix rdf: <" . SMWExporter::expandURI( '&rdf;' ) . "> .\n" . |
33 | 87 | "@prefix rdfs: <" . SMWExporter::expandURI( '&rdfs;' ) . "> .\n" . |
34 | 88 | "@prefix owl: <" . SMWExporter::expandURI( '&owl;' ) . "> .\n" . |
— | — | @@ -38,12 +92,15 @@ |
39 | 93 | "@prefix property: <" . SMWExporter::expandURI( '&property;' ) . "> .\n" . |
40 | 94 | "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n" . // note that this XSD URI is hardcoded below (its unlikely to change, of course) |
41 | 95 | "@prefix wikiurl: <" . SMWExporter::expandURI( '&wikiurl;' ) . "> .\n"; |
| 96 | + } |
42 | 97 | $this->global_namespaces = array( 'rdf' => true, 'rdfs' => true, 'owl' => true, 'swivt' => true, 'wiki' => true, 'property' => true ); |
43 | 98 | $this->post_ns_buffer = "\n"; |
44 | 99 | } |
45 | 100 | |
46 | 101 | 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 | + } |
48 | 105 | } |
49 | 106 | |
50 | 107 | public function serializeDeclaration( $uri, $typename ) { |
— | — | @@ -56,12 +113,15 @@ |
57 | 114 | $this->serializeNestedExpData( array_pop( $this->subexpdata ), '' ); |
58 | 115 | } |
59 | 116 | $this->serializeNamespaces(); |
60 | | - |
61 | 117 | } |
62 | 118 | |
63 | 119 | protected function serializeNamespace( $shortname, $uri ) { |
64 | 120 | $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 | + } |
66 | 126 | } |
67 | 127 | |
68 | 128 | /** |
Index: trunk/extensions/SemanticMediaWiki/includes/export/SMW_Serializer.php |
— | — | @@ -40,10 +40,12 @@ |
41 | 41 | * that one can append additional namespace declarations to $pre_ns_buffer |
42 | 42 | * so that they affect all current elements. The buffers are flushed during |
43 | 43 | * output in order to achieve "streaming" RDF export for larger files. |
| 44 | + * @var string |
44 | 45 | */ |
45 | 46 | protected $pre_ns_buffer; |
46 | 47 | /** |
47 | 48 | * See documentation for $pre_ns_buffer. |
| 49 | + * @var string |
48 | 50 | */ |
49 | 51 | protected $post_ns_buffer; |
50 | 52 | /** |
— | — | @@ -51,6 +53,7 @@ |
52 | 54 | * resourcename => decl-flag, where decl-flag is a sum of flags |
53 | 55 | * SMW_SERIALIZER_DECL_CLASS, SMW_SERIALIZER_DECL_OPROP, |
54 | 56 | * SMW_SERIALIZER_DECL_APROP. |
| 57 | + * @var array of integer |
55 | 58 | */ |
56 | 59 | protected $decl_todo; |
57 | 60 | /** |
— | — | @@ -58,6 +61,7 @@ |
59 | 62 | * resourcename => decl-flag, where decl-flag is a sum of flags |
60 | 63 | * SMW_SERIALIZER_DECL_CLASS, SMW_SERIALIZER_DECL_OPROP, |
61 | 64 | * SMW_SERIALIZER_DECL_APROP. |
| 65 | + * @var array of integer |
62 | 66 | */ |
63 | 67 | protected $decl_done; |
64 | 68 | /** |
— | — | @@ -68,12 +72,14 @@ |
69 | 73 | * the client already. But we wait with printing the current block so that |
70 | 74 | * extra namespaces from this array can still be printed (note that one |
71 | 75 | * never know which extra namespaces you encounter during export). |
| 76 | + * @var array of string |
72 | 77 | */ |
73 | 78 | protected $extra_namespaces; |
74 | 79 | /** |
75 | 80 | * Array of namespaces that have been declared globally already. Contains |
76 | 81 | * entries of format 'namespace abbreviation' => true, assuming that the |
77 | 82 | * same abbreviation always refers to the same URI. |
| 83 | + * @var array of string |
78 | 84 | */ |
79 | 85 | protected $global_namespaces; |
80 | 86 | |
— | — | @@ -150,8 +156,8 @@ |
151 | 157 | /** |
152 | 158 | * Serialize a single declaration for the given $uri (expanded) and type |
153 | 159 | * (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 |
156 | 162 | * owl:datatypeProperty |
157 | 163 | */ |
158 | 164 | abstract public function serializeDeclaration( $uri, $typename ); |
— | — | @@ -195,8 +201,8 @@ |
196 | 202 | * Namespaces that were serialized in such a way that they remain |
197 | 203 | * available for all following output should be added to |
198 | 204 | * $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 |
201 | 207 | */ |
202 | 208 | abstract protected function serializeNamespace( $shortname, $uri ); |
203 | 209 | |