2011-07-05 17:35:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-02-16 02:48:14 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-07-05 17:35:18 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
final class PhabricatorFeedQuery {
|
|
|
|
|
|
|
|
private $filterPHIDs;
|
|
|
|
private $limit = 100;
|
|
|
|
private $after;
|
2012-02-16 02:48:14 +01:00
|
|
|
private $before;
|
2011-07-05 17:35:18 +02:00
|
|
|
|
|
|
|
public function setFilterPHIDs(array $phids) {
|
|
|
|
$this->filterPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLimit($limit) {
|
|
|
|
$this->limit = $limit;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAfter($after) {
|
|
|
|
$this->after = $after;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-16 02:48:14 +01:00
|
|
|
public function setBefore($before) {
|
|
|
|
$this->before = $before;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-07-05 17:35:18 +02:00
|
|
|
public function execute() {
|
|
|
|
|
|
|
|
$ref_table = new PhabricatorFeedStoryReference();
|
|
|
|
$story_table = new PhabricatorFeedStoryData();
|
|
|
|
|
|
|
|
$conn = $story_table->establishConnection('r');
|
|
|
|
|
|
|
|
$where = array();
|
|
|
|
if ($this->filterPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'ref.objectPHID IN (%Ls)',
|
|
|
|
$this->filterPHIDs);
|
|
|
|
}
|
|
|
|
|
2012-02-16 02:48:14 +01:00
|
|
|
// For "before" queries, we can just add a constraint to the WHERE clause.
|
|
|
|
// For "after" queries, we must also reverse the result ordering, since
|
|
|
|
// otherwise we'll always grab the first page of results if there's a limit.
|
|
|
|
// After MySQL applies the limit, we reverse the page in PHP (below) to
|
|
|
|
// ensure consistent ordering.
|
|
|
|
|
|
|
|
$order = 'DESC';
|
|
|
|
|
|
|
|
if ($this->after) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'ref.chronologicalKey > %s',
|
|
|
|
$this->after);
|
|
|
|
$order = 'ASC';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->before) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'ref.chronologicalKey < %s',
|
|
|
|
$this->before);
|
|
|
|
}
|
|
|
|
|
2011-07-05 17:35:18 +02:00
|
|
|
if ($where) {
|
|
|
|
$where = 'WHERE ('.implode(') AND (', $where).')';
|
|
|
|
} else {
|
|
|
|
$where = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn,
|
|
|
|
'SELECT story.* FROM %T ref
|
|
|
|
JOIN %T story ON ref.chronologicalKey = story.chronologicalKey
|
|
|
|
%Q
|
2012-04-10 00:43:38 +02:00
|
|
|
GROUP BY ref.chronologicalKey
|
|
|
|
ORDER BY ref.chronologicalKey %Q
|
2011-07-05 17:35:18 +02:00
|
|
|
LIMIT %d',
|
|
|
|
$ref_table->getTableName(),
|
|
|
|
$story_table->getTableName(),
|
|
|
|
$where,
|
2012-02-16 02:48:14 +01:00
|
|
|
$order,
|
2011-07-05 17:35:18 +02:00
|
|
|
$this->limit);
|
2012-02-16 02:48:14 +01:00
|
|
|
|
|
|
|
if ($order != 'DESC') {
|
|
|
|
// If we did order ASC to pull 'after' data, reverse the result set so
|
|
|
|
// that stories are returned in a consistent (descending) order.
|
|
|
|
$data = array_reverse($data);
|
|
|
|
}
|
|
|
|
|
2012-07-02 19:37:22 +02:00
|
|
|
return PhabricatorFeedStory::loadAllFromRows($data);
|
2011-07-05 17:35:18 +02:00
|
|
|
}
|
2012-07-02 19:37:22 +02:00
|
|
|
|
2011-07-05 17:35:18 +02:00
|
|
|
}
|