2012-03-06 04:51:16 +01:00
|
|
|
<?php
|
|
|
|
|
2015-06-15 10:02:26 +02:00
|
|
|
final class PhabricatorJumpNavHandler extends Phobject {
|
2013-10-02 22:11:56 +02:00
|
|
|
|
|
|
|
public static function getJumpResponse(PhabricatorUser $viewer, $jump) {
|
2012-03-06 04:51:16 +01:00
|
|
|
$jump = trim($jump);
|
|
|
|
|
|
|
|
$patterns = array(
|
2015-03-02 23:32:08 +01:00
|
|
|
'/^a$/i' => 'uri:/audit/',
|
|
|
|
'/^f$/i' => 'uri:/feed/',
|
|
|
|
'/^d$/i' => 'uri:/differential/',
|
|
|
|
'/^r$/i' => 'uri:/diffusion/',
|
|
|
|
'/^t$/i' => 'uri:/maniphest/',
|
|
|
|
'/^p$/i' => 'uri:/project/',
|
|
|
|
'/^u$/i' => 'uri:/people/',
|
|
|
|
'/^p\s+(.+)$/i' => 'project',
|
|
|
|
'/^u\s+(\S+)$/i' => 'user',
|
|
|
|
'/^task:\s*(.+)/i' => 'create-task',
|
|
|
|
'/^(?:s)\s+(\S+)/i' => 'find-symbol',
|
|
|
|
'/^r\s+(.+)$/i' => 'find-repository',
|
2012-03-06 04:51:16 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($patterns as $pattern => $effect) {
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match($pattern, $jump, $matches)) {
|
|
|
|
if (!strncmp($effect, 'uri:', 4)) {
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI(substr($effect, 4));
|
|
|
|
} else {
|
|
|
|
switch ($effect) {
|
|
|
|
case 'user':
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/p/'.$matches[1].'/');
|
|
|
|
case 'project':
|
2012-06-17 17:46:22 +02:00
|
|
|
$project = self::findCloselyNamedProject($matches[1]);
|
2012-03-06 04:51:16 +01:00
|
|
|
if ($project) {
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/project/view/'.$project->getID().'/');
|
|
|
|
} else {
|
|
|
|
$jump = $matches[1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'find-symbol':
|
2012-08-07 18:29:34 +02:00
|
|
|
$context = '';
|
|
|
|
$symbol = $matches[1];
|
|
|
|
$parts = array();
|
|
|
|
if (preg_match('/(.*)(?:\\.|::|->)(.*)/', $symbol, $parts)) {
|
|
|
|
$context = '&context='.phutil_escape_uri($parts[1]);
|
|
|
|
$symbol = $parts[2];
|
|
|
|
}
|
2012-03-06 04:51:16 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
2012-08-07 18:29:34 +02:00
|
|
|
->setURI("/diffusion/symbol/$symbol/?jump=true$context");
|
2013-12-05 23:26:38 +01:00
|
|
|
case 'find-repository':
|
|
|
|
$name = $matches[1];
|
|
|
|
$repositories = id(new PhabricatorRepositoryQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withNameContains($name)
|
|
|
|
->execute();
|
|
|
|
if (count($repositories) == 1) {
|
|
|
|
// Just one match, jump to repository.
|
|
|
|
$uri = '/diffusion/'.head($repositories)->getCallsign().'/';
|
|
|
|
} else {
|
|
|
|
// More than one match, jump to search.
|
|
|
|
$uri = urisprintf('/diffusion/?order=name&name=%s', $name);
|
|
|
|
}
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
2012-03-06 04:51:16 +01:00
|
|
|
case 'create-task':
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/maniphest/task/create/?title='
|
|
|
|
.phutil_escape_uri($matches[1]));
|
|
|
|
default:
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new Exception(pht("Unknown jump effect '%s'!", $effect));
|
2012-03-06 04:51:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 22:11:56 +02:00
|
|
|
// If none of the patterns matched, look for an object by name.
|
|
|
|
$objects = id(new PhabricatorObjectQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withNames(array($jump))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
if (count($objects) == 1) {
|
|
|
|
$handle = id(new PhabricatorHandleQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withPHIDs(mpull($objects, 'getPHID'))
|
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($handle->getURI());
|
|
|
|
}
|
|
|
|
|
2012-03-06 04:51:16 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function findCloselyNamedProject($name) {
|
|
|
|
$project = id(new PhabricatorProject())->loadOneWhere(
|
|
|
|
'name = %s',
|
2012-06-12 01:22:08 +02:00
|
|
|
$name);
|
2012-03-06 04:51:16 +01:00
|
|
|
if ($project) {
|
|
|
|
return $project;
|
|
|
|
} else { // no exact match, try a fuzzy match
|
|
|
|
$projects = id(new PhabricatorProject())->loadAllWhere(
|
|
|
|
'name LIKE %~',
|
|
|
|
$name);
|
|
|
|
if ($projects) {
|
|
|
|
$min_name_length = PHP_INT_MAX;
|
|
|
|
$best_project = null;
|
|
|
|
foreach ($projects as $project) {
|
|
|
|
$name_length = strlen($project->getName());
|
|
|
|
if ($name_length <= $min_name_length) {
|
|
|
|
$min_name_length = $name_length;
|
|
|
|
$best_project = $project;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $best_project;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|