1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +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:
epriestley 2013-05-27 13:42:18 -07:00
parent 0abb0c41bf
commit 451ddd76bf
6 changed files with 156 additions and 74 deletions

View file

@ -14,20 +14,21 @@ abstract class PhabricatorPasteController extends PhabricatorController {
$nav->addLabel(pht('Queries'));
$engine = id(new PhabricatorPasteSearchEngine())
->setViewer($user);
$named_queries = id(new PhabricatorNamedQueryQuery())
->setViewer($user)
->withUserPHIDs(array($user->getPHID()))
->withEngineClassNames(array('PhabricatorPasteSearchEngine'))
->withEngineClassNames(array(get_class($engine)))
->execute();
$named_queries = $named_queries + $engine->getBuiltinQueries($user);
foreach ($named_queries as $query) {
$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->addLabel(pht('Search'));

View file

@ -2,38 +2,34 @@
final class PhabricatorPasteListController extends PhabricatorPasteController {
public function shouldRequireLogin() {
return false;
}
private $filter;
private $queryKey;
public function shouldAllowPublic() {
return true;
}
public function willProcessRequest(array $data) {
$this->filter = idx($data, 'filter');
$this->queryKey = idx($data, 'queryKey');
$this->queryKey = idx($data, 'queryKey', 'all');
}
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$engine = id(new PhabricatorPasteSearchEngine())
->setViewer($user);
if ($request->isFormPost()) {
$saved = id(new PhabricatorPasteSearchEngine())
->buildSavedQueryFromRequest($request);
if (count($saved->getParameter('authorPHIDs')) == 0) {
return id(new AphrontRedirectResponse())
->setURI('/paste/filter/advanced/');
}
return id(new AphrontRedirectResponse())
->setURI('/paste/query/'.$saved->getQueryKey().'/');
return id(new AphrontRedirectResponse())->setURI(
$engine->getQueryResultsPageURI(
$engine->buildSavedQueryFromRequest($request)));
}
$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())
->setViewer($user)
->withQueryKeys(array($this->queryKey))
@ -42,20 +38,15 @@ final class PhabricatorPasteListController extends PhabricatorPasteController {
if (!$saved_query) {
return new Aphront404Response();
}
$query = id(new PhabricatorPasteSearchEngine())
->buildQueryFromSavedQuery($saved_query);
$nav->selectFilter('query/'.$this->queryKey);
$filter = null;
} else {
$filter = $nav->selectFilter('filter/'.$this->filter);
$engine->setPasteSearchFilter($filter);
$saved_query = $engine->buildSavedQueryFromRequest($request);
$query = $engine->buildQueryFromSavedQuery($saved_query);
}
$query = id(new PhabricatorPasteSearchEngine())
->buildQueryFromSavedQuery($saved_query);
$filter = $nav->selectFilter(
'query/'.$saved_query->getQueryKey(),
'filter/advanced');
$pager = new AphrontCursorPagerView();
$pager->readFromRequest($request);
$pastes = $query->setViewer($request->getUser())
@ -68,10 +59,7 @@ final class PhabricatorPasteListController extends PhabricatorPasteController {
if ($this->queryKey !== null || $filter == "filter/advanced") {
$form = $engine->buildSearchForm($saved_query);
$nav->appendChild(
array(
$form
));
$nav->appendChild($form);
}
$nav->appendChild(

View file

@ -7,15 +7,20 @@ final class PhabricatorPasteQueriesController
$request = $this->getRequest();
$user = $request->getUser();
$engine = id(new PhabricatorPasteSearchEngine())
->setViewer($user);
$nav = $this->buildSideNavView();
$nav->selectFilter('savedqueries');
$named_queries = id(new PhabricatorNamedQueryQuery())
->setViewer($user)
->withUserPHIDs(array($user->getPHID()))
->withEngineClassNames(array('PhabricatorPasteSearchEngine'))
->withEngineClassNames(array(get_class($engine)))
->execute();
$named_queries += $engine->getBuiltinQueries();
$list = new PhabricatorObjectItemListView();
$list->setUser($user);
@ -26,12 +31,18 @@ final class PhabricatorPasteQueriesController
$item = id(new PhabricatorObjectItemView())
->setHeader($named_query->getQueryName())
->setHref('/paste/query/'.$named_query->getQueryKey().'/')
->addIcon('none', $date_created)
->addAction(
->setHref('/paste/query/'.$named_query->getQueryKey().'/');
if ($named_query->getIsBuiltin()) {
$item->addIcon('lock-grey', pht('Builtin'));
$item->setBarColor('grey');
} else {
$item->addIcon('none', $date_created);
$item->addAction(
id(new PhabricatorMenuItemView())
->setIcon('edit')
->setHref('/search/edit/'.$named_query->getQueryKey().'/'));
}
$list->addItem($item);
}

View file

@ -8,9 +8,6 @@
final class PhabricatorPasteSearchEngine
extends PhabricatorApplicationSearchEngine {
protected $filter;
protected $user;
/**
* Create a saved query object from the request.
*
@ -18,18 +15,10 @@ final class PhabricatorPasteSearchEngine
* @return The saved query that is built.
*/
public function buildSavedQueryFromRequest(AphrontRequest $request) {
$saved = new PhabricatorSavedQuery();
if ($this->filter == "filter/my") {
$user = $request->getUser();
$saved->setParameter('authorPHIDs', array($user->getPHID()));
} else {
$data = $request->getRequestData();
if (array_key_exists('set_users', $data)) {
$saved->setParameter('authorPHIDs', $data['set_users']);
}
}
$saved->setParameter(
'authorPHIDs',
array_values($request->getArr('set_users')));
try {
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
@ -67,12 +56,12 @@ final class PhabricatorPasteSearchEngine
public function buildSearchForm(PhabricatorSavedQuery $saved_query) {
$phids = $saved_query->getParameter('authorPHIDs', array());
$handles = id(new PhabricatorObjectHandleData($phids))
->setViewer($this->user)
->setViewer($this->requireViewer())
->loadHandles();
$users_searched = mpull($handles, 'getFullName', 'getPHID');
$form = id(new AphrontFormView())
->setUser($this->user);
->setUser($this->requireViewer());
$form->appendChild(
id(new AphrontFormTokenizerControl())
@ -83,7 +72,7 @@ final class PhabricatorPasteSearchEngine
$form->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Filter Pastes'))
->setValue(pht('Query'))
->addCancelButton(
'/search/edit/'.$saved_query->getQueryKey().'/',
pht('Save Custom Query...')));
@ -91,22 +80,36 @@ final class PhabricatorPasteSearchEngine
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) {
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);
}
}

View file

@ -4,10 +4,26 @@
* Represents an abstract search engine for an application. It supports
* creating and storing saved queries.
*
* @task builtin Builtin Queries
*
* @group search
*/
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.
*
@ -43,4 +59,57 @@ abstract class PhabricatorApplicationSearchEngine {
* @return string URI where the query can be executed.
*/
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!");
}
}

View file

@ -11,6 +11,16 @@ final class PhabricatorNamedQuery extends PhabricatorSearchDAO
protected $userPHID = "";
protected $engineClassName = "";
private $isBuiltin;
public function setIsBuiltin($is_builtin) {
$this->isBuiltin = $is_builtin;
return $this;
}
public function getIsBuiltin() {
return $this->isBuiltin;
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */