| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | final class PhabricatorRepositorySearchEngine | 
					
						
							|  |  |  |   extends PhabricatorApplicationSearchEngine { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 10:08:37 -07:00
										 |  |  |   public function getApplicationClassName() { | 
					
						
							|  |  |  |     return 'PhabricatorApplicationDiffusion'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |   public function buildSavedQueryFromRequest(AphrontRequest $request) { | 
					
						
							|  |  |  |     $saved = new PhabricatorSavedQuery(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $saved->setParameter('callsigns', $request->getStrList('callsigns')); | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |     $saved->setParameter('status', $request->getStr('status')); | 
					
						
							| 
									
										
										
										
											2013-09-08 11:49:06 -07:00
										 |  |  |     $saved->setParameter('order', $request->getStr('order')); | 
					
						
							| 
									
										
										
										
											2014-01-10 09:12:50 -08:00
										 |  |  |     $saved->setParameter('hosted', $request->getStr('hosted')); | 
					
						
							| 
									
										
										
										
											2013-09-19 11:56:48 -07:00
										 |  |  |     $saved->setParameter('types', $request->getArr('types')); | 
					
						
							| 
									
										
										
										
											2013-12-05 14:26:38 -08:00
										 |  |  |     $saved->setParameter('name', $request->getStr('name')); | 
					
						
							| 
									
										
										
										
											2014-01-30 11:45:43 -08:00
										 |  |  |     $saved->setParameter('anyProjectPHIDs', $request->getArr('anyProjects')); | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $saved; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { | 
					
						
							|  |  |  |     $query = id(new PhabricatorRepositoryQuery()) | 
					
						
							| 
									
										
										
										
											2014-01-03 12:24:09 -08:00
										 |  |  |       ->needProjectPHIDs(true) | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |       ->needCommitCounts(true) | 
					
						
							|  |  |  |       ->needMostRecentCommits(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $callsigns = $saved->getParameter('callsigns'); | 
					
						
							|  |  |  |     if ($callsigns) { | 
					
						
							|  |  |  |       $query->withCallsigns($callsigns); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |     $status = $saved->getParameter('status'); | 
					
						
							|  |  |  |     $status = idx($this->getStatusValues(), $status); | 
					
						
							|  |  |  |     if ($status) { | 
					
						
							|  |  |  |       $query->withStatus($status); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-08 11:49:06 -07:00
										 |  |  |     $order = $saved->getParameter('order'); | 
					
						
							|  |  |  |     $order = idx($this->getOrderValues(), $order); | 
					
						
							|  |  |  |     if ($order) { | 
					
						
							|  |  |  |       $query->setOrder($order); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       $query->setOrder(head($this->getOrderValues())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 09:12:50 -08:00
										 |  |  |     $hosted = $saved->getParameter('hosted'); | 
					
						
							|  |  |  |     $hosted = idx($this->getHostedValues(), $hosted); | 
					
						
							|  |  |  |     if ($hosted) { | 
					
						
							|  |  |  |       $query->withHosted($hosted); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 11:56:48 -07:00
										 |  |  |     $types = $saved->getParameter('types'); | 
					
						
							|  |  |  |     if ($types) { | 
					
						
							|  |  |  |       $query->withTypes($types); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 14:26:38 -08:00
										 |  |  |     $name = $saved->getParameter('name'); | 
					
						
							|  |  |  |     if (strlen($name)) { | 
					
						
							|  |  |  |       $query->withNameContains($name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 11:45:43 -08:00
										 |  |  |     $any_project_phids = $saved->getParameter('anyProjectPHIDs'); | 
					
						
							|  |  |  |     if ($any_project_phids) { | 
					
						
							|  |  |  |       $query->withAnyProjects($any_project_phids); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |     return $query; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function buildSearchForm( | 
					
						
							|  |  |  |     AphrontFormView $form, | 
					
						
							|  |  |  |     PhabricatorSavedQuery $saved_query) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $callsigns = $saved_query->getParameter('callsigns', array()); | 
					
						
							| 
									
										
										
										
											2013-09-19 11:56:48 -07:00
										 |  |  |     $types = $saved_query->getParameter('types', array()); | 
					
						
							|  |  |  |     $types = array_fuse($types); | 
					
						
							| 
									
										
										
										
											2013-12-05 14:26:38 -08:00
										 |  |  |     $name = $saved_query->getParameter('name'); | 
					
						
							| 
									
										
										
										
											2014-01-30 11:45:43 -08:00
										 |  |  |     $any_project_phids = $saved_query->getParameter('anyProjectPHIDs', array()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($any_project_phids) { | 
					
						
							|  |  |  |       $any_project_handles = id(new PhabricatorHandleQuery()) | 
					
						
							|  |  |  |         ->setViewer($this->requireViewer()) | 
					
						
							|  |  |  |         ->withPHIDs($any_project_phids) | 
					
						
							|  |  |  |         ->execute(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       $any_project_handles = array(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $form | 
					
						
							|  |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormTextControl()) | 
					
						
							|  |  |  |           ->setName('callsigns') | 
					
						
							|  |  |  |           ->setLabel(pht('Callsigns')) | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |           ->setValue(implode(', ', $callsigns))) | 
					
						
							| 
									
										
										
										
											2013-12-05 14:26:38 -08:00
										 |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormTextControl()) | 
					
						
							|  |  |  |           ->setName('name') | 
					
						
							|  |  |  |           ->setLabel(pht('Name Contains')) | 
					
						
							|  |  |  |           ->setValue($name)) | 
					
						
							| 
									
										
										
										
											2014-01-30 11:45:43 -08:00
										 |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormTokenizerControl()) | 
					
						
							|  |  |  |           ->setDatasource('/typeahead/common/projects/') | 
					
						
							|  |  |  |           ->setName('anyProjects') | 
					
						
							|  |  |  |           ->setLabel(pht('In Any Project')) | 
					
						
							|  |  |  |           ->setValue($any_project_handles)) | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormSelectControl()) | 
					
						
							|  |  |  |           ->setName('status') | 
					
						
							|  |  |  |           ->setLabel(pht('Status')) | 
					
						
							|  |  |  |           ->setValue($saved_query->getParameter('status')) | 
					
						
							| 
									
										
										
										
											2014-01-10 09:12:50 -08:00
										 |  |  |           ->setOptions($this->getStatusOptions())) | 
					
						
							|  |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormSelectControl()) | 
					
						
							|  |  |  |           ->setName('hosted') | 
					
						
							|  |  |  |           ->setLabel(pht('Hosted')) | 
					
						
							|  |  |  |           ->setValue($saved_query->getParameter('hosted')) | 
					
						
							|  |  |  |           ->setOptions($this->getHostedOptions())); | 
					
						
							| 
									
										
										
										
											2013-09-19 11:56:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $type_control = id(new AphrontFormCheckboxControl()) | 
					
						
							|  |  |  |       ->setLabel(pht('Types')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $all_types = PhabricatorRepositoryType::getAllRepositoryTypes(); | 
					
						
							|  |  |  |     foreach ($all_types as $key => $name) { | 
					
						
							|  |  |  |       $type_control->addCheckbox( | 
					
						
							|  |  |  |         'types[]', | 
					
						
							|  |  |  |         $key, | 
					
						
							|  |  |  |         $name, | 
					
						
							|  |  |  |         isset($types[$key])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $form | 
					
						
							|  |  |  |       ->appendChild($type_control) | 
					
						
							| 
									
										
										
										
											2013-09-08 11:49:06 -07:00
										 |  |  |       ->appendChild( | 
					
						
							|  |  |  |         id(new AphrontFormSelectControl()) | 
					
						
							|  |  |  |           ->setName('order') | 
					
						
							|  |  |  |           ->setLabel(pht('Order')) | 
					
						
							|  |  |  |           ->setValue($saved_query->getParameter('order')) | 
					
						
							|  |  |  |           ->setOptions($this->getOrderOptions())); | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   protected function getURI($path) { | 
					
						
							|  |  |  |     return '/diffusion/'.$path; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function getBuiltinQueryNames() { | 
					
						
							|  |  |  |     $names = array( | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |       'active' => pht('Active Repositories'), | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |       'all' => pht('All Repositories'), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $names; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function buildSavedQueryFromBuiltin($query_key) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $query = $this->newSavedQuery(); | 
					
						
							|  |  |  |     $query->setQueryKey($query_key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch ($query_key) { | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |       case 'active': | 
					
						
							|  |  |  |         return $query->setParameter('status', 'open'); | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  |       case 'all': | 
					
						
							|  |  |  |         return $query; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return parent::buildSavedQueryFromBuiltin($query_key); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:23 -07:00
										 |  |  |   private function getStatusOptions() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       '' => pht('Active and Inactive Repositories'), | 
					
						
							|  |  |  |       'open' => pht('Active Repositories'), | 
					
						
							|  |  |  |       'closed' => pht('Inactive Repositories'), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function getStatusValues() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       '' => PhabricatorRepositoryQuery::STATUS_ALL, | 
					
						
							|  |  |  |       'open' => PhabricatorRepositoryQuery::STATUS_OPEN, | 
					
						
							|  |  |  |       'closed' => PhabricatorRepositoryQuery::STATUS_CLOSED, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-08 11:49:06 -07:00
										 |  |  |   private function getOrderOptions() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       'committed' => pht('Most Recent Commit'), | 
					
						
							|  |  |  |       'name' => pht('Name'), | 
					
						
							|  |  |  |       'callsign' => pht('Callsign'), | 
					
						
							|  |  |  |       'created' => pht('Date Created'), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function getOrderValues() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       'committed' => PhabricatorRepositoryQuery::ORDER_COMMITTED, | 
					
						
							|  |  |  |       'name' => PhabricatorRepositoryQuery::ORDER_NAME, | 
					
						
							|  |  |  |       'callsign' => PhabricatorRepositoryQuery::ORDER_CALLSIGN, | 
					
						
							|  |  |  |       'created' => PhabricatorRepositoryQuery::ORDER_CREATED, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 09:12:50 -08:00
										 |  |  |   private function getHostedOptions() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       '' => pht('Hosted and Remote Repositories'), | 
					
						
							|  |  |  |       'phabricator' => pht('Hosted Repositories'), | 
					
						
							|  |  |  |       'remote' => pht('Remote Repositories'), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function getHostedValues() { | 
					
						
							|  |  |  |     return array( | 
					
						
							|  |  |  |       '' => PhabricatorRepositoryQuery::HOSTED_ALL, | 
					
						
							|  |  |  |       'phabricator' => PhabricatorRepositoryQuery::HOSTED_PHABRICATOR, | 
					
						
							|  |  |  |       'remote' => PhabricatorRepositoryQuery::HOSTED_REMOTE, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-09-08 11:49:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 10:08:37 -07:00
										 |  |  |   protected function getRequiredHandlePHIDsForResultList( | 
					
						
							|  |  |  |     array $repositories, | 
					
						
							|  |  |  |     PhabricatorSavedQuery $query) { | 
					
						
							|  |  |  |     return array_mergev(mpull($repositories, 'getProjectPHIDs')); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   protected function renderResultList( | 
					
						
							|  |  |  |     array $repositories, | 
					
						
							|  |  |  |     PhabricatorSavedQuery $query, | 
					
						
							|  |  |  |     array $handles) { | 
					
						
							|  |  |  |     assert_instances_of($repositories, 'PhabricatorRepository'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $viewer = $this->requireViewer();; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $list = new PHUIObjectItemListView(); | 
					
						
							|  |  |  |     foreach ($repositories as $repository) { | 
					
						
							|  |  |  |       $id = $repository->getID(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $item = id(new PHUIObjectItemView()) | 
					
						
							|  |  |  |         ->setUser($viewer) | 
					
						
							|  |  |  |         ->setHeader($repository->getName()) | 
					
						
							|  |  |  |         ->setObjectName('r'.$repository->getCallsign()) | 
					
						
							|  |  |  |         ->setHref($this->getApplicationURI($repository->getCallsign().'/')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $commit = $repository->getMostRecentCommit(); | 
					
						
							|  |  |  |       if ($commit) { | 
					
						
							|  |  |  |         $commit_link = DiffusionView::linkCommit( | 
					
						
							|  |  |  |             $repository, | 
					
						
							|  |  |  |             $commit->getCommitIdentifier(), | 
					
						
							|  |  |  |             $commit->getSummary()); | 
					
						
							|  |  |  |         $item->setSubhead($commit_link); | 
					
						
							|  |  |  |         $item->setEpoch($commit->getEpoch()); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $item->addIcon( | 
					
						
							|  |  |  |         'none', | 
					
						
							|  |  |  |         PhabricatorRepositoryType::getNameForRepositoryType( | 
					
						
							|  |  |  |           $repository->getVersionControlSystem())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $size = $repository->getCommitCount(); | 
					
						
							|  |  |  |       if ($size) { | 
					
						
							|  |  |  |         $history_uri = DiffusionRequest::generateDiffusionURI( | 
					
						
							|  |  |  |           array( | 
					
						
							|  |  |  |             'callsign' => $repository->getCallsign(), | 
					
						
							|  |  |  |             'action' => 'history', | 
					
						
							|  |  |  |           )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $item->addAttribute( | 
					
						
							|  |  |  |           phutil_tag( | 
					
						
							|  |  |  |             'a', | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |               'href' => $history_uri, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             pht('%s Commit(s)', new PhutilNumber($size)))); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         $item->addAttribute(pht('No Commits')); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $project_handles = array_select_keys( | 
					
						
							|  |  |  |         $handles, | 
					
						
							|  |  |  |         $repository->getProjectPHIDs()); | 
					
						
							|  |  |  |       if ($project_handles) { | 
					
						
							|  |  |  |         $item->addAttribute( | 
					
						
							|  |  |  |           id(new ManiphestTaskProjectsView()) | 
					
						
							|  |  |  |             ->setHandles($project_handles)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!$repository->isTracked()) { | 
					
						
							|  |  |  |         $item->setDisabled(true); | 
					
						
							|  |  |  |         $item->addIcon('disable-grey', pht('Inactive')); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $list->addItem($item); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $list; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-10 15:26:08 -07:00
										 |  |  | } |