1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-28 01:32:42 +01:00
phorge-phorge/src/applications/project/engineextension/ProjectDatasourceEngineExtension.php
epriestley 4bccb1547d Modularize the "jump nav" behaviors in global search
Summary: Depends on D19087. Ref T13079. This still doesn't feel like the most clean, general system in the world, but is a step forward from hard-coded `switch()` stuff.

Test Plan:
- Jumped to `r`.
- Jumped to `a`.
- Jumped to `r poe` (multiple results).
- Jumped to `r poetry` (one result).
- Jumped to `r syzygy` (no results).
- Jumped to `p`.
- Jumped to `p robot` (multiple results); `p assessment` (one result).
  - The behavior for `p <string>` has changed slightly but should be more powerful now (it's consistent with `r <string>`).
- Jumped to `s <symbol>` and `s <context>-><symbol>`.
- Jumped to `d`.
- Jumped to `f`.
- Jumped to `t`.
- Jumped to `T123`, `D123`, `@dog`, `PHID-DREV-abcd`, etc.

Maniphest Tasks: T13079

Differential Revision: https://secure.phabricator.com/D19088
2018-02-14 18:08:07 -08:00

57 lines
1.5 KiB
PHP

<?php
final class ProjectDatasourceEngineExtension
extends PhabricatorDatasourceEngineExtension {
public function newQuickSearchDatasources() {
return array(
new PhabricatorProjectDatasource(),
);
}
public function newJumpURI($query) {
$viewer = $this->getViewer();
// Send "p" to Projects.
if (preg_match('/^p\z/i', $query)) {
return '/diffusion/';
}
// Send "p <string>" to a search for similar projects.
$matches = null;
if (preg_match('/^p\s+(.+)\z/i', $query, $matches)) {
$raw_query = $matches[1];
$engine = id(new PhabricatorProject())
->newFerretEngine();
$compiler = id(new PhutilSearchQueryCompiler())
->setEnableFunctions(true);
$raw_tokens = $compiler->newTokens($raw_query);
$fulltext_tokens = array();
foreach ($raw_tokens as $raw_token) {
$fulltext_token = id(new PhabricatorFulltextToken())
->setToken($raw_token);
$fulltext_tokens[] = $fulltext_token;
}
$projects = id(new PhabricatorProjectQuery())
->setViewer($viewer)
->withFerretConstraint($engine, $fulltext_tokens)
->execute();
if (count($projects) == 1) {
// Just one match, jump to project.
return head($projects)->getURI();
} else {
// More than one match, jump to search.
return urisprintf(
'/project/?order=relevance&query=%s#R',
$raw_query);
}
}
return null;
}
}