From 602bc4e2fd5b5d0c194c4bac48b65282056ec0cd Mon Sep 17 00:00:00 2001 From: Chad Little Date: Wed, 22 Jul 2015 07:30:52 -0700 Subject: [PATCH] PhamePostSearchEngine Summary: A very basic search engine for Phame Posts Test Plan: Built a dashboard to test various queries. Saw posts. Reviewers: btrahan, epriestley Reviewed By: epriestley Subscribers: epriestley, Korvin Maniphest Tasks: T6269 Differential Revision: https://secure.phabricator.com/D13671 --- src/__phutil_library_map__.php | 2 + .../phame/query/PhamePostQuery.php | 22 ++-- .../phame/query/PhamePostSearchEngine.php | 110 ++++++++++++++++++ 3 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 src/applications/phame/query/PhamePostSearchEngine.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 55f8d3c709..83620574c6 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3034,6 +3034,7 @@ phutil_register_library_map(array( 'PhamePostPreviewController' => 'applications/phame/controller/post/PhamePostPreviewController.php', 'PhamePostPublishController' => 'applications/phame/controller/post/PhamePostPublishController.php', 'PhamePostQuery' => 'applications/phame/query/PhamePostQuery.php', + 'PhamePostSearchEngine' => 'applications/phame/query/PhamePostSearchEngine.php', 'PhamePostTransaction' => 'applications/phame/storage/PhamePostTransaction.php', 'PhamePostTransactionQuery' => 'applications/phame/query/PhamePostTransactionQuery.php', 'PhamePostUnpublishController' => 'applications/phame/controller/post/PhamePostUnpublishController.php', @@ -7007,6 +7008,7 @@ phutil_register_library_map(array( 'PhamePostPreviewController' => 'PhameController', 'PhamePostPublishController' => 'PhameController', 'PhamePostQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', + 'PhamePostSearchEngine' => 'PhabricatorApplicationSearchEngine', 'PhamePostTransaction' => 'PhabricatorApplicationTransaction', 'PhamePostTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhamePostUnpublishController' => 'PhameController', diff --git a/src/applications/phame/query/PhamePostQuery.php b/src/applications/phame/query/PhamePostQuery.php index 2eed8d2960..011314c1bf 100644 --- a/src/applications/phame/query/PhamePostQuery.php +++ b/src/applications/phame/query/PhamePostQuery.php @@ -81,61 +81,59 @@ final class PhamePostQuery extends PhabricatorCursorPagedPolicyAwareQuery { return $posts; } - protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { - $where = array(); + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { + $where = parent::buildWhereClauseParts($conn); if ($this->ids) { $where[] = qsprintf( - $conn_r, + $conn, 'p.id IN (%Ld)', $this->ids); } if ($this->phids) { $where[] = qsprintf( - $conn_r, + $conn, 'p.phid IN (%Ls)', $this->phids); } if ($this->bloggerPHIDs) { $where[] = qsprintf( - $conn_r, + $conn, 'p.bloggerPHID IN (%Ls)', $this->bloggerPHIDs); } if ($this->phameTitles) { $where[] = qsprintf( - $conn_r, + $conn, 'p.phameTitle IN (%Ls)', $this->phameTitles); } if ($this->visibility !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'p.visibility = %d', $this->visibility); } if ($this->publishedAfter !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'p.datePublished > %d', $this->publishedAfter); } if ($this->blogPHIDs) { $where[] = qsprintf( - $conn_r, + $conn, 'p.blogPHID in (%Ls)', $this->blogPHIDs); } - $where[] = $this->buildPagingClause($conn_r); - - return $this->formatWhereClause($where); + return $where; } public function getQueryApplicationClass() { diff --git a/src/applications/phame/query/PhamePostSearchEngine.php b/src/applications/phame/query/PhamePostSearchEngine.php new file mode 100644 index 0000000000..600d633627 --- /dev/null +++ b/src/applications/phame/query/PhamePostSearchEngine.php @@ -0,0 +1,110 @@ +newQuery(); + + if (strlen($map['visibility'])) { + $query->withVisibility($map['visibility']); + } + + return $query; + } + + protected function buildCustomSearchFields() { + return array( + id(new PhabricatorSearchSelectField()) + ->setKey('visibility') + ->setOptions(array( + '' => pht('All'), + PhamePost::VISIBILITY_PUBLISHED => pht('Live'), + PhamePost::VISIBILITY_DRAFT => pht('Draft'), + )), + ); + } + + protected function getURI($path) { + return '/phame/post/'.$path; + } + + protected function getBuiltinQueryNames() { + $names = array( + 'all' => pht('All'), + 'live' => pht('Live'), + 'draft' => pht('Draft'), + ); + return $names; + } + + public function buildSavedQueryFromBuiltin($query_key) { + $query = $this->newSavedQuery(); + $query->setQueryKey($query_key); + + switch ($query_key) { + case 'all': + return $query; + case 'live': + return $query->setParameter( + 'visibility', PhamePost::VISIBILITY_PUBLISHED); + case 'draft': + return $query->setParameter( + 'visibility', PhamePost::VISIBILITY_DRAFT); + } + + return parent::buildSavedQueryFromBuiltin($query_key); + } + protected function renderResultList( + array $posts, + PhabricatorSavedQuery $query, + array $handles) { + + assert_instances_of($posts, 'PhamePost'); + $viewer = $this->requireViewer(); + + $list = new PHUIObjectItemListView(); + $list->setUser($viewer); + + foreach ($posts as $post) { + $id = $post->getID(); + $blog = $viewer->renderHandle($post->getBlogPHID())->render(); + $item = id(new PHUIObjectItemView()) + ->setUser($viewer) + ->setObject($post) + ->setHeader($post->getTitle()) + ->setStatusIcon('fa-star') + ->setHref($this->getApplicationURI("/post/view/{$id}/")) + ->addAttribute( + pht('Blog: %s', $blog)); + if ($post->isDraft()) { + $item->setStatusIcon('fa-star-o grey'); + $item->setDisabled(true); + $item->addIcon('none', pht('Draft Post')); + } else { + $date = $post->getDatePublished(); + $item->setEpoch($date); + } + $list->addItem($item); + } + + $result = new PhabricatorApplicationSearchResultView(); + $result->setObjectList($list); + $result->setNoDataString(pht('No blogs found.')); + + return $result; + } + +}