2011-07-05 17:35:18 +02:00
|
|
|
<?php
|
|
|
|
|
2012-09-13 19:15:08 +02:00
|
|
|
final class PhabricatorFeedQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2011-07-05 17:35:18 +02:00
|
|
|
|
|
|
|
private $filterPHIDs;
|
2013-06-26 01:29:47 +02:00
|
|
|
private $chronologicalKeys;
|
2011-07-05 17:35:18 +02:00
|
|
|
|
|
|
|
public function setFilterPHIDs(array $phids) {
|
|
|
|
$this->filterPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:29:47 +02:00
|
|
|
public function withChronologicalKeys(array $keys) {
|
|
|
|
$this->chronologicalKeys = $keys;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:28:02 +01:00
|
|
|
protected function loadPage() {
|
2011-07-05 17:35:18 +02:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
$story_table = new PhabricatorFeedStoryData();
|
|
|
|
$conn = $story_table->establishConnection('r');
|
2011-07-05 17:35:18 +02:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
$data = queryfx_all(
|
|
|
|
$conn,
|
|
|
|
'SELECT story.* FROM %T story %Q %Q %Q %Q %Q',
|
|
|
|
$story_table->getTableName(),
|
|
|
|
$this->buildJoinClause($conn),
|
|
|
|
$this->buildWhereClause($conn),
|
|
|
|
$this->buildGroupClause($conn),
|
|
|
|
$this->buildOrderClause($conn),
|
|
|
|
$this->buildLimitClause($conn));
|
|
|
|
|
Allow policy-aware queries to prefilter results
Summary:
Provides a simple way for policy-aware queries to pre-filter results without needing to maintain separate cursors, and fixes a bunch of filter-related edge cases.
- For reverse-paged cursor queries, we previously reversed each individual set of results. If the final result set is built out of multiple pages, it's in the wrong order overall, with each page in the correct order in sequence. Instead, reverse everything at the end. This also simplifies construction of queries.
- `AphrontCursorPagerView` would always render a "<< First" link when paging backward, even if we were on the first page of results.
- Add a filtering hook to let queries perform in-application pre-policy filtering as simply as possible (i.e., without maintaing their own cursors over the result sets).
Test Plan: Made feed randomly prefilter half the results, and paged forward and backward. Observed correct result ordering, pagination, and next/previous links.
Reviewers: btrahan, vrana
Reviewed By: btrahan
CC: aran
Differential Revision: https://secure.phabricator.com/D3787
2012-10-23 21:01:11 +02:00
|
|
|
return $data;
|
|
|
|
}
|
2012-07-03 00:41:19 +02:00
|
|
|
|
Allow policy-aware queries to prefilter results
Summary:
Provides a simple way for policy-aware queries to pre-filter results without needing to maintain separate cursors, and fixes a bunch of filter-related edge cases.
- For reverse-paged cursor queries, we previously reversed each individual set of results. If the final result set is built out of multiple pages, it's in the wrong order overall, with each page in the correct order in sequence. Instead, reverse everything at the end. This also simplifies construction of queries.
- `AphrontCursorPagerView` would always render a "<< First" link when paging backward, even if we were on the first page of results.
- Add a filtering hook to let queries perform in-application pre-policy filtering as simply as possible (i.e., without maintaing their own cursors over the result sets).
Test Plan: Made feed randomly prefilter half the results, and paged forward and backward. Observed correct result ordering, pagination, and next/previous links.
Reviewers: btrahan, vrana
Reviewed By: btrahan
CC: aran
Differential Revision: https://secure.phabricator.com/D3787
2012-10-23 21:01:11 +02:00
|
|
|
protected function willFilterPage(array $data) {
|
2012-10-23 21:01:59 +02:00
|
|
|
return PhabricatorFeedStory::loadAllFromRows($data, $this->getViewer());
|
2012-02-16 02:48:14 +01:00
|
|
|
}
|
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
private function buildJoinClause(AphrontDatabaseConnection $conn_r) {
|
|
|
|
// NOTE: We perform this join unconditionally (even if we have no filter
|
|
|
|
// PHIDs) to omit rows which have no story references. These story data
|
|
|
|
// rows are notifications or realtime alerts.
|
2011-07-05 17:35:18 +02:00
|
|
|
|
|
|
|
$ref_table = new PhabricatorFeedStoryReference();
|
2012-07-03 00:41:19 +02:00
|
|
|
return qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'JOIN %T ref ON ref.chronologicalKey = story.chronologicalKey',
|
|
|
|
$ref_table->getTableName());
|
|
|
|
}
|
2011-07-05 17:35:18 +02:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
2011-07-05 17:35:18 +02:00
|
|
|
$where = array();
|
2012-07-03 00:41:19 +02:00
|
|
|
|
2011-07-05 17:35:18 +02:00
|
|
|
if ($this->filterPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
2012-07-03 00:41:19 +02:00
|
|
|
$conn_r,
|
2011-07-05 17:35:18 +02:00
|
|
|
'ref.objectPHID IN (%Ls)',
|
|
|
|
$this->filterPHIDs);
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:29:47 +02:00
|
|
|
if ($this->chronologicalKeys) {
|
|
|
|
// NOTE: We want to use integers in the query so we can take advantage
|
|
|
|
// of keys, but can't use %d on 32-bit systems. Make sure all the keys
|
|
|
|
// are integers and then format them raw.
|
|
|
|
|
|
|
|
$keys = $this->chronologicalKeys;
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (!ctype_digit($key)) {
|
|
|
|
throw new Exception("Key '{$key}' is not a valid chronological key!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'ref.chronologicalKey IN (%Q)',
|
|
|
|
implode(', ', $keys));
|
|
|
|
}
|
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
2012-02-16 02:48:14 +01:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
2012-02-16 02:48:14 +01:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
private function buildGroupClause(AphrontDatabaseConnection $conn_r) {
|
|
|
|
return qsprintf(
|
|
|
|
$conn_r,
|
2012-10-04 23:49:23 +02:00
|
|
|
'GROUP BY '.($this->filterPHIDs
|
|
|
|
? 'ref.chronologicalKey'
|
|
|
|
: 'story.chronologicalKey'));
|
2012-07-03 00:41:19 +02:00
|
|
|
}
|
2011-07-05 17:35:18 +02:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
protected function getPagingColumn() {
|
2012-10-04 23:49:23 +02:00
|
|
|
return ($this->filterPHIDs
|
|
|
|
? 'ref.chronologicalKey'
|
|
|
|
: 'story.chronologicalKey');
|
2012-07-03 00:41:19 +02:00
|
|
|
}
|
2012-02-16 02:48:14 +01:00
|
|
|
|
2012-07-03 00:41:19 +02:00
|
|
|
protected function getPagingValue($item) {
|
Allow policy-aware queries to prefilter results
Summary:
Provides a simple way for policy-aware queries to pre-filter results without needing to maintain separate cursors, and fixes a bunch of filter-related edge cases.
- For reverse-paged cursor queries, we previously reversed each individual set of results. If the final result set is built out of multiple pages, it's in the wrong order overall, with each page in the correct order in sequence. Instead, reverse everything at the end. This also simplifies construction of queries.
- `AphrontCursorPagerView` would always render a "<< First" link when paging backward, even if we were on the first page of results.
- Add a filtering hook to let queries perform in-application pre-policy filtering as simply as possible (i.e., without maintaing their own cursors over the result sets).
Test Plan: Made feed randomly prefilter half the results, and paged forward and backward. Observed correct result ordering, pagination, and next/previous links.
Reviewers: btrahan, vrana
Reviewed By: btrahan
CC: aran
Differential Revision: https://secure.phabricator.com/D3787
2012-10-23 21:01:11 +02:00
|
|
|
if ($item instanceof PhabricatorFeedStory) {
|
|
|
|
return $item->getChronologicalKey();
|
|
|
|
}
|
|
|
|
return $item['chronologicalKey'];
|
2011-07-05 17:35:18 +02:00
|
|
|
}
|
2012-07-02 19:37:22 +02:00
|
|
|
|
2011-07-05 17:35:18 +02:00
|
|
|
}
|