Path: blob/master/src/infrastructure/edges/conduit/EdgeSearchConduitAPIMethod.php
12241 views
<?php12final class EdgeSearchConduitAPIMethod3extends ConduitAPIMethod {45public function getAPIMethodName() {6return 'edge.search';7}89public function getMethodDescription() {10return pht('Read edge relationships between objects.');11}1213protected function newDocumentationPages(PhabricatorUser $viewer) {14$rows = array();15foreach ($this->getConduitEdgeTypeMap() as $key => $type) {16$inverse_constant = $type->getInverseEdgeConstant();17if ($inverse_constant) {18$inverse_type = PhabricatorEdgeType::getByConstant($inverse_constant);19$inverse = $inverse_type->getConduitKey();20} else {21$inverse = null;22}2324$rows[] = array(25$key,26$type->getConduitName(),27$inverse,28new PHUIRemarkupView($viewer, $type->getConduitDescription()),29);30}3132$types_table = id(new AphrontTableView($rows))33->setHeaders(34array(35pht('Constant'),36pht('Name'),37pht('Inverse'),38pht('Description'),39))40->setColumnClasses(41array(42'mono',43'pri',44'mono',45'wide',46));474849return array(50$this->newDocumentationBoxPage($viewer, pht('Edge Types'), $types_table)51->setAnchor('types'),52);53}5455protected function defineParamTypes() {56return array(57'sourcePHIDs' => 'list<phid>',58'types' => 'list<const>',59'destinationPHIDs' => 'optional list<phid>',60) + $this->getPagerParamTypes();61}6263protected function defineReturnType() {64return 'list<dict>';65}6667protected function defineErrorTypes() {68return array();69}7071protected function execute(ConduitAPIRequest $request) {72$viewer = $request->getUser();73$pager = $this->newPager($request);7475$source_phids = $request->getValue('sourcePHIDs', array());76$edge_types = $request->getValue('types', array());77$destination_phids = $request->getValue('destinationPHIDs', array());7879$object_query = id(new PhabricatorObjectQuery())80->setViewer($viewer)81->withNames($source_phids);8283$object_query->execute();84$objects = $object_query->getNamedResults();85foreach ($source_phids as $phid) {86if (empty($objects[$phid])) {87throw new Exception(88pht(89'Source PHID "%s" does not identify a valid object, or you do '.90'not have permission to view it.',91$phid));92}93}94$source_phids = mpull($objects, 'getPHID');9596if (!$edge_types) {97throw new Exception(98pht(99'Edge search must specify a nonempty list of edge types.'));100}101102$edge_map = $this->getConduitEdgeTypeMap();103104$constant_map = array();105$edge_constants = array();106foreach ($edge_types as $edge_type) {107if (!isset($edge_map[$edge_type])) {108throw new Exception(109pht(110'Edge type "%s" is not a recognized edge type.',111$edge_type));112}113114$constant = $edge_map[$edge_type]->getEdgeConstant();115116$edge_constants[] = $constant;117$constant_map[$constant] = $edge_type;118}119120$edge_query = id(new PhabricatorEdgeObjectQuery())121->setViewer($viewer)122->withSourcePHIDs($source_phids)123->withEdgeTypes($edge_constants);124125if ($destination_phids) {126$edge_query->withDestinationPHIDs($destination_phids);127}128129$edge_objects = $edge_query->executeWithCursorPager($pager);130131$edges = array();132foreach ($edge_objects as $edge_object) {133$edges[] = array(134'sourcePHID' => $edge_object->getSourcePHID(),135'edgeType' => $constant_map[$edge_object->getEdgeType()],136'destinationPHID' => $edge_object->getDestinationPHID(),137);138}139140$results = array(141'data' => $edges,142);143144return $this->addPagerResults($results, $pager);145}146147private function getConduitEdgeTypeMap() {148$types = PhabricatorEdgeType::getAllTypes();149150$map = array();151foreach ($types as $type) {152$key = $type->getConduitKey();153if ($key === null) {154continue;155}156157$map[$key] = $type;158}159160ksort($map);161162return $map;163}164}165166167