2011-07-11 17:54:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-09-14 17:02:31 +02:00
|
|
|
/**
|
|
|
|
* @group phriction
|
|
|
|
*/
|
2011-07-11 17:54:22 +02:00
|
|
|
class PhrictionDocumentController
|
|
|
|
extends PhrictionController {
|
|
|
|
|
|
|
|
private $slug;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->slug = $data['slug'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
2011-07-12 05:14:46 +02:00
|
|
|
$request = $this->getRequest();
|
2011-07-13 03:03:20 +02:00
|
|
|
$user = $request->getUser();
|
2011-07-12 05:14:46 +02:00
|
|
|
|
2011-07-11 17:54:22 +02:00
|
|
|
$slug = PhrictionDocument::normalizeSlug($this->slug);
|
|
|
|
if ($slug != $this->slug) {
|
|
|
|
$uri = PhrictionDocument::getSlugURI($slug);
|
|
|
|
// Canonicalize pages to their one true URI.
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
|
|
|
}
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
require_celerity_resource('phriction-document-css');
|
|
|
|
|
|
|
|
$document = id(new PhrictionDocument())->loadOneWhere(
|
|
|
|
'slug = %s',
|
|
|
|
$slug);
|
|
|
|
|
2011-07-12 17:08:03 +02:00
|
|
|
$breadcrumbs = $this->renderBreadcrumbs($slug);
|
2011-07-13 03:03:20 +02:00
|
|
|
$version_note = null;
|
2011-07-12 17:08:03 +02:00
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
if (!$document) {
|
2011-07-12 05:14:46 +02:00
|
|
|
$create_uri = '/phriction/edit/?slug='.$slug;
|
|
|
|
|
|
|
|
$page_content =
|
|
|
|
'<div class="phriction-content">'.
|
|
|
|
'<em>No content here!</em><br />'.
|
|
|
|
'No document found at <tt>'.phutil_escape_html($slug).'</tt>. '.
|
|
|
|
'You can <strong>'.
|
|
|
|
phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $create_uri,
|
|
|
|
),
|
|
|
|
'create a new document').'</strong>.'.
|
|
|
|
'</div>';
|
2011-07-11 21:34:53 +02:00
|
|
|
$page_title = 'Page Not Found';
|
|
|
|
$button = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
2011-07-12 05:14:46 +02:00
|
|
|
'href' => $create_uri,
|
2011-07-11 21:34:53 +02:00
|
|
|
'class' => 'green button',
|
|
|
|
),
|
|
|
|
'Create Page');
|
2011-12-15 21:00:17 +01:00
|
|
|
$buttons = $button;
|
2011-07-11 21:34:53 +02:00
|
|
|
} else {
|
2011-07-13 03:03:20 +02:00
|
|
|
$version = $request->getInt('v');
|
|
|
|
if ($version) {
|
|
|
|
$content = id(new PhrictionContent())->loadOneWhere(
|
|
|
|
'documentID = %d AND version = %d',
|
|
|
|
$document->getID(),
|
|
|
|
$version);
|
|
|
|
if (!$content) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($content->getID() != $document->getContentID()) {
|
|
|
|
$version_note = new AphrontErrorView();
|
|
|
|
$version_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
|
|
|
$version_note->setTitle('Older Version');
|
|
|
|
$version_note->appendChild(
|
|
|
|
'You are viewing an older version of this document, as it '.
|
|
|
|
'appeared on '.
|
|
|
|
phabricator_datetime($content->getDateCreated(), $user).'.');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$content = id(new PhrictionContent())->load($document->getContentID());
|
|
|
|
}
|
2011-07-11 21:34:53 +02:00
|
|
|
$page_title = $content->getTitle();
|
2011-07-12 05:14:46 +02:00
|
|
|
|
|
|
|
$phids = array($content->getAuthorPHID());
|
|
|
|
$handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
|
|
|
|
|
|
|
|
$age = time() - $content->getDateCreated();
|
|
|
|
$age = floor($age / (60 * 60 * 24));
|
|
|
|
|
|
|
|
if ($age < 1) {
|
|
|
|
$when = 'today';
|
|
|
|
} else if ($age == 1) {
|
|
|
|
$when = 'yesterday';
|
|
|
|
} else {
|
|
|
|
$when = "{$age} days ago";
|
|
|
|
}
|
|
|
|
|
|
|
|
$byline =
|
|
|
|
'<div class="phriction-byline">'.
|
|
|
|
"Last updated {$when} by ".
|
|
|
|
$handles[$content->getAuthorPHID()]->renderLink().'.'.
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
$engine = PhabricatorMarkupEngine::newPhrictionMarkupEngine();
|
|
|
|
|
2011-12-17 18:19:08 +01:00
|
|
|
$doc_status = $document->getStatus();
|
|
|
|
if ($doc_status == PhrictionDocumentStatus::STATUS_EXISTS) {
|
|
|
|
$core_content =
|
|
|
|
'<div class="phabricator-remarkup">'.
|
|
|
|
$engine->markupText($content->getContent()).
|
|
|
|
'</div>';
|
|
|
|
} else if ($doc_status == PhrictionDocumentStatus::STATUS_DELETED) {
|
|
|
|
$notice = new AphrontErrorView();
|
|
|
|
$notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
|
|
|
$notice->setTitle('Document Deleted');
|
|
|
|
$notice->appendChild(
|
|
|
|
'This document has been deleted. You can edit it to put new content '.
|
|
|
|
'here, or use history to revert to an earlier version.');
|
|
|
|
$core_content = $notice->render();
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unknown document status '{$doc_status}'!");
|
|
|
|
}
|
|
|
|
|
2011-07-12 05:14:46 +02:00
|
|
|
$page_content =
|
|
|
|
'<div class="phriction-content">'.
|
|
|
|
$byline.
|
2011-12-17 18:19:08 +01:00
|
|
|
$core_content.
|
2011-07-12 05:14:46 +02:00
|
|
|
'</div>';
|
|
|
|
|
2011-12-15 21:00:17 +01:00
|
|
|
$edit_button = phutil_render_tag(
|
2011-07-11 21:34:53 +02:00
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '/phriction/edit/'.$document->getID().'/',
|
|
|
|
'class' => 'button',
|
|
|
|
),
|
2011-12-17 18:19:08 +01:00
|
|
|
'Edit Document');
|
2011-12-15 21:00:17 +01:00
|
|
|
$history_button = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => PhrictionDocument::getSlugURI($slug, 'history'),
|
|
|
|
'class' => 'button grey',
|
|
|
|
),
|
|
|
|
'View History');
|
2011-12-15 23:37:44 +01:00
|
|
|
// these float right so history_button which is right most goes first
|
2011-12-15 21:00:17 +01:00
|
|
|
$buttons = $history_button.$edit_button;
|
2011-07-11 21:34:53 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 03:03:20 +02:00
|
|
|
if ($version_note) {
|
|
|
|
$version_note = $version_note->render();
|
|
|
|
}
|
|
|
|
|
2011-07-15 23:56:39 +02:00
|
|
|
$children = $this->renderChildren($slug);
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
$page =
|
|
|
|
'<div class="phriction-header">'.
|
2011-12-15 21:00:17 +01:00
|
|
|
$buttons.
|
2011-07-11 21:34:53 +02:00
|
|
|
'<h1>'.phutil_escape_html($page_title).'</h1>'.
|
2011-07-12 17:08:03 +02:00
|
|
|
$breadcrumbs.
|
2011-07-11 21:34:53 +02:00
|
|
|
'</div>'.
|
2011-07-13 03:03:20 +02:00
|
|
|
$version_note.
|
2011-07-15 23:56:39 +02:00
|
|
|
$page_content.
|
|
|
|
$children;
|
2011-07-11 17:54:22 +02:00
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
2011-07-11 21:34:53 +02:00
|
|
|
$page,
|
2011-07-11 17:54:22 +02:00
|
|
|
array(
|
2011-07-12 00:06:19 +02:00
|
|
|
'title' => 'Phriction - '.$page_title,
|
2011-07-11 17:54:22 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-12 17:08:03 +02:00
|
|
|
private function renderBreadcrumbs($slug) {
|
|
|
|
|
|
|
|
$ancestor_handles = array();
|
|
|
|
$ancestral_slugs = PhrictionDocument::getSlugAncestry($slug);
|
|
|
|
$ancestral_slugs[] = $slug;
|
|
|
|
if ($ancestral_slugs) {
|
|
|
|
$empty_slugs = array_fill_keys($ancestral_slugs, null);
|
|
|
|
$ancestors = id(new PhrictionDocument())->loadAllWhere(
|
|
|
|
'slug IN (%Ls)',
|
|
|
|
$ancestral_slugs);
|
|
|
|
$ancestors = mpull($ancestors, null, 'getSlug');
|
|
|
|
|
|
|
|
$ancestor_phids = mpull($ancestors, 'getPHID');
|
|
|
|
$handles = array();
|
|
|
|
if ($ancestor_phids) {
|
|
|
|
$handles = id(new PhabricatorObjectHandleData($ancestor_phids))
|
|
|
|
->loadHandles();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ancestor_handles = array();
|
|
|
|
foreach ($ancestral_slugs as $slug) {
|
|
|
|
if (isset($ancestors[$slug])) {
|
|
|
|
$ancestor_handles[] = $handles[$ancestors[$slug]->getPHID()];
|
|
|
|
} else {
|
|
|
|
$handle = new PhabricatorObjectHandle();
|
|
|
|
$handle->setName(PhrictionDocument::getDefaultSlugTitle($slug));
|
|
|
|
$handle->setURI(PhrictionDocument::getSlugURI($slug));
|
|
|
|
$ancestor_handles[] = $handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$breadcrumbs = array();
|
|
|
|
foreach ($ancestor_handles as $ancestor_handle) {
|
|
|
|
$breadcrumbs[] = $ancestor_handle->renderLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '/phriction/',
|
|
|
|
),
|
|
|
|
'Document Index');
|
|
|
|
|
|
|
|
return
|
|
|
|
'<div class="phriction-breadcrumbs">'.
|
|
|
|
$list.' · '.
|
|
|
|
'<span class="phriction-document-crumbs">'.
|
|
|
|
implode(" \xC2\xBB ", $breadcrumbs).
|
|
|
|
'</span>'.
|
|
|
|
'</div>';
|
|
|
|
}
|
|
|
|
|
2011-07-15 23:56:39 +02:00
|
|
|
private function renderChildren($slug) {
|
|
|
|
$document_dao = new PhrictionDocument();
|
|
|
|
$content_dao = new PhrictionContent();
|
|
|
|
$conn = $document_dao->establishConnection('r');
|
|
|
|
|
|
|
|
$limit = 50;
|
|
|
|
$d_child = PhrictionDocument::getSlugDepth($slug) + 1;
|
|
|
|
$d_grandchild = PhrictionDocument::getSlugDepth($slug) + 2;
|
|
|
|
|
|
|
|
// Select children and grandchildren.
|
|
|
|
$children = queryfx_all(
|
|
|
|
$conn,
|
|
|
|
'SELECT d.slug, d.depth, c.title FROM %T d JOIN %T c
|
|
|
|
ON d.contentID = c.id
|
|
|
|
WHERE d.slug LIKE %> AND d.depth IN (%d, %d)
|
2011-12-17 18:19:08 +01:00
|
|
|
AND d.status = %d
|
2011-07-15 23:56:39 +02:00
|
|
|
ORDER BY d.depth, c.title LIMIT %d',
|
|
|
|
$document_dao->getTableName(),
|
|
|
|
$content_dao->getTableName(),
|
|
|
|
($slug == '/' ? '' : $slug),
|
|
|
|
$d_child,
|
|
|
|
$d_grandchild,
|
2011-12-17 18:19:08 +01:00
|
|
|
PhrictionDocumentStatus::STATUS_EXISTS,
|
2011-07-15 23:56:39 +02:00
|
|
|
$limit);
|
|
|
|
|
|
|
|
if (!$children) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to render in one of three modes to try to accommodate
|
|
|
|
// different information scales:
|
|
|
|
//
|
|
|
|
// - If we found fewer than $limit rows, we know we have all the children
|
|
|
|
// and grandchildren and there aren't all that many. We can just render
|
|
|
|
// everything.
|
|
|
|
// - If we found $limit rows but the results included some grandchildren,
|
|
|
|
// we just throw them out and render only the children, as we know we
|
|
|
|
// have them all.
|
|
|
|
// - If we found $limit rows and the results have no grandchildren, we
|
|
|
|
// have a ton of children. Render them and then let the user know that
|
|
|
|
// this is not an exhaustive list.
|
|
|
|
|
|
|
|
if (count($children) == $limit) {
|
|
|
|
$more_children = true;
|
|
|
|
foreach ($children as $child) {
|
|
|
|
if ($child['depth'] == $d_grandchild) {
|
|
|
|
$more_children = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$show_grandchildren = false;
|
|
|
|
} else {
|
|
|
|
$show_grandchildren = true;
|
|
|
|
$more_children = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grandchildren = array();
|
|
|
|
foreach ($children as $key => $child) {
|
|
|
|
if ($child['depth'] == $d_child) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
unset($children[$key]);
|
|
|
|
if ($show_grandchildren) {
|
|
|
|
$ancestors = PhrictionDocument::getSlugAncestry($child['slug']);
|
|
|
|
$grandchildren[end($ancestors)][] = $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in any missing children.
|
|
|
|
$known_slugs = ipull($children, null, 'slug');
|
|
|
|
foreach ($grandchildren as $slug => $ignored) {
|
|
|
|
if (empty($known_slugs[$slug])) {
|
|
|
|
$children[] = array(
|
|
|
|
'slug' => $slug,
|
|
|
|
'depth' => $d_child,
|
|
|
|
'title' => PhrictionDocument::getDefaultSlugTitle($slug),
|
|
|
|
'empty' => true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = array();
|
|
|
|
$list[] = '<ul>';
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$list[] = $this->renderChildDocumentLink($child);
|
|
|
|
$grand = idx($grandchildren, $child['slug'], array());
|
|
|
|
if ($grand) {
|
|
|
|
$list[] = '<ul>';
|
|
|
|
foreach ($grand as $grandchild) {
|
|
|
|
$list[] = $this->renderChildDocumentLink($grandchild);
|
|
|
|
}
|
|
|
|
$list[] = '</ul>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($more_children) {
|
|
|
|
$list[] = '<li>More...</li>';
|
|
|
|
}
|
|
|
|
$list[] = '</ul>';
|
|
|
|
$list = implode("\n", $list);
|
|
|
|
|
|
|
|
return
|
|
|
|
'<div class="phriction-children">'.
|
|
|
|
'<div class="phriction-children-header">Document Hierarchy</div>'.
|
|
|
|
$list.
|
|
|
|
'</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renderChildDocumentLink(array $info) {
|
2011-12-17 18:19:08 +01:00
|
|
|
$title = nonempty($info['title'], '(Untitled Document)');
|
2011-07-15 23:56:39 +02:00
|
|
|
$item = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => PhrictionDocument::getSlugURI($info['slug']),
|
|
|
|
),
|
2011-12-17 18:19:08 +01:00
|
|
|
phutil_escape_html($title));
|
2011-07-15 23:56:39 +02:00
|
|
|
|
|
|
|
if (isset($info['empty'])) {
|
|
|
|
$item = '<em>'.$item.'</em>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<li>'.$item.'</li>';
|
|
|
|
}
|
|
|
|
|
2011-07-11 17:54:22 +02:00
|
|
|
}
|