Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / phame / query / PhamePostQuery.php
blobd7396e553fc7afba0a32b8cf8f47469af808e528
1 <?php
3 final class PhamePostQuery extends PhabricatorCursorPagedPolicyAwareQuery {
5 private $ids;
6 private $blogPHIDs;
7 private $bloggerPHIDs;
8 private $visibility;
9 private $publishedAfter;
10 private $phids;
12 private $needHeaderImage;
14 public function withIDs(array $ids) {
15 $this->ids = $ids;
16 return $this;
19 public function withPHIDs(array $phids) {
20 $this->phids = $phids;
21 return $this;
24 public function withBloggerPHIDs(array $blogger_phids) {
25 $this->bloggerPHIDs = $blogger_phids;
26 return $this;
29 public function withBlogPHIDs(array $blog_phids) {
30 $this->blogPHIDs = $blog_phids;
31 return $this;
34 public function withVisibility(array $visibility) {
35 $this->visibility = $visibility;
36 return $this;
39 public function withPublishedAfter($time) {
40 $this->publishedAfter = $time;
41 return $this;
44 public function needHeaderImage($need) {
45 $this->needHeaderImage = $need;
46 return $this;
49 public function newResultObject() {
50 return new PhamePost();
53 protected function loadPage() {
54 return $this->loadStandardPage($this->newResultObject());
57 protected function willFilterPage(array $posts) {
58 // We require blogs to do visibility checks, so load them unconditionally.
59 $blog_phids = mpull($posts, 'getBlogPHID');
61 $blogs = id(new PhameBlogQuery())
62 ->setViewer($this->getViewer())
63 ->needProfileImage(true)
64 ->withPHIDs($blog_phids)
65 ->execute();
67 $blogs = mpull($blogs, null, 'getPHID');
68 foreach ($posts as $key => $post) {
69 $blog_phid = $post->getBlogPHID();
71 $blog = idx($blogs, $blog_phid);
72 if (!$blog) {
73 $this->didRejectResult($post);
74 unset($posts[$key]);
75 continue;
78 $post->attachBlog($blog);
81 if ($this->needHeaderImage) {
82 $file_phids = mpull($posts, 'getHeaderImagePHID');
83 $file_phids = array_filter($file_phids);
84 if ($file_phids) {
85 $files = id(new PhabricatorFileQuery())
86 ->setParentQuery($this)
87 ->setViewer($this->getViewer())
88 ->withPHIDs($file_phids)
89 ->execute();
90 $files = mpull($files, null, 'getPHID');
91 } else {
92 $files = array();
95 foreach ($posts as $post) {
96 $file = idx($files, $post->getHeaderImagePHID());
97 if ($file) {
98 $post->attachHeaderImageFile($file);
103 return $posts;
106 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
107 $where = parent::buildWhereClauseParts($conn);
109 if ($this->ids !== null) {
110 $where[] = qsprintf(
111 $conn,
112 'p.id IN (%Ld)',
113 $this->ids);
116 if ($this->phids !== null) {
117 $where[] = qsprintf(
118 $conn,
119 'p.phid IN (%Ls)',
120 $this->phids);
123 if ($this->bloggerPHIDs !== null) {
124 $where[] = qsprintf(
125 $conn,
126 'p.bloggerPHID IN (%Ls)',
127 $this->bloggerPHIDs);
130 if ($this->visibility !== null) {
131 $where[] = qsprintf(
132 $conn,
133 'p.visibility IN (%Ld)',
134 $this->visibility);
137 if ($this->publishedAfter !== null) {
138 $where[] = qsprintf(
139 $conn,
140 'p.datePublished > %d',
141 $this->publishedAfter);
144 if ($this->blogPHIDs !== null) {
145 $where[] = qsprintf(
146 $conn,
147 'p.blogPHID in (%Ls)',
148 $this->blogPHIDs);
151 return $where;
154 public function getBuiltinOrders() {
155 return array(
156 'datePublished' => array(
157 'vector' => array('datePublished', 'id'),
158 'name' => pht('Publish Date'),
160 ) + parent::getBuiltinOrders();
163 public function getOrderableColumns() {
164 return parent::getOrderableColumns() + array(
165 'datePublished' => array(
166 'table' => $this->getPrimaryTableAlias(),
167 'column' => 'datePublished',
168 'type' => 'int',
169 'reverse' => false,
174 protected function newPagingMapFromPartialObject($object) {
175 return array(
176 'id' => (int)$object->getID(),
177 'datePublished' => (int)$object->getDatePublished(),
181 public function getQueryApplicationClass() {
182 // TODO: Does setting this break public blogs?
183 return null;
186 protected function getPrimaryTableAlias() {
187 return 'p';