Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / phame / query / PhamePostQuery.php
blob61ee4f2a253594e2faf26be44e2784a06b5dd4a9
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 willFilterPage(array $posts) {
54 // We require blogs to do visibility checks, so load them unconditionally.
55 $blog_phids = mpull($posts, 'getBlogPHID');
57 $blogs = id(new PhameBlogQuery())
58 ->setViewer($this->getViewer())
59 ->needProfileImage(true)
60 ->withPHIDs($blog_phids)
61 ->execute();
63 $blogs = mpull($blogs, null, 'getPHID');
64 foreach ($posts as $key => $post) {
65 $blog_phid = $post->getBlogPHID();
67 $blog = idx($blogs, $blog_phid);
68 if (!$blog) {
69 $this->didRejectResult($post);
70 unset($posts[$key]);
71 continue;
74 $post->attachBlog($blog);
77 if ($this->needHeaderImage) {
78 $file_phids = mpull($posts, 'getHeaderImagePHID');
79 $file_phids = array_filter($file_phids);
80 if ($file_phids) {
81 $files = id(new PhabricatorFileQuery())
82 ->setParentQuery($this)
83 ->setViewer($this->getViewer())
84 ->withPHIDs($file_phids)
85 ->execute();
86 $files = mpull($files, null, 'getPHID');
87 } else {
88 $files = array();
91 foreach ($posts as $post) {
92 $file = idx($files, $post->getHeaderImagePHID());
93 if ($file) {
94 $post->attachHeaderImageFile($file);
99 return $posts;
102 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
103 $where = parent::buildWhereClauseParts($conn);
105 if ($this->ids !== null) {
106 $where[] = qsprintf(
107 $conn,
108 'p.id IN (%Ld)',
109 $this->ids);
112 if ($this->phids !== null) {
113 $where[] = qsprintf(
114 $conn,
115 'p.phid IN (%Ls)',
116 $this->phids);
119 if ($this->bloggerPHIDs !== null) {
120 $where[] = qsprintf(
121 $conn,
122 'p.bloggerPHID IN (%Ls)',
123 $this->bloggerPHIDs);
126 if ($this->visibility !== null) {
127 $where[] = qsprintf(
128 $conn,
129 'p.visibility IN (%Ld)',
130 $this->visibility);
133 if ($this->publishedAfter !== null) {
134 $where[] = qsprintf(
135 $conn,
136 'p.datePublished > %d',
137 $this->publishedAfter);
140 if ($this->blogPHIDs !== null) {
141 $where[] = qsprintf(
142 $conn,
143 'p.blogPHID in (%Ls)',
144 $this->blogPHIDs);
147 return $where;
150 public function getBuiltinOrders() {
151 return array(
152 'datePublished' => array(
153 'vector' => array('datePublished', 'id'),
154 'name' => pht('Publish Date'),
156 ) + parent::getBuiltinOrders();
159 public function getOrderableColumns() {
160 return parent::getOrderableColumns() + array(
161 'datePublished' => array(
162 'table' => $this->getPrimaryTableAlias(),
163 'column' => 'datePublished',
164 'type' => 'int',
165 'reverse' => false,
170 protected function newPagingMapFromPartialObject($object) {
171 return array(
172 'id' => (int)$object->getID(),
173 'datePublished' => (int)$object->getDatePublished(),
177 public function getQueryApplicationClass() {
178 // TODO: Does setting this break public blogs?
179 return null;
182 protected function getPrimaryTableAlias() {
183 return 'p';