public function __construct(LingoMessageLog &$messages = null) { parent::__construct($messages); // get the store $store = smwfGetStore(); // Create query $desc = new SMWSomeProperty(new SMWDIProperty('___glt'), new SMWThingDescription()); $desc->addPrintRequest(new SMWPrintRequest(SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty('___glt'))); $desc->addPrintRequest(new SMWPrintRequest(SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty('___gld'))); $desc->addPrintRequest(new SMWPrintRequest(SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty('___gll'))); $query = new SMWQuery($desc, false, false); $query->sort = true; $query->sortkeys['___glt'] = 'ASC'; // get the query result $this->mQueryResult = $store->getQueryResult($query); }
/** * Recursively create an SMWSparqlCondition from an SMWSomeProperty. * * @param $description SMWSomeProperty * @param $joinVariable string name, see buildSparqlCondition() * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() * @return SMWSparqlCondition */ protected function buildPropertyCondition(SMWSomeProperty $description, $joinVariable, $orderByProperty) { $diProperty = $description->getProperty(); //*** Find out if we should order by the values of this property ***// if (array_key_exists($diProperty->getKey(), $this->m_sortkeys)) { $innerOrderByProperty = $diProperty; } else { $innerOrderByProperty = null; } //*** Prepare inner condition ***// $innerJoinVariable = $this->getNextVariable(); $innerCondition = $this->buildSparqlCondition($description->getDescription(), $innerJoinVariable, $innerOrderByProperty); $namespaces = $innerCondition->namespaces; if ($innerCondition instanceof SMWSparqlFalseCondition) { return new SMWSparqlFalseCondition(); } elseif ($innerCondition instanceof SMWSparqlSingletonCondition) { $matchElement = $innerCondition->matchElement; $objectName = SMWTurtleSerializer::getTurtleNameForExpElement($matchElement); if ($matchElement instanceof SMWExpNsResource) { $namespaces[$matchElement->getNamespaceId()] = $matchElement->getNamespace(); } } else { $objectName = '?' . $innerJoinVariable; } //*** Exchange arguments when property is inverse ***// if ($diProperty->isInverse()) { // don't check if this really makes sense $subjectName = $objectName; $objectName = '?' . $joinVariable; $diNonInverseProperty = new SMWDIProperty($diProperty->getKey(), false); } else { $subjectName = '?' . $joinVariable; $diNonInverseProperty = $diProperty; } //*** Build the condition ***// $typeId = $diProperty->findPropertyTypeID(); $diType = SMWDataValueFactory::getDataItemId($typeId); // for types that use helper properties in encoding values, refer to this helper property: if (SMWExporter::hasHelperExpElement($diType)) { $propertyExpElement = SMWExporter::getResourceElementForProperty($diNonInverseProperty, true); } else { $propertyExpElement = SMWExporter::getResourceElementForProperty($diNonInverseProperty); } $propertyName = SMWTurtleSerializer::getTurtleNameForExpElement($propertyExpElement); if ($propertyExpElement instanceof SMWExpNsResource) { $namespaces[$propertyExpElement->getNamespaceId()] = $propertyExpElement->getNamespace(); } $condition = "{$subjectName} {$propertyName} {$objectName} .\n"; $innerConditionString = $innerCondition->getCondition() . $innerCondition->getWeakConditionString(); if ($innerConditionString !== '') { if ($innerCondition instanceof SMWSparqlFilterCondition) { $condition .= $innerConditionString; } else { $condition .= "{ {$innerConditionString}}\n"; } } $result = new SMWSparqlWhereCondition($condition, true, $namespaces); //*** Record inner ordering variable if found ***// $result->orderVariables = $innerCondition->orderVariables; if (!is_null($innerOrderByProperty) && $innerCondition->orderByVariable !== '') { $result->orderVariables[$diProperty->getKey()] = $innerCondition->orderByVariable; } $this->addOrderByDataForProperty($result, $joinVariable, $orderByProperty, SMWDataItem::TYPE_WIKIPAGE); return $result; }
/** * Modify the given query object to account for some property condition for * the given property. If it is not possible to generate a query for the * given data, the query type is changed to SMWSQLStore3Query::Q_NOQUERY. Callers need * to check for this and discard the query in this case. * * @note This method does not support sortkey (_SKEY) property queries, * since they do not have a normal property table. This should not be a * problem since comparators on sortkeys are supported indirectly when * using comparators on wikipages. There is no reason to create any * query with _SKEY ad users cannot do so either (no user label). * * @since 1.8 */ protected function compileSomePropertyDescription(SMWSQLStore3Query $query, SMWSomeProperty $description) { $property = $description->getProperty(); $tableid = SMWSQLStore3::findPropertyTableID($property); if ($tableid === '') { // Give up $query->type = SMWSQLStore3Query::Q_NOQUERY; return; } $proptables = SMWSQLStore3::getPropertyTables(); $proptable = $proptables[$tableid]; if (!$proptable->usesIdSubject()) { // no queries with such tables // (only redirects are affected in practice) $query->type = SMWSQLStore3Query::Q_NOQUERY; return; } $typeid = $property->findPropertyTypeID(); $diType = SMWDataValueFactory::getDataItemId($typeid); if ($property->isInverse() && $diType != SMWDataItem::TYPE_WIKIPAGE) { // can only invert properties that point to pages $query->type = SMWSQLStore3Query::Q_NOQUERY; return; } $diHandler = $this->m_store->getDataItemHandlerForDIType($diType); $indexField = $diHandler->getIndexField(); $sortkey = $property->getKey(); // TODO: strictly speaking, the DB key is not what we want here, since sortkey is based on a "wiki value" // *** Now construct the query ... ***// $query->jointable = $proptable->getName(); // *** Add conditions for selecting rows for this property ***// if (!$proptable->isFixedPropertyTable()) { $pid = $this->m_store->smwIds->getSMWPropertyID($property); // Construct property hierarchy: $pqid = SMWSQLStore3Query::$qnum; $pquery = new SMWSQLStore3Query(); $pquery->type = SMWSQLStore3Query::Q_PROP_HIERARCHY; $pquery->joinfield = array($pid); $query->components[$pqid] = "{$query->alias}.p_id"; $this->m_queries[$pqid] = $pquery; // Alternative code without property hierarchies: // $query->where = "{$query->alias}.p_id=" . $this->m_dbs->addQuotes( $pid ); } // else: no property column, no hierarchy queries // *** Add conditions on the value of the property ***// if ($diType == SMWDataItem::TYPE_WIKIPAGE) { $o_id = $indexField; if ($property->isInverse()) { $s_id = $o_id; $o_id = 's_id'; } else { $s_id = 's_id'; } $query->joinfield = "{$query->alias}.{$s_id}"; // process page description like main query $sub = $this->compileQueries($description->getDescription()); if ($sub >= 0) { $query->components[$sub] = "{$query->alias}.{$o_id}"; } if (array_key_exists($sortkey, $this->m_sortkeys)) { // TODO: This SMW IDs table is possibly duplicated in the query. // Example: [[has capital::!Berlin]] with sort=has capital // Can we prevent that? (PERFORMANCE) $query->from = ' INNER JOIN ' . $this->m_dbs->tableName(SMWSql3SmwIds::tableName) . " AS ids{$query->alias} ON ids{$query->alias}.smw_id={$query->alias}.{$o_id}"; $query->sortfields[$sortkey] = "ids{$query->alias}.smw_sortkey"; } } else { // non-page value description $query->joinfield = "{$query->alias}.s_id"; $this->compilePropertyValueDescription($query, $description->getDescription(), $proptable, $diHandler, 'AND'); if (array_key_exists($sortkey, $this->m_sortkeys)) { $query->sortfields[$sortkey] = "{$query->alias}.{$indexField}"; } } }
public function prune( &$maxsize, &$maxdepth, &$log ) { if ( ( $maxsize <= 0 ) || ( $maxdepth <= 0 ) ) { $log[] = $this->getQueryString(); return new SMWThingDescription(); } $maxsize--; $maxdepth--; $result = new SMWSomeProperty( $this->m_property, $this->m_description->prune( $maxsize, $maxdepth, $log ) ); $result->setPrintRequests( $this->getPrintRequests() ); return $result; }