Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F90814
D7783.diff
All Users
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D7783.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -779,6 +779,7 @@
'HeraldTranscriptController' => 'applications/herald/controller/HeraldTranscriptController.php',
'HeraldTranscriptListController' => 'applications/herald/controller/HeraldTranscriptListController.php',
'HeraldTranscriptQuery' => 'applications/herald/query/HeraldTranscriptQuery.php',
+ 'HeraldTranscriptTestCase' => 'applications/herald/storage/__tests__/HeraldTranscriptTestCase.php',
'Javelin' => 'infrastructure/javelin/Javelin.php',
'JavelinReactorExample' => 'applications/uiexample/examples/JavelinReactorExample.php',
'JavelinUIExample' => 'applications/uiexample/examples/JavelinUIExample.php',
@@ -3211,6 +3212,7 @@
'HeraldTranscriptController' => 'HeraldController',
'HeraldTranscriptListController' => 'HeraldController',
'HeraldTranscriptQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
+ 'HeraldTranscriptTestCase' => 'PhabricatorTestCase',
'JavelinReactorExample' => 'PhabricatorUIExample',
'JavelinUIExample' => 'PhabricatorUIExample',
'JavelinViewExample' => 'PhabricatorUIExample',
diff --git a/src/applications/herald/storage/__tests__/HeraldTranscriptTestCase.php b/src/applications/herald/storage/__tests__/HeraldTranscriptTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/applications/herald/storage/__tests__/HeraldTranscriptTestCase.php
@@ -0,0 +1,47 @@
+<?php
+
+final class HeraldTranscriptTestCase extends PhabricatorTestCase {
+
+ public function testTranscriptTruncation() {
+ $long_string = str_repeat('x', 1024 * 1024);
+ $short_string = str_repeat('x', 4096)."\n<...>";
+
+ $long_array = array(
+ 'a' => $long_string,
+ 'b' => $long_string,
+ );
+
+ $mixed_array = array(
+ 'a' => 'abc',
+ 'b' => 'def',
+ 'c' => $long_string,
+ );
+
+ $fields = array(
+ 'ls' => $long_string,
+ 'la' => $long_array,
+ 'ma' => $mixed_array,
+ );
+
+ $truncated_fields = id(new HeraldObjectTranscript())
+ ->setFields($fields)
+ ->getFields();
+
+ $this->assertEqual($short_string, $truncated_fields['ls']);
+
+ $this->assertEqual(
+ array('a', '<...>'),
+ array_keys($truncated_fields['la']));
+ $this->assertEqual(
+ $short_string.'!<...>',
+ implode('!', $truncated_fields['la']));
+
+ $this->assertEqual(
+ array('a', 'b', 'c'),
+ array_keys($truncated_fields['ma']));
+ $this->assertEqual(
+ 'abc!def!'.substr($short_string, 6),
+ implode('!', $truncated_fields['ma']));
+
+ }
+}
\ No newline at end of file
diff --git a/src/applications/herald/storage/transcript/HeraldObjectTranscript.php b/src/applications/herald/storage/transcript/HeraldObjectTranscript.php
--- a/src/applications/herald/storage/transcript/HeraldObjectTranscript.php
+++ b/src/applications/herald/storage/transcript/HeraldObjectTranscript.php
@@ -35,11 +35,41 @@
}
public function setFields(array $fields) {
+ foreach ($fields as $key => $value) {
+ $fields[$key] = self::truncateValue($value, 4096);
+ }
+
$this->fields = $fields;
return $this;
}
public function getFields() {
return $this->fields;
}
+
+ private static function truncateValue($value, $length) {
+ if (is_string($value)) {
+ if (strlen($value) <= $length) {
+ return $value;
+ } else {
+ // NOTE: phutil_utf8_shorten() has huge runtime for giant strings.
+ return phutil_utf8ize(substr($value, 0, $length)."\n<...>");
+ }
+ } else if (is_array($value)) {
+ foreach ($value as $key => $v) {
+ if ($length <= 0) {
+ $value['<...>'] = '<...>';
+ unset($value[$key]);
+ } else {
+ $v = self::truncateValue($v, $length);
+ $length -= strlen($v);
+ $value[$key] = $v;
+ }
+ }
+ return $value;
+ } else {
+ return $value;
+ }
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/vy/wl/tlsgfnxdutjyqn67
Default Alt Text
D7783.diff (3 KB)
Attached To
Mode
D7783: Truncate object fields in Herald transcripts
Attached
Detach File
Event Timeline
Log In to Comment