r88652 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88651‎ | r88652 | r88653 >
Date:17:07, 23 May 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php
@@ -395,7 +395,7 @@
396396 return $xmlParser->makeResultFromXml( $xmlResult );
397397 } else {
398398 $this->throwSparqlErrors( $this->m_updateEndpoint, $sparql );
399 - return new SMWSparqlResultWrapper( array(), array(), SMWSparqlResultWrapper::ERROR_UNREACHABLE );
 399+ return new SMWSparqlResultWrapper( array(), array(), array(), SMWSparqlResultWrapper::ERROR_UNREACHABLE );
400400 }
401401 }
402402
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php
@@ -19,6 +19,47 @@
2020 class SMWSparqlDatabase4Store extends SMWSparqlDatabase {
2121
2222 /**
 23+ * Execute a SPARQL query and return an SMWSparqlResultWrapper object
 24+ * that contains the results. Compared to SMWSparqlDatabase::doQuery(),
 25+ * this also supports the parameter "restricted=1" which 4Store provides
 26+ * to enforce strict resource bounds on query answering. The method also
 27+ * checks if these bounds have been met, and records this in the query
 28+ * result.
 29+ *
 30+ * @note The restricted option in 4Store mainly enforces the given soft
 31+ * limit more strictly. To disable/configure it, simply change the soft
 32+ * limit settings of your 4Store server.
 33+ *
 34+ * @param $sparql string with the complete SPARQL query (SELECT or ASK)
 35+ * @return SMWSparqlResultWrapper
 36+ */
 37+ public function doQuery( $sparql ) {
 38+ //$result = parent::doQuery( $sparql );
 39+ curl_setopt( $this->m_curlhandle, CURLOPT_URL, $this->m_queryEndpoint );
 40+ curl_setopt( $this->m_curlhandle, CURLOPT_POST, true );
 41+ $parameterString = "query=" . urlencode( $sparql ) . "&restricted=1";
 42+ curl_setopt( $this->m_curlhandle, CURLOPT_POSTFIELDS, $parameterString );
 43+
 44+ $xmlResult = curl_exec( $this->m_curlhandle );
 45+
 46+ if ( curl_errno( $this->m_curlhandle ) == 0 ) {
 47+ $xmlParser = new SMWSparqlResultParser();
 48+ $result = $xmlParser->makeResultFromXml( $xmlResult );
 49+ } else {
 50+ $this->throwSparqlErrors( $this->m_updateEndpoint, $sparql );
 51+ $result = new SMWSparqlResultWrapper( array(), array(), array(), SMWSparqlResultWrapper::ERROR_UNREACHABLE );
 52+ }
 53+
 54+ foreach ( $result->getComments() as $comment ) {
 55+ if ( strpos( $comment, 'warning: hit complexity limit' ) === 0 ||
 56+ strpos( $comment, 'some results have been dropped' ) === 0 ) {
 57+ $result->setErrorCode( SMWSparqlResultWrapper::ERROR_INCOMPLETE );
 58+ } //else debug_zval_dump($comment);
 59+ }
 60+ return $result;
 61+ }
 62+
 63+ /**
2364 * Execute a HTTP-based SPARQL POST request according to
2465 * http://www.w3.org/2009/sparql/docs/http-rdf-update/.
2566 * The method throws exceptions based on
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php
@@ -32,6 +32,13 @@
3333 protected $m_data;
3434
3535 /**
 36+ * List of comment strings found in the XML file (without surrounding
 37+ * markup, i.e. the actual string only).
 38+ * @var array of string
 39+ */
 40+ protected $m_comments;
 41+
 42+ /**
3643 * Stack of open XML tags during parsing.
3744 * @var array of string
3845 */
@@ -61,19 +68,28 @@
6269 xml_set_object( $parser, $this );
6370 xml_set_element_handler( $parser, 'xmlHandleOpen', 'xmlHandleClose' );
6471 xml_set_character_data_handler($parser, 'xmlHandleCData' );
 72+ xml_set_default_handler( $parser, 'xmlHandleDefault' );
6573 //xml_set_start_namespace_decl_handler($parser, 'xmlHandleNsDeclaration' );
6674
6775 $this->m_xml_opentags = array();
6876 $this->m_header = array();
6977 $this->m_data = array();
 78+ $this->m_comments = array();
7079
7180 xml_parse( $parser, $xmlQueryResult, true );
7281
7382 xml_parser_free( $parser );
7483
75 - return new SMWSparqlResultWrapper( $this->m_header, $this->m_data );
 84+ return new SMWSparqlResultWrapper( $this->m_header, $this->m_data, $this->m_comments );
7685 }
7786
 87+ protected function xmlHandleDefault( $parser, $data ) {
 88+ if ( substr( $data, 0, 4 ) == '<!--' ) {
 89+ $comment = substr( $data, 4, strlen( $data ) - 7 );
 90+ $this->m_comments[] = trim( $comment );
 91+ }
 92+ }
 93+
7894 /**
7995 * Handle an opening XML tag during parsing.
8096 * @see xml_set_element_handler
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php
@@ -24,6 +24,8 @@
2525 const ERROR_NOERROR = 0;
2626 /// Error code: service unreachable; result will be empty
2727 const ERROR_UNREACHABLE = 1;
 28+ /// Error code: results might be incomplete (e.g. due to some resource limit being reached)
 29+ const ERROR_INCOMPLETE = 2;
2830
2931 /**
3032 * Associative array mapping SPARQL variable names to column indices.
@@ -40,6 +42,13 @@
4143 protected $m_data;
4244
4345 /**
 46+ * List of comment strings found in the XML file (without surrounding
 47+ * markup, i.e. the actual string only).
 48+ * @var array of string
 49+ */
 50+ protected $m_comments;
 51+
 52+ /**
4453 * Error code.
4554 * @var integer
4655 */
@@ -50,10 +59,13 @@
5160 *
5261 * @param $header array mapping SPARQL variable names to column indices
5362 * @param $data array of array of (SMWExpElement or null)
 63+ * @param $comments array of string comments if the result contained any
 64+ * @param $errorCode integer an error code
5465 */
55 - public function __construct( array $header, array $data, $errorCode = self::ERROR_NOERROR ) {
 66+ public function __construct( array $header, array $data, array $comments = array(), $errorCode = self::ERROR_NOERROR ) {
5667 $this->m_header = $header;
5768 $this->m_data = $data;
 69+ $this->m_comments = $comments;
5870 $this->m_errorCode = $errorCode;
5971 reset( $this->m_data );
6072 }
@@ -71,13 +83,38 @@
7284 * Return error code. SMWSparqlResultWrapper::ERROR_NOERROR (0)
7385 * indicates that no error occurred.
7486 *
75 - * @return interger error code
 87+ * @return integer error code
7688 */
7789 public function getErrorCode() {
7890 return $this->m_errorCode;
7991 }
8092
8193 /**
 94+ * Set the error code of this result set. This is used for allowing
 95+ * callers to add additional errors discovered only later on. It does
 96+ * not allow removing existing errors, since it will not accept
 97+ * SMWSparqlResultWrapper::ERROR_NOERROR as a parameter.
 98+ *
 99+ * @param $errorCode integer error code
 100+ */
 101+ public function setErrorCode( $errorCode ) {
 102+ if ( $errorCode != self::ERROR_NOERROR ) {
 103+ $this->m_errorCode = $errorCode;
 104+ }
 105+ }
 106+
 107+ /**
 108+ * Return a list of comment strings found in the SPARQL result. Comments
 109+ * are used by some RDF stores to provide additional information or
 110+ * warnings that can thus be accessed.
 111+ *
 112+ * @return array of string
 113+ */
 114+ public function getComments() {
 115+ return $this->m_comments;
 116+ }
 117+
 118+ /**
82119 * Check if the result is what one would get for a SPARQL ASK query
83120 * that returned true. Returns false in all other cases (including
84121 * the case that the results do not look at all like the result of
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php
@@ -441,9 +441,16 @@
442442
443443 $result = new SMWQueryResult( $query->getDescription()->getPrintrequests(), $query, $resultDataItems, $this->m_store, $hasFurtherResults );
444444
445 - if ( $sparqlResultWrapper->getErrorCode() != SMWSparqlResultWrapper::ERROR_NOERROR ) {
446 - smwfLoadExtensionMessages( 'SemanticMediaWiki' );
447 - $result->addErrors( array( wfMsgForContent( 'smw_db_sparqlqueryproblem' ) ) );
 445+ switch ( $sparqlResultWrapper->getErrorCode() ) {
 446+ case SMWSparqlResultWrapper::ERROR_NOERROR: break;
 447+ case SMWSparqlResultWrapper::ERROR_INCOMPLETE:
 448+ smwfLoadExtensionMessages( 'SemanticMediaWiki' );
 449+ $result->addErrors( array( wfMsgForContent( 'smw_db_sparqlqueryincomplete' ) ) );
 450+ break;
 451+ default:
 452+ smwfLoadExtensionMessages( 'SemanticMediaWiki' );
 453+ $result->addErrors( array( wfMsgForContent( 'smw_db_sparqlqueryproblem' ) ) );
 454+ break;
448455 }
449456
450457 return $result;
Index: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
@@ -143,6 +143,7 @@
144144
145145 // Messages from the database backend
146146 '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.',
 147+ '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.',
147148
148149 // Messages for pages of types and properties
149150 'smw_type_header' => 'Properties of type "$1"',

Status & tagging log