Path: blob/master/src/infrastructure/diff/view/PHUIDiffInlineThreader.php
12242 views
<?php12final class PHUIDiffInlineThreader extends Phobject {34public function reorderAndThreadCommments(array $comments) {5$comments = msort($comments, 'getID');67// Build an empty map of all the comments we actually have. If a comment8// is a reply but the parent has gone missing, we don't want it to vanish9// completely.10$comment_phids = mpull($comments, 'getPHID');11$replies = array_fill_keys($comment_phids, array());1213// Now, remove all comments which are replies, leaving only the top-level14// comments.15foreach ($comments as $key => $comment) {16$reply_phid = $comment->getReplyToCommentPHID();17if (isset($replies[$reply_phid])) {18$replies[$reply_phid][] = $comment;19unset($comments[$key]);20}21}2223// For each top level comment, add the comment, then add any replies24// to it. Do this recursively so threads are shown in threaded order.25$results = array();26foreach ($comments as $comment) {27$results[] = $comment;28$phid = $comment->getPHID();29$descendants = $this->getInlineReplies($replies, $phid, 1);30foreach ($descendants as $descendant) {31$results[] = $descendant;32}33}3435// If we have anything left, they were cyclic references. Just dump36// them in a the end. This should be impossible, but users are very37// creative.38foreach ($replies as $phid => $comments) {39foreach ($comments as $comment) {40$results[] = $comment;41}42}4344return $results;45}4647private function getInlineReplies(array &$replies, $phid, $depth) {48$comments = idx($replies, $phid, array());49unset($replies[$phid]);5051$results = array();52foreach ($comments as $comment) {53$results[] = $comment;54$descendants = $this->getInlineReplies(55$replies,56$comment->getPHID(),57$depth + 1);58foreach ($descendants as $descendant) {59$results[] = $descendant;60}61}6263return $results;64}65}666768