2013-04-14 15:53:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an abstract search engine for an application. It supports
|
|
|
|
* creating and storing saved queries.
|
|
|
|
*
|
2014-05-07 23:56:30 +02:00
|
|
|
* @task construct Constructing Engines
|
2014-05-08 18:18:02 +02:00
|
|
|
* @task app Applications
|
2014-05-07 23:56:30 +02:00
|
|
|
* @task builtin Builtin Queries
|
|
|
|
* @task uri Query URIs
|
|
|
|
* @task dates Date Filters
|
2015-04-13 00:16:55 +02:00
|
|
|
* @task order Result Ordering
|
2014-05-07 23:56:30 +02:00
|
|
|
* @task read Reading Utilities
|
2014-05-08 17:24:47 +02:00
|
|
|
* @task exec Paging and Executing Queries
|
|
|
|
* @task render Rendering Results
|
2013-04-14 15:53:20 +02:00
|
|
|
*/
|
|
|
|
abstract class PhabricatorApplicationSearchEngine {
|
|
|
|
|
2014-05-08 18:18:02 +02:00
|
|
|
private $application;
|
2013-05-27 22:42:18 +02:00
|
|
|
private $viewer;
|
2013-05-31 02:32:12 +02:00
|
|
|
private $errors = array();
|
Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.
This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.
Broadly, custom fields may elect to:
- build indicies when objects are updated;
- populate ApplicationSearch forms with new controls;
- read inputs entered into those controls out of the request; and
- apply constraints to search queries.
Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.
Test Plan:
I added a new searchable "Company" field to People:
{F58229}
This also cleaned up the disable/reorder view a little bit:
{F58230}
As it did before, this field appears on the edit screen:
{F58231}
However, because it has `search`, it also appears on the search screen:
{F58232}
When queried, it returns the expected results:
{F58233}
And the actually good bit of all this is that the query can take advantage of indexes:
mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | appsearch_0 | ref | key_join,key_find | key_find | 232 | const,const | 1 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | user | eq_ref | phid | phid | 194 | phabricator2_user.appsearch_0.objectPHID | 1 | |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T418, T1703, T2625, T3794
Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 22:44:34 +02:00
|
|
|
private $customFields = false;
|
2014-05-08 19:08:37 +02:00
|
|
|
private $request;
|
2014-06-25 00:59:06 +02:00
|
|
|
private $context;
|
|
|
|
|
|
|
|
const CONTEXT_LIST = 'list';
|
|
|
|
const CONTEXT_PANEL = 'panel';
|
2013-05-27 22:42:18 +02:00
|
|
|
|
|
|
|
public function setViewer(PhabricatorUser $viewer) {
|
|
|
|
$this->viewer = $viewer;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function requireViewer() {
|
|
|
|
if (!$this->viewer) {
|
2015-05-13 23:53:52 +02:00
|
|
|
throw new PhutilInvalidStateException('setViewer');
|
2013-05-27 22:42:18 +02:00
|
|
|
}
|
|
|
|
return $this->viewer;
|
|
|
|
}
|
|
|
|
|
2014-06-25 00:59:06 +02:00
|
|
|
public function setContext($context) {
|
|
|
|
$this->context = $context;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPanelContext() {
|
|
|
|
return ($this->context == self::CONTEXT_PANEL);
|
|
|
|
}
|
|
|
|
|
2015-02-11 22:43:59 +01:00
|
|
|
public function canUseInPanelContext() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:42:05 +02:00
|
|
|
public function saveQuery(PhabricatorSavedQuery $query) {
|
|
|
|
$query->setEngineClassName(get_class($this));
|
|
|
|
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
try {
|
|
|
|
$query->save();
|
2014-08-05 23:51:21 +02:00
|
|
|
} catch (AphrontDuplicateKeyQueryException $ex) {
|
2014-05-20 20:42:05 +02:00
|
|
|
// Ignore, this is just a repeated search.
|
|
|
|
}
|
|
|
|
unset($unguarded);
|
|
|
|
}
|
|
|
|
|
2013-04-14 15:53:20 +02:00
|
|
|
/**
|
|
|
|
* Create a saved query object from the request.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest The search request.
|
|
|
|
* @return PhabricatorSavedQuery
|
|
|
|
*/
|
|
|
|
abstract public function buildSavedQueryFromRequest(
|
|
|
|
AphrontRequest $request);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the saved query.
|
|
|
|
*
|
|
|
|
* @param PhabricatorSavedQuery The saved query to operate on.
|
|
|
|
* @return The result of the query.
|
|
|
|
*/
|
|
|
|
abstract public function buildQueryFromSavedQuery(
|
|
|
|
PhabricatorSavedQuery $saved);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the search form using the request.
|
|
|
|
*
|
2013-05-27 22:43:47 +02:00
|
|
|
* @param AphrontFormView Form to populate.
|
2013-04-14 15:53:20 +02:00
|
|
|
* @param PhabricatorSavedQuery The query from which to build the form.
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-27 22:43:47 +02:00
|
|
|
abstract public function buildSearchForm(
|
|
|
|
AphrontFormView $form,
|
|
|
|
PhabricatorSavedQuery $query);
|
2013-04-14 15:53:20 +02:00
|
|
|
|
2013-05-31 02:32:12 +02:00
|
|
|
public function getErrors() {
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addError($error) {
|
|
|
|
$this->errors[] = $error;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-05-27 22:41:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an application URI corresponding to the results page of a query.
|
|
|
|
* Normally, this is something like `/application/query/QUERYKEY/`.
|
|
|
|
*
|
2013-05-27 22:43:02 +02:00
|
|
|
* @param string The query key to build a URI for.
|
|
|
|
* @return string URI where the query can be executed.
|
2013-05-27 22:42:44 +02:00
|
|
|
* @task uri
|
2013-05-27 22:41:39 +02:00
|
|
|
*/
|
2013-05-30 23:09:02 +02:00
|
|
|
public function getQueryResultsPageURI($query_key) {
|
|
|
|
return $this->getURI('query/'.$query_key.'/');
|
|
|
|
}
|
2013-05-27 22:42:18 +02:00
|
|
|
|
|
|
|
|
2013-05-27 22:42:44 +02:00
|
|
|
/**
|
|
|
|
* Return an application URI for query management. This is used when, e.g.,
|
|
|
|
* a query deletion operation is cancelled.
|
|
|
|
*
|
|
|
|
* @return string URI where queries can be managed.
|
|
|
|
* @task uri
|
|
|
|
*/
|
2013-05-30 23:09:02 +02:00
|
|
|
public function getQueryManagementURI() {
|
|
|
|
return $this->getURI('query/edit/');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the URI to a path within the application. Used to construct default
|
|
|
|
* URIs for management and results.
|
|
|
|
*
|
|
|
|
* @return string URI to path.
|
|
|
|
* @task uri
|
|
|
|
*/
|
|
|
|
abstract protected function getURI($path);
|
2013-05-27 22:42:44 +02:00
|
|
|
|
|
|
|
|
2014-06-12 22:22:20 +02:00
|
|
|
/**
|
|
|
|
* Return a human readable description of the type of objects this query
|
|
|
|
* searches for.
|
|
|
|
*
|
|
|
|
* For example, "Tasks" or "Commits".
|
|
|
|
*
|
|
|
|
* @return string Human-readable description of what this engine is used to
|
|
|
|
* find.
|
|
|
|
*/
|
|
|
|
abstract public function getResultTypeDescription();
|
|
|
|
|
|
|
|
|
2013-05-27 22:42:18 +02:00
|
|
|
public function newSavedQuery() {
|
|
|
|
return id(new PhabricatorSavedQuery())
|
|
|
|
->setEngineClassName(get_class($this));
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:30:36 +01:00
|
|
|
public function addNavigationItems(PHUIListView $menu) {
|
2013-05-30 23:09:02 +02:00
|
|
|
$viewer = $this->requireViewer();
|
|
|
|
|
2013-12-26 21:30:36 +01:00
|
|
|
$menu->newLabel(pht('Queries'));
|
2013-05-30 23:09:02 +02:00
|
|
|
|
2013-06-05 14:28:25 +02:00
|
|
|
$named_queries = $this->loadEnabledNamedQueries();
|
2013-05-30 23:09:02 +02:00
|
|
|
|
|
|
|
foreach ($named_queries as $query) {
|
|
|
|
$key = $query->getQueryKey();
|
|
|
|
$uri = $this->getQueryResultsPageURI($key);
|
2013-12-26 21:30:36 +01:00
|
|
|
$menu->newLink($query->getQueryName(), $uri, 'query/'.$key);
|
2013-05-30 23:09:02 +02:00
|
|
|
}
|
|
|
|
|
2013-05-31 19:51:20 +02:00
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$manage_uri = $this->getQueryManagementURI();
|
2013-12-26 21:30:36 +01:00
|
|
|
$menu->newLink(pht('Edit Queries...'), $manage_uri, 'query/edit');
|
2013-05-31 19:51:20 +02:00
|
|
|
}
|
2013-05-30 23:09:02 +02:00
|
|
|
|
2013-05-31 19:51:05 +02:00
|
|
|
$menu->newLabel(pht('Search'));
|
2013-05-30 23:09:02 +02:00
|
|
|
$advanced_uri = $this->getQueryResultsPageURI('advanced');
|
2013-12-26 21:30:36 +01:00
|
|
|
$menu->newLink(pht('Advanced Search'), $advanced_uri, 'query/advanced');
|
2013-05-30 23:09:02 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:28:25 +02:00
|
|
|
public function loadAllNamedQueries() {
|
|
|
|
$viewer = $this->requireViewer();
|
|
|
|
|
|
|
|
$named_queries = id(new PhabricatorNamedQueryQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withUserPHIDs(array($viewer->getPHID()))
|
|
|
|
->withEngineClassNames(array(get_class($this)))
|
|
|
|
->execute();
|
|
|
|
$named_queries = mpull($named_queries, null, 'getQueryKey');
|
|
|
|
|
|
|
|
$builtin = $this->getBuiltinQueries($viewer);
|
|
|
|
$builtin = mpull($builtin, null, 'getQueryKey');
|
|
|
|
|
|
|
|
foreach ($named_queries as $key => $named_query) {
|
|
|
|
if ($named_query->getIsBuiltin()) {
|
|
|
|
if (isset($builtin[$key])) {
|
|
|
|
$named_queries[$key]->setQueryName($builtin[$key]->getQueryName());
|
|
|
|
unset($builtin[$key]);
|
|
|
|
} else {
|
|
|
|
unset($named_queries[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($builtin[$key]);
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:22:27 +02:00
|
|
|
$named_queries = msort($named_queries, 'getSortKey');
|
|
|
|
|
2013-06-05 14:28:25 +02:00
|
|
|
return $named_queries + $builtin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadEnabledNamedQueries() {
|
|
|
|
$named_queries = $this->loadAllNamedQueries();
|
|
|
|
foreach ($named_queries as $key => $named_query) {
|
|
|
|
if ($named_query->getIsBuiltin() && $named_query->getIsDisabled()) {
|
|
|
|
unset($named_queries[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $named_queries;
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:18:27 +02:00
|
|
|
protected function setQueryProjects(
|
|
|
|
PhabricatorCursorPagedPolicyAwareQuery $query,
|
|
|
|
PhabricatorSavedQuery $saved) {
|
|
|
|
|
|
|
|
$datasource = id(new PhabricatorProjectLogicalDatasource())
|
|
|
|
->setViewer($this->requireViewer());
|
|
|
|
|
|
|
|
$projects = $saved->getParameter('projects', array());
|
|
|
|
$constraints = $datasource->evaluateTokens($projects);
|
|
|
|
|
|
|
|
if ($constraints) {
|
|
|
|
$query->withEdgeLogicConstraints(
|
|
|
|
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST,
|
|
|
|
$constraints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 23:09:02 +02:00
|
|
|
|
2014-05-08 18:18:02 +02:00
|
|
|
/* -( Applications )------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
protected function getApplicationURI($path = '') {
|
|
|
|
return $this->getApplication()->getApplicationURI($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getApplication() {
|
|
|
|
if (!$this->application) {
|
|
|
|
$class = $this->getApplicationClassName();
|
|
|
|
|
|
|
|
$this->application = id(new PhabricatorApplicationQuery())
|
|
|
|
->setViewer($this->requireViewer())
|
|
|
|
->withClasses(array($class))
|
|
|
|
->withInstalled(true)
|
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
if (!$this->application) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Application "%s" is not installed!',
|
|
|
|
$class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->application;
|
|
|
|
}
|
|
|
|
|
2015-02-05 00:47:48 +01:00
|
|
|
abstract public function getApplicationClassName();
|
2014-05-08 18:18:02 +02:00
|
|
|
|
|
|
|
|
2014-05-07 23:56:30 +02:00
|
|
|
/* -( Constructing Engines )----------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all available application search engines.
|
|
|
|
*
|
|
|
|
* @return list<PhabricatorApplicationSearchEngine> All available engines.
|
|
|
|
* @task construct
|
|
|
|
*/
|
|
|
|
public static function getAllEngines() {
|
|
|
|
$engines = id(new PhutilSymbolLoader())
|
|
|
|
->setAncestorClass(__CLASS__)
|
|
|
|
->loadObjects();
|
|
|
|
|
|
|
|
return $engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an engine by class name, if it exists.
|
|
|
|
*
|
|
|
|
* @return PhabricatorApplicationSearchEngine|null Engine, or null if it does
|
|
|
|
* not exist.
|
|
|
|
* @task construct
|
|
|
|
*/
|
|
|
|
public static function getEngineByClassName($class_name) {
|
|
|
|
return idx(self::getAllEngines(), $class_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-27 22:42:18 +02:00
|
|
|
/* -( Builtin Queries )---------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task builtin
|
|
|
|
*/
|
|
|
|
public function getBuiltinQueries() {
|
|
|
|
$names = $this->getBuiltinQueryNames();
|
|
|
|
|
|
|
|
$queries = array();
|
2013-06-06 01:22:27 +02:00
|
|
|
$sequence = 0;
|
2013-05-27 22:42:18 +02:00
|
|
|
foreach ($names as $key => $name) {
|
|
|
|
$queries[$key] = id(new PhabricatorNamedQuery())
|
2013-06-06 01:22:27 +02:00
|
|
|
->setUserPHID($this->requireViewer()->getPHID())
|
|
|
|
->setEngineClassName(get_class($this))
|
2013-05-27 22:42:18 +02:00
|
|
|
->setQueryName($name)
|
|
|
|
->setQueryKey($key)
|
2013-06-06 01:22:27 +02:00
|
|
|
->setSequence((1 << 24) + $sequence++)
|
|
|
|
->setIsBuiltin(true);
|
2013-05-27 22:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $queries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-29 23:00:52 +02:00
|
|
|
/**
|
|
|
|
* @task builtin
|
|
|
|
*/
|
|
|
|
public function getBuiltinQuery($query_key) {
|
|
|
|
if (!$this->isBuiltinQuery($query_key)) {
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new Exception(pht("'%s' is not a builtin!", $query_key));
|
2013-05-29 23:00:52 +02:00
|
|
|
}
|
|
|
|
return idx($this->getBuiltinQueries(), $query_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-27 22:42:18 +02:00
|
|
|
/**
|
|
|
|
* @task builtin
|
|
|
|
*/
|
|
|
|
protected function getBuiltinQueryNames() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task builtin
|
|
|
|
*/
|
|
|
|
public function isBuiltinQuery($query_key) {
|
|
|
|
$builtins = $this->getBuiltinQueries();
|
|
|
|
return isset($builtins[$query_key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task builtin
|
|
|
|
*/
|
|
|
|
public function buildSavedQueryFromBuiltin($query_key) {
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new Exception(pht("Builtin '%s' is not supported!", $query_key));
|
2013-05-27 22:42:18 +02:00
|
|
|
}
|
2013-05-31 02:32:12 +02:00
|
|
|
|
|
|
|
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
/* -( Reading Utilities )--------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a list of user PHIDs from a request in a flexible way. This method
|
|
|
|
* supports either of these forms:
|
|
|
|
*
|
|
|
|
* users[]=alincoln&users[]=htaft
|
|
|
|
* users=alincoln,htaft
|
|
|
|
*
|
|
|
|
* Additionally, users can be specified either by PHID or by name.
|
|
|
|
*
|
|
|
|
* The main goal of this flexibility is to allow external programs to generate
|
|
|
|
* links to pages (like "alincoln's open revisions") without needing to make
|
|
|
|
* API calls.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest Request to read user PHIDs from.
|
|
|
|
* @param string Key to read in the request.
|
2013-10-05 05:41:50 +02:00
|
|
|
* @param list<const> Other permitted PHID types.
|
2015-04-19 16:14:28 +02:00
|
|
|
* @return list<phid> List of user PHIDs and selector functions.
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
* @task read
|
|
|
|
*/
|
2013-10-05 05:41:50 +02:00
|
|
|
protected function readUsersFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key,
|
|
|
|
array $allow_types = array()) {
|
2013-12-26 19:40:22 +01:00
|
|
|
|
|
|
|
$list = $this->readListFromRequest($request, $key);
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
|
|
|
|
$phids = array();
|
|
|
|
$names = array();
|
2013-10-05 05:41:50 +02:00
|
|
|
$allow_types = array_fuse($allow_types);
|
2015-04-19 16:14:28 +02:00
|
|
|
$user_type = PhabricatorPeopleUserPHIDType::TYPECONST;
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
foreach ($list as $item) {
|
2013-10-05 05:41:50 +02:00
|
|
|
$type = phid_get_type($item);
|
|
|
|
if ($type == $user_type) {
|
|
|
|
$phids[] = $item;
|
|
|
|
} else if (isset($allow_types[$type])) {
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
$phids[] = $item;
|
|
|
|
} else {
|
2015-04-17 00:30:41 +02:00
|
|
|
if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
|
|
|
|
// If this is a function, pass it through unchanged; we'll evaluate
|
|
|
|
// it later.
|
|
|
|
$phids[] = $item;
|
|
|
|
} else {
|
|
|
|
$names[] = $item;
|
|
|
|
}
|
Allow construction of ApplicationSearch queries with GET
Summary:
Ref T3775 (discussion here). Ref T2625.
T3775 presents two problems:
# Existing tools which linked to `/differential/active/epriestley/` (that is, put a username in the URL) can't generate search links now.
# Humans can't edit the URL anymore, either.
I think (1) is an actual issue, and this fixes it. I think (2) is pretty fluff, and this doesn't really try to fix it, although it probably improves it.
The fix for (1) is:
- Provide a helper to read a parameter containing either a list of user PHIDs or a list of usernames, so `/?users[]=PHID-USER-xyz` (from a tokenizer) and `/?users=alincoln,htaft` (from an external program) are equivalent inputs.
- Rename all the form parameters to be more digestable (`authorPHIDs` -> `authors`). Almost all of them were in this form already anyway. This just gives us `?users=alincoln` instead of `userPHIDs=alincoln`.
- Inside ApplicationSearch, if a request has no query associated with it but does have query parameters, build a query from the request instead of issuing the user's default query. Basically, this means that `/differential/` runs the default query, while `/differential/?users=x` runs a custom query.
Test Plan: {F56612}
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T2625, T3775
Differential Revision: https://secure.phabricator.com/D6840
2013-08-29 20:52:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($names) {
|
|
|
|
$users = id(new PhabricatorPeopleQuery())
|
|
|
|
->setViewer($this->requireViewer())
|
|
|
|
->withUsernames($names)
|
|
|
|
->execute();
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$phids[] = $user->getPHID();
|
|
|
|
}
|
|
|
|
$phids = array_unique($phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $phids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-19 16:14:28 +02:00
|
|
|
/**
|
|
|
|
* Read a list of project PHIDs from a request in a flexible way.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest Request to read user PHIDs from.
|
|
|
|
* @param string Key to read in the request.
|
|
|
|
* @return list<phid> List of projet PHIDs and selector functions.
|
|
|
|
* @task read
|
|
|
|
*/
|
|
|
|
protected function readProjectsFromRequest(AphrontRequest $request, $key) {
|
|
|
|
$list = $this->readListFromRequest($request, $key);
|
|
|
|
|
|
|
|
$phids = array();
|
|
|
|
$slugs = array();
|
|
|
|
$project_type = PhabricatorProjectProjectPHIDType::TYPECONST;
|
|
|
|
foreach ($list as $item) {
|
|
|
|
$type = phid_get_type($item);
|
|
|
|
if ($type == $project_type) {
|
|
|
|
$phids[] = $item;
|
|
|
|
} else {
|
|
|
|
if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
|
|
|
|
// If this is a function, pass it through unchanged; we'll evaluate
|
|
|
|
// it later.
|
|
|
|
$phids[] = $item;
|
|
|
|
} else {
|
|
|
|
$slugs[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($slugs) {
|
|
|
|
$projects = id(new PhabricatorProjectQuery())
|
|
|
|
->setViewer($this->requireViewer())
|
|
|
|
->withSlugs($slugs)
|
|
|
|
->execute();
|
|
|
|
foreach ($projects as $project) {
|
|
|
|
$phids[] = $project->getPHID();
|
|
|
|
}
|
|
|
|
$phids = array_unique($phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $phids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a list of subscribers from a request in a flexible way.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest Request to read PHIDs from.
|
|
|
|
* @param string Key to read in the request.
|
|
|
|
* @return list<phid> List of object PHIDs.
|
|
|
|
* @task read
|
|
|
|
*/
|
|
|
|
protected function readSubscribersFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key) {
|
|
|
|
return $this->readUsersFromRequest(
|
|
|
|
$request,
|
|
|
|
$key,
|
|
|
|
array(
|
|
|
|
PhabricatorProjectProjectPHIDType::TYPECONST,
|
|
|
|
PhabricatorMailingListListPHIDType::TYPECONST,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-05 20:59:33 +01:00
|
|
|
/**
|
|
|
|
* Read a list of generic PHIDs from a request in a flexible way. Like
|
|
|
|
* @{method:readUsersFromRequest}, this method supports either array or
|
|
|
|
* comma-delimited forms. Objects can be specified either by PHID or by
|
|
|
|
* object name.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest Request to read PHIDs from.
|
|
|
|
* @param string Key to read in the request.
|
|
|
|
* @param list<const> Optional, list of permitted PHID types.
|
|
|
|
* @return list<phid> List of object PHIDs.
|
|
|
|
*
|
|
|
|
* @task read
|
|
|
|
*/
|
|
|
|
protected function readPHIDsFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key,
|
|
|
|
array $allow_types = array()) {
|
|
|
|
|
2013-12-26 19:40:22 +01:00
|
|
|
$list = $this->readListFromRequest($request, $key);
|
2013-12-05 20:59:33 +01:00
|
|
|
|
|
|
|
$objects = id(new PhabricatorObjectQuery())
|
|
|
|
->setViewer($this->requireViewer())
|
|
|
|
->withNames($list)
|
|
|
|
->execute();
|
|
|
|
$list = mpull($objects, 'getPHID');
|
|
|
|
|
|
|
|
if (!$list) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only certain PHID types are allowed, filter out all the others.
|
|
|
|
if ($allow_types) {
|
|
|
|
$allow_types = array_fuse($allow_types);
|
|
|
|
foreach ($list as $key => $phid) {
|
|
|
|
if (empty($allow_types[phid_get_type($phid)])) {
|
|
|
|
unset($list[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:40:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a list of items from the request, in either array format or string
|
|
|
|
* format:
|
|
|
|
*
|
|
|
|
* list[]=item1&list[]=item2
|
|
|
|
* list=item1,item2
|
|
|
|
*
|
|
|
|
* This provides flexibility when constructing URIs, especially from external
|
|
|
|
* sources.
|
|
|
|
*
|
2014-02-03 21:52:19 +01:00
|
|
|
* @param AphrontRequest Request to read strings from.
|
2013-12-26 19:40:22 +01:00
|
|
|
* @param string Key to read in the request.
|
|
|
|
* @return list<string> List of values.
|
|
|
|
*/
|
|
|
|
protected function readListFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key) {
|
|
|
|
$list = $request->getArr($key, null);
|
|
|
|
if ($list === null) {
|
|
|
|
$list = $request->getStrList($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$list) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:10:18 +01:00
|
|
|
protected function readDateFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key) {
|
|
|
|
|
2015-05-04 19:08:49 +02:00
|
|
|
$value = AphrontFormDateControlValue::newFromRequest($request, $key);
|
|
|
|
|
|
|
|
if ($value->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value->getDictionary();
|
2014-02-06 19:10:18 +01:00
|
|
|
}
|
|
|
|
|
2013-10-07 02:10:29 +02:00
|
|
|
protected function readBoolFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
$key) {
|
|
|
|
if (!strlen($request->getStr($key))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $request->getBool($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function getBoolFromQuery(PhabricatorSavedQuery $query, $key) {
|
|
|
|
$value = $query->getParameter($key);
|
|
|
|
if ($value === null) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
return $value ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-31 02:32:12 +02:00
|
|
|
/* -( Dates )-------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task dates
|
|
|
|
*/
|
|
|
|
protected function parseDateTime($date_time) {
|
|
|
|
if (!strlen($date_time)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-03 21:58:11 +02:00
|
|
|
return PhabricatorTime::parseLocalTime($date_time, $this->requireViewer());
|
2013-05-31 02:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task dates
|
|
|
|
*/
|
|
|
|
protected function buildDateRange(
|
|
|
|
AphrontFormView $form,
|
|
|
|
PhabricatorSavedQuery $saved_query,
|
|
|
|
$start_key,
|
|
|
|
$start_name,
|
|
|
|
$end_key,
|
|
|
|
$end_name) {
|
|
|
|
|
|
|
|
$start_str = $saved_query->getParameter($start_key);
|
|
|
|
$start = null;
|
|
|
|
if (strlen($start_str)) {
|
|
|
|
$start = $this->parseDateTime($start_str);
|
|
|
|
if (!$start) {
|
|
|
|
$this->addError(
|
|
|
|
pht(
|
|
|
|
'"%s" date can not be parsed.',
|
|
|
|
$start_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$end_str = $saved_query->getParameter($end_key);
|
|
|
|
$end = null;
|
|
|
|
if (strlen($end_str)) {
|
|
|
|
$end = $this->parseDateTime($end_str);
|
|
|
|
if (!$end) {
|
|
|
|
$this->addError(
|
|
|
|
pht(
|
|
|
|
'"%s" date can not be parsed.',
|
|
|
|
$end_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($start && $end && ($start >= $end)) {
|
|
|
|
$this->addError(
|
|
|
|
pht(
|
|
|
|
'"%s" must be a date before "%s".',
|
|
|
|
$start_name,
|
|
|
|
$end_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new PHUIFormFreeformDateControl())
|
|
|
|
->setName($start_key)
|
|
|
|
->setLabel($start_name)
|
|
|
|
->setValue($start_str))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setName($end_key)
|
|
|
|
->setLabel($end_name)
|
|
|
|
->setValue($end_str));
|
|
|
|
}
|
2013-07-01 21:36:34 +02:00
|
|
|
|
|
|
|
|
2015-04-13 00:16:55 +02:00
|
|
|
/* -( Result Ordering )---------------------------------------------------- */
|
|
|
|
|
2015-04-16 16:43:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save order selection to a @{class:PhabricatorSavedQuery}.
|
|
|
|
*/
|
|
|
|
protected function saveQueryOrder(
|
|
|
|
PhabricatorSavedQuery $saved,
|
|
|
|
AphrontRequest $request) {
|
|
|
|
|
|
|
|
$saved->setParameter('order', $request->getStr('order'));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set query ordering from a saved value.
|
|
|
|
*/
|
|
|
|
protected function setQueryOrder(
|
|
|
|
PhabricatorCursorPagedPolicyAwareQuery $query,
|
|
|
|
PhabricatorSavedQuery $saved) {
|
|
|
|
|
|
|
|
$order = $saved->getParameter('order');
|
2015-04-16 21:11:17 +02:00
|
|
|
$builtin = $query->getBuiltinOrders();
|
|
|
|
if (strlen($order) && isset($builtin[$order])) {
|
|
|
|
$query->setOrder($order);
|
|
|
|
} else {
|
|
|
|
// If the order is invalid or not available, we choose the first
|
|
|
|
// builtin order. This isn't always the default order for the query,
|
|
|
|
// but is the first value in the "Order" dropdown, and makes the query
|
|
|
|
// behavior more consistent with the UI. In queries where the two
|
|
|
|
// orders differ, this order is the preferred order for humans.
|
|
|
|
$query->setOrder(head_key($builtin));
|
2015-04-16 16:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-13 00:16:55 +02:00
|
|
|
protected function appendOrderFieldsToForm(
|
|
|
|
AphrontFormView $form,
|
|
|
|
PhabricatorSavedQuery $saved,
|
|
|
|
PhabricatorCursorPagedPolicyAwareQuery $query) {
|
|
|
|
|
|
|
|
$orders = $query->getBuiltinOrders();
|
|
|
|
$orders = ipull($orders, 'name');
|
|
|
|
|
|
|
|
$form->appendControl(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel(pht('Order'))
|
|
|
|
->setName('order')
|
|
|
|
->setOptions($orders)
|
|
|
|
->setValue($saved->getParameter('order')));
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:24:47 +02:00
|
|
|
/* -( Paging and Executing Queries )--------------------------------------- */
|
2013-07-01 21:36:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function getPageSize(PhabricatorSavedQuery $saved) {
|
|
|
|
return $saved->getParameter('limit', 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-08 17:24:47 +02:00
|
|
|
public function shouldUseOffsetPaging() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function newPagerForSavedQuery(PhabricatorSavedQuery $saved) {
|
|
|
|
if ($this->shouldUseOffsetPaging()) {
|
|
|
|
$pager = new AphrontPagerView();
|
|
|
|
} else {
|
|
|
|
$pager = new AphrontCursorPagerView();
|
|
|
|
}
|
|
|
|
|
|
|
|
$page_size = $this->getPageSize($saved);
|
|
|
|
if (is_finite($page_size)) {
|
|
|
|
$pager->setPageSize($page_size);
|
|
|
|
} else {
|
|
|
|
// Consider an INF pagesize to mean a large finite pagesize.
|
|
|
|
|
|
|
|
// TODO: It would be nice to handle this more gracefully, but math
|
|
|
|
// with INF seems to vary across PHP versions, systems, and runtimes.
|
|
|
|
$pager->setPageSize(0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function executeQuery(
|
|
|
|
PhabricatorPolicyAwareQuery $query,
|
|
|
|
AphrontView $pager) {
|
|
|
|
|
|
|
|
$query->setViewer($this->requireViewer());
|
|
|
|
|
|
|
|
if ($this->shouldUseOffsetPaging()) {
|
|
|
|
$objects = $query->executeWithOffsetPager($pager);
|
|
|
|
} else {
|
|
|
|
$objects = $query->executeWithCursorPager($pager);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -( Rendering )---------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2014-05-08 19:08:37 +02:00
|
|
|
public function setRequest(AphrontRequest $request) {
|
|
|
|
$this->request = $request;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequest() {
|
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:24:47 +02:00
|
|
|
public function renderResults(
|
|
|
|
array $objects,
|
|
|
|
PhabricatorSavedQuery $query) {
|
2014-05-08 17:40:59 +02:00
|
|
|
|
|
|
|
$phids = $this->getRequiredHandlePHIDsForResultList($objects, $query);
|
|
|
|
|
|
|
|
if ($phids) {
|
|
|
|
$handles = id(new PhabricatorHandleQuery())
|
|
|
|
->setViewer($this->requireViewer())
|
|
|
|
->witHPHIDs($phids)
|
|
|
|
->execute();
|
|
|
|
} else {
|
|
|
|
$handles = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->renderResultList($objects, $query, $handles);
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:49:32 +02:00
|
|
|
protected function getRequiredHandlePHIDsForResultList(
|
2014-05-08 17:40:59 +02:00
|
|
|
array $objects,
|
|
|
|
PhabricatorSavedQuery $query) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:49:32 +02:00
|
|
|
protected function renderResultList(
|
2014-05-08 17:40:59 +02:00
|
|
|
array $objects,
|
|
|
|
PhabricatorSavedQuery $query,
|
|
|
|
array $handles) {
|
2014-05-08 17:24:47 +02:00
|
|
|
throw new Exception(pht('Not supported here yet!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.
This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.
Broadly, custom fields may elect to:
- build indicies when objects are updated;
- populate ApplicationSearch forms with new controls;
- read inputs entered into those controls out of the request; and
- apply constraints to search queries.
Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.
Test Plan:
I added a new searchable "Company" field to People:
{F58229}
This also cleaned up the disable/reorder view a little bit:
{F58230}
As it did before, this field appears on the edit screen:
{F58231}
However, because it has `search`, it also appears on the search screen:
{F58232}
When queried, it returns the expected results:
{F58233}
And the actually good bit of all this is that the query can take advantage of indexes:
mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | appsearch_0 | ref | key_join,key_find | key_find | 232 | const,const | 1 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | user | eq_ref | phid | phid | 194 | phabricator2_user.appsearch_0.objectPHID | 1 | |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T418, T1703, T2625, T3794
Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 22:44:34 +02:00
|
|
|
/* -( Application Search )------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an object to use to define custom fields for this search.
|
|
|
|
*
|
|
|
|
* To integrate with custom fields, subclasses should override this method
|
|
|
|
* and return an instance of the application object which implements
|
|
|
|
* @{interface:PhabricatorCustomFieldInterface}.
|
|
|
|
*
|
|
|
|
* @return PhabricatorCustomFieldInterface|null Object with custom fields.
|
|
|
|
* @task appsearch
|
|
|
|
*/
|
|
|
|
public function getCustomFieldObject() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the custom fields for this search.
|
|
|
|
*
|
|
|
|
* @return PhabricatorCustomFieldList|null Custom fields, if this search
|
|
|
|
* supports custom fields.
|
|
|
|
* @task appsearch
|
|
|
|
*/
|
|
|
|
public function getCustomFieldList() {
|
|
|
|
if ($this->customFields === false) {
|
|
|
|
$object = $this->getCustomFieldObject();
|
|
|
|
if ($object) {
|
|
|
|
$fields = PhabricatorCustomField::getObjectFields(
|
|
|
|
$object,
|
|
|
|
PhabricatorCustomField::ROLE_APPLICATIONSEARCH);
|
2014-03-25 22:02:18 +01:00
|
|
|
$fields->setViewer($this->requireViewer());
|
Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.
This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.
Broadly, custom fields may elect to:
- build indicies when objects are updated;
- populate ApplicationSearch forms with new controls;
- read inputs entered into those controls out of the request; and
- apply constraints to search queries.
Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.
Test Plan:
I added a new searchable "Company" field to People:
{F58229}
This also cleaned up the disable/reorder view a little bit:
{F58230}
As it did before, this field appears on the edit screen:
{F58231}
However, because it has `search`, it also appears on the search screen:
{F58232}
When queried, it returns the expected results:
{F58233}
And the actually good bit of all this is that the query can take advantage of indexes:
mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | appsearch_0 | ref | key_join,key_find | key_find | 232 | const,const | 1 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | user | eq_ref | phid | phid | 194 | phabricator2_user.appsearch_0.objectPHID | 1 | |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T418, T1703, T2625, T3794
Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 22:44:34 +02:00
|
|
|
} else {
|
|
|
|
$fields = null;
|
|
|
|
}
|
|
|
|
$this->customFields = $fields;
|
|
|
|
}
|
|
|
|
return $this->customFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves data from the request into a saved query.
|
|
|
|
*
|
|
|
|
* @param AphrontRequest Request to read.
|
|
|
|
* @param PhabricatorSavedQuery Query to write to.
|
|
|
|
* @return void
|
|
|
|
* @task appsearch
|
|
|
|
*/
|
|
|
|
protected function readCustomFieldsFromRequest(
|
|
|
|
AphrontRequest $request,
|
|
|
|
PhabricatorSavedQuery $saved) {
|
|
|
|
|
|
|
|
$list = $this->getCustomFieldList();
|
|
|
|
if (!$list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
$value = $field->readApplicationSearchValueFromRequest(
|
|
|
|
$this,
|
|
|
|
$request);
|
|
|
|
$saved->setParameter($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies data from a saved query to an executable query.
|
|
|
|
*
|
|
|
|
* @param PhabricatorCursorPagedPolicyAwareQuery Query to constrain.
|
|
|
|
* @param PhabricatorSavedQuery Saved query to read.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function applyCustomFieldsToQuery(
|
|
|
|
PhabricatorCursorPagedPolicyAwareQuery $query,
|
|
|
|
PhabricatorSavedQuery $saved) {
|
|
|
|
|
|
|
|
$list = $this->getCustomFieldList();
|
|
|
|
if (!$list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
$value = $field->applyApplicationSearchConstraintToQuery(
|
|
|
|
$this,
|
|
|
|
$query,
|
|
|
|
$saved->getParameter($key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 10:22:16 +02:00
|
|
|
protected function applyOrderByToQuery(
|
|
|
|
PhabricatorCursorPagedPolicyAwareQuery $query,
|
|
|
|
array $standard_values,
|
|
|
|
$order) {
|
|
|
|
|
|
|
|
if (substr($order, 0, 7) === 'custom:') {
|
|
|
|
$list = $this->getCustomFieldList();
|
|
|
|
if (!$list) {
|
|
|
|
$query->setOrderBy(head($standard_values));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
|
|
|
|
if ($key === $order) {
|
|
|
|
$index = $field->buildOrderIndex();
|
|
|
|
|
|
|
|
if ($index === null) {
|
|
|
|
$query->setOrderBy(head($standard_values));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->withApplicationSearchOrder(
|
|
|
|
$field,
|
|
|
|
$index,
|
|
|
|
false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$order = idx($standard_values, $order);
|
|
|
|
if ($order) {
|
|
|
|
$query->setOrderBy($order);
|
|
|
|
} else {
|
|
|
|
$query->setOrderBy(head($standard_values));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function getCustomFieldOrderOptions() {
|
|
|
|
$list = $this->getCustomFieldList();
|
|
|
|
if (!$list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_order = array();
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
if ($field->shouldAppearInApplicationSearch()) {
|
|
|
|
if ($field->buildOrderIndex() !== null) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
$custom_order[$key] = $field->getFieldName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $custom_order;
|
|
|
|
}
|
Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.
This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.
Broadly, custom fields may elect to:
- build indicies when objects are updated;
- populate ApplicationSearch forms with new controls;
- read inputs entered into those controls out of the request; and
- apply constraints to search queries.
Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.
Test Plan:
I added a new searchable "Company" field to People:
{F58229}
This also cleaned up the disable/reorder view a little bit:
{F58230}
As it did before, this field appears on the edit screen:
{F58231}
However, because it has `search`, it also appears on the search screen:
{F58232}
When queried, it returns the expected results:
{F58233}
And the actually good bit of all this is that the query can take advantage of indexes:
mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | appsearch_0 | ref | key_join,key_find | key_find | 232 | const,const | 1 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | user | eq_ref | phid | phid | 194 | phabricator2_user.appsearch_0.objectPHID | 1 | |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T418, T1703, T2625, T3794
Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 22:44:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique key identifying a field.
|
|
|
|
*
|
|
|
|
* @param PhabricatorCustomField Field to identify.
|
|
|
|
* @return string Unique identifier, suitable for use as an input name.
|
|
|
|
*/
|
|
|
|
public function getKeyForCustomField(PhabricatorCustomField $field) {
|
|
|
|
return 'custom:'.$field->getFieldIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add inputs to an application search form so the user can query on custom
|
|
|
|
* fields.
|
|
|
|
*
|
|
|
|
* @param AphrontFormView Form to update.
|
|
|
|
* @param PhabricatorSavedQuery Values to prefill.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function appendCustomFieldsToForm(
|
|
|
|
AphrontFormView $form,
|
|
|
|
PhabricatorSavedQuery $saved) {
|
|
|
|
|
|
|
|
$list = $this->getCustomFieldList();
|
|
|
|
if (!$list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phids = array();
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
$value = $saved->getParameter($key);
|
|
|
|
$phids[$key] = $field->getRequiredHandlePHIDsForApplicationSearch($value);
|
|
|
|
}
|
|
|
|
$all_phids = array_mergev($phids);
|
|
|
|
|
|
|
|
$handles = array();
|
|
|
|
if ($all_phids) {
|
|
|
|
$handles = id(new PhabricatorHandleQuery())
|
2013-09-17 01:04:46 +02:00
|
|
|
->setViewer($this->requireViewer())
|
Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.
This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.
Broadly, custom fields may elect to:
- build indicies when objects are updated;
- populate ApplicationSearch forms with new controls;
- read inputs entered into those controls out of the request; and
- apply constraints to search queries.
Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.
Test Plan:
I added a new searchable "Company" field to People:
{F58229}
This also cleaned up the disable/reorder view a little bit:
{F58230}
As it did before, this field appears on the edit screen:
{F58231}
However, because it has `search`, it also appears on the search screen:
{F58232}
When queried, it returns the expected results:
{F58233}
And the actually good bit of all this is that the query can take advantage of indexes:
mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | appsearch_0 | ref | key_join,key_find | key_find | 232 | const,const | 1 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | user | eq_ref | phid | phid | 194 | phabricator2_user.appsearch_0.objectPHID | 1 | |
+----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T418, T1703, T2625, T3794
Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 22:44:34 +02:00
|
|
|
->withPHIDs($all_phids)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list->getFields() as $field) {
|
|
|
|
$key = $this->getKeyForCustomField($field);
|
|
|
|
$value = $saved->getParameter($key);
|
|
|
|
$field->appendToApplicationSearchForm(
|
|
|
|
$this,
|
|
|
|
$form,
|
|
|
|
$value,
|
|
|
|
array_select_keys($handles, $phids[$key]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-14 15:53:20 +02:00
|
|
|
}
|