1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 14:52:41 +01:00

Modernize the Herald rule search engine

Summary: Ref T13216. Update the Herald Rule SearchEngine and Query to use a more modern style.

Test Plan: Ran various rule queries in the UI, got sensible results

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13216

Differential Revision: https://secure.phabricator.com/D19803
This commit is contained in:
epriestley 2018-11-14 11:22:54 -08:00
parent ea6d2afa86
commit bbd292b9b3
2 changed files with 68 additions and 123 deletions

View file

@ -70,19 +70,12 @@ final class HeraldRuleQuery extends PhabricatorCursorPagedPolicyAwareQuery {
return $this; return $this;
} }
public function newResultObject() {
return new HeraldRule();
}
protected function loadPage() { protected function loadPage() {
$table = new HeraldRule(); return $this->loadStandardPage($this->newResultObject());
$conn_r = $table->establishConnection('r');
$data = queryfx_all(
$conn_r,
'SELECT rule.* FROM %T rule %Q %Q %Q',
$table->getTableName(),
$this->buildWhereClause($conn_r),
$this->buildOrderClause($conn_r),
$this->buildLimitClause($conn_r));
return $table->loadAllFromArray($data);
} }
protected function willFilterPage(array $rules) { protected function willFilterPage(array $rules) {
@ -175,38 +168,38 @@ final class HeraldRuleQuery extends PhabricatorCursorPagedPolicyAwareQuery {
return $rules; return $rules;
} }
protected function buildWhereClause(AphrontDatabaseConnection $conn) { protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = array(); $where = parent::buildWhereClauseParts($conn);
if ($this->ids) { if ($this->ids !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.id IN (%Ld)', 'rule.id IN (%Ld)',
$this->ids); $this->ids);
} }
if ($this->phids) { if ($this->phids !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.phid IN (%Ls)', 'rule.phid IN (%Ls)',
$this->phids); $this->phids);
} }
if ($this->authorPHIDs) { if ($this->authorPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.authorPHID IN (%Ls)', 'rule.authorPHID IN (%Ls)',
$this->authorPHIDs); $this->authorPHIDs);
} }
if ($this->ruleTypes) { if ($this->ruleTypes !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.ruleType IN (%Ls)', 'rule.ruleType IN (%Ls)',
$this->ruleTypes); $this->ruleTypes);
} }
if ($this->contentTypes) { if ($this->contentTypes !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.contentType IN (%Ls)', 'rule.contentType IN (%Ls)',
@ -220,23 +213,21 @@ final class HeraldRuleQuery extends PhabricatorCursorPagedPolicyAwareQuery {
(int)$this->disabled); (int)$this->disabled);
} }
if ($this->datasourceQuery) { if ($this->datasourceQuery !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.name LIKE %>', 'rule.name LIKE %>',
$this->datasourceQuery); $this->datasourceQuery);
} }
if ($this->triggerObjectPHIDs) { if ($this->triggerObjectPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'rule.triggerObjectPHID IN (%Ls)', 'rule.triggerObjectPHID IN (%Ls)',
$this->triggerObjectPHIDs); $this->triggerObjectPHIDs);
} }
$where[] = $this->buildPagingClause($conn); return $where;
return $this->formatWhereClause($conn, $where);
} }
private function validateRuleAuthors(array $rules) { private function validateRuleAuthors(array $rules) {
@ -281,4 +272,8 @@ final class HeraldRuleQuery extends PhabricatorCursorPagedPolicyAwareQuery {
return 'PhabricatorHeraldApplication'; return 'PhabricatorHeraldApplication';
} }
protected function getPrimaryTableAlias() {
return 'rule';
}
} }

View file

@ -10,90 +10,68 @@ final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine {
return 'PhabricatorHeraldApplication'; return 'PhabricatorHeraldApplication';
} }
public function buildSavedQueryFromRequest(AphrontRequest $request) { public function newQuery() {
$saved = new PhabricatorSavedQuery(); return new HeraldRuleQuery();
$saved->setParameter(
'authorPHIDs',
$this->readUsersFromRequest($request, 'authors'));
$saved->setParameter('contentType', $request->getStr('contentType'));
$saved->setParameter('ruleType', $request->getStr('ruleType'));
$saved->setParameter(
'disabled',
$this->readBoolFromRequest($request, 'disabled'));
return $saved;
} }
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { protected function buildCustomSearchFields() {
$query = id(new HeraldRuleQuery()); $viewer = $this->requireViewer();
$author_phids = $saved->getParameter('authorPHIDs'); $rule_types = HeraldRuleTypeConfig::getRuleTypeMap();
if ($author_phids) { $content_types = HeraldAdapter::getEnabledAdapterMap($viewer);
$query->withAuthorPHIDs($author_phids);
return array(
id(new PhabricatorUsersSearchField())
->setLabel(pht('Authors'))
->setKey('authorPHIDs')
->setAliases(array('author', 'authors', 'authorPHID'))
->setDescription(
pht('Search for rules with given authors.')),
id(new PhabricatorSearchCheckboxesField())
->setKey('ruleTypes')
->setAliases(array('ruleType'))
->setLabel(pht('Rule Type'))
->setDescription(
pht('Search for rules of given types.'))
->setOptions($rule_types),
id(new PhabricatorSearchCheckboxesField())
->setKey('contentTypes')
->setLabel(pht('Content Type'))
->setDescription(
pht('Search for rules affecting given types of content.'))
->setOptions($content_types),
id(new PhabricatorSearchThreeStateField())
->setLabel(pht('Rule Status'))
->setKey('disabled')
->setOptions(
pht('(Show All)'),
pht('Show Only Disabled Rules'),
pht('Show Only Enabled Rules')),
);
} }
$content_type = $saved->getParameter('contentType'); protected function buildQueryFromParameters(array $map) {
$content_type = idx($this->getContentTypeValues(), $content_type); $query = $this->newQuery();
if ($content_type) {
$query->withContentTypes(array($content_type)); if ($map['authorPHIDs']) {
$query->withAuthorPHIDs($map['authorPHIDs']);
} }
$rule_type = $saved->getParameter('ruleType'); if ($map['contentTypes']) {
$rule_type = idx($this->getRuleTypeValues(), $rule_type); $query->withContentTypes($map['contentTypes']);
if ($rule_type) {
$query->withRuleTypes(array($rule_type));
} }
$disabled = $saved->getParameter('disabled'); if ($map['ruleTypes']) {
if ($disabled !== null) { $query->withRuleTypes($map['ruleTypes']);
$query->withDisabled($disabled); }
if ($map['disabled'] !== null) {
$query->withDisabled($map['disabled']);
} }
return $query; return $query;
} }
public function buildSearchForm(
AphrontFormView $form,
PhabricatorSavedQuery $saved_query) {
$author_phids = $saved_query->getParameter('authorPHIDs', array());
$content_type = $saved_query->getParameter('contentType');
$rule_type = $saved_query->getParameter('ruleType');
$form
->appendControl(
id(new AphrontFormTokenizerControl())
->setDatasource(new PhabricatorPeopleDatasource())
->setName('authors')
->setLabel(pht('Authors'))
->setValue($author_phids))
->appendChild(
id(new AphrontFormSelectControl())
->setName('contentType')
->setLabel(pht('Content Type'))
->setValue($content_type)
->setOptions($this->getContentTypeOptions()))
->appendChild(
id(new AphrontFormSelectControl())
->setName('ruleType')
->setLabel(pht('Rule Type'))
->setValue($rule_type)
->setOptions($this->getRuleTypeOptions()))
->appendChild(
id(new AphrontFormSelectControl())
->setName('disabled')
->setLabel(pht('Rule Status'))
->setValue($this->getBoolFromQuery($saved_query, 'disabled'))
->setOptions(
array(
'' => pht('Show Enabled and Disabled Rules'),
'false' => pht('Show Only Enabled Rules'),
'true' => pht('Show Only Disabled Rules'),
)));
}
protected function getURI($path) { protected function getURI($path) {
return '/herald/'.$path; return '/herald/'.$path;
} }
@ -131,35 +109,6 @@ final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine {
return parent::buildSavedQueryFromBuiltin($query_key); return parent::buildSavedQueryFromBuiltin($query_key);
} }
private function getContentTypeOptions() {
return array(
'' => pht('(All Content Types)'),
) + HeraldAdapter::getEnabledAdapterMap($this->requireViewer());
}
private function getContentTypeValues() {
return array_fuse(
array_keys(
HeraldAdapter::getEnabledAdapterMap($this->requireViewer())));
}
private function getRuleTypeOptions() {
return array(
'' => pht('(All Rule Types)'),
) + HeraldRuleTypeConfig::getRuleTypeMap();
}
private function getRuleTypeValues() {
return array_fuse(array_keys(HeraldRuleTypeConfig::getRuleTypeMap()));
}
protected function getRequiredHandlePHIDsForResultList(
array $rules,
PhabricatorSavedQuery $query) {
return mpull($rules, 'getAuthorPHID');
}
protected function renderResultList( protected function renderResultList(
array $rules, array $rules,
PhabricatorSavedQuery $query, PhabricatorSavedQuery $query,
@ -167,6 +116,7 @@ final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine {
assert_instances_of($rules, 'HeraldRule'); assert_instances_of($rules, 'HeraldRule');
$viewer = $this->requireViewer(); $viewer = $this->requireViewer();
$handles = $viewer->loadHandles(mpull($rules, 'getAuthorPHID'));
$content_type_map = HeraldAdapter::getEnabledAdapterMap($viewer); $content_type_map = HeraldAdapter::getEnabledAdapterMap($viewer);