Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / storage / PhabricatorRepositoryCommitData.php
blob92a5f717fe820e9b5c4c500915ec5b57c6d5acbf
1 <?php
3 final class PhabricatorRepositoryCommitData extends PhabricatorRepositoryDAO {
5 protected $commitID;
6 protected $authorName = '';
7 protected $commitMessage = '';
8 protected $commitDetails = array();
9 private $commitRef;
11 protected function getConfiguration() {
12 return array(
13 self::CONFIG_TIMESTAMPS => false,
14 self::CONFIG_SERIALIZATION => array(
15 'commitDetails' => self::SERIALIZATION_JSON,
17 self::CONFIG_COLUMN_SCHEMA => array(
18 'authorName' => 'text',
19 'commitMessage' => 'text',
21 self::CONFIG_KEY_SCHEMA => array(
22 'commitID' => array(
23 'columns' => array('commitID'),
24 'unique' => true,
27 ) + parent::getConfiguration();
30 public function getSummary() {
31 $message = $this->getCommitMessage();
32 return self::summarizeCommitMessage($message);
35 public static function summarizeCommitMessage($message) {
36 $max_bytes = id(new PhabricatorRepositoryCommit())
37 ->getColumnMaximumByteLength('summary');
39 $summary = phutil_split_lines($message, $retain_endings = false);
40 $summary = head($summary);
41 $summary = id(new PhutilUTF8StringTruncator())
42 ->setMaximumBytes($max_bytes)
43 ->setMaximumGlyphs(80)
44 ->truncateString($summary);
46 return $summary;
49 public function getCommitDetail($key, $default = null) {
50 return idx($this->commitDetails, $key, $default);
53 public function setCommitDetail($key, $value) {
54 $this->commitDetails[$key] = $value;
55 return $this;
58 public function toDictionary() {
59 return array(
60 'commitID' => $this->commitID,
61 'authorName' => $this->authorName,
62 'commitMessage' => $this->commitMessage,
63 'commitDetails' => json_encode($this->commitDetails),
67 public static function newFromDictionary(array $dict) {
68 return id(new PhabricatorRepositoryCommitData())
69 ->loadFromArray($dict);
72 public function newPublisherHoldReasons() {
73 $holds = $this->getCommitDetail('holdReasons');
75 // Look for the legacy "autocloseReason" if we don't have a modern list
76 // of hold reasons.
77 if (!$holds) {
78 $old_hold = $this->getCommitDetail('autocloseReason');
79 if ($old_hold) {
80 $holds = array($old_hold);
84 if (!$holds) {
85 $holds = array();
88 foreach ($holds as $key => $reason) {
89 $holds[$key] = PhabricatorRepositoryPublisherHoldReason::newForHoldKey(
90 $reason);
93 return array_values($holds);
96 public function getAuthorString() {
97 $ref = $this->getCommitRef();
99 $author = $ref->getAuthor();
100 if ($author !== null && strlen($author)) {
101 return $author;
104 $author = phutil_string_cast($this->authorName);
105 if (strlen($author)) {
106 return $author;
109 return null;
112 public function getAuthorDisplayName() {
113 return $this->getCommitRef()->getAuthorName();
116 public function getAuthorEmail() {
117 return $this->getCommitRef()->getAuthorEmail();
120 public function getAuthorEpoch() {
121 $epoch = $this->getCommitRef()->getAuthorEpoch();
123 if ($epoch) {
124 return (int)$epoch;
127 return null;
130 public function getCommitterString() {
131 $ref = $this->getCommitRef();
133 $committer = $ref->getCommitter();
134 if ($committer !== null && strlen($committer)) {
135 return $committer;
138 return $this->getCommitDetailString('committer');
141 public function getCommitterDisplayName() {
142 return $this->getCommitRef()->getCommitterName();
145 public function getCommitterEmail() {
146 return $this->getCommitRef()->getCommitterEmail();
149 private function getCommitDetailString($key) {
150 $string = $this->getCommitDetail($key);
151 $string = phutil_string_cast($string);
153 if (strlen($string)) {
154 return $string;
157 return null;
160 public function setCommitRef(DiffusionCommitRef $ref) {
161 $this->setCommitDetail('ref', $ref->newDictionary());
162 $this->commitRef = null;
164 return $this;
167 public function getCommitRef() {
168 if ($this->commitRef === null) {
169 $map = $this->getCommitDetail('ref', array());
171 if (!is_array($map)) {
172 $map = array();
175 $map = $map + array(
176 'authorName' => $this->getCommitDetailString('authorName'),
177 'authorEmail' => $this->getCommitDetailString('authorEmail'),
178 'authorEpoch' => $this->getCommitDetailString('authorEpoch'),
179 'committerName' => $this->getCommitDetailString('committerName'),
180 'committerEmail' => $this->getCommitDetailString('committerEmail'),
181 'message' => $this->getCommitMessage(),
184 $ref = DiffusionCommitRef::newFromDictionary($map);
185 $this->commitRef = $ref;
188 return $this->commitRef;