PostThreadFixer

sDatabaseType = 'pgsql'; $oFixer->sServer = 'localhost'; $oFixer->sDatabase = 'database'; $oFixer->sUserID = 'username'; $oFixer->sPassword = 'password'; $oFixer->sCommentTable = 'comments'; // show lots of debugging messages? $oFixer->debug = true; // modify the database? $oFixer->modify = true; // do all nodes, please. $oFixer->FixAllComments(); // end of code // start of class class clsPostThreadFixer { var $nid; var $nThread; var $sDatabaseType; var $sServer; var $sDatabase; var $sUserID; var $sPassword; var $db; var $sCommentTable; var $bConnected; var $debug; var $modify; function clsPostThreadFixer($nid = -1) { $this->setNid($nid); // give lots of messages. $this->debug = true; // just test for now. $this->modify = false; } function Connect() { if(!$this->bConnected) { $this->db = &ADONewConnection($this->sDatabaseType); if(!$this->db->Connect($this->sServer, $this->sUserID, $this->sPassword, $this->sDatabase)) { echo 'Failed to connect to database.
'; echo 'Error Message ['.$this->db->ErrorMsg().']'; $this->bConnected = false; } else { $this->bConnected = true; } } return $this->bConnected; } function setNid($nid = -1) { $f = 'setNid: '; if($this->debug) { echo $f.'---------------------------------------------'; echo $f.'$nid was '.$this->nid.'
'; } // set the nid $this->nid = $nid; // init the thread id $this->nThread = 0; if($this->debug) { echo $f.'$nid is now '.$this->nid.', Thread ID has been reset.
'; } } // sPid is n if the pid should be nonzero, otherwise the pid is matched. function getThreadsToProcessQuery($sPid = '0', $sEmptyName = '', $sOrderBy = 'thread') { $f = 'getThreadsToProcessQuery: '; if($this->debug) echo $f.'sPid = '.$sPid.' EmptyName = '.$sEmptyName.' OrderBy '.$sOrderBy.'
'; $sql = 'select cid, pid, name, thread '; $sql .= 'from '.$this->sCommentTable. ' '; $sql .= 'where nid = '.$this->nid.' and pid '; if($sPid == 'n') { $sql .= '<> 0 '; } else { $sql .= '= '.$sPid.' '; } // Do we explicitly look for those with empty names, those with names, or don't care. if($sEmptyName == 'y') { $sql .= 'and name = '.$this->db->qstr('') . ' '; } elseif($sEmptyName == 'n') { $sql .= 'and name <> '.$this->db->qstr('') . ' '; } $sql .= 'order by '.$sOrderBy; if($this->debug) echo $f.'sql = '.$sql.'
'; return $sql; } function getThreadsToProcess($sPid = '0', $sEmptyName = 'y', $sOrderBy = 'thread') { $f = 'getThreadsToProcess: '; $sql = $this->getThreadsToProcessQuery($sPid, $sEmptyName, $sOrderBy); $rs = $this->db->Execute($sql); if($this->debug) echo $f.'Record Count: '.$rs->RecordCount().'
'; return $rs; } function FixAllComments() { $f = 'FixAllComments: '; echo $f.'fixing all Nids.
'; $sStartTime = date('r'); echo $f.'start time '.$sStartTime.'

'; 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); } } ?>