public function execute() { global $wgDBtype; if ($this->hasOption('setup')) { $store = new SMWSQLStore3(); // Lets do a drop to ensure the user doesn't has any Store3 tables already (happens when running this script twice) $tables = array('smw_stats'); foreach (SMWSQLStore3::getPropertyTables() as $proptable) { $tables[] = $proptable->name; } $dbw = wfGetDB(DB_MASTER); foreach ($tables as $table) { $name = $dbw->tableName($table); $dbw->query('DROP TABLE ' . ($wgDBtype == 'postgres' ? '' : 'IF EXISTS ') . $name, 'SMWMigrate::drop'); } $store->setup(); //enter user defined properties into smw_stats (internal ones are handled by setup already ) $query = 'Replace into ' . $dbw->tableName('smw_stats') . ' (pid,usage_count) Select smw_id,0 from ' . $dbw->tableName('smw_ids') . ' where smw_namespace = ' . SMW_NS_PROPERTY . ' and smw_iw = "" '; $dbw->query($query, 'SMWMigrate:commandLine'); } elseif ($this->hasOption('migrate')) { $options = array(); if ($this->hasArg(0)) { if ($this->hasArg(1)) { $options['LIMIT'] = $this->getArg(0); $options['OFFSET'] = $this->getArg(1); } } $dbw = wfGetDB(DB_MASTER); $oldStore = new SMWSQLStore2(); $newStore = new SMWSQLStore3(); $proptables = SMWSQLStore3::getPropertyTables(); //get properties $res = $dbw->select('smw_ids', array('smw_id', 'smw_title', 'smw_namespace'), array('smw_namespace' => SMW_NS_PROPERTY), __METHOD__, $options); foreach ($res as $row) { $property = new SMWDIProperty($row->smw_title); echo 'Now migrating data for Property ' . $property->getLabel() . " into Store3 \n"; //get the table $tableId = SMWSQLStore3::findPropertyTableID($property); $proptable = $proptables[$tableId]; //get the DIHandler $dataItemId = SMWDataValueFactory::getDataItemId($property->findPropertyTypeId()); $diHandler = $newStore->getDataItemHandlerForDIType($dataItemId); $subjects = $oldStore->getPropertySubjects($property, null); $insertions = array(); foreach ($subjects as $subject) { $sid = $newStore->makeSMWPageID($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), $subject->getSubobjectName(), true, str_replace('_', ' ', $subject->getDBkey()) . $subject->getSubobjectName()); //now prepare udpates $propvals = $oldStore->getPropertyValues($subject, $property); $uvals = $proptable->idsubject ? array('s_id' => $sid) : array('s_title' => $subject->getDBkey(), 's_namespace' => $subject->getNamespace()); if ($proptable->fixedproperty == false) { $uvals['p_id'] = $newStore->makeSMWPropertyID($property); } foreach ($propvals as $propval) { $uvals = array_merge($uvals, $diHandler->getInsertValues($propval)); $insertions[] = $uvals; } } // now write to the DB for all subjects (is this too much?) $dbw->insert($proptable->name, $insertions, "SMW::migrate{$proptable->name}"); } $dbw->freeResult($res); } else { echo "Sorry I refuse to work without any options currently"; } }
static function createRDF($title, $rdfDataArray, $fullexport = true, $backlinks = false) { // if it's not a full export, don't add internal object data if (!$fullexport) { return true; } $pageName = $title->getDBkey(); $namespace = $title->getNamespace(); // Go through all SIOs for the current page, create RDF for // each one, and add it to the general array. $iw = ''; $db = wfGetDB(DB_SLAVE); $res = $db->select('smw_ids', array('smw_id', 'smw_namespace', 'smw_title'), 'smw_title LIKE ' . $db->addQuotes($pageName . '#%') . ' AND ' . 'smw_namespace=' . $db->addQuotes($namespace) . ' AND smw_iw=' . $db->addQuotes($iw), 'SIO::getSMWPageObjectIDs'); while ($row = $db->fetchObject($res)) { $value = new SIOInternalObjectValue($row->smw_title, intval($row->smw_namespace)); if (class_exists('SMWSqlStubSemanticData')) { // SMW >= 1.6 $semdata = new SMWSqlStubSemanticData($value, false); } else { $semdata = new SMWSemanticData($value, false); } $propertyTables = SMWSQLStore2::getPropertyTables(); foreach ($propertyTables as $tableName => $propertyTable) { $data = smwfGetStore()->fetchSemanticData($row->smw_id, null, $propertyTable); foreach ($data as $d) { $semdata->addPropertyStubValue(reset($d), end($d)); } } $rdfDataArray[] = SMWExporter::makeExportData($semdata, null); } return true; }
function drop( $verbose = true ) { return parent::drop(); }
public function drop( $verbose = true ) { parent::drop( $verbose ); smwfGetSparqlDatabase()->delete( "?s ?p ?o", "?s ?p ?o" ); }
/** * 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 SMW_SQL2_NOQUERY. Callers need * to check for this and discard the query in this case. * @todo Check if hierarchy queries work as expected. */ protected function compilePropertyCondition(SMWSQLStore2Query $query, $property, SMWDescription $valuedesc) { $tableid = SMWSQLStore2::findPropertyTableID($property); if ($tableid == '') { // probably a type-polymorphic property $typeid = $valuedesc->getTypeID(); $tableid = SMWSQLStore2::findTypeTableID($typeid); } else { // normal property $typeid = $property->getPropertyTypeID(); } if ($tableid == '') { // Still no table to query? Give up. $query->type = SMW_SQL2_NOQUERY; return; } $proptables = SMWSQLStore2::getPropertyTables(); $proptable = $proptables[$tableid]; if (!$proptable->idsubject) { // no queries with such tables (there is really no demand, as only redirects are affected) $query->type = SMW_SQL2_NOQUERY; return; } list($sig, $valueindex, $labelindex) = SMWSQLStore2::getTypeSignature($typeid); $sortkey = $property->getDBkey(); // TODO: strictly speaking, the DB key is not what we want here, since sortkey is based on a "wiki value" // *** Basic settings: table, joinfield, and objectfields ***// $query->jointable = $proptable->name; if ($property->isInverse()) { // see if we can support inverses by inverting the proptable data if (count($proptable->objectfields) == 1 && reset($proptable->objectfields) == 'p') { $query->joinfield = $query->alias . '.' . reset(array_keys($proptable->objectfields)); $objectfields = array('s_id' => 'p'); $valueindex = $labelindex = 3; // should normally not change, but let's be strict } else { // no inverses supported for this property, stop here $query->type = SMW_SQL2_NOQUERY; return; } } else { // normal forward property $query->joinfield = "{$query->alias}.s_id"; $objectfields = $proptable->objectfields; } // *** Add conditions for selecting rows for this property, maybe with a hierarchy ***// if ($proptable->fixedproperty == false) { $pid = $this->m_store->getSMWPropertyID($property); if (!$property->getPropertyID() || $property->getPropertyTypeID() != '__err') { // also make property hierarchy (may or may not be executed later on) // exclude type-polymorphic properties _1, _2, ... (2nd check above suffices, but 1st is faster to check) // we could also exclude other cases here, if desired $pqid = SMWSQLStore2Query::$qnum; $pquery = new SMWSQLStore2Query(); $pquery->type = SMW_SQL2_PROP_HIERARCHY; $pquery->joinfield = array($pid); $query->components[$pqid] = "{$query->alias}.p_id"; $this->m_queries[$pqid] = $pquery; } else { $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 (count($objectfields) == 1 && reset($objectfields) == 'p') { // page description, process like main query $sub = $this->compileQueries($valuedesc); $objectfield = reset(array_keys($objectfields)); if ($sub >= 0) { $query->components[$sub] = "{$query->alias}.{$objectfield}"; } } else { // non-page value description; expressive features mainly based on value $this->compileAttributeWhere($query, $valuedesc, $proptable, $valueindex); // (no need to pass on $objectfields since they are just as in $proptable in this case) } // *** Incorporate ordering if desired ***// if ($valueindex >= 0 && array_key_exists($sortkey, $this->m_sortkeys)) { // This code might be overly general: it supports datatypes of arbitrary signatures // and valueindex (sortkeys). It can even order pages by something other than their // sortkey (e.g. by their namespace?!), and it can handle values consisting of a page // and some more data fields before or after. Supporting pages in this way requires us // to iterate over the table fields since one page corresponds to four values in a // type's signature. Thankfully, signatures are short so this iteration is not notable. $smwidjoinfield = false; $fieldName = $this->getDBFieldsForDVIndex($objectfields, $valueindex, $smwidjoinfield); if ($fieldName) { if ($smwidjoinfield) { // TODO: is this smw_ids possibly duplicated in the query? Can we prevent that? (PERFORMANCE) $query->from = ' INNER JOIN ' . $this->m_dbs->tableName('smw_ids') . " AS ids{$query->alias} ON ids{$query->alias}.smw_id={$query->alias}.{$smwidjoinfield}"; $query->sortfields[$sortkey] = "ids{$query->alias}.{$fieldName}"; } else { $query->sortfields[$sortkey] = "{$query->alias}.{$fieldName}"; } } } }
function ft_getPropertyValues($property, $store) { $pid = $store->getSMWPropertyID($property); $db =& wfGetDB(DB_SLAVE); $result = array(); $mode = SMWSQLStore2::getStorageMode($property->getPropertyTypeID()); switch ($mode) { case SMW_SQL2_TEXT2: $res = $db->select('smw_text2', 'value_blob', 'p_id=' . $db->addQuotes($pid)); while ($row = $db->fetchObject($res)) { $dv = SMWDataValueFactory::newPropertyObjectValue($property); $dv->setOutputFormat($outputformat); $dv->setDBkeys(array($row->value_blob)); $result[] = $dv; } $db->freeResult($res); break; case SMW_SQL2_RELS2: $res = $db->select(array('smw_rels2', 'smw_ids'), 'smw_namespace, smw_title, smw_iw', 'p_id=' . $db->addQuotes($pid) . ' AND o_id=smw_id'); while ($row = $db->fetchObject($res)) { $dv = SMWDataValueFactory::newPropertyObjectValue($property); $dv->setOutputFormat($outputformat); $dv->setDBkeys(array($row->smw_title, $row->smw_namespace, $row->smw_iw)); $result[] = $dv; } $db->freeResult($res); break; case SMW_SQL2_ATTS2: if ($requestoptions !== NULL && $requestoptions->boundary !== NULL) { $value_column = $requestoptions->boundary->isNumeric() ? 'value_num' : 'value_xsd'; } else { $testval = SMWDatavalueFactory::newTypeIDValue($property->getPropertyTypeID()); $value_column = $testval->isNumeric() ? 'value_num' : 'value_xsd'; } $sql = 'p_id=' . $db->addQuotes($pid); $res = $db->select('smw_atts2', 'value_unit, value_xsd', 'p_id=' . $db->addQuotes($pid)); while ($row = $db->fetchObject($res)) { $dv = SMWDataValueFactory::newPropertyObjectValue($property); $dv->setOutputFormat($outputformat); $dv->setDBkeys(array($row->value_xsd, $row->value_unit)); $result[] = $dv; } $db->freeResult($res); break; } return $result; }
/** * Given an SMWDescription that is just a conjunction or disjunction of * SMWValueDescription objects, create a plain WHERE condition string for it. */ protected function compileAttributeWhere(SMWDescription $description, $jointable) { if ($description instanceof SMWValueDescription) { $dv = $description->getDatavalue(); if (SMWSQLStore2::getStorageMode($dv->getTypeID()) == SMW_SQL2_SPEC2) { $keys = $dv->getDBkeys(); $value = $keys[0]; $field = "{$jointable}.value_string"; } else { // should be SMW_SQL2_ATTS2 if ($dv->isNumeric()) { $value = $dv->getNumericValue(); $field = "{$jointable}.value_num"; } else { $keys = $dv->getDBkeys(); $value = $keys[0]; $field = "{$jointable}.value_xsd"; } } switch ($description->getComparator()) { case SMW_CMP_LEQ: $comp = '<='; break; case SMW_CMP_GEQ: $comp = '>='; break; case SMW_CMP_NEQ: $comp = '!='; break; case SMW_CMP_LIKE: if ($dv->getTypeID() == '_str') { $comp = ' LIKE '; $value = str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $value); } else { // LIKE only supported for strings $comp = '='; } break; case SMW_CMP_EQ: default: $comp = '='; break; } $result = "{$field}{$comp}" . $this->m_dbs->addQuotes($value); } elseif ($description instanceof SMWConjunction || $description instanceof SMWDisjunction) { $op = $description instanceof SMWConjunction ? ' AND ' : ' OR '; $result = ''; foreach ($description->getDescriptions() as $subdesc) { $result = $result . ($result != '' ? $op : '') . $this->compileAttributeWhere($subdesc, $jointable); } $result = "({$result})"; } else { $result = ''; } return $result; }
function doUpdateData(SMWSemanticData $data) { return parent::updateData($data); }