/** * Does update. First deletes, then inserts. * @param $data */ public function updateData(SMWSemanticData $data) { // TODO: Should doDataUpdate() be used instead? (See SMWStore class) $exportData = SMWExporter::getInstance()->makeExportData($data); $subjectUri = SMWExporter::getInstance()->expandURI($exportData->getSubject()->getUri()); $this->removeDataForURI($subjectUri); $tripleList = $exportData->getTripleList(); $sparqlUpdateText = "INSERT INTO <> {\n"; // Follows ARC2 SPARQL+ syntax (not SPARQL Update) foreach ($tripleList as $triple) { $subject = $triple[0]; $predicate = $triple[1]; $object = $triple[2]; $objectStr = ""; $subjectStr = ""; $predicateStr = ""; if ($object instanceof SMWExpLiteral) { // TODO: Add escaping for results of getLexicalForm()? $objectStr = "\"" . $object->getLexicalForm() . "\"" . ($object->getDatatype() == "" ? "" : "^^<" . $object->getDatatype() . ">"); } elseif ($object instanceof SMWExpResource) { $objectStr = "<" . SMWExporter::getInstance()->expandURI($object->getUri()) . ">"; } else { $objectStr = "\"\""; } if ($subject instanceof SMWExpResource) { $subjectStr = "<" . SMWExporter::getInstance()->expandURI($subject->getUri()) . ">"; } if ($predicate instanceof SMWExpResource) { $predicateStr = "<" . SMWExporter::getInstance()->expandURI($predicate->getUri()) . ">"; } $sparqlUpdateText .= $subjectStr . " " . $predicateStr . " " . $objectStr . " .\n"; } $sparqlUpdateText .= "}\n"; wfDebugLog('SPARQL_LOG', $sparqlUpdateText); // TODO: Remove debug code? $response = $this->executeArc2Query($sparqlUpdateText); return parent::updateData($data); }
/** * @see SMWStore::refreshData * * @todo This method will be overhauled in SMW 1.9 to become cleaner * and more robust. * * @param integer $index * @param integer $count * @param mixed $namespaces Array or false * @param boolean $usejobs * * @return decimal between 0 and 1 to indicate the overall progress of the refreshing */ public function refreshData(&$index, $count, $namespaces = false, $usejobs = true) { $updatejobs = array(); $emptyrange = true; // was nothing done in this run? // Update by MediaWiki page id --> make sure we get all pages. $tids = array(); // Array of ids for ($i = $index; $i < $index + $count; $i++) { $tids[] = $i; } $titles = Title::newFromIDs($tids); foreach ($titles as $title) { if ($namespaces == false || in_array($title->getNamespace(), $namespaces)) { $updatejobs[] = new SMWUpdateJob($title); $emptyrange = false; } } // update by internal SMW id --> make sure we get all objects in SMW $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select(SMWSql3SmwIds::tableName, array('smw_id', 'smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array("smw_id >= {$index} ", " smw_id < " . $dbr->addQuotes($index + $count)), __METHOD__); foreach ($res as $row) { $emptyrange = false; // note this even if no jobs were created if ($namespaces && !in_array($row->smw_namespace, $namespaces)) { continue; } // Find page to refresh, even for special properties: if ($row->smw_title != '' && $row->smw_title[0] != '_') { $titleKey = $row->smw_title; } elseif ($row->smw_namespace == SMW_NS_PROPERTY && $row->smw_iw == '' && $row->smw_subobject == '') { $titleKey = str_replace(' ', '_', SMWDIProperty::findPropertyLabel($row->smw_title)); } else { $titleKey = ''; } if ($row->smw_subobject !== '') { // leave subobjects alone; they ought to be changed with their pages } elseif (($row->smw_iw === '' || $row->smw_iw == SMW_SQL3_SMWREDIIW) && $titleKey != '') { // objects representing pages // TODO: special treament of redirects needed, since the store will // not act on redirects that did not change according to its records $title = Title::makeTitleSafe($row->smw_namespace, $titleKey); if ($title !== null && !$title->exists()) { $updatejobs[] = new UpdateJob($title); } } elseif ($row->smw_iw == SMW_SQL3_SMWIW_OUTDATED) { // remove outdated internal object references $dbw = wfGetDB(DB_MASTER); foreach ($this->store->getPropertyTables() as $proptable) { if ($proptable->usesIdSubject()) { $dbw->delete($proptable->getName(), array('s_id' => $row->smw_id), __METHOD__); } } $dbw->delete(SMWSql3SmwIds::tableName, array('smw_id' => $row->smw_id), __METHOD__); } elseif ($titleKey != '') { // "normal" interwiki pages or outdated internal objects -- delete $diWikiPage = new SMWDIWikiPage($titleKey, $row->smw_namespace, $row->smw_iw); $emptySemanticData = new SMWSemanticData($diWikiPage); $this->store->updateData($emptySemanticData); } } $dbr->freeResult($res); wfRunHooks('smwRefreshDataJobs', array(&$updatejobs)); if ($usejobs) { JobBase::batchInsert($updatejobs); } else { foreach ($updatejobs as $job) { $job->run(); } } $nextpos = $index + $count; if ($emptyrange) { // nothing found, check if there will be more pages later on $next1 = $dbr->selectField('page', 'page_id', "page_id >= {$nextpos}", __METHOD__, array('ORDER BY' => "page_id ASC")); $next2 = $dbr->selectField(SMWSql3SmwIds::tableName, 'smw_id', "smw_id >= {$nextpos}", __METHOD__, array('ORDER BY' => "smw_id ASC")); $nextpos = $next2 != 0 && $next2 < $next1 ? $next2 : $next1; } $max1 = $dbr->selectField('page', 'MAX(page_id)', '', __METHOD__); $max2 = $dbr->selectField(SMWSql3SmwIds::tableName, 'MAX(smw_id)', '', __METHOD__); $index = $nextpos ? $nextpos : -1; return $index > 0 ? $index / max($max1, $max2) : 1; }