static function smwfProcessSPARQLQueryParserFunctionGTP( &$parser ) { global $smwgWebserviceEndpoint; if ( !isset( $smwgWebserviceEndpoint ) ) return ''; global $smwgIQRunningNumber; $smwgIQRunningNumber++; $params = func_get_args(); array_shift( $params ); // we already know the $parser ... SMWSPARQLQueryProcessor::processFunctionParams( $params, $querystring, $params, $printouts ); $query = SMWSPARQLQueryProcessor::createQuery( $querystring, $params, SMWQueryProcessor::INLINE_QUERY, '', $printouts ); if ( !( ( $query->querymode == SMWQuery::MODE_INSTANCES ) || ( $query->querymode == SMWQuery::MODE_NONE ) ) ) { return ''; } self::prequery( $params, $printouts, $label, $wom_id ); // source from SMWHalo, SMW_SPARQLQueryProcessor.php // Query routing allows extensions to provide alternative stores as data sources // The while feature is experimental and is not properly integrated with most of SMW's architecture. For instance, some query printers just fetch their own store. // / TODO: case-insensitive global $smwgQuerySources; $query->params = $params; // this is a hack if ( array_key_exists( "source", $params ) && array_key_exists( $params["source"], $smwgQuerySources ) ) { $store = new $smwgQuerySources[$params["source"]](); } else { $store = smwfGetStore(); // default store } $res = $store->getQueryResult( $query ); if ( !is_array( $res ) ) { $qResults['tsc'] = $res; } else { $qResults = $res; } foreach ( $qResults as $source => $res ) { while ( $row = $res->getNext() ) { $firstcol = true; foreach ( $row as $field ) { $object = $field->getNextObject(); $text = $object->getWikiValue(); self::$queryProps[$wom_id][$label][] = $text; // get the first column only break; } } } return ''; }
function execute($p) { global $wgOut, $wgRequest, $smwgQEnabled, $smwgRSSEnabled, $smwgMW_1_14; wfProfileIn('doSpecialAskTSC (SMWHalo)'); $this->extractQueryParameters($p); $format = $this->getResultFormat($this->m_params); $query = SMWSPARQLQueryProcessor::createQuery($this->m_querystring, $this->m_params, SMWQueryProcessor::SPECIAL_PAGE, $format, $this->m_printouts); $res = smwfGetStore()->getQueryResult($query); $printer = SMWQueryProcessor::getResultPrinter($format, SMWQueryProcessor::SPECIAL_PAGE, $res); $result_mime = $printer->getMimeType($res); if ($result_mime == false) { if ($res->getCount() > 0) { $result = '<div style="text-align: center;">'; $result .= '</div>' . $printer->getResult($res, $this->m_params, SMW_OUTPUT_HTML); $result .= '<div style="text-align: center;"></div>'; } else { $result = '<div style="text-align: center;">' . wfMsg('smw_result_noresults') . '</div>'; } } else { // make a stand-alone file $result = $printer->getResult($res, $this->m_params, SMW_OUTPUT_FILE); $result_name = $printer->getFileName($res); // only fetch that after initialising the parameters } if ($result_mime == false) { if ($this->m_querystring) { $wgOut->setHTMLtitle($this->m_querystring); } else { $wgOut->setHTMLtitle(wfMsg('ask')); } $wgOut->addHTML($result); } else { $wgOut->disable(); header("Content-type: {$result_mime}; charset=UTF-8"); if ($result_name !== false) { header("Content-Disposition: attachment; filename={$result_name}"); } print $result; } wfProfileOut('doSpecialAskTSC (SMWHalo)'); }
/** * Get derived properties. * @param SMWSemanticData $semData * Annotated facts of an article * @return SMWSemanticData * Derived facts of the article */ public static function getDerivedProperties(SMWSemanticData $semData) { global $smwgIP, $smwgHaloIP, $smwgTripleStoreGraph; require_once $smwgIP . '/includes/SMW_QueryProcessor.php'; require_once $smwgHaloIP . '/includes/storage/SMW_TripleStore.php'; $derivedProperties = new SMWSemanticData($semData->getSubject()); $subject = $semData->getSubject()->getDBkey(); global $wgContLang; $subject = $semData->getSubject(); $ns = strtolower($wgContLang->getNSText($subject->getNamespace())); if (empty($ns)) { $ns = 'a'; } $localName = $subject->getDBkey(); $inst = $smwgTripleStoreGraph . TSNamespaces::$INST_NS_SUFFIX; // $queryText = "PREFIX a:<$inst> SELECT ?pred ?obj WHERE { a:$subject ?pred ?obj . }"; // $queryText = "SELECT ?pred ?obj WHERE { a:$subject ?pred ?obj . }"; $queryText = "SELECT ?pred ?obj WHERE { <" . $smwgTripleStoreGraph . "/{$ns}#{$localName}> ?pred ?obj . }"; // echo $queryText; wfRunHooks('BeforeDerivedPropertyQuery', array(&$queryText)); // Ask for all properties of the subject (derived and ground facts) $q = SMWSPARQLQueryProcessor::createQuery($queryText, array()); $res = smwfGetStore()->getQueryResult($q); // SMWQueryResult wfRunHooks('AfterDerivedPropertyQuery', array()); wfRunHooks('FilterQueryResults', array(&$res, array('pred'))); $propVal = array(); while ($row = $res->getNext()) { //$row: SMWResultArray[] $i = 0; $valuesForProperty = array(); $key = false; if (count($row) == 2) { $properties = array(); $values = array(); // There may be several properties with the same values $p = $row[0]; while (($object = $p->getNextObject()) !== false) { if ($object instanceof SMWURIValue) { $keys = $object->getDBkeys(); $properties[] = $keys[0]; } else { $properties[] = $object->getDBkey(); } } // Retrieve the values of the properties $v = $row[1]; while (($object = $v->getNextObject()) !== false) { $values[] = $object; } } foreach ($properties as $p) { if (array_key_exists($p, $propVal)) { // The same property may appear several times $propVal[$p] = array_merge($values, $propVal[$p]); } else { $propVal[$p] = $values; } } } // Check is a property is derived or directly annotated foreach ($propVal as $propName => $derivedValues) { // does the property already exist? $prop = SMWPropertyValue::makeUserProperty($propName); $values = $semData->getPropertyValues($prop); foreach ($derivedValues as $dv) { $isDerived = true; $val = null; foreach ($values as $v) { if ($dv->getTypeID() == '_wpg' && $v->getTypeID() == '_wpg') { $vt1 = $dv->getTitle(); $vt2 = $v->getTitle(); if (isset($vt1) && isset($vt2) && $vt1->getText() == $vt2->getText()) { $isDerived = false; break; } } else { if ($dv->getTypeID() == '_wpg' && $v->getTypeID() != '_wpg') { // how can this happen? $isDerived = false; break; } else { if ($dv->isNumeric()) { if ($dv->getWikiValue() == $v->getWikiValue()) { $isDerived = false; break; } } else { if (array_shift($dv->getDBkeys()) == array_shift($v->getDBkeys())) { $isDerived = false; break; } } } } } if ($isDerived) { $property = SMWPropertyValue::makeUserProperty($propName); $derivedProperties->addPropertyObjectValue($property, $dv); } } } return $derivedProperties; }
public function updateQueryResult($queryId) { $queryString = SMWQRCQueryManagementHandler::getInstance()->getSearchMetadataQueryString($queryId); // SMWQueryProcessor::processFunctionParams(array($queryString), $queryString, $params, $printouts); $query = SMWQueryProcessor::createQuery($queryString, $params); $queryResults = $this->getQueryResult($query, true, false)->getResults(); //echo('<pre>'.print_r($queryResults, true).'</pre>'); if (count($queryResults) > 0) { //this query is still in use global $smwgDefaultStore; $defaultStore = new $smwgDefaultStore(); $title = $queryResults[0]->getTitle(); $semanticData = $defaultStore->getsemanticData($title); $metadata = SMWQRCQueryManagementHandler::getInstance()->getQueryCallMetadata($semanticData, $queryId); $queryParams = array($metadata['queryString']); //if($metadata['limit']) $queryParams[] = 'limit='.$metadata['limit']; if ($metadata['offset']) { $queryParams[] = 'offset=' . $metadata['offset']; } if (array_key_exists('extraPropertyPrintouts', $metadata)) { foreach (explode(';', $metadata['extraPropertyPrintouts']) as $pP) { $queryParams[] = '?' . $pP; } } if (array_key_exists('extraCategoryPrintouts', $metadata)) { $queryParams[] = '?Category'; } //echo('<pre>'.print_r($queryParams, true).'</pre>'); if (array_key_exists('isSPARQLQuery', $metadata)) { SMWSPARQLQueryProcessor::processFunctionParams($queryParams, $querystring, $params, $printouts); $query = SMWSPARQLQueryProcessor::createQuery($querystring, $params, SMWQueryProcessor::INLINE_QUERY, 'table', $printouts); } else { SMWQueryProcessor::processFunctionParams($queryParams, $querystring, $params, $printouts); $query = SMWQueryProcessor::createQuery($querystring, $params); } //echo('<pre>'.print_r($query, true).'</pre>'); $this->getQueryResult($query, true); //invalidate parser caches global $invalidateParserCache; if ($invalidateParserCache) { foreach ($queryResults as $qR) { $title = $qR->getTitle(); $title->invalidateCache(); // wfGetParserCacheStorage()->delete( // ParserCache::singleton()->getKey(Article::newFromID($title->getArticleID()), new ParserOptions())); } } } else { $qrcStore = SMWQRCStore::getInstance()->getDB(); $qrcStore->deleteQueryData($queryId); } return true; }
public static function getResultFromQueryString($querystring, $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY) { $format = SMWQueryProcessor::getResultFormat($params); $query = SMWSPARQLQueryProcessor::createQuery($querystring, $params, $context, $format, $extraprintouts); $result = SMWQueryProcessor::getResultFromQuery($query, $params, $extraprintouts, $outputmode, $context, $format); return $result; }