forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorVersionedDraft.php
113 lines (91 loc) · 2.7 KB
/
PhabricatorVersionedDraft.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
final class PhabricatorVersionedDraft extends PhabricatorDraftDAO {
const KEY_VERSION = 'draft.version';
protected $objectPHID;
protected $authorPHID;
protected $version;
protected $properties = array();
protected function getConfiguration() {
return array(
self::CONFIG_SERIALIZATION => array(
'properties' => self::SERIALIZATION_JSON,
),
self::CONFIG_COLUMN_SCHEMA => array(
'version' => 'uint32',
),
self::CONFIG_KEY_SCHEMA => array(
'key_object' => array(
'columns' => array('objectPHID', 'authorPHID', 'version'),
'unique' => true,
),
),
) + parent::getConfiguration();
}
public function setProperty($key, $value) {
$this->properties[$key] = $value;
return $this;
}
public function getProperty($key, $default = null) {
return idx($this->properties, $key, $default);
}
public static function loadDrafts(
array $object_phids,
$viewer_phid) {
$rows = id(new self())->loadAllWhere(
'objectPHID IN (%Ls) AND authorPHID = %s ORDER BY version ASC',
$object_phids,
$viewer_phid);
$map = array();
foreach ($rows as $row) {
$map[$row->getObjectPHID()] = $row;
}
return $map;
}
public static function loadDraft(
$object_phid,
$viewer_phid) {
return id(new PhabricatorVersionedDraft())->loadOneWhere(
'objectPHID = %s AND authorPHID = %s ORDER BY version DESC LIMIT 1',
$object_phid,
$viewer_phid);
}
public static function loadOrCreateDraft(
$object_phid,
$viewer_phid,
$version) {
$draft = self::loadDraft($object_phid, $viewer_phid);
if ($draft) {
return $draft;
}
try {
return id(new self())
->setObjectPHID($object_phid)
->setAuthorPHID($viewer_phid)
->setVersion((int)$version)
->save();
} catch (AphrontDuplicateKeyQueryException $ex) {
$duplicate_exception = $ex;
}
// In rare cases we can race ourselves, and at one point there was a bug
// which caused the browser to submit two preview requests at exactly
// the same time. If the insert failed with a duplicate key exception,
// try to load the colliding row to recover from it.
$draft = self::loadDraft($object_phid, $viewer_phid);
if ($draft) {
return $draft;
}
throw $duplicate_exception;
}
public static function purgeDrafts(
$object_phid,
$viewer_phid) {
$draft = new PhabricatorVersionedDraft();
$conn_w = $draft->establishConnection('w');
queryfx(
$conn_w,
'DELETE FROM %T WHERE objectPHID = %s AND authorPHID = %s',
$draft->getTableName(),
$object_phid,
$viewer_phid);
}
}