From d653fa13de78edee501c5269024f1742bb8d5dc2 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 15 May 2014 19:17:22 -0700 Subject: [PATCH] Move paste rendering into SearchEngine Summary: Ref T4986. One note: - I've disabled syntax highlighting in the previews. When we miss caches this is just way way too slow and has frustrated me several times in the past. The value of syntax highlighting these snippets is not huge. We could maybe ajax this in or use it //if// we get a cache hit in the future, but just kill it for the moment. Test Plan: Viewed pastes. Created a paste panel. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T4986 Differential Revision: https://secure.phabricator.com/D9138 --- .../PhabricatorPasteListController.php | 58 +--------------- .../query/PhabricatorPasteSearchEngine.php | 68 +++++++++++++++++-- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/src/applications/paste/controller/PhabricatorPasteListController.php b/src/applications/paste/controller/PhabricatorPasteListController.php index c46b4cae43..c4134972ca 100644 --- a/src/applications/paste/controller/PhabricatorPasteListController.php +++ b/src/applications/paste/controller/PhabricatorPasteListController.php @@ -1,10 +1,6 @@ delegateToController($controller); } - public function renderResultsList( - array $pastes, - PhabricatorSavedQuery $query) { - assert_instances_of($pastes, 'PhabricatorPaste'); - - $user = $this->getRequest()->getUser(); - - $this->loadHandles(mpull($pastes, 'getAuthorPHID')); - - $lang_map = PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); - - $list = new PHUIObjectItemListView(); - $list->setUser($user); - foreach ($pastes as $paste) { - $created = phabricator_date($paste->getDateCreated(), $user); - $author = $this->getHandle($paste->getAuthorPHID())->renderLink(); - $source_code = $this->buildSourceCodeView($paste, 5)->render(); - - $source_code = phutil_tag( - 'div', - array( - 'class' => 'phabricator-source-code-summary', - ), - $source_code); - - $line_count = count(explode("\n", $paste->getContent())); - $line_count = pht( - '%s Line(s)', - new PhutilNumber($line_count)); - - $title = nonempty($paste->getTitle(), pht('(An Untitled Masterwork)')); - - $item = id(new PHUIObjectItemView()) - ->setObjectName('P'.$paste->getID()) - ->setHeader($title) - ->setHref('/P'.$paste->getID()) - ->setObject($paste) - ->addByline(pht('Author: %s', $author)) - ->addIcon('none', $line_count) - ->appendChild($source_code); - - $lang_name = $paste->getLanguage(); - if ($lang_name) { - $lang_name = idx($lang_map, $lang_name, $lang_name); - $item->addIcon('none', $lang_name); - } - - $list->addItem($item); - } - - return $list; - } } diff --git a/src/applications/paste/query/PhabricatorPasteSearchEngine.php b/src/applications/paste/query/PhabricatorPasteSearchEngine.php index 542931edb9..18e63e3b2e 100644 --- a/src/applications/paste/query/PhabricatorPasteSearchEngine.php +++ b/src/applications/paste/query/PhabricatorPasteSearchEngine.php @@ -1,8 +1,5 @@ needContent(true) + ->needRawContent(true) ->withAuthorPHIDs($saved->getParameter('authorPHIDs', array())) ->withLanguages($saved->getParameter('languages', array())); @@ -126,4 +123,67 @@ final class PhabricatorPasteSearchEngine return parent::buildSavedQueryFromBuiltin($query_key); } + protected function getRequiredHandlePHIDsForResultList( + array $pastes, + PhabricatorSavedQuery $query) { + return mpull($pastes, 'getAuthorPHID'); + } + + protected function renderResultList( + array $pastes, + PhabricatorSavedQuery $query, + array $handles) { + assert_instances_of($pastes, 'PhabricatorPaste'); + + $viewer = $this->requireViewer(); + + $lang_map = PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); + + $list = new PHUIObjectItemListView(); + $list->setUser($viewer); + foreach ($pastes as $paste) { + $created = phabricator_date($paste->getDateCreated(), $viewer); + $author = $handles[$paste->getAuthorPHID()]->renderLink(); + + $lines = phutil_split_lines($paste->getRawContent()); + + $preview = id(new PhabricatorSourceCodeView()) + ->setLimit(5) + ->setLines($lines) + ->setURI(new PhutilURI($paste->getURI())); + + $source_code = phutil_tag( + 'div', + array( + 'class' => 'phabricator-source-code-summary', + ), + $preview); + + $line_count = count($lines); + $line_count = pht( + '%s Line(s)', + new PhutilNumber($line_count)); + + $title = nonempty($paste->getTitle(), pht('(An Untitled Masterwork)')); + + $item = id(new PHUIObjectItemView()) + ->setObjectName('P'.$paste->getID()) + ->setHeader($title) + ->setHref('/P'.$paste->getID()) + ->setObject($paste) + ->addByline(pht('Author: %s', $author)) + ->addIcon('none', $line_count) + ->appendChild($source_code); + + $lang_name = $paste->getLanguage(); + if ($lang_name) { + $lang_name = idx($lang_map, $lang_name, $lang_name); + $item->addIcon('none', $lang_name); + } + + $list->addItem($item); + } + + return $list; + } }