mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-22 20:51:10 +01:00
Make builtin queries more generic
Summary: Ref T2625. Currently, Paste hard-codes its filters as a separate layer above the query layer. Instead, expose these as "Builtin" queries which we construct at runtime. They act like normal saved queries, except in cases where it doesn't make sense. (I'm probably going to let you hide them too, and maybe even rename them, although for now they're just immutable.) Test Plan: {F44340} Reviewers: btrahan, blc Reviewed By: btrahan CC: aran Maniphest Tasks: T2625 Differential Revision: https://secure.phabricator.com/D6058
This commit is contained in:
parent
0abb0c41bf
commit
451ddd76bf
6 changed files with 156 additions and 74 deletions
|
@ -14,20 +14,21 @@ abstract class PhabricatorPasteController extends PhabricatorController {
|
||||||
|
|
||||||
$nav->addLabel(pht('Queries'));
|
$nav->addLabel(pht('Queries'));
|
||||||
|
|
||||||
|
$engine = id(new PhabricatorPasteSearchEngine())
|
||||||
|
->setViewer($user);
|
||||||
|
|
||||||
$named_queries = id(new PhabricatorNamedQueryQuery())
|
$named_queries = id(new PhabricatorNamedQueryQuery())
|
||||||
->setViewer($user)
|
->setViewer($user)
|
||||||
->withUserPHIDs(array($user->getPHID()))
|
->withUserPHIDs(array($user->getPHID()))
|
||||||
->withEngineClassNames(array('PhabricatorPasteSearchEngine'))
|
->withEngineClassNames(array(get_class($engine)))
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
|
$named_queries = $named_queries + $engine->getBuiltinQueries($user);
|
||||||
|
|
||||||
foreach ($named_queries as $query) {
|
foreach ($named_queries as $query) {
|
||||||
$nav->addFilter('query/'.$query->getQueryKey(), $query->getQueryName());
|
$nav->addFilter('query/'.$query->getQueryKey(), $query->getQueryName());
|
||||||
}
|
}
|
||||||
|
|
||||||
$nav->addFilter('filter/all', pht('All Pastes'));
|
|
||||||
if ($user->isLoggedIn()) {
|
|
||||||
$nav->addFilter('filter/my', pht('My Pastes'));
|
|
||||||
}
|
|
||||||
$nav->addFilter('savedqueries', pht('Edit Queries...'));
|
$nav->addFilter('savedqueries', pht('Edit Queries...'));
|
||||||
|
|
||||||
$nav->addLabel(pht('Search'));
|
$nav->addLabel(pht('Search'));
|
||||||
|
|
|
@ -2,38 +2,34 @@
|
||||||
|
|
||||||
final class PhabricatorPasteListController extends PhabricatorPasteController {
|
final class PhabricatorPasteListController extends PhabricatorPasteController {
|
||||||
|
|
||||||
public function shouldRequireLogin() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private $filter;
|
|
||||||
private $queryKey;
|
private $queryKey;
|
||||||
|
|
||||||
|
public function shouldAllowPublic() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public function willProcessRequest(array $data) {
|
public function willProcessRequest(array $data) {
|
||||||
$this->filter = idx($data, 'filter');
|
$this->queryKey = idx($data, 'queryKey', 'all');
|
||||||
$this->queryKey = idx($data, 'queryKey');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processRequest() {
|
public function processRequest() {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$user = $request->getUser();
|
$user = $request->getUser();
|
||||||
|
|
||||||
|
$engine = id(new PhabricatorPasteSearchEngine())
|
||||||
|
->setViewer($user);
|
||||||
|
|
||||||
if ($request->isFormPost()) {
|
if ($request->isFormPost()) {
|
||||||
$saved = id(new PhabricatorPasteSearchEngine())
|
return id(new AphrontRedirectResponse())->setURI(
|
||||||
->buildSavedQueryFromRequest($request);
|
$engine->getQueryResultsPageURI(
|
||||||
if (count($saved->getParameter('authorPHIDs')) == 0) {
|
$engine->buildSavedQueryFromRequest($request)));
|
||||||
return id(new AphrontRedirectResponse())
|
|
||||||
->setURI('/paste/filter/advanced/');
|
|
||||||
}
|
|
||||||
return id(new AphrontRedirectResponse())
|
|
||||||
->setURI('/paste/query/'.$saved->getQueryKey().'/');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$nav = $this->buildSideNavView();
|
$nav = $this->buildSideNavView();
|
||||||
$engine = id(new PhabricatorPasteSearchEngine())
|
|
||||||
->setPasteSearchUser($request->getUser());
|
|
||||||
|
|
||||||
if ($this->queryKey !== null) {
|
if ($engine->isBuiltinQuery($this->queryKey)) {
|
||||||
|
$saved_query = $engine->buildSavedQueryFromBuiltin($this->queryKey);
|
||||||
|
} else {
|
||||||
$saved_query = id(new PhabricatorSavedQueryQuery())
|
$saved_query = id(new PhabricatorSavedQueryQuery())
|
||||||
->setViewer($user)
|
->setViewer($user)
|
||||||
->withQueryKeys(array($this->queryKey))
|
->withQueryKeys(array($this->queryKey))
|
||||||
|
@ -42,19 +38,14 @@ final class PhabricatorPasteListController extends PhabricatorPasteController {
|
||||||
if (!$saved_query) {
|
if (!$saved_query) {
|
||||||
return new Aphront404Response();
|
return new Aphront404Response();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$query = id(new PhabricatorPasteSearchEngine())
|
$query = id(new PhabricatorPasteSearchEngine())
|
||||||
->buildQueryFromSavedQuery($saved_query);
|
->buildQueryFromSavedQuery($saved_query);
|
||||||
|
|
||||||
$nav->selectFilter('query/'.$this->queryKey);
|
$filter = $nav->selectFilter(
|
||||||
$filter = null;
|
'query/'.$saved_query->getQueryKey(),
|
||||||
} else {
|
'filter/advanced');
|
||||||
$filter = $nav->selectFilter('filter/'.$this->filter);
|
|
||||||
$engine->setPasteSearchFilter($filter);
|
|
||||||
|
|
||||||
$saved_query = $engine->buildSavedQueryFromRequest($request);
|
|
||||||
$query = $engine->buildQueryFromSavedQuery($saved_query);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pager = new AphrontCursorPagerView();
|
$pager = new AphrontCursorPagerView();
|
||||||
$pager->readFromRequest($request);
|
$pager->readFromRequest($request);
|
||||||
|
@ -68,10 +59,7 @@ final class PhabricatorPasteListController extends PhabricatorPasteController {
|
||||||
|
|
||||||
if ($this->queryKey !== null || $filter == "filter/advanced") {
|
if ($this->queryKey !== null || $filter == "filter/advanced") {
|
||||||
$form = $engine->buildSearchForm($saved_query);
|
$form = $engine->buildSearchForm($saved_query);
|
||||||
$nav->appendChild(
|
$nav->appendChild($form);
|
||||||
array(
|
|
||||||
$form
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$nav->appendChild(
|
$nav->appendChild(
|
||||||
|
|
|
@ -7,15 +7,20 @@ final class PhabricatorPasteQueriesController
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$user = $request->getUser();
|
$user = $request->getUser();
|
||||||
|
|
||||||
|
$engine = id(new PhabricatorPasteSearchEngine())
|
||||||
|
->setViewer($user);
|
||||||
|
|
||||||
$nav = $this->buildSideNavView();
|
$nav = $this->buildSideNavView();
|
||||||
$nav->selectFilter('savedqueries');
|
$nav->selectFilter('savedqueries');
|
||||||
|
|
||||||
$named_queries = id(new PhabricatorNamedQueryQuery())
|
$named_queries = id(new PhabricatorNamedQueryQuery())
|
||||||
->setViewer($user)
|
->setViewer($user)
|
||||||
->withUserPHIDs(array($user->getPHID()))
|
->withUserPHIDs(array($user->getPHID()))
|
||||||
->withEngineClassNames(array('PhabricatorPasteSearchEngine'))
|
->withEngineClassNames(array(get_class($engine)))
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
|
$named_queries += $engine->getBuiltinQueries();
|
||||||
|
|
||||||
$list = new PhabricatorObjectItemListView();
|
$list = new PhabricatorObjectItemListView();
|
||||||
$list->setUser($user);
|
$list->setUser($user);
|
||||||
|
|
||||||
|
@ -26,12 +31,18 @@ final class PhabricatorPasteQueriesController
|
||||||
|
|
||||||
$item = id(new PhabricatorObjectItemView())
|
$item = id(new PhabricatorObjectItemView())
|
||||||
->setHeader($named_query->getQueryName())
|
->setHeader($named_query->getQueryName())
|
||||||
->setHref('/paste/query/'.$named_query->getQueryKey().'/')
|
->setHref('/paste/query/'.$named_query->getQueryKey().'/');
|
||||||
->addIcon('none', $date_created)
|
|
||||||
->addAction(
|
if ($named_query->getIsBuiltin()) {
|
||||||
|
$item->addIcon('lock-grey', pht('Builtin'));
|
||||||
|
$item->setBarColor('grey');
|
||||||
|
} else {
|
||||||
|
$item->addIcon('none', $date_created);
|
||||||
|
$item->addAction(
|
||||||
id(new PhabricatorMenuItemView())
|
id(new PhabricatorMenuItemView())
|
||||||
->setIcon('edit')
|
->setIcon('edit')
|
||||||
->setHref('/search/edit/'.$named_query->getQueryKey().'/'));
|
->setHref('/search/edit/'.$named_query->getQueryKey().'/'));
|
||||||
|
}
|
||||||
|
|
||||||
$list->addItem($item);
|
$list->addItem($item);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,6 @@
|
||||||
final class PhabricatorPasteSearchEngine
|
final class PhabricatorPasteSearchEngine
|
||||||
extends PhabricatorApplicationSearchEngine {
|
extends PhabricatorApplicationSearchEngine {
|
||||||
|
|
||||||
protected $filter;
|
|
||||||
protected $user;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a saved query object from the request.
|
* Create a saved query object from the request.
|
||||||
*
|
*
|
||||||
|
@ -18,18 +15,10 @@ final class PhabricatorPasteSearchEngine
|
||||||
* @return The saved query that is built.
|
* @return The saved query that is built.
|
||||||
*/
|
*/
|
||||||
public function buildSavedQueryFromRequest(AphrontRequest $request) {
|
public function buildSavedQueryFromRequest(AphrontRequest $request) {
|
||||||
|
|
||||||
$saved = new PhabricatorSavedQuery();
|
$saved = new PhabricatorSavedQuery();
|
||||||
|
$saved->setParameter(
|
||||||
if ($this->filter == "filter/my") {
|
'authorPHIDs',
|
||||||
$user = $request->getUser();
|
array_values($request->getArr('set_users')));
|
||||||
$saved->setParameter('authorPHIDs', array($user->getPHID()));
|
|
||||||
} else {
|
|
||||||
$data = $request->getRequestData();
|
|
||||||
if (array_key_exists('set_users', $data)) {
|
|
||||||
$saved->setParameter('authorPHIDs', $data['set_users']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
||||||
|
@ -67,12 +56,12 @@ final class PhabricatorPasteSearchEngine
|
||||||
public function buildSearchForm(PhabricatorSavedQuery $saved_query) {
|
public function buildSearchForm(PhabricatorSavedQuery $saved_query) {
|
||||||
$phids = $saved_query->getParameter('authorPHIDs', array());
|
$phids = $saved_query->getParameter('authorPHIDs', array());
|
||||||
$handles = id(new PhabricatorObjectHandleData($phids))
|
$handles = id(new PhabricatorObjectHandleData($phids))
|
||||||
->setViewer($this->user)
|
->setViewer($this->requireViewer())
|
||||||
->loadHandles();
|
->loadHandles();
|
||||||
$users_searched = mpull($handles, 'getFullName', 'getPHID');
|
$users_searched = mpull($handles, 'getFullName', 'getPHID');
|
||||||
|
|
||||||
$form = id(new AphrontFormView())
|
$form = id(new AphrontFormView())
|
||||||
->setUser($this->user);
|
->setUser($this->requireViewer());
|
||||||
|
|
||||||
$form->appendChild(
|
$form->appendChild(
|
||||||
id(new AphrontFormTokenizerControl())
|
id(new AphrontFormTokenizerControl())
|
||||||
|
@ -83,7 +72,7 @@ final class PhabricatorPasteSearchEngine
|
||||||
|
|
||||||
$form->appendChild(
|
$form->appendChild(
|
||||||
id(new AphrontFormSubmitControl())
|
id(new AphrontFormSubmitControl())
|
||||||
->setValue(pht('Filter Pastes'))
|
->setValue(pht('Query'))
|
||||||
->addCancelButton(
|
->addCancelButton(
|
||||||
'/search/edit/'.$saved_query->getQueryKey().'/',
|
'/search/edit/'.$saved_query->getQueryKey().'/',
|
||||||
pht('Save Custom Query...')));
|
pht('Save Custom Query...')));
|
||||||
|
@ -91,22 +80,36 @@ final class PhabricatorPasteSearchEngine
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPasteSearchFilter($filter) {
|
|
||||||
$this->filter = $filter;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPasteSearchFilter() {
|
|
||||||
return $this->filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setPasteSearchUser($user) {
|
|
||||||
$this->user = $user;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getQueryResultsPageURI(PhabricatorSavedQuery $query) {
|
public function getQueryResultsPageURI(PhabricatorSavedQuery $query) {
|
||||||
return '/paste/query/'.$query->getQueryKey().'/';
|
return '/paste/query/'.$query->getQueryKey().'/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getBuiltinQueryNames() {
|
||||||
|
$names = array(
|
||||||
|
'all' => pht('All Pastes'),
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($this->requireViewer()->isLoggedIn()) {
|
||||||
|
$names['authored'] = pht('Authored');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $names;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildSavedQueryFromBuiltin($query_key) {
|
||||||
|
|
||||||
|
$query = $this->newSavedQuery();
|
||||||
|
|
||||||
|
switch ($query_key) {
|
||||||
|
case 'all':
|
||||||
|
return $query;
|
||||||
|
case 'authored':
|
||||||
|
return $query->setParameter(
|
||||||
|
'authorPHIDs',
|
||||||
|
array($this->requireViewer()->getPHID()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::buildSavedQueryFromBuiltin($query_key);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,26 @@
|
||||||
* Represents an abstract search engine for an application. It supports
|
* Represents an abstract search engine for an application. It supports
|
||||||
* creating and storing saved queries.
|
* creating and storing saved queries.
|
||||||
*
|
*
|
||||||
|
* @task builtin Builtin Queries
|
||||||
|
*
|
||||||
* @group search
|
* @group search
|
||||||
*/
|
*/
|
||||||
abstract class PhabricatorApplicationSearchEngine {
|
abstract class PhabricatorApplicationSearchEngine {
|
||||||
|
|
||||||
|
private $viewer;
|
||||||
|
|
||||||
|
public function setViewer(PhabricatorUser $viewer) {
|
||||||
|
$this->viewer = $viewer;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function requireViewer() {
|
||||||
|
if (!$this->viewer) {
|
||||||
|
throw new Exception("Call setViewer() before using an engine!");
|
||||||
|
}
|
||||||
|
return $this->viewer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a saved query object from the request.
|
* Create a saved query object from the request.
|
||||||
*
|
*
|
||||||
|
@ -43,4 +59,57 @@ abstract class PhabricatorApplicationSearchEngine {
|
||||||
* @return string URI where the query can be executed.
|
* @return string URI where the query can be executed.
|
||||||
*/
|
*/
|
||||||
abstract public function getQueryResultsPageURI(PhabricatorSavedQuery $query);
|
abstract public function getQueryResultsPageURI(PhabricatorSavedQuery $query);
|
||||||
|
|
||||||
|
|
||||||
|
public function newSavedQuery() {
|
||||||
|
return id(new PhabricatorSavedQuery())
|
||||||
|
->setEngineClassName(get_class($this));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* -( Builtin Queries )---------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @task builtin
|
||||||
|
*/
|
||||||
|
public function getBuiltinQueries() {
|
||||||
|
$names = $this->getBuiltinQueryNames();
|
||||||
|
|
||||||
|
$queries = array();
|
||||||
|
foreach ($names as $key => $name) {
|
||||||
|
$queries[$key] = id(new PhabricatorNamedQuery())
|
||||||
|
->setQueryName($name)
|
||||||
|
->setQueryKey($key)
|
||||||
|
->setIsBuiltin(true)
|
||||||
|
->makeEphemeral();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $queries;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
||||||
|
throw new Exception("Builtin '{$query_key}' is not supported!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,16 @@ final class PhabricatorNamedQuery extends PhabricatorSearchDAO
|
||||||
protected $userPHID = "";
|
protected $userPHID = "";
|
||||||
protected $engineClassName = "";
|
protected $engineClassName = "";
|
||||||
|
|
||||||
|
private $isBuiltin;
|
||||||
|
|
||||||
|
public function setIsBuiltin($is_builtin) {
|
||||||
|
$this->isBuiltin = $is_builtin;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIsBuiltin() {
|
||||||
|
return $this->isBuiltin;
|
||||||
|
}
|
||||||
|
|
||||||
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue