Files
phabricator/src/applications/search/query/PhabricatorSearchDocumentQuery.php
epriestley 8efdc4aabf Replace getPagingValue() with cursor methods
Summary:
Ref T7803. Prior to this change sequence, Query classes conflated paging values (the actual thing that goes in a "x > 3" clause) with cursor values (arbitrary identifiers which track where the user is in a result list).

Although the two can sometimes be the same, the vast majority of implementations are simpler and better when object IDs are used as cursors and paging values are derived from them.

The new stuff handles this in a consistent way, so we're free to separate getPagingValue() from paging. The new method is essentially getResultCursor().

This also implements getPageCursors(), which allows queries to return directional cursors. The inability to do this was a practical limitation blocking the implementation of T7803.

Test Plan:
  - Browsed a bunch of results and paged through queries.
  - Grepped for removed methods.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7803

Differential Revision: https://secure.phabricator.com/D12383
2015-04-13 11:58:38 -07:00

85 lines
2.1 KiB
PHP

<?php
final class PhabricatorSearchDocumentQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $savedQuery;
public function withSavedQuery(PhabricatorSavedQuery $query) {
$this->savedQuery = $query;
return $this;
}
protected function loadPage() {
$phids = $this->loadDocumentPHIDsWithoutPolicyChecks();
$handles = id(new PhabricatorHandleQuery())
->setViewer($this->getViewer())
->withPHIDs($phids)
->execute();
// Retain engine order.
$handles = array_select_keys($handles, $phids);
return $handles;
}
protected function willFilterPage(array $handles) {
// NOTE: This is used by the object selector dialog to exclude the object
// you're looking at, so that, e.g., a task can't be set as a dependency
// of itself in the UI.
// TODO: Remove this after object selection moves to ApplicationSearch.
$exclude = array();
if ($this->savedQuery) {
$exclude_phids = $this->savedQuery->getParameter('excludePHIDs', array());
$exclude = array_fuse($exclude_phids);
}
foreach ($handles as $key => $handle) {
if (!$handle->isComplete()) {
unset($handles[$key]);
continue;
}
if ($handle->getPolicyFiltered()) {
unset($handles[$key]);
continue;
}
if (isset($exclude[$handle->getPHID()])) {
unset($handles[$key]);
continue;
}
}
return $handles;
}
public function loadDocumentPHIDsWithoutPolicyChecks() {
$query = id(clone($this->savedQuery))
->setParameter('offset', $this->getOffset())
->setParameter('limit', $this->getRawResultLimit());
$engine = PhabricatorSearchEngineSelector::newSelector()->newEngine();
return $engine->executeSearch($query);
}
public function getQueryApplicationClass() {
return 'PhabricatorSearchApplication';
}
protected function getResultCursor($result) {
throw new Exception(
pht(
'This query does not support cursor paging; it must be offset '.
'paged.'));
}
protected function nextPage(array $page) {
$this->setOffset($this->getOffset() + count($page));
return $this;
}
}