|
| 1 | +<?php |
| 2 | + |
| 3 | +final class EdgeSearchConduitAPIMethod |
| 4 | + extends ConduitAPIMethod { |
| 5 | + |
| 6 | + public function getAPIMethodName() { |
| 7 | + return 'edge.search'; |
| 8 | + } |
| 9 | + |
| 10 | + public function getMethodDescription() { |
| 11 | + return pht('Read edge relationships between objects.'); |
| 12 | + } |
| 13 | + |
| 14 | + public function getMethodDocumentation() { |
| 15 | + $viewer = $this->getViewer(); |
| 16 | + |
| 17 | + $rows = array(); |
| 18 | + foreach ($this->getConduitEdgeTypeMap() as $key => $type) { |
| 19 | + $inverse_constant = $type->getInverseEdgeConstant(); |
| 20 | + if ($inverse_constant) { |
| 21 | + $inverse_type = PhabricatorEdgeType::getByConstant($inverse_constant); |
| 22 | + $inverse = $inverse_type->getConduitKey(); |
| 23 | + } else { |
| 24 | + $inverse = null; |
| 25 | + } |
| 26 | + |
| 27 | + $rows[] = array( |
| 28 | + $key, |
| 29 | + $type->getConduitName(), |
| 30 | + $inverse, |
| 31 | + new PHUIRemarkupView($viewer, $type->getConduitDescription()), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + $types_table = id(new AphrontTableView($rows)) |
| 36 | + ->setHeaders( |
| 37 | + array( |
| 38 | + pht('Constant'), |
| 39 | + pht('Name'), |
| 40 | + pht('Inverse'), |
| 41 | + pht('Description'), |
| 42 | + )) |
| 43 | + ->setColumnClasses( |
| 44 | + array( |
| 45 | + 'mono', |
| 46 | + 'pri', |
| 47 | + 'mono', |
| 48 | + 'wide', |
| 49 | + )); |
| 50 | + |
| 51 | + return id(new PHUIObjectBoxView()) |
| 52 | + ->setHeaderText(pht('Edge Types')) |
| 53 | + ->setTable($types_table); |
| 54 | + } |
| 55 | + |
| 56 | + public function getMethodStatus() { |
| 57 | + return self::METHOD_STATUS_UNSTABLE; |
| 58 | + } |
| 59 | + |
| 60 | + public function getMethodStatusDescription() { |
| 61 | + return pht('This method is new and experimental.'); |
| 62 | + } |
| 63 | + |
| 64 | + protected function defineParamTypes() { |
| 65 | + return array( |
| 66 | + 'sourcePHIDs' => 'list<phid>', |
| 67 | + 'types' => 'list<const>', |
| 68 | + 'destinationPHIDs' => 'optional list<phid>', |
| 69 | + ) + $this->getPagerParamTypes(); |
| 70 | + } |
| 71 | + |
| 72 | + protected function defineReturnType() { |
| 73 | + return 'list<dict>'; |
| 74 | + } |
| 75 | + |
| 76 | + protected function defineErrorTypes() { |
| 77 | + return array(); |
| 78 | + } |
| 79 | + |
| 80 | + protected function execute(ConduitAPIRequest $request) { |
| 81 | + $viewer = $request->getUser(); |
| 82 | + $pager = $this->newPager($request); |
| 83 | + |
| 84 | + $source_phids = $request->getValue('sourcePHIDs', array()); |
| 85 | + $edge_types = $request->getValue('types', array()); |
| 86 | + $destination_phids = $request->getValue('destinationPHIDs', array()); |
| 87 | + |
| 88 | + $object_query = id(new PhabricatorObjectQuery()) |
| 89 | + ->setViewer($viewer) |
| 90 | + ->withNames($source_phids); |
| 91 | + |
| 92 | + $object_query->execute(); |
| 93 | + $objects = $object_query->getNamedResults(); |
| 94 | + foreach ($source_phids as $phid) { |
| 95 | + if (empty($objects[$phid])) { |
| 96 | + throw new Exception( |
| 97 | + pht( |
| 98 | + 'Source PHID "%s" does not identify a valid object, or you do '. |
| 99 | + 'not have permission to view it.', |
| 100 | + $phid)); |
| 101 | + } |
| 102 | + } |
| 103 | + $source_phids = mpull($objects, 'getPHID'); |
| 104 | + |
| 105 | + if (!$edge_types) { |
| 106 | + throw new Exception( |
| 107 | + pht( |
| 108 | + 'Edge search must specify a nonempty list of edge types.')); |
| 109 | + } |
| 110 | + |
| 111 | + $edge_map = $this->getConduitEdgeTypeMap(); |
| 112 | + |
| 113 | + $constant_map = array(); |
| 114 | + $edge_constants = array(); |
| 115 | + foreach ($edge_types as $edge_type) { |
| 116 | + if (!isset($edge_map[$edge_type])) { |
| 117 | + throw new Exception( |
| 118 | + pht( |
| 119 | + 'Edge type "%s" is not a recognized edge type.', |
| 120 | + $edge_type)); |
| 121 | + } |
| 122 | + |
| 123 | + $constant = $edge_map[$edge_type]->getEdgeConstant(); |
| 124 | + |
| 125 | + $edge_constants[] = $constant; |
| 126 | + $constant_map[$constant] = $edge_type; |
| 127 | + } |
| 128 | + |
| 129 | + $edge_query = id(new PhabricatorEdgeObjectQuery()) |
| 130 | + ->setViewer($viewer) |
| 131 | + ->withSourcePHIDs($source_phids) |
| 132 | + ->withEdgeTypes($edge_constants); |
| 133 | + |
| 134 | + if ($destination_phids) { |
| 135 | + $edge_query->withDestinationPHIDs($destination_phids); |
| 136 | + } |
| 137 | + |
| 138 | + $edge_objects = $edge_query->executeWithCursorPager($pager); |
| 139 | + |
| 140 | + $edges = array(); |
| 141 | + foreach ($edge_objects as $edge_object) { |
| 142 | + $edges[] = array( |
| 143 | + 'sourcePHID' => $edge_object->getSourcePHID(), |
| 144 | + 'edgeType' => $constant_map[$edge_object->getEdgeType()], |
| 145 | + 'destinationPHID' => $edge_object->getDestinationPHID(), |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + $results = array( |
| 150 | + 'data' => $edges, |
| 151 | + ); |
| 152 | + |
| 153 | + return $this->addPagerResults($results, $pager); |
| 154 | + } |
| 155 | + |
| 156 | + private function getConduitEdgeTypeMap() { |
| 157 | + $types = PhabricatorEdgeType::getAllTypes(); |
| 158 | + |
| 159 | + $map = array(); |
| 160 | + foreach ($types as $type) { |
| 161 | + $key = $type->getConduitKey(); |
| 162 | + if ($key === null) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + $map[$key] = $type; |
| 167 | + } |
| 168 | + |
| 169 | + ksort($map); |
| 170 | + |
| 171 | + return $map; |
| 172 | + } |
| 173 | +} |
0 commit comments