/** * Change an internal id to another value. If no target value is given, the * value is changed to become the last id entry (based on the automatic id * increment of the database). Whatever currently occupies this id will be * moved consistently in all relevant tables. Whatever currently occupies * the target id will be ignored (it should be ensured that nothing is * moved to an id that is still in use somewhere). * * @since 1.8 * @param integer $curid * @param integer $targetid */ public function moveSMWPageID($curid, $targetid = 0) { $db = wfGetDB(DB_MASTER, 'smw'); $row = $db->selectRow(SMWSql3SmwIds::tableName, '*', array('smw_id' => $curid), __METHOD__); if ($row === false) { return; } // no id at current position, ignore if ($targetid == 0) { // append new id $db->insert(SMWSql3SmwIds::tableName, array('smw_id' => $db->nextSequenceValue('smw_ids_smw_id_seq'), 'smw_title' => $row->smw_title, 'smw_namespace' => $row->smw_namespace, 'smw_iw' => $row->smw_iw, 'smw_subobject' => $row->smw_subobject, 'smw_sortkey' => $row->smw_sortkey), __METHOD__); $targetid = $db->insertId(); } else { // change to given id $db->insert(SMWSql3SmwIds::tableName, array('smw_id' => $targetid, 'smw_title' => $row->smw_title, 'smw_namespace' => $row->smw_namespace, 'smw_iw' => $row->smw_iw, 'smw_subobject' => $row->smw_subobject, 'smw_sortkey' => $row->smw_sortkey), __METHOD__); } $db->delete(SMWSql3SmwIds::tableName, array('smw_id' => $curid), 'SMWSQLStore3::moveSMWPageID'); $this->setCache($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject, $targetid, $row->smw_sortkey); $this->store->changeSMWPageID($curid, $targetid, $row->smw_namespace, $row->smw_namespace); }
/** * Helper method to write information about some redirect. Various updates * can be necessary if redirects are resolved as identities in SMW. The * title and namespace of the affected page and of its updated redirect * target are given. The target can be empty ('') to delete any redirect. * Returns the canonical ID that is now to be used for the subject. * * This method does not change the ids of the affected pages, and thus it * is not concerned with updates of the data that is currently stored for * the subject. Normally, a subject that is a redirect will not have other * data, but this method does not depend on this. * * @note Please make sure you fully understand this code before making any * changes here. Keeping the redirect structure consistent is important, * and errors in this code can go unnoticed for quite some time. * * @note This method merely handles the addition or deletion of a redirect * statement in the wiki. It does not assume that any page contents has * been changed (e.g. moved). See changeTitle() for additional handling in * this case. * * @todo Clean up this code. * * @since 1.8 * @param string $subject_t * @param integer $subject_ns * @param string $curtarget_t * @param integer $curtarget_ns * @return integer the new canonical ID of the subject */ protected function updateRedirects($subject_t, $subject_ns, $curtarget_t = '', $curtarget_ns = -1) { global $smwgQEqualitySupport, $smwgEnableUpdateJobs; $count = 0; //track count changes for redi property // *** First get id of subject, old redirect target, and current (new) redirect target ***// $sid_sort = ''; $sid = $this->store->smwIds->getSMWPageIDandSort($subject_t, $subject_ns, '', '', $sid_sort, false); // find real id of subject, if any /// NOTE: $sid can be 0 here; this is useful to know since it means that fewer table updates are needed $new_tid = $curtarget_t ? $this->store->smwIds->makeSMWPageID($curtarget_t, $curtarget_ns, '', '', false) : 0; // real id of new target, if given $db = wfGetDB(DB_SLAVE); $row = $db->selectRow(array('smw_fpt_redi'), 'o_id', array('s_title' => $subject_t, 's_namespace' => $subject_ns), __METHOD__); $old_tid = $row !== false ? $row->o_id : 0; // real id of old target, if any /// NOTE: $old_tid and $new_tid both (intentionally) ignore further redirects: no redirect chains if ($old_tid == $new_tid) { // no change, all happy return $new_tid == 0 ? $sid : $new_tid; } // note that this means $old_tid != $new_tid in all cases below // *** Make relevant changes in property tables (don't write the new redirect yet) ***// $db = wfGetDB(DB_MASTER); // now we need to write something if ($old_tid == 0 && $sid != 0 && $smwgQEqualitySupport != SMW_EQ_NONE) { // new redirect // $smwgQEqualitySupport requires us to change all tables' page references from $sid to $new_tid. // Since references must not be 0, we don't have to do this is $sid == 0. $this->store->changeSMWPageID($sid, $new_tid, $subject_ns, $curtarget_ns, false, true); } elseif ($old_tid != 0) { // existing redirect is changed or deleted $db->delete('smw_fpt_redi', array('s_title' => $subject_t, 's_namespace' => $subject_ns), __METHOD__); $count--; if ($smwgEnableUpdateJobs && $smwgQEqualitySupport != SMW_EQ_NONE) { // entries that refer to old target may in fact refer to subject, // but we don't know which: schedule affected pages for update $jobs = array(); foreach (SMWSQLStore3::getPropertyTables() as $proptable) { if ($proptable->getName() == 'smw_fpt_redi') { continue; // can safely be skipped } if ($proptable->usesIdSubject()) { $from = $db->tableName($proptable->getName()) . ' INNER JOIN ' . $db->tableName(SMWSql3SmwIds::tableName) . ' ON s_id=smw_id'; $select = 'DISTINCT smw_title AS t,smw_namespace AS ns'; } else { $from = $db->tableName($proptable->getName()); $select = 'DISTINCT s_title AS t,s_namespace AS ns'; } if ($subject_ns == SMW_NS_PROPERTY && !$proptable->isFixedPropertyTable()) { $res = $db->select($from, $select, array('p_id' => $old_tid), __METHOD__); foreach ($res as $row) { $title = Title::makeTitleSafe($row->ns, $row->t); if (!is_null($title)) { $jobs[] = new SMWUpdateJob($title); } } $db->freeResult($res); } foreach ($proptable->getFields($this->store) as $fieldname => $type) { if ($type == 'p') { $res = $db->select($from, $select, array($fieldname => $old_tid), __METHOD__); foreach ($res as $row) { $title = Title::makeTitleSafe($row->ns, $row->t); if (!is_null($title)) { $jobs[] = new SMWUpdateJob($title); } } $db->freeResult($res); } } } /// NOTE: we do not update the concept cache here; this remains an offline task /// NOTE: this only happens if $smwgEnableUpdateJobs was true above: Job::batchInsert($jobs); } } // *** Finally, write the new redirect data ***// if ($new_tid != 0) { // record a new redirect // Redirecting done right: // (1) make a new ID with iw SMW_SQL3_SMWREDIIW or // change iw field of current ID in this way, // (2) write smw_fpt_redi table, // (3) update canonical cache. // This order must be obeyed unless you really understand what you are doing! if ($old_tid == 0 && $smwgQEqualitySupport != SMW_EQ_NONE) { // mark subject as redirect (if it was no redirect before) if ($sid == 0) { // every redirect page must have an ID $sid = $this->store->smwIds->makeSMWPageID($subject_t, $subject_ns, SMW_SQL3_SMWREDIIW, '', false); } else { $db->update(SMWSql3SmwIds::tableName, array('smw_iw' => SMW_SQL3_SMWREDIIW), array('smw_id' => $sid), __METHOD__); $this->store->smwIds->setCache($subject_t, $subject_ns, '', '', 0, ''); $this->store->smwIds->setCache($subject_t, $subject_ns, SMW_SQL3_SMWREDIIW, '', $sid, $sid_sort); } } $db->insert('smw_fpt_redi', array('s_title' => $subject_t, 's_namespace' => $subject_ns, 'o_id' => $new_tid), __METHOD__); $count++; } else { // delete old redirect // This case implies $old_tid != 0 (or we would have new_tid == old_tid above). // Therefore $subject had a redirect, and it must also have an ID. // This shows that $sid != 0 here. if ($smwgQEqualitySupport != SMW_EQ_NONE) { // mark subject as non-redirect $db->update(SMWSql3SmwIds::tableName, array('smw_iw' => ''), array('smw_id' => $sid), __METHOD__); $this->store->smwIds->setCache($subject_t, $subject_ns, SMW_SQL3_SMWREDIIW, '', 0, ''); $this->store->smwIds->setCache($subject_t, $subject_ns, '', '', $sid, $sid_sort); } } // *** Flush some caches to be safe, though they are not essential in runs with redirect updates ***// unset($this->store->m_semdata[$sid]); unset($this->store->m_semdata[$new_tid]); unset($this->store->m_semdata[$old_tid]); unset($this->store->m_sdstate[$sid]); unset($this->store->m_sdstate[$new_tid]); unset($this->store->m_sdstate[$old_tid]); // *** Update reference count for _REDI property ***// $this->addToPropertyUsageCount($this->store->smwIds->getSMWPropertyID(new SMWDIProperty('_REDI')), $count, $db); return $new_tid == 0 ? $sid : $new_tid; }
/** * Change an internal id to another value. If no target value is given, the * value is changed to become the last id entry (based on the automatic id * increment of the database). Whatever currently occupies this id will be * moved consistently in all relevant tables. Whatever currently occupies * the target id will be ignored (it should be ensured that nothing is * moved to an id that is still in use somewhere). * * @since 1.8 * @param integer $curid * @param integer $targetid */ public function moveSMWPageID($curid, $targetid = 0) { $db = $this->store->getConnection(); $row = $db->selectRow(self::tableName, '*', array('smw_id' => $curid), __METHOD__); if ($row === false) { return; // no id at current position, ignore } $db->beginAtomicTransaction(__METHOD__); if ($targetid == 0) { // append new id $sequenceValue = $db->nextSequenceValue($this->getIdTable() . '_smw_id_seq'); // Bug 42659 $db->insert(self::tableName, array('smw_id' => $sequenceValue, 'smw_title' => $row->smw_title, 'smw_namespace' => $row->smw_namespace, 'smw_iw' => $row->smw_iw, 'smw_subobject' => $row->smw_subobject, 'smw_sortkey' => $row->smw_sortkey), __METHOD__); $targetid = $db->insertId(); } else { // change to given id $db->insert(self::tableName, array('smw_id' => $targetid, 'smw_title' => $row->smw_title, 'smw_namespace' => $row->smw_namespace, 'smw_iw' => $row->smw_iw, 'smw_subobject' => $row->smw_subobject, 'smw_sortkey' => $row->smw_sortkey), __METHOD__); } $db->delete(self::tableName, array('smw_id' => $curid), __METHOD__); $this->setCache($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject, $targetid, $row->smw_sortkey); $this->store->changeSMWPageID($curid, $targetid, $row->smw_namespace, $row->smw_namespace); $db->commitAtomicTransaction(__METHOD__); }