'; if(!$this->Connect()) return -1; $sql = 'select distinct(nid) from '.$this->sCommentTable. ' order by nid'; $rs = $this->db->Execute($sql); $a = $rs->GetArray($rs); $nCount = $rs->RecordCount(); unset($rs); foreach($a as $sKey=>$sValue) { $this->FixComments($sValue[0]); } echo '
'.
$f.'All Nids fixed.
'.
$f.'start time '.$sStartTime.'
'.
$f.'end time '.date('r').'
';
}
function FixComments($nid = -1) {
$f = 'FixComments: ';
if ($nid > -1) {
$this->setNid($nid);
} elseif ($this->nid < 0) {
return -1;
}
if($this->debug) echo $f.'Processing nid '.$this->nid.'
';
if(!$this->Connect())
return -1;
// we have a nid to process.
// Process the threads with no name set, ordering by thread.
// Then process threads with a name set, ordering by timestamp.
$aName = array('y','n');
$aOrderBy = array('thread','timestamp');
for($i = 0; $i <= 1; $i++) {
$rs = $this->getThreadsToProcess('0', $aName[$i], $aOrderBy[$i]);
$aCommentRows = $rs->GetArray();
unset($rs);
foreach($aCommentRows as $sKey => $row) {
$cid = $row['cid'];
if($this->debug) echo $f.'Processing comment cid '.$cid.'.
'.
$f.'Current ThreadID is '.$row['thread'].'
';
$sThread = $this->int2vancode($this->nThread + 1);
if($this->debug) echo $f.'Vancode of new thread ID '.($this->nThread + 1).' is '.$sThread.'.
';
$this->nThread++;
$this->replaceThreadID($cid, $row['thread'], $sThread);
}
}
//
return 1;
}
// recursively replaces thread IDs.
function replaceThreadID($cid, $sOldThreadID, $sThreadID) {
$f = 'replaceThreadID: ';
if($this->debug) echo $f.'param cid '.$cid.' sOldThreadID '.$sOldThreadID.' sThreadID '.$sThreadID.'
';
// find all comments pointing to this one.
$rs = $this->getThreadsToProcess($cid, '');
// grab as array
$aRS = $rs->GetArray();
foreach($aRS as $nKey => $aRow) {
// replace this one's subthreads, recursively.
if($this->debug) echo $f.'found subthread cid '.$aRow['cid'].'
';
$this->replaceThreadID($aRow['cid'], $aRow['thread'], $sThreadID);
}
if($this->debug) echo $f.'renaming thread for cid '.$cid.' to '.$sThreadID.'
';
// break it out by dot.
$aSubThreadID = explode('.', $sOldThreadID);
// any dots?
if(sizeof($aSubThreadID) > 1) {
// overwrite first thread ID with new one.
$aSubThreadID[0] = $sThreadID;
// bring together.
$sNewThreadID = implode('.', $aSubThreadID);
} else {
// no dots, just write in the new thread ID.
$sNewThreadID = $sThreadID.'/';
}
$sSubSql = 'update '.$this->sCommentTable.
' set thread = '.$this->db->qstr($sNewThreadID).
' where cid = '.$cid;
if($this->debug) echo $f.'update: '.$sSubSql.'
';
// execute.
if($this->modify) $this->db->Execute($sSubSql);
if($this->debug) echo $f.'done.
';
}
function replaceThreadID_old($cid, $sThreadID) {
// overwrite the thread ID in the table.
$f = 'replaceThreadID: ';
if($this->debug) echo $f.'parameters: cid '.$cid.' sThreadID '.$sThreadID.'
';
$sWriteThreadID = $sThreadID . '/';
$sql = 'update '.$this->sCommentTable.
' set thread = '.$this->db->qstr($sWriteThreadID).
' where cid = '.$cid;
if($this->debug) echo $f.'update: '.$sql.'
';
if($this->modify) $this->db->Execute($sql);
if($this->debug) echo $f.'comment modified in table.
';
// find all threads pointing to this one.
// we don't care if the name is set or not.
if($rs = $this->getThreadsToProcess($cid, '')) {
if($rs->RecordCount() > 0) {
if($this->debug) echo $f.'found threads pointing to this cid.
';
while($row = $rs->FetchRow()) {
// get comment id.
$nSubCid = $row['cid'];
// get thread value.
$sSubThreadID = $row['thread'];
if($this->debug) echo $f.'subthread cid '.$nSubCid.' has thread id '.$sSubThreadID.'.
';
// break it out by dot.
$aSubThreadID = explode('.', $sSubThreadID);
// overwrite first thread ID with new one.
$aSubThreadID[0] = $sThreadID;
// bring together.
$sSubThreadID = implode('.', $aSubThreadID);
if($this->debug) echo $f.'changed to '.$sSubThreadID.'.
';
// write to database.
$sSubSql = 'update '.$this->sCommentTable.
' set thread = '.$this->db->qstr($sSubThreadID).
' where cid = '.$nSubCid;
if($this->debug) echo $f.'update: '.$sSubSql;
// execute.
if($this->modify) $this->db->Execute($sSubSql);
if($this->debug) echo $f.'written to database.
';
}
unset($row);
unset($rs);
} else {
if($this->debug) echo $f.'no comments point to this one.
';
}
}
if($this->debug) echo $f.'completed.
';
}
// Copied vancode functions from comment.module.
/**
* Generate vancode.
*
* Consists of a leading character indicating length, followed by N digits
* with a numerical value in base 36. Vancodes can be sorted as strings
* without messing up numerical order.
*
* It goes:
* 00, 01, 02, ..., 0y, 0z,
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*/
function int2vancode($i = 0) {
$num = base_convert((int)$i, 10, 36);
$length = strlen($num);
return chr($length + ord('0') - 1) . $num;
}
/**
* Decode vancode back to an integer.
*/
function vancode2int($c = '00') {
return base_convert(substr($c, 1), 36, 10);
}
}
?>