Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase.php |
— | — | @@ -395,7 +395,7 @@ |
396 | 396 | return $xmlParser->makeResultFromXml( $xmlResult ); |
397 | 397 | } else { |
398 | 398 | $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 ); |
400 | 400 | } |
401 | 401 | } |
402 | 402 | |
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlDatabase4Store.php |
— | — | @@ -19,6 +19,47 @@ |
20 | 20 | class SMWSparqlDatabase4Store extends SMWSparqlDatabase { |
21 | 21 | |
22 | 22 | /** |
| 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 | + /** |
23 | 64 | * Execute a HTTP-based SPARQL POST request according to |
24 | 65 | * http://www.w3.org/2009/sparql/docs/http-rdf-update/. |
25 | 66 | * The method throws exceptions based on |
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultParser.php |
— | — | @@ -32,6 +32,13 @@ |
33 | 33 | protected $m_data; |
34 | 34 | |
35 | 35 | /** |
| 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 | + /** |
36 | 43 | * Stack of open XML tags during parsing. |
37 | 44 | * @var array of string |
38 | 45 | */ |
— | — | @@ -61,19 +68,28 @@ |
62 | 69 | xml_set_object( $parser, $this ); |
63 | 70 | xml_set_element_handler( $parser, 'xmlHandleOpen', 'xmlHandleClose' ); |
64 | 71 | xml_set_character_data_handler($parser, 'xmlHandleCData' ); |
| 72 | + xml_set_default_handler( $parser, 'xmlHandleDefault' ); |
65 | 73 | //xml_set_start_namespace_decl_handler($parser, 'xmlHandleNsDeclaration' ); |
66 | 74 | |
67 | 75 | $this->m_xml_opentags = array(); |
68 | 76 | $this->m_header = array(); |
69 | 77 | $this->m_data = array(); |
| 78 | + $this->m_comments = array(); |
70 | 79 | |
71 | 80 | xml_parse( $parser, $xmlQueryResult, true ); |
72 | 81 | |
73 | 82 | xml_parser_free( $parser ); |
74 | 83 | |
75 | | - return new SMWSparqlResultWrapper( $this->m_header, $this->m_data ); |
| 84 | + return new SMWSparqlResultWrapper( $this->m_header, $this->m_data, $this->m_comments ); |
76 | 85 | } |
77 | 86 | |
| 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 | + |
78 | 94 | /** |
79 | 95 | * Handle an opening XML tag during parsing. |
80 | 96 | * @see xml_set_element_handler |
Index: trunk/extensions/SemanticMediaWiki/includes/sparql/SMW_SparqlResultWrapper.php |
— | — | @@ -24,6 +24,8 @@ |
25 | 25 | const ERROR_NOERROR = 0; |
26 | 26 | /// Error code: service unreachable; result will be empty |
27 | 27 | 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; |
28 | 30 | |
29 | 31 | /** |
30 | 32 | * Associative array mapping SPARQL variable names to column indices. |
— | — | @@ -40,6 +42,13 @@ |
41 | 43 | protected $m_data; |
42 | 44 | |
43 | 45 | /** |
| 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 | + /** |
44 | 53 | * Error code. |
45 | 54 | * @var integer |
46 | 55 | */ |
— | — | @@ -50,10 +59,13 @@ |
51 | 60 | * |
52 | 61 | * @param $header array mapping SPARQL variable names to column indices |
53 | 62 | * @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 |
54 | 65 | */ |
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 ) { |
56 | 67 | $this->m_header = $header; |
57 | 68 | $this->m_data = $data; |
| 69 | + $this->m_comments = $comments; |
58 | 70 | $this->m_errorCode = $errorCode; |
59 | 71 | reset( $this->m_data ); |
60 | 72 | } |
— | — | @@ -71,13 +83,38 @@ |
72 | 84 | * Return error code. SMWSparqlResultWrapper::ERROR_NOERROR (0) |
73 | 85 | * indicates that no error occurred. |
74 | 86 | * |
75 | | - * @return interger error code |
| 87 | + * @return integer error code |
76 | 88 | */ |
77 | 89 | public function getErrorCode() { |
78 | 90 | return $this->m_errorCode; |
79 | 91 | } |
80 | 92 | |
81 | 93 | /** |
| 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 | + /** |
82 | 119 | * Check if the result is what one would get for a SPARQL ASK query |
83 | 120 | * that returned true. Returns false in all other cases (including |
84 | 121 | * 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 @@ |
442 | 442 | |
443 | 443 | $result = new SMWQueryResult( $query->getDescription()->getPrintrequests(), $query, $resultDataItems, $this->m_store, $hasFurtherResults ); |
444 | 444 | |
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; |
448 | 455 | } |
449 | 456 | |
450 | 457 | return $result; |
Index: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php |
— | — | @@ -143,6 +143,7 @@ |
144 | 144 | |
145 | 145 | // Messages from the database backend |
146 | 146 | '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.', |
147 | 148 | |
148 | 149 | // Messages for pages of types and properties |
149 | 150 | 'smw_type_header' => 'Properties of type "$1"', |