http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88652
Revision: 88652
Author: mkroetzsch
Date: 2011-05-23 17:07:59 +0000 (Mon, 23 May 2011)
Log Message:
-----------
added support for enforcing hard resource limits in query answering when using
4Store, together with suitable warnings to inform the user of the potential
incompleteness of query results
Modified Paths:
--------------
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php
trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php
trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
Modified:
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
2011-05-23 17:05:39 UTC (rev 88651)
+++ trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -395,7 +395,7 @@
return $xmlParser->makeResultFromXml( $xmlResult );
} else {
$this->throwSparqlErrors( $this->m_updateEndpoint,
$sparql );
- return new SMWSparqlResultWrapper( array(), array(),
SMWSparqlResultWrapper::ERROR_UNREACHABLE );
+ return new SMWSparqlResultWrapper( array(), array(),
array(), SMWSparqlResultWrapper::ERROR_UNREACHABLE );
}
}
Modified:
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
===================================================================
---
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
2011-05-23 17:05:39 UTC (rev 88651)
+++
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -19,6 +19,47 @@
class SMWSparqlDatabase4Store extends SMWSparqlDatabase {
/**
+ * Execute a SPARQL query and return an SMWSparqlResultWrapper object
+ * that contains the results. Compared to SMWSparqlDatabase::doQuery(),
+ * this also supports the parameter "restricted=1" which 4Store provides
+ * to enforce strict resource bounds on query answering. The method also
+ * checks if these bounds have been met, and records this in the query
+ * result.
+ *
+ * @note The restricted option in 4Store mainly enforces the given soft
+ * limit more strictly. To disable/configure it, simply change the soft
+ * limit settings of your 4Store server.
+ *
+ * @param $sparql string with the complete SPARQL query (SELECT or ASK)
+ * @return SMWSparqlResultWrapper
+ */
+ public function doQuery( $sparql ) {
+ //$result = parent::doQuery( $sparql );
+ curl_setopt( $this->m_curlhandle, CURLOPT_URL,
$this->m_queryEndpoint );
+ curl_setopt( $this->m_curlhandle, CURLOPT_POST, true );
+ $parameterString = "query=" . urlencode( $sparql ) .
"&restricted=1";
+ curl_setopt( $this->m_curlhandle, CURLOPT_POSTFIELDS,
$parameterString );
+
+ $xmlResult = curl_exec( $this->m_curlhandle );
+
+ if ( curl_errno( $this->m_curlhandle ) == 0 ) {
+ $xmlParser = new SMWSparqlResultParser();
+ $result = $xmlParser->makeResultFromXml( $xmlResult );
+ } else {
+ $this->throwSparqlErrors( $this->m_updateEndpoint,
$sparql );
+ $result = new SMWSparqlResultWrapper( array(), array(),
array(), SMWSparqlResultWrapper::ERROR_UNREACHABLE );
+ }
+
+ foreach ( $result->getComments() as $comment ) {
+ if ( strpos( $comment, 'warning: hit complexity limit'
) === 0 ||
+ strpos( $comment, 'some results have been dropped'
) === 0 ) {
+ $result->setErrorCode(
SMWSparqlResultWrapper::ERROR_INCOMPLETE );
+ } //else debug_zval_dump($comment);
+ }
+ return $result;
+ }
+
+ /**
* Execute a HTTP-based SPARQL POST request according to
* http://www.w3.org/2009/sparql/docs/http-rdf-update/.
* The method throws exceptions based on
Modified:
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php
===================================================================
---
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php
2011-05-23 17:05:39 UTC (rev 88651)
+++
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -32,6 +32,13 @@
protected $m_data;
/**
+ * List of comment strings found in the XML file (without surrounding
+ * markup, i.e. the actual string only).
+ * @var array of string
+ */
+ protected $m_comments;
+
+ /**
* Stack of open XML tags during parsing.
* @var array of string
*/
@@ -61,19 +68,28 @@
xml_set_object( $parser, $this );
xml_set_element_handler( $parser, 'xmlHandleOpen',
'xmlHandleClose' );
xml_set_character_data_handler($parser, 'xmlHandleCData' );
+ xml_set_default_handler( $parser, 'xmlHandleDefault' );
//xml_set_start_namespace_decl_handler($parser,
'xmlHandleNsDeclaration' );
$this->m_xml_opentags = array();
$this->m_header = array();
$this->m_data = array();
+ $this->m_comments = array();
xml_parse( $parser, $xmlQueryResult, true );
xml_parser_free( $parser );
- return new SMWSparqlResultWrapper( $this->m_header,
$this->m_data );
+ return new SMWSparqlResultWrapper( $this->m_header,
$this->m_data, $this->m_comments );
}
+ protected function xmlHandleDefault( $parser, $data ) {
+ if ( substr( $data, 0, 4 ) == '<!--' ) {
+ $comment = substr( $data, 4, strlen( $data ) - 7 );
+ $this->m_comments[] = trim( $comment );
+ }
+ }
+
/**
* Handle an opening XML tag during parsing.
* @see xml_set_element_handler
Modified:
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php
===================================================================
---
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php
2011-05-23 17:05:39 UTC (rev 88651)
+++
trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -24,6 +24,8 @@
const ERROR_NOERROR = 0;
/// Error code: service unreachable; result will be empty
const ERROR_UNREACHABLE = 1;
+ /// Error code: results might be incomplete (e.g. due to some resource
limit being reached)
+ const ERROR_INCOMPLETE = 2;
/**
* Associative array mapping SPARQL variable names to column indices.
@@ -40,6 +42,13 @@
protected $m_data;
/**
+ * List of comment strings found in the XML file (without surrounding
+ * markup, i.e. the actual string only).
+ * @var array of string
+ */
+ protected $m_comments;
+
+ /**
* Error code.
* @var integer
*/
@@ -50,10 +59,13 @@
*
* @param $header array mapping SPARQL variable names to column indices
* @param $data array of array of (SMWExpElement or null)
+ * @param $comments array of string comments if the result contained any
+ * @param $errorCode integer an error code
*/
- public function __construct( array $header, array $data, $errorCode =
self::ERROR_NOERROR ) {
+ public function __construct( array $header, array $data, array
$comments = array(), $errorCode = self::ERROR_NOERROR ) {
$this->m_header = $header;
$this->m_data = $data;
+ $this->m_comments = $comments;
$this->m_errorCode = $errorCode;
reset( $this->m_data );
}
@@ -71,13 +83,38 @@
* Return error code. SMWSparqlResultWrapper::ERROR_NOERROR (0)
* indicates that no error occurred.
*
- * @return interger error code
+ * @return integer error code
*/
public function getErrorCode() {
return $this->m_errorCode;
}
/**
+ * Set the error code of this result set. This is used for allowing
+ * callers to add additional errors discovered only later on. It does
+ * not allow removing existing errors, since it will not accept
+ * SMWSparqlResultWrapper::ERROR_NOERROR as a parameter.
+ *
+ * @param $errorCode integer error code
+ */
+ public function setErrorCode( $errorCode ) {
+ if ( $errorCode != self::ERROR_NOERROR ) {
+ $this->m_errorCode = $errorCode;
+ }
+ }
+
+ /**
+ * Return a list of comment strings found in the SPARQL result. Comments
+ * are used by some RDF stores to provide additional information or
+ * warnings that can thus be accessed.
+ *
+ * @return array of string
+ */
+ public function getComments() {
+ return $this->m_comments;
+ }
+
+ /**
* Check if the result is what one would get for a SPARQL ASK query
* that returned true. Returns false in all other cases (including
* the case that the results do not look at all like the result of
Modified:
trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php
===================================================================
---
trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php
2011-05-23 17:05:39 UTC (rev 88651)
+++
trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -441,9 +441,16 @@
$result = new SMWQueryResult(
$query->getDescription()->getPrintrequests(), $query, $resultDataItems,
$this->m_store, $hasFurtherResults );
- if ( $sparqlResultWrapper->getErrorCode() !=
SMWSparqlResultWrapper::ERROR_NOERROR ) {
- smwfLoadExtensionMessages( 'SemanticMediaWiki' );
- $result->addErrors( array( wfMsgForContent(
'smw_db_sparqlqueryproblem' ) ) );
+ switch ( $sparqlResultWrapper->getErrorCode() ) {
+ case SMWSparqlResultWrapper::ERROR_NOERROR: break;
+ case SMWSparqlResultWrapper::ERROR_INCOMPLETE:
+ smwfLoadExtensionMessages( 'SemanticMediaWiki'
);
+ $result->addErrors( array( wfMsgForContent(
'smw_db_sparqlqueryincomplete' ) ) );
+ break;
+ default:
+ smwfLoadExtensionMessages( 'SemanticMediaWiki'
);
+ $result->addErrors( array( wfMsgForContent(
'smw_db_sparqlqueryproblem' ) ) );
+ break;
}
return $result;
Modified: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
2011-05-23 17:05:39 UTC (rev 88651)
+++ trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
2011-05-23 17:07:59 UTC (rev 88652)
@@ -143,6 +143,7 @@
// Messages from the database backend
'smw_db_sparqlqueryproblem' => 'The query result could not be
obtained from the SPARQL database. This error might be temporary or indicate a
bug in the database software.',
+ 'smw_db_sparqlqueryincomplete'=> 'Answering the query turned out to be
too difficult and was aborted. Some results could be missing. If possible, try
using a simpler query instead.',
// Messages for pages of types and properties
'smw_type_header' => 'Properties of type "$1"',
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs