Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / phame / controller / PhameLiveController.php
blob472f73c8c1f427e281c8a38f09d6625c01d7715d
1 <?php
3 abstract class PhameLiveController extends PhameController {
5 private $isExternal;
6 private $isLive;
7 private $blog;
8 private $post;
10 public function shouldAllowPublic() {
11 return true;
14 protected function getIsExternal() {
15 return $this->isExternal;
18 protected function getIsLive() {
19 return $this->isLive;
22 protected function getBlog() {
23 return $this->blog;
26 protected function getPost() {
27 return $this->post;
30 protected function setupLiveEnvironment() {
31 $request = $this->getRequest();
32 $viewer = $this->getViewer();
34 $site = $request->getSite();
35 $blog_id = $request->getURIData('blogID');
36 $post_id = $request->getURIData('id');
38 if ($site instanceof PhameBlogSite) {
39 // This is a live page on a custom domain. We already looked up the blog
40 // in the Site handler by examining the domain, so we don't need to do
41 // more lookups.
43 $blog = $site->getBlog();
44 $is_external = true;
45 $is_live = true;
46 } else if ($blog_id) {
47 // This is a blog detail view, an internal blog live view, or an
48 // internal post live view The internal post detail view is handled
49 // below.
51 $is_external = false;
52 if ($request->getURIData('live')) {
53 $is_live = true;
54 } else {
55 $is_live = false;
58 $blog_query = id(new PhameBlogQuery())
59 ->setViewer($viewer)
60 ->needProfileImage(true)
61 ->needHeaderImage(true)
62 ->withIDs(array($blog_id));
64 // If this is a live view, only show active blogs.
65 if ($is_live) {
66 $blog_query->withStatuses(
67 array(
68 PhameBlog::STATUS_ACTIVE,
69 ));
72 $blog = $blog_query->executeOne();
73 if (!$blog) {
74 $this->isExternal = $is_external;
75 $this->isLive = $is_live;
76 return new Aphront404Response();
79 } else {
80 // This is a post detail page, so we'll figure out the blog by loading
81 // the post first.
82 $is_external = false;
83 $is_live = false;
84 $blog = null;
87 $this->isExternal = $is_external;
88 $this->isLive = $is_live;
90 if (strlen($post_id)) {
91 $post_query = id(new PhamePostQuery())
92 ->setViewer($viewer)
93 ->needHeaderImage(true)
94 ->withIDs(array($post_id));
96 // Only show published posts on external domains.
97 if ($is_external) {
98 $post_query->withVisibility(
99 array(PhameConstants::VISIBILITY_PUBLISHED));
102 $post = $post_query->executeOne();
103 if (!$post) {
104 // Not a valid Post
105 $this->blog = $blog;
106 return new Aphront404Response();
109 // If this is a post detail page, the URI didn't come with a blog ID,
110 // so fill that in.
111 if (!$blog) {
112 $blog = $post->getBlog();
114 } else {
115 $post = null;
118 $this->blog = $blog;
119 $this->post = $post;
121 // If we have a post, canonicalize the URI to the post's current slug and
122 // redirect the user if it isn't correct. Likewise, canonicalize the URI
123 // if the blog ID is wrong. See T13353.
124 if ($post) {
125 $slug = $request->getURIData('slug');
127 $wrong_slug = ($post->getSlug() !== $slug);
128 $wrong_blog = ($post->getBlog()->getID() !== $blog->getID());
130 if ($wrong_slug || $wrong_blog) {
131 if ($is_live) {
132 if ($is_external) {
133 $uri = $post->getExternalLiveURI();
134 } else {
135 $uri = $post->getInternalLiveURI();
137 } else {
138 $uri = $post->getViewURI();
141 $response = id(new AphrontRedirectResponse())
142 ->setURI($uri);
144 if ($is_external) {
145 $response->setIsExternal(true);
148 return $response;
152 return null;
155 protected function buildApplicationCrumbs() {
156 $blog = $this->getBlog();
157 $post = $this->getPost();
159 $is_live = $this->getIsLive();
160 $is_external = $this->getIsExternal();
162 // If this is an external view, don't put the "Phame" crumb or the
163 // "Blogs" crumb into the crumbs list.
164 if ($is_external) {
165 $crumbs = new PHUICrumbsView();
166 // Link back to parent site
167 if ($blog->getParentSite() && $blog->getParentDomain()) {
168 $crumbs->addTextCrumb(
169 $blog->getParentSite(),
170 $blog->getExternalParentURI());
172 } else {
173 $crumbs = parent::buildApplicationCrumbs();
174 $crumbs->addTextCrumb(
175 pht('Blogs'),
176 $this->getApplicationURI('blog/'));
179 $crumbs->setBorder(true);
181 if ($blog) {
182 if ($post) {
183 if ($is_live) {
184 if ($is_external) {
185 $blog_uri = $blog->getExternalLiveURI();
186 } else {
187 $blog_uri = $blog->getInternalLiveURI();
189 } else {
190 $blog_uri = $blog->getViewURI();
192 } else {
193 $blog_uri = null;
196 $crumbs->addTextCrumb($blog->getName(), $blog_uri);
199 if ($post) {
200 if (!$is_external) {
201 $crumbs->addTextCrumb('J'.$post->getID());
205 return $crumbs;
208 public function willSendResponse(AphrontResponse $response) {
209 if ($this->getIsExternal()) {
210 if ($response instanceof Aphront404Response) {
211 $page = $this->newPage()
212 ->setCrumbs($this->buildApplicationCrumbs());
214 $response = id(new Phame404Response())
215 ->setPage($page);
219 return parent::willSendResponse($response);
222 public function newPage() {
223 $page = parent::newPage();
225 if ($this->getIsLive()) {
226 $page
227 ->addClass('phame-live-view')
228 ->setShowChrome(false)
229 ->setShowFooter(false);
232 return $page;