From bee043b1636269316886b4de954ea185cd59cbc8 Mon Sep 17 00:00:00 2001 From: Christopher Wetherill Date: Thu, 26 Jan 2017 18:12:02 -0500 Subject: [PATCH] Display paste line count alongside snippets Summary: Fixes T11547. I //think// this mostly gets about addressing @epriestley's comments in D16465 and stores each paste's line count in its snippet so that we can display the actual number of lines in the paste rather than '5 Lines'. Let me know if this is on the right track! Test Plan: Open /paste and see that each paste's actual line count is reported. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley Maniphest Tasks: T11547 Differential Revision: https://secure.phabricator.com/D17256 --- .../paste/query/PhabricatorPasteQuery.php | 11 ++++++++--- .../paste/query/PhabricatorPasteSearchEngine.php | 2 +- .../paste/snippet/PhabricatorPasteSnippet.php | 8 +++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/applications/paste/query/PhabricatorPasteQuery.php b/src/applications/paste/query/PhabricatorPasteQuery.php index 0d8b1542b7..110083a64e 100644 --- a/src/applications/paste/query/PhabricatorPasteQuery.php +++ b/src/applications/paste/query/PhabricatorPasteQuery.php @@ -185,6 +185,7 @@ final class PhabricatorPasteQuery $paste->getFilePHID(), $paste->getLanguage(), 'snippet', + 'v2', PhabricatorHash::digestForIndex($paste->getTitle()), )); } @@ -294,7 +295,8 @@ final class PhabricatorPasteQuery $snippet_data = phutil_json_decode($caches[$key], true); $snippet = new PhabricatorPasteSnippet( phutil_safe_html($snippet_data['content']), - $snippet_data['type']); + $snippet_data['type'], + $snippet_data['contentLineCount']); $paste->attachSnippet($snippet); $have_cache[$paste->getPHID()] = true; } else { @@ -326,6 +328,7 @@ final class PhabricatorPasteQuery $snippet_data = array( 'content' => (string)$snippet->getContent(), 'type' => (string)$snippet->getType(), + 'contentLineCount' => $snippet->getContentLineCount(), ); $write_data[$this->getSnippetCacheKey($paste)] = phutil_json_encode( $snippet_data); @@ -358,7 +361,8 @@ final class PhabricatorPasteQuery } $lines = phutil_split_lines($snippet); - if (count($lines) > 5) { + $line_count = count($lines); + if ($line_count > 5) { $snippet_type = PhabricatorPasteSnippet::FIRST_LINES; $snippet = implode('', array_slice($lines, 0, 5)); } @@ -368,7 +372,8 @@ final class PhabricatorPasteQuery $snippet, $paste->getTitle(), $paste->getLanguage()), - $snippet_type); + $snippet_type, + $line_count); } private function highlightSource($source, $title, $language) { diff --git a/src/applications/paste/query/PhabricatorPasteSearchEngine.php b/src/applications/paste/query/PhabricatorPasteSearchEngine.php index d6b1b0e2eb..e269088f60 100644 --- a/src/applications/paste/query/PhabricatorPasteSearchEngine.php +++ b/src/applications/paste/query/PhabricatorPasteSearchEngine.php @@ -166,7 +166,7 @@ final class PhabricatorPasteSearchEngine $preview); $created = phabricator_datetime($paste->getDateCreated(), $viewer); - $line_count = count($lines); + $line_count = $paste->getSnippet()->getContentLineCount(); $line_count = pht( '%s Line(s)', new PhutilNumber($line_count)); diff --git a/src/applications/paste/snippet/PhabricatorPasteSnippet.php b/src/applications/paste/snippet/PhabricatorPasteSnippet.php index 4a001e5955..c5faa50b81 100644 --- a/src/applications/paste/snippet/PhabricatorPasteSnippet.php +++ b/src/applications/paste/snippet/PhabricatorPasteSnippet.php @@ -8,10 +8,12 @@ final class PhabricatorPasteSnippet extends Phobject { private $content; private $type; + private $contentLineCount; - public function __construct($content, $type) { + public function __construct($content, $type, $content_line_count) { $this->content = $content; $this->type = $type; + $this->contentLineCount = $content_line_count; } public function getContent() { @@ -21,4 +23,8 @@ final class PhabricatorPasteSnippet extends Phobject { public function getType() { return $this->type; } + + public function getContentLineCount() { + return $this->contentLineCount; + } }