Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/storage/DifferentialViewState.php
12256 views
1
<?php
2
3
final class DifferentialViewState
4
extends DifferentialDAO
5
implements PhabricatorPolicyInterface {
6
7
protected $viewerPHID;
8
protected $objectPHID;
9
protected $viewState = array();
10
11
private $hasModifications;
12
13
protected function getConfiguration() {
14
return array(
15
self::CONFIG_SERIALIZATION => array(
16
'viewState' => self::SERIALIZATION_JSON,
17
),
18
self::CONFIG_KEY_SCHEMA => array(
19
'key_viewer' => array(
20
'columns' => array('viewerPHID', 'objectPHID'),
21
'unique' => true,
22
),
23
'key_object' => array(
24
'columns' => array('objectPHID'),
25
),
26
'key_modified' => array(
27
'columns' => array('dateModified'),
28
),
29
),
30
) + parent::getConfiguration();
31
}
32
33
public function setChangesetProperty(
34
DifferentialChangeset $changeset,
35
$key,
36
$value) {
37
38
if ($this->getChangesetProperty($changeset, $key) === $value) {
39
return;
40
}
41
42
$properties = array(
43
'value' => $value,
44
'epoch' => PhabricatorTime::getNow(),
45
);
46
47
$diff_id = $changeset->getDiffID();
48
if ($diff_id !== null) {
49
$properties['diffID'] = (int)$diff_id;
50
}
51
52
$changeset_id = $changeset->getID();
53
if ($changeset_id !== null) {
54
$properties['changesetID'] = (int)$changeset_id;
55
}
56
57
$path_hash = $this->getChangesetPathHash($changeset);
58
$changeset_phid = $this->getChangesetKey($changeset);
59
60
$this->hasModifications = true;
61
62
$this->viewState['changesets'][$path_hash][$key][$changeset_phid] =
63
$properties;
64
}
65
66
public function getChangesetProperty(
67
DifferentialChangeset $changeset,
68
$key,
69
$default = null) {
70
71
$entries = $this->getChangesetPropertyEntries(
72
$changeset,
73
$key);
74
75
$entries = isort($entries, 'epoch');
76
77
$entry = last($entries);
78
if (!is_array($entry)) {
79
$entry = array();
80
}
81
82
return idx($entry, 'value', $default);
83
}
84
85
public function getChangesetPropertyEntries(
86
DifferentialChangeset $changeset,
87
$key) {
88
$path_hash = $this->getChangesetPathHash($changeset);
89
90
$entries = idxv($this->viewState, array('changesets', $path_hash, $key));
91
if (!is_array($entries)) {
92
$entries = array();
93
}
94
95
return $entries;
96
}
97
98
public function getHasModifications() {
99
return $this->hasModifications;
100
}
101
102
private function getChangesetPathHash(DifferentialChangeset $changeset) {
103
$path = $changeset->getFilename();
104
return PhabricatorHash::digestForIndex($path);
105
}
106
107
private function getChangesetKey(DifferentialChangeset $changeset) {
108
$key = $changeset->getID();
109
110
if ($key === null) {
111
return '*';
112
}
113
114
return (string)$key;
115
}
116
117
public static function copyViewStatesToObject($src_phid, $dst_phid) {
118
$table = new self();
119
$conn = $table->establishConnection('w');
120
121
queryfx(
122
$conn,
123
'INSERT IGNORE INTO %R
124
(viewerPHID, objectPHID, viewState, dateCreated, dateModified)
125
SELECT viewerPHID, %s, viewState, dateCreated, dateModified
126
FROM %R WHERE objectPHID = %s',
127
$table,
128
$dst_phid,
129
$table,
130
$src_phid);
131
}
132
133
/* -( PhabricatorPolicyInterface )----------------------------------------- */
134
135
136
public function getCapabilities() {
137
return array(
138
PhabricatorPolicyCapability::CAN_VIEW,
139
);
140
}
141
142
public function getPolicy($capability) {
143
return PhabricatorPolicies::POLICY_NOONE;
144
}
145
146
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
147
return ($viewer->getPHID() === $this->getViewerPHID());
148
}
149
150
}
151
152