1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-19 12:00:55 +01:00

phutil_utf8_shorten => PhutilUTF8StringTruncator

Summary: Ref T3307. Only one I thought was tricky was Excel; I went with bytes there like it was email.

Test Plan: played around on a few endpoints but mostly thought carefully

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T3307

Differential Revision: https://secure.phabricator.com/D10392
This commit is contained in:
Bob Trahan 2014-08-29 15:15:13 -07:00
parent 482784b9b2
commit b93bc7e479
23 changed files with 76 additions and 28 deletions

View file

@ -214,8 +214,9 @@ final class PhabricatorCalendarEventSearchEngine
->setBarColor($color) ->setBarColor($color)
->addByline(pht('Creator: %s', $creator_handle->renderLink())) ->addByline(pht('Creator: %s', $creator_handle->renderLink()))
->addAttribute(pht('From %s to %s', $from, $to)) ->addAttribute(pht('From %s to %s', $from, $to))
->addAttribute( ->addAttribute(id(new PhutilUTF8StringTruncator())
phutil_utf8_shorten($event->getDescription(), 64)); ->setMaximumGlyphs(64)
->truncateString($event->getDescription()));
$list->addItem($item); $list->addItem($item);
} }

View file

@ -108,7 +108,9 @@ final class PhabricatorChatLogChannelLogController
$out = array(); $out = array();
foreach ($blocks as $block) { foreach ($blocks as $block) {
$author = $block['author']; $author = $block['author'];
$author = phutil_utf8_shorten($author, 18); $author = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(18)
->truncateString($author);
$author = phutil_tag('td', array('class' => 'author'), $author); $author = phutil_tag('td', array('class' => 'author'), $author);
$href = $uri->alter('at', $block['id']); $href = $uri->alter('at', $block['id']);

View file

@ -59,9 +59,9 @@ final class ConduitConnectConduitAPIMethod extends ConduitAPIMethod {
$client = $request->getValue('client'); $client = $request->getValue('client');
$client_version = (int)$request->getValue('clientVersion'); $client_version = (int)$request->getValue('clientVersion');
$client_description = (string)$request->getValue('clientDescription'); $client_description = (string)$request->getValue('clientDescription');
// TODO: This should be character-oriented, not display-oriented. $client_description = id(new PhutilUTF8StringTruncator())
// See T3307. ->setMaximumCodepoints(255)
$client_description = phutil_utf8_shorten($client_description, 255); ->truncateString($client_description);
$username = (string)$request->getValue('user'); $username = (string)$request->getValue('user');
// Log the connection, regardless of the outcome of checks below. // Log the connection, regardless of the outcome of checks below.

View file

@ -20,7 +20,9 @@ final class ConpherenceFileWidgetView extends ConpherenceWidgetView {
''); '');
$file_view = id(new PhabricatorFileLinkView()) $file_view = id(new PhabricatorFileLinkView())
->setFilePHID($file->getPHID()) ->setFilePHID($file->getPHID())
->setFileName(phutil_utf8_shorten($file->getName(), 28)) ->setFileName(id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(28)
->truncateString($file->getName()))
->setFileViewable($file->isViewableImage()) ->setFileViewable($file->isViewableImage())
->setFileViewURI($file->getBestURI()) ->setFileViewURI($file->getBestURI())
->setCustomClass('file-title'); ->setCustomClass('file-title');

View file

@ -62,8 +62,10 @@ final class DifferentialDiffViewController extends DifferentialController {
array( array(
'value' => $revision->getID(), 'value' => $revision->getID(),
), ),
phutil_utf8_shorten( id(new PhutilUTF8StringTruncator())
'D'.$revision->getID().' '.$revision->getTitle(), 128)); ->setMaximumGlyphs(128)
->truncateString(
'D'.$revision->getID().' '.$revision->getTitle()));
} }
$select[] = hsprintf('</optgroup>'); $select[] = hsprintf('</optgroup>');
} }

View file

@ -69,7 +69,9 @@ final class DifferentialHovercardEventListener
if ($rev->getSummary()) { if ($rev->getSummary()) {
$hovercard->addField(pht('Summary'), $hovercard->addField(pht('Summary'),
phutil_utf8_shorten($rev->getSummary(), 120)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(120)
->truncateString($rev->getSummary()));
} }
$hovercard->addTag( $hovercard->addTag(

View file

@ -141,7 +141,11 @@ final class DifferentialCommitMessageParser {
if (isset($fields[$key_title])) { if (isset($fields[$key_title])) {
$terminal = '...'; $terminal = '...';
$title = $fields[$key_title]; $title = $fields[$key_title];
$short = phutil_utf8_shorten($title, 250, $terminal); $short = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(250)
->setTerminator($terminal)
->truncateString($title);
if ($short != $title) { if ($short != $title) {
// If we shortened the title, split the rest into the summary, so // If we shortened the title, split the rest into the summary, so

View file

@ -77,7 +77,9 @@ final class DifferentialLocalCommitsView extends AphrontView {
$message = idx($commit, 'message'); $message = idx($commit, 'message');
$summary = idx($commit, 'summary'); $summary = idx($commit, 'summary');
$summary = phutil_utf8_shorten($summary, 80); $summary = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(80)
->truncateString($summary);
$view = new AphrontMoreView(); $view = new AphrontMoreView();
$view->setSome($summary); $view->setSome($summary);

View file

@ -684,7 +684,10 @@ final class DiffusionBrowseFileController extends DiffusionBrowseController {
'size' => 600, 'size' => 600,
), ),
), ),
phutil_utf8_shorten($line['commit'], 9, '')); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(9)
->setTerminator('')
->truncateString($line['commit']));
$revision_id = null; $revision_id = null;
if (idx($commits, $commit)) { if (idx($commits, $commit)) {

View file

@ -378,7 +378,9 @@ abstract class PhabricatorFeedStory
final protected function renderSummary($text, $len = 128) { final protected function renderSummary($text, $len = 128) {
if ($len) { if ($len) {
$text = phutil_utf8_shorten($text, $len); $text = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs($len)
->truncateString($text);
} }
switch ($this->getRenderingTarget()) { switch ($this->getRenderingTarget()) {
case PhabricatorApplicationTransaction::TARGET_HTML: case PhabricatorApplicationTransaction::TARGET_HTML:

View file

@ -196,7 +196,9 @@ final class HarbormasterBuild extends HarbormasterDAO
$log_source, $log_source,
$log_type) { $log_type) {
$log_source = phutil_utf8_shorten($log_source, 250); $log_source = id(new PhutilUTF8StringTruncator())
->setMaximumCodepoints(250)
->truncateString($log_source);
$log = HarbormasterBuildLog::initializeNewBuildLog($build_target) $log = HarbormasterBuildLog::initializeNewBuildLog($build_target)
->setLogSource($log_source) ->setLogSource($log_source)

View file

@ -52,7 +52,7 @@ final class HeraldObjectTranscript {
if (strlen($value) <= $length) { if (strlen($value) <= $length) {
return $value; return $value;
} else { } else {
// NOTE: phutil_utf8_shorten() has huge runtime for giant strings. // NOTE: PhutilUTF8StringTruncator has huge runtime for giant strings.
return phutil_utf8ize(substr($value, 0, $length)."\n<...>"); return phutil_utf8ize(substr($value, 0, $length)."\n<...>");
} }
} else if (is_array($value)) { } else if (is_array($value)) {

View file

@ -104,7 +104,9 @@ final class ManiphestExcelDefaultFormat extends ManiphestExcelFormat {
$task->getTitle(), $task->getTitle(),
$projects, $projects,
PhabricatorEnv::getProductionURI('/T'.$task->getID()), PhabricatorEnv::getProductionURI('/T'.$task->getID()),
phutil_utf8_shorten($task->getDescription(), 512), id(new PhutilUTF8StringTruncator())
->setMaximumBytes(512)
->truncateString($task->getDescription()),
); );
} }

View file

@ -563,7 +563,9 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
$body = idx($params, 'body', ''); $body = idx($params, 'body', '');
$max = PhabricatorEnv::getEnvConfig('metamta.email-body-limit'); $max = PhabricatorEnv::getEnvConfig('metamta.email-body-limit');
if (strlen($body) > $max) { if (strlen($body) > $max) {
$body = phutil_utf8_shorten($body, $max); $body = id(new PhutilUTF8StringTruncator())
->setMaximumBytes($max)
->truncateString($body);
$body .= "\n"; $body .= "\n";
$body .= pht('(This email was truncated at %d bytes.)', $max); $body .= pht('(This email was truncated at %d bytes.)', $max);
} }

View file

@ -59,7 +59,9 @@ final class PhabricatorPeopleHovercardEventListener
if ($profile->getBlurb()) { if ($profile->getBlurb()) {
$hovercard->addField(pht('Blurb'), $hovercard->addField(pht('Blurb'),
phutil_utf8_shorten($profile->getBlurb(), 120)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(120)
->truncateString($profile->getBlurb()));
} }
$event->setValue('hovercard', $hovercard); $event->setValue('hovercard', $hovercard);

View file

@ -90,7 +90,9 @@ final class PhameCreatePostConduitAPIMethod extends PhameConduitAPIMethod {
$post->setTitle($title); $post->setTitle($title);
$phame_title = $request->getValue( $phame_title = $request->getValue(
'phameTitle', 'phameTitle',
phutil_utf8_shorten($title, 64)); id(new PhutilUTF8StringTruncator())
->setMaximumCodepoints(64)
->truncateString($title));
$post->setPhameTitle(PhabricatorSlug::normalize($phame_title)); $post->setPhameTitle(PhabricatorSlug::normalize($phame_title));
$post->setBody($body); $post->setBody($body);
$post->save(); $post->save();

View file

@ -297,7 +297,9 @@ final class PholioTransaction extends PhabricatorApplicationTransaction {
if ($text) { if ($text) {
return phutil_escape_html_newlines( return phutil_escape_html_newlines(
phutil_utf8_shorten($text, 128)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(128)
->truncateString($text));
} }
return parent::getBodyForFeed($story); return parent::getBodyForFeed($story);

View file

@ -264,6 +264,9 @@ final class PhrictionDocumentEditor extends PhabricatorEditor {
} }
if ($feed_action) { if ($feed_action) {
$content = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(140)
->truncateString($new_content->getContent());
id(new PhabricatorFeedStoryPublisher()) id(new PhabricatorFeedStoryPublisher())
->setRelatedPHIDs($related_phids) ->setRelatedPHIDs($related_phids)
->setStoryAuthorPHID($this->getActor()->getPHID()) ->setStoryAuthorPHID($this->getActor()->getPHID())
@ -273,7 +276,7 @@ final class PhrictionDocumentEditor extends PhabricatorEditor {
array( array(
'phid' => $document->getPHID(), 'phid' => $document->getPHID(),
'action' => $feed_action, 'action' => $feed_action,
'content' => phutil_utf8_shorten($new_content->getContent(), 140), 'content' => $content,
'project' => $project_phid, 'project' => $project_phid,
'movedFromPHID' => $this->fromDocumentPHID, 'movedFromPHID' => $this->fromDocumentPHID,
)) ))

View file

@ -71,7 +71,9 @@ final class PonderAnswerTransaction
switch ($this->getTransactionType()) { switch ($this->getTransactionType()) {
case self::TYPE_CONTENT: case self::TYPE_CONTENT:
return phutil_escape_html_newlines( return phutil_escape_html_newlines(
phutil_utf8_shorten($new, 128)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(128)
->truncateString($new));
break; break;
} }
return parent::getBodyForFeed($story); return parent::getBodyForFeed($story);

View file

@ -252,14 +252,18 @@ final class PonderQuestionTransaction
if ($old === null) { if ($old === null) {
$question = $story->getObject($this->getObjectPHID()); $question = $story->getObject($this->getObjectPHID());
return phutil_escape_html_newlines( return phutil_escape_html_newlines(
phutil_utf8_shorten($question->getContent(), 128)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(128)
->truncateString($question->getContent()));
} }
break; break;
case self::TYPE_ANSWERS: case self::TYPE_ANSWERS:
$answer = $this->getNewAnswerObject($story); $answer = $this->getNewAnswerObject($story);
if ($answer) { if ($answer) {
return phutil_escape_html_newlines( return phutil_escape_html_newlines(
phutil_utf8_shorten($answer->getContent(), 128)); id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(128)
->truncateString($answer->getContent()));
} }
break; break;
} }

View file

@ -30,7 +30,9 @@ final class PhabricatorRepositoryCommitData extends PhabricatorRepositoryDAO {
public static function summarizeCommitMessage($message) { public static function summarizeCommitMessage($message) {
$summary = phutil_split_lines($message, $retain_endings = false); $summary = phutil_split_lines($message, $retain_endings = false);
$summary = head($summary); $summary = head($summary);
$summary = phutil_utf8_shorten($summary, self::SUMMARY_MAX_LENGTH); $summary = id(new PhutilUTF8StringTruncator())
->setMaximumCodepoints(self::SUMMARY_MAX_LENGTH)
->truncateString($summary);
return $summary; return $summary;
} }

View file

@ -155,7 +155,9 @@ final class PhabricatorSlowvoteSearchEngine
$description = $poll->getDescription(); $description = $poll->getDescription();
if (strlen($description)) { if (strlen($description)) {
$item->addAttribute(phutil_utf8_shorten($poll->getDescription(), 120)); $item->addAttribute(id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(120)
->truncateString($poll->getDescription()));
} }
if ($author) { if ($author) {

View file

@ -58,9 +58,12 @@ final class AphrontFormPolicyControl extends AphrontFormControl {
continue; continue;
} }
} }
$policy_short_name = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(28)
->truncateString($policy->getName());
$options[$policy->getType()][$policy->getPHID()] = array( $options[$policy->getType()][$policy->getPHID()] = array(
'name' => phutil_utf8_shorten($policy->getName(), 28), 'name' => $policy_short_name,
'full' => $policy->getName(), 'full' => $policy->getName(),
'icon' => $policy->getIcon(), 'icon' => $policy->getIcon(),
); );