/** * 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" . ( ( $this->m_defaultGraph !== '' )? '&default-graph-uri=' . urlencode( $this->m_defaultGraph ) : '' ); 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; }
/** * Build an SMWQueryResult object from a SMWSparqlResultWrapper. This * function is used to generate instance query results, and the given * result wrapper must have an according format (one result column that * contains URIs of wiki pages). * * @param $sparqlResultWrapper SMWSparqlResultWrapper * @param $query SMWQuery, SMWQueryResults hold a reference to original query * @return SMWQueryResult */ protected function getQueryResultFromSparqlResult(SMWSparqlResultWrapper $sparqlResultWrapper, SMWQuery $query) { $resultDataItems = array(); foreach ($sparqlResultWrapper as $resultRow) { if (count($resultRow) > 0) { $dataItem = SMWExporter::findDataItemForExpElement($resultRow[0]); if (!is_null($dataItem)) { $resultDataItems[] = $dataItem; } } } if ($sparqlResultWrapper->numRows() > $query->getLimit()) { array_pop($resultDataItems); $hasFurtherResults = true; } else { $hasFurtherResults = false; } $result = new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, $resultDataItems, $this->m_store, $hasFurtherResults); switch ($sparqlResultWrapper->getErrorCode()) { case SMWSparqlResultWrapper::ERROR_NOERROR: break; case SMWSparqlResultWrapper::ERROR_INCOMPLETE: $result->addErrors(array(wfMessage('smw_db_sparqlqueryincomplete')->inContentLanguage()->text())); break; default: $result->addErrors(array(wfMessage('smw_db_sparqlqueryproblem')->inContentLanguage()->text())); break; } return $result; }