3 final class PhabricatorVersionedDraft
extends PhabricatorDraftDAO
{
5 const KEY_VERSION
= 'draft.version';
10 protected $properties = array();
12 protected function getConfiguration() {
14 self
::CONFIG_SERIALIZATION
=> array(
15 'properties' => self
::SERIALIZATION_JSON
,
17 self
::CONFIG_COLUMN_SCHEMA
=> array(
18 'version' => 'uint32',
20 self
::CONFIG_KEY_SCHEMA
=> array(
21 'key_object' => array(
22 'columns' => array('objectPHID', 'authorPHID', 'version'),
26 ) + parent
::getConfiguration();
29 public function setProperty($key, $value) {
30 $this->properties
[$key] = $value;
34 public function getProperty($key, $default = null) {
35 return idx($this->properties
, $key, $default);
38 public static function loadDrafts(
42 $rows = id(new self())->loadAllWhere(
43 'objectPHID IN (%Ls) AND authorPHID = %s ORDER BY version ASC',
48 foreach ($rows as $row) {
49 $map[$row->getObjectPHID()] = $row;
55 public static function loadDraft(
59 return id(new PhabricatorVersionedDraft())->loadOneWhere(
60 'objectPHID = %s AND authorPHID = %s ORDER BY version DESC LIMIT 1',
65 public static function loadOrCreateDraft(
70 $draft = self
::loadDraft($object_phid, $viewer_phid);
77 ->setObjectPHID($object_phid)
78 ->setAuthorPHID($viewer_phid)
79 ->setVersion((int)$version)
81 } catch (AphrontDuplicateKeyQueryException
$ex) {
82 $duplicate_exception = $ex;
85 // In rare cases we can race ourselves, and at one point there was a bug
86 // which caused the browser to submit two preview requests at exactly
87 // the same time. If the insert failed with a duplicate key exception,
88 // try to load the colliding row to recover from it.
90 $draft = self
::loadDraft($object_phid, $viewer_phid);
95 throw $duplicate_exception;
98 public static function purgeDrafts(
102 $draft = new PhabricatorVersionedDraft();
103 $conn_w = $draft->establishConnection('w');
107 'DELETE FROM %T WHERE objectPHID = %s AND authorPHID = %s',
108 $draft->getTableName(),