@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @extends PhabricatorCursorPagedPolicyAwareQuery<PhamePost>
5 */
6final class PhamePostQuery extends PhabricatorCursorPagedPolicyAwareQuery {
7
8 private $ids;
9 private $blogPHIDs;
10 private $bloggerPHIDs;
11 private $visibility;
12 private $publishedAfter;
13 private $phids;
14
15 private $needHeaderImage;
16
17 public function withIDs(array $ids) {
18 $this->ids = $ids;
19 return $this;
20 }
21
22 public function withPHIDs(array $phids) {
23 $this->phids = $phids;
24 return $this;
25 }
26
27 public function withBloggerPHIDs(array $blogger_phids) {
28 $this->bloggerPHIDs = $blogger_phids;
29 return $this;
30 }
31
32 public function withBlogPHIDs(array $blog_phids) {
33 $this->blogPHIDs = $blog_phids;
34 return $this;
35 }
36
37 public function withVisibility(array $visibility) {
38 $this->visibility = $visibility;
39 return $this;
40 }
41
42 public function withPublishedAfter($time) {
43 $this->publishedAfter = $time;
44 return $this;
45 }
46
47 public function needHeaderImage($need) {
48 $this->needHeaderImage = $need;
49 return $this;
50 }
51
52 public function newResultObject() {
53 return new PhamePost();
54 }
55
56 protected function willFilterPage(array $posts) {
57 // We require blogs to do visibility checks, so load them unconditionally.
58 $blog_phids = mpull($posts, 'getBlogPHID');
59
60 $blogs = id(new PhameBlogQuery())
61 ->setViewer($this->getViewer())
62 ->needProfileImage(true)
63 ->withPHIDs($blog_phids)
64 ->execute();
65
66 $blogs = mpull($blogs, null, 'getPHID');
67 foreach ($posts as $key => $post) {
68 $blog_phid = $post->getBlogPHID();
69
70 $blog = idx($blogs, $blog_phid);
71 if (!$blog) {
72 $this->didRejectResult($post);
73 unset($posts[$key]);
74 continue;
75 }
76
77 $post->attachBlog($blog);
78 }
79
80 if ($this->needHeaderImage) {
81 $file_phids = mpull($posts, 'getHeaderImagePHID');
82 $file_phids = array_filter($file_phids);
83 if ($file_phids) {
84 $files = id(new PhabricatorFileQuery())
85 ->setParentQuery($this)
86 ->setViewer($this->getViewer())
87 ->withPHIDs($file_phids)
88 ->execute();
89 $files = mpull($files, null, 'getPHID');
90 } else {
91 $files = array();
92 }
93
94 foreach ($posts as $post) {
95 $file = idx($files, $post->getHeaderImagePHID());
96 if ($file) {
97 $post->attachHeaderImageFile($file);
98 }
99 }
100 }
101
102 return $posts;
103 }
104
105 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
106 $where = parent::buildWhereClauseParts($conn);
107
108 if ($this->ids !== null) {
109 $where[] = qsprintf(
110 $conn,
111 'p.id IN (%Ld)',
112 $this->ids);
113 }
114
115 if ($this->phids !== null) {
116 $where[] = qsprintf(
117 $conn,
118 'p.phid IN (%Ls)',
119 $this->phids);
120 }
121
122 if ($this->bloggerPHIDs !== null) {
123 $where[] = qsprintf(
124 $conn,
125 'p.bloggerPHID IN (%Ls)',
126 $this->bloggerPHIDs);
127 }
128
129 if ($this->visibility !== null) {
130 $where[] = qsprintf(
131 $conn,
132 'p.visibility IN (%Ld)',
133 $this->visibility);
134 }
135
136 if ($this->publishedAfter !== null) {
137 $where[] = qsprintf(
138 $conn,
139 'p.datePublished > %d',
140 $this->publishedAfter);
141 }
142
143 if ($this->blogPHIDs !== null) {
144 $where[] = qsprintf(
145 $conn,
146 'p.blogPHID in (%Ls)',
147 $this->blogPHIDs);
148 }
149
150 return $where;
151 }
152
153 public function getBuiltinOrders() {
154 return array(
155 'datePublished' => array(
156 'vector' => array('datePublished', 'id'),
157 'name' => pht('Publish Date'),
158 ),
159 ) + parent::getBuiltinOrders();
160 }
161
162 public function getOrderableColumns() {
163 return parent::getOrderableColumns() + array(
164 'datePublished' => array(
165 'table' => $this->getPrimaryTableAlias(),
166 'column' => 'datePublished',
167 'type' => 'int',
168 'reverse' => false,
169 ),
170 );
171 }
172
173 protected function newPagingMapFromPartialObject($object) {
174 return array(
175 'id' => (int)$object->getID(),
176 'datePublished' => (int)$object->getDatePublished(),
177 );
178 }
179
180 public function getQueryApplicationClass() {
181 // TODO: Does setting this break public blogs?
182 return null;
183 }
184
185 protected function getPrimaryTableAlias() {
186 return 'p';
187 }
188
189}