r87195 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87194‎ | r87195 | r87196 >
Date:17:28, 1 May 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
move HTTP Post implementation to 4Store database connector, since it is nonstandard and specific to this backend
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
@@ -377,7 +377,9 @@
378378 * method does not throw anything, then an empty result with an error
379379 * code is returned.
380380 *
381 - * @note This method currently is specific to 4Store. It uses POST parameters that are not given in the specification.
 381+ * @note This method has not been tesetd sufficiently since 4Store uses
 382+ * another post encoding. To avoid using it, simply do not provide a
 383+ * data endpoint URL when configuring the SPARQL database.
382384 *
383385 * @param $payload string Turtle serialization of data to send
384386 * @return SMWSparqlResultWrapper
@@ -388,16 +390,14 @@
389391 }
390392 curl_setopt( $this->m_curlhandle, CURLOPT_URL, $this->m_dataEndpoint );
391393 curl_setopt( $this->m_curlhandle, CURLOPT_POST, true );
392 - $parameterString = "data=" . urlencode( $payload ) . '&graph=default&mime-type=application/x-turtle';
393 - curl_setopt( $this->m_curlhandle, CURLOPT_POSTFIELDS, $parameterString );
394394
395 -//// POST as file, fails in 4Store
396 -// $payloadFile = tmpfile();
397 -// fwrite( $payloadFile, $payload );
398 -// fseek( $payloadFile, 0 );
399 -// curl_setopt( $this->m_curlhandle, CURLOPT_INFILE, $payloadFile );
400 -// curl_setopt( $this->m_curlhandle, CURLOPT_INFILESIZE, strlen( $payload ) );
401 -// curl_setopt( $this->m_curlhandle, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-turtle' ) );
 395+ // POST as file (fails in 4Store)
 396+ $payloadFile = tmpfile();
 397+ fwrite( $payloadFile, $payload );
 398+ fseek( $payloadFile, 0 );
 399+ curl_setopt( $this->m_curlhandle, CURLOPT_INFILE, $payloadFile );
 400+ curl_setopt( $this->m_curlhandle, CURLOPT_INFILESIZE, strlen( $payload ) );
 401+ curl_setopt( $this->m_curlhandle, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-turtle' ) );
402402
403403 curl_exec( $this->m_curlhandle );
404404
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
@@ -18,27 +18,36 @@
1919 class SMWSparqlDatabase4Store extends SMWSparqlDatabase {
2020
2121 /**
22 - * ASK wrapper.
23 - * "ASK" is buggy in 4Store and must be avoided.
 22+ * Execute a HTTP-based SPARQL POST request according to
 23+ * http://www.w3.org/2009/sparql/docs/http-rdf-update/.
 24+ * The method throws exceptions based on
 25+ * SMWSparqlDatabase::throwSparqlErrors(). If errors occur and this
 26+ * method does not throw anything, then an empty result with an error
 27+ * code is returned.
 28+ *
 29+ * This method is specific to 4Store since it uses POST parameters that
 30+ * are not given in the specification.
2431 *
25 - * @note This does not avoid the 4Store bug: all SPARQL queries are
26 - * potentially answered incorrectly by 4Store v.1.1.2. Nothing we can
27 - * do.
28 - *
29 - * @see SMWSparqlDatabase::ask
30 - * @param $where string WHERE part of the query, without surrounding { }
31 - * @param $extraNamespaces array (associative) of namespaceId => namespaceUri
 32+ * @param $payload string Turtle serialization of data to send
3233 * @return SMWSparqlResultWrapper
3334 */
34 - public function ask( $where, $extraNamespaces = array() ) {
35 - $sparql = self::getPrefixString( $extraNamespaces ) . "SELECT * WHERE {\n" . $where . "\n} LIMIT 1";
36 - $result = $this->doQuery( $sparql );
37 - if ( $result->numRows() > 0 ) {
38 - $expLiteral = new SMWExpLiteral( 'true', 'http://www.w3.org/2001/XMLSchema#boolean' );
39 - } else {
40 - $expLiteral = new SMWExpLiteral( 'false', 'http://www.w3.org/2001/XMLSchema#boolean' );
 35+ public function doHttpPost( $payload ) {
 36+ if ( $this->m_dataEndpoint == '' ) {
 37+ throw new SMWSparqlDatabaseError( SMWSparqlDatabaseError::ERROR_NOSERVICE, "SPARQL POST with data: $payload", 'not specified', $error );
4138 }
42 - return new SMWSparqlResultWrapper( array( '' => 0 ), array( array( $expLiteral ) ) );
 39+ curl_setopt( $this->m_curlhandle, CURLOPT_URL, $this->m_dataEndpoint );
 40+ curl_setopt( $this->m_curlhandle, CURLOPT_POST, true );
 41+ $parameterString = "data=" . urlencode( $payload ) . '&graph=default&mime-type=application/x-turtle';
 42+ curl_setopt( $this->m_curlhandle, CURLOPT_POSTFIELDS, $parameterString );
 43+
 44+ curl_exec( $this->m_curlhandle );
 45+
 46+ if ( curl_errno( $this->m_curlhandle ) == 0 ) {
 47+ return true;
 48+ } else { ///TODO The error reporting based on SPARQL (Update) is not adequate for the HTTP POST protocol
 49+ $this->throwSparqlErrors( $this->m_dataEndpoint, $payload );
 50+ return false;
 51+ }
4352 }
4453
4554 }
\ No newline at end of file

Status & tagging log