@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorRepositoryCommitHint
4 extends PhabricatorRepositoryDAO
5 implements PhabricatorPolicyInterface {
6
7 protected $repositoryPHID;
8 protected $oldCommitIdentifier;
9 protected $newCommitIdentifier;
10 protected $hintType;
11
12 const HINT_NONE = 'none';
13 const HINT_REWRITTEN = 'rewritten';
14 const HINT_UNREADABLE = 'unreadable';
15
16 protected function getConfiguration() {
17 return array(
18 self::CONFIG_TIMESTAMPS => false,
19 self::CONFIG_COLUMN_SCHEMA => array(
20 'oldCommitIdentifier' => 'text40',
21 'newCommitIdentifier' => 'text40?',
22 'hintType' => 'text32',
23 ),
24 self::CONFIG_KEY_SCHEMA => array(
25 'key_old' => array(
26 'columns' => array('repositoryPHID', 'oldCommitIdentifier'),
27 'unique' => true,
28 ),
29 ),
30 ) + parent::getConfiguration();
31 }
32
33 public static function getAllHintTypes() {
34 return array(
35 self::HINT_NONE,
36 self::HINT_REWRITTEN,
37 self::HINT_UNREADABLE,
38 );
39 }
40
41 public static function updateHint($repository_phid, $old, $new, $type) {
42 switch ($type) {
43 case self::HINT_NONE:
44 break;
45 case self::HINT_REWRITTEN:
46 if (!$new) {
47 throw new Exception(
48 pht(
49 'When hinting a commit ("%s") as rewritten, you must provide '.
50 'the commit it was rewritten into.',
51 $old));
52 }
53 break;
54 case self::HINT_UNREADABLE:
55 if ($new) {
56 throw new Exception(
57 pht(
58 'When hinting a commit ("%s") as unreadable, you must not '.
59 'provide a new commit ("%s").',
60 $old,
61 $new));
62 }
63 break;
64 default:
65 $all_types = self::getAllHintTypes();
66 throw new Exception(
67 pht(
68 'Hint type ("%s") for commit ("%s") is not valid. Valid hints '.
69 'are: %s.',
70 $type,
71 $old,
72 implode(', ', $all_types)));
73 }
74
75 $table = new self();
76 $table_name = $table->getTableName();
77 $conn = $table->establishConnection('w');
78
79 if ($type == self::HINT_NONE) {
80 queryfx(
81 $conn,
82 'DELETE FROM %T WHERE repositoryPHID = %s AND oldCommitIdentifier = %s',
83 $table_name,
84 $repository_phid,
85 $old);
86 } else {
87 queryfx(
88 $conn,
89 'INSERT INTO %T
90 (repositoryPHID, oldCommitIdentifier, newCommitIdentifier, hintType)
91 VALUES (%s, %s, %ns, %s)
92 ON DUPLICATE KEY UPDATE
93 newCommitIdentifier = VALUES(newCommitIdentifier),
94 hintType = VALUES(hintType)',
95 $table_name,
96 $repository_phid,
97 $old,
98 $new,
99 $type);
100 }
101 }
102
103
104 public function isUnreadable() {
105 return ($this->getHintType() == self::HINT_UNREADABLE);
106 }
107
108 public function isRewritten() {
109 return ($this->getHintType() == self::HINT_REWRITTEN);
110 }
111
112
113/* -( PhabricatorPolicyInterface )----------------------------------------- */
114
115
116 public function getCapabilities() {
117 return array(
118 PhabricatorPolicyCapability::CAN_VIEW,
119 );
120 }
121
122 public function getPolicy($capability) {
123 switch ($capability) {
124 case PhabricatorPolicyCapability::CAN_VIEW:
125 return PhabricatorPolicies::getMostOpenPolicy();
126 }
127 }
128
129 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
130 return false;
131 }
132
133}