2011-07-05 08:35:18 -07:00
|
|
|
<?php
|
|
|
|
|
|
2012-09-13 10:15:08 -07:00
|
|
|
final class PhabricatorFeedQuery
|
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2011-07-05 08:35:18 -07:00
|
|
|
|
|
|
|
|
private $filterPHIDs;
|
2013-06-25 16:29:47 -07:00
|
|
|
private $chronologicalKeys;
|
2011-07-05 08:35:18 -07:00
|
|
|
|
|
|
|
|
public function setFilterPHIDs(array $phids) {
|
|
|
|
|
$this->filterPHIDs = $phids;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 16:29:47 -07:00
|
|
|
public function withChronologicalKeys(array $keys) {
|
|
|
|
|
$this->chronologicalKeys = $keys;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 11:28:02 -08:00
|
|
|
protected function loadPage() {
|
2011-07-05 08:35:18 -07:00
|
|
|
|
2012-07-02 15:41:19 -07:00
|
|
|
$story_table = new PhabricatorFeedStoryData();
|
|
|
|
|
$conn = $story_table->establishConnection('r');
|
2011-07-05 08:35:18 -07:00
|
|
|
|
2012-07-02 15:41:19 -07: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 12:01:11 -07:00
|
|
|
return $data;
|
|
|
|
|
}
|
2012-07-02 15:41:19 -07: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 12:01:11 -07:00
|
|
|
protected function willFilterPage(array $data) {
|
2012-10-23 12:01:59 -07:00
|
|
|
return PhabricatorFeedStory::loadAllFromRows($data, $this->getViewer());
|
2012-02-15 17:48:14 -08:00
|
|
|
}
|
|
|
|
|
|
2012-07-02 15:41:19 -07: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 08:35:18 -07:00
|
|
|
|
|
|
|
|
$ref_table = new PhabricatorFeedStoryReference();
|
2012-07-02 15:41:19 -07:00
|
|
|
return qsprintf(
|
|
|
|
|
$conn_r,
|
|
|
|
|
'JOIN %T ref ON ref.chronologicalKey = story.chronologicalKey',
|
|
|
|
|
$ref_table->getTableName());
|
|
|
|
|
}
|
2011-07-05 08:35:18 -07:00
|
|
|
|
2012-07-02 15:41:19 -07:00
|
|
|
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
2011-07-05 08:35:18 -07:00
|
|
|
$where = array();
|
2012-07-02 15:41:19 -07:00
|
|
|
|
2011-07-05 08:35:18 -07:00
|
|
|
if ($this->filterPHIDs) {
|
|
|
|
|
$where[] = qsprintf(
|
2012-07-02 15:41:19 -07:00
|
|
|
$conn_r,
|
2011-07-05 08:35:18 -07:00
|
|
|
'ref.objectPHID IN (%Ls)',
|
|
|
|
|
$this->filterPHIDs);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 16:29:47 -07: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-02 15:41:19 -07:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
2012-02-15 17:48:14 -08:00
|
|
|
|
2012-07-02 15:41:19 -07:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
|
}
|
2012-02-15 17:48:14 -08:00
|
|
|
|
2012-07-02 15:41:19 -07:00
|
|
|
private function buildGroupClause(AphrontDatabaseConnection $conn_r) {
|
|
|
|
|
return qsprintf(
|
|
|
|
|
$conn_r,
|
2012-10-04 14:49:23 -07:00
|
|
|
'GROUP BY '.($this->filterPHIDs
|
|
|
|
|
? 'ref.chronologicalKey'
|
|
|
|
|
: 'story.chronologicalKey'));
|
2012-07-02 15:41:19 -07:00
|
|
|
}
|
2011-07-05 08:35:18 -07:00
|
|
|
|
2012-07-02 15:41:19 -07:00
|
|
|
protected function getPagingColumn() {
|
2012-10-04 14:49:23 -07:00
|
|
|
return ($this->filterPHIDs
|
|
|
|
|
? 'ref.chronologicalKey'
|
|
|
|
|
: 'story.chronologicalKey');
|
2012-07-02 15:41:19 -07:00
|
|
|
}
|
2012-02-15 17:48:14 -08:00
|
|
|
|
2012-07-02 15:41:19 -07: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 12:01:11 -07:00
|
|
|
if ($item instanceof PhabricatorFeedStory) {
|
|
|
|
|
return $item->getChronologicalKey();
|
|
|
|
|
}
|
|
|
|
|
return $item['chronologicalKey'];
|
2011-07-05 08:35:18 -07:00
|
|
|
}
|
2012-07-02 10:37:22 -07:00
|
|
|
|
Lock policy queries to their applications
Summary:
While we mostly have reasonable effective object accessibility when you lock a user out of an application, it's primarily enforced at the controller level. Users can still, e.g., load the handles of objects they can't actually see. Instead, lock the queries to the applications so that you can, e.g., never load a revision if you don't have access to Differential.
This has several parts:
- For PolicyAware queries, provide an application class name method.
- If the query specifies a class name and the user doesn't have permission to use it, fail the entire query unconditionally.
- For handles, simplify query construction and count all the PHIDs as "restricted" so we get a UI full of "restricted" instead of "unknown" handles.
Test Plan:
- Added a unit test to verify I got all the class names right.
- Browsed around, logged in/out as a normal user with public policies on and off.
- Browsed around, logged in/out as a restricted user with public policies on and off. With restrictions, saw all traces of restricted apps removed or restricted.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Differential Revision: https://secure.phabricator.com/D7367
2013-10-21 17:20:27 -07:00
|
|
|
|
|
|
|
|
public function getQueryApplicationClass() {
|
|
|
|
|
return 'PhabricatorApplicationFeed';
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-05 08:35:18 -07:00
|
|
|
}
|