public function testSlugDepth() { $slugs = array('/' => 0, 'a/' => 1, 'a/b/' => 2, 'a////b/' => 2); foreach ($slugs as $slug => $depth) { $this->assertEqual($depth, PhabricatorSlug::getDepth($slug), "Depth of '{$slug}'"); } }
public function setSlug($slug) { $this->slug = PhabricatorSlug::normalize($slug); $this->depth = PhabricatorSlug::getDepth($slug); return $this; }
private function renderDocumentChildren($slug) { $document_dao = new PhrictionDocument(); $content_dao = new PhrictionContent(); $conn = $document_dao->establishConnection('r'); $limit = 250; $d_child = PhabricatorSlug::getDepth($slug) + 1; $d_grandchild = PhabricatorSlug::getDepth($slug) + 2; // Select children and grandchildren. $children = queryfx_all($conn, 'SELECT d.slug, d.depth, c.title FROM %T d JOIN %T c ON d.contentID = c.id WHERE d.slug LIKE %> AND d.depth IN (%d, %d) AND d.status IN (%Ld) ORDER BY d.depth, c.title LIMIT %d', $document_dao->getTableName(), $content_dao->getTableName(), $slug == '/' ? '' : $slug, $d_child, $d_grandchild, array(PhrictionDocumentStatus::STATUS_EXISTS, PhrictionDocumentStatus::STATUS_STUB), $limit); if (!$children) { return; } // We're going to render in one of three modes to try to accommodate // different information scales: // // - If we found fewer than $limit rows, we know we have all the children // and grandchildren and there aren't all that many. We can just render // everything. // - If we found $limit rows but the results included some grandchildren, // we just throw them out and render only the children, as we know we // have them all. // - If we found $limit rows and the results have no grandchildren, we // have a ton of children. Render them and then let the user know that // this is not an exhaustive list. if (count($children) == $limit) { $more_children = true; foreach ($children as $child) { if ($child['depth'] == $d_grandchild) { $more_children = false; } } $show_grandchildren = false; } else { $show_grandchildren = true; $more_children = false; } $grandchildren = array(); foreach ($children as $key => $child) { if ($child['depth'] == $d_child) { continue; } else { unset($children[$key]); if ($show_grandchildren) { $ancestors = PhabricatorSlug::getAncestry($child['slug']); $grandchildren[end($ancestors)][] = $child; } } } // Fill in any missing children. $known_slugs = ipull($children, null, 'slug'); foreach ($grandchildren as $slug => $ignored) { if (empty($known_slugs[$slug])) { $children[] = array('slug' => $slug, 'depth' => $d_child, 'title' => PhabricatorSlug::getDefaultTitle($slug), 'empty' => true); } } $children = isort($children, 'title'); $list = array(); foreach ($children as $child) { $list[] = hsprintf('<li>'); $list[] = $this->renderChildDocumentLink($child); $grand = idx($grandchildren, $child['slug'], array()); if ($grand) { $list[] = hsprintf('<ul>'); foreach ($grand as $grandchild) { $list[] = hsprintf('<li>'); $list[] = $this->renderChildDocumentLink($grandchild); $list[] = hsprintf('</li>'); } $list[] = hsprintf('</ul>'); } $list[] = hsprintf('</li>'); } if ($more_children) { $list[] = phutil_tag('li', array(), pht('More...')); } $content = array(phutil_tag('div', array('class' => 'phriction-children-header ' . 'sprite-gradient gradient-lightblue-header'), pht('Document Hierarchy')), phutil_tag('div', array('class' => 'phriction-children'), phutil_tag('ul', array(), $list))); return id(new PHUIDocumentView())->setOffset(true)->appendChild($content); }
private function renderDocumentChildren($slug) { $d_child = PhabricatorSlug::getDepth($slug) + 1; $d_grandchild = PhabricatorSlug::getDepth($slug) + 2; $limit = 250; $query = id(new PhrictionDocumentQuery())->setViewer($this->getRequest()->getUser())->withDepths(array($d_child, $d_grandchild))->withSlugPrefix($slug == '/' ? '' : $slug)->withStatuses(array(PhrictionDocumentStatus::STATUS_EXISTS, PhrictionDocumentStatus::STATUS_STUB))->setLimit($limit)->setOrder(PhrictionDocumentQuery::ORDER_HIERARCHY)->needContent(true); $children = $query->execute(); if (!$children) { return; } // We're going to render in one of three modes to try to accommodate // different information scales: // // - If we found fewer than $limit rows, we know we have all the children // and grandchildren and there aren't all that many. We can just render // everything. // - If we found $limit rows but the results included some grandchildren, // we just throw them out and render only the children, as we know we // have them all. // - If we found $limit rows and the results have no grandchildren, we // have a ton of children. Render them and then let the user know that // this is not an exhaustive list. if (count($children) == $limit) { $more_children = true; foreach ($children as $child) { if ($child->getDepth() == $d_grandchild) { $more_children = false; } } $show_grandchildren = false; } else { $show_grandchildren = true; $more_children = false; } $children_dicts = array(); $grandchildren_dicts = array(); foreach ($children as $key => $child) { $child_dict = array('slug' => $child->getSlug(), 'depth' => $child->getDepth(), 'title' => $child->getContent()->getTitle()); if ($child->getDepth() == $d_child) { $children_dicts[] = $child_dict; continue; } else { unset($children[$key]); if ($show_grandchildren) { $ancestors = PhabricatorSlug::getAncestry($child->getSlug()); $grandchildren_dicts[end($ancestors)][] = $child_dict; } } } // Fill in any missing children. $known_slugs = mpull($children, null, 'getSlug'); foreach ($grandchildren_dicts as $slug => $ignored) { if (empty($known_slugs[$slug])) { $children_dicts[] = array('slug' => $slug, 'depth' => $d_child, 'title' => PhabricatorSlug::getDefaultTitle($slug), 'empty' => true); } } $children_dicts = isort($children_dicts, 'title'); $list = array(); foreach ($children_dicts as $child) { $list[] = hsprintf('<li class="remarkup-list-item">'); $list[] = $this->renderChildDocumentLink($child); $grand = idx($grandchildren_dicts, $child['slug'], array()); if ($grand) { $list[] = hsprintf('<ul class="remarkup-list">'); foreach ($grand as $grandchild) { $list[] = hsprintf('<li class="remarkup-list-item">'); $list[] = $this->renderChildDocumentLink($grandchild); $list[] = hsprintf('</li>'); } $list[] = hsprintf('</ul>'); } $list[] = hsprintf('</li>'); } if ($more_children) { $list[] = phutil_tag('li', array('class' => 'remarkup-list-item'), pht('More...')); } $header = id(new PHUIHeaderView())->setHeader(pht('Document Hierarchy')); $box = id(new PHUIObjectBoxView())->setHeader($header)->appendChild(phutil_tag('div', array('class' => 'phabricator-remarkup mlt mlb'), phutil_tag('ul', array('class' => 'remarkup-list'), $list))); return phutil_tag_div('phui-document-box', $box); }