private function findReferenceByPropertyTable($proptable, $id)
 {
     $row = false;
     if ($proptable->usesIdSubject()) {
         $row = $this->connection->selectRow($proptable->getName(), array('s_id'), array('s_id' => $id), __METHOD__);
     }
     if ($row !== false) {
         return $row;
     }
     $fields = $proptable->getFields($this->store);
     // Check whether an object reference exists or not
     if (isset($fields['o_id'])) {
         // This next time someone ... I'm going to Alaska
         $field = strpos($proptable->getName(), 'redi') ? array('s_title', 's_namespace') : array('s_id');
         $row = $this->connection->selectRow($proptable->getName(), $field, array('o_id' => $id), __METHOD__);
         if ($row !== false && strpos($proptable->getName(), 'redi')) {
             $row->s_id = $this->store->getObjectIds()->findRedirectIdFor($row->s_title, $row->s_namespace);
         }
     }
     // If the property table is not a fixed table (== assigns a whole
     // table to a specific property with the p_id column being suppressed)
     // then check for the p_id field
     if ($row === false && !$proptable->isFixedPropertyTable()) {
         $row = $this->connection->selectRow($proptable->getName(), array('s_id'), array('p_id' => $id), __METHOD__);
     }
     // If the query table contains a reference then we keep the object (could
     // be a subject, property, or printrequest) where in case the query is
     // removed the object will also loose its reference
     if ($row === false) {
         $row = $this->connection->selectRow(SQLStore::QUERY_LINKS_TABLE, array('s_id'), array('o_id' => $id), __METHOD__);
     }
     return $row;
 }
 private function triggerResetCacheEventBy($id)
 {
     $subject = $this->store->getObjectIds()->getDataItemById($id);
     if (!$subject instanceof DIWikiPage) {
         return;
     }
     $eventHandler = EventHandler::getInstance();
     $dispatchContext = $eventHandler->newDispatchContext();
     $dispatchContext->set('subject', $subject);
     $eventHandler->getEventDispatcher()->dispatch('cached.propertyvalues.prefetcher.reset', $dispatchContext);
     $eventHandler->getEventDispatcher()->dispatch('property.specification.change', $dispatchContext);
     $eventHandler->getEventDispatcher()->dispatch('factbox.cache.delete', $dispatchContext);
 }
 private function doCheckPredefinedPropertyBorder($connection)
 {
     // Check if we already have this structure
     $expectedID = SQLStore::FIXED_PROPERTY_ID_UPPERBOUND;
     $currentID = $connection->selectRow(SQLStore::ID_TABLE, 'smw_id', 'smw_iw=' . $connection->addQuotes(SMW_SQL3_SMWBORDERIW));
     if ($currentID !== false && $currentID->smw_id == $expectedID) {
         return $this->messageReporter->reportMessage("   ... space for internal properties already allocated.\n");
     }
     // Legacy bound
     $currentID = $currentID === false ? 50 : $currentID->smw_id;
     $this->messageReporter->reportMessage("   ... allocating space for internal properties ...\n");
     $this->store->getObjectIds()->moveSMWPageID($expectedID);
     $connection->insert(SQLStore::ID_TABLE, array('smw_id' => $expectedID, 'smw_title' => '', 'smw_namespace' => 0, 'smw_iw' => SMW_SQL3_SMWBORDERIW, 'smw_subobject' => '', 'smw_sortkey' => ''), __METHOD__);
     $this->messageReporter->reportMessage("   ... moving from {$currentID} to {$expectedID} ");
     // make way for built-in ids
     for ($i = $currentID; $i < $expectedID; $i++) {
         $this->store->getObjectIds()->moveSMWPageID($i);
         $this->messageReporter->reportMessage('.');
     }
     $this->messageReporter->reportMessage("\n   ... done.\n");
 }
 private function markPossibleDuplicateProperties($row)
 {
     $db = $this->store->getConnection('mw.db');
     // Use the sortkey (comparing the label and not the "_..." key) in order
     // to match possible duplicate properties by label (not by key)
     $duplicates = $db->select(\SMWSql3SmwIds::TABLE_NAME, array('smw_id', 'smw_title'), array("smw_id !=" . $db->addQuotes($row->smw_id), "smw_sortkey =" . $db->addQuotes($row->smw_sortkey), "smw_namespace =" . $row->smw_namespace, "smw_subobject =" . $db->addQuotes($row->smw_subobject)), __METHOD__, array('ORDER BY' => "smw_id ASC"));
     if ($duplicates === false) {
         return;
     }
     // Instead of copying ID's across DB tables have the re-parse to ensure
     // that all property value ID's are reassigned together while the duplicate
     // is marked for removal until the next run
     foreach ($duplicates as $duplicate) {
         // If titles don't match then continue because it could be that
         // Property:Foo with displaytitle foobar -> sortkey ->foobar
         // Property:Bar with displaytitle foobar -> sortkey ->foobar
         if ($row->smw_title !== $duplicate->smw_title) {
             continue;
         }
         $this->store->getObjectIds()->updateInterwikiField($duplicate->smw_id, new DIWikiPage($row->smw_title, $row->smw_namespace, SMW_SQL3_SMWDELETEIW));
     }
 }