Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / draft / storage / PhabricatorDraft.php
blobf50e44fa0f29304b79e1e1fc3c5b4f8505f4a2b4
1 <?php
3 final class PhabricatorDraft extends PhabricatorDraftDAO {
5 protected $authorPHID;
6 protected $draftKey;
7 protected $draft;
8 protected $metadata = array();
10 private $deleted = false;
12 protected function getConfiguration() {
13 return array(
14 self::CONFIG_SERIALIZATION => array(
15 'metadata' => self::SERIALIZATION_JSON,
17 self::CONFIG_COLUMN_SCHEMA => array(
18 'draftKey' => 'text64',
19 'draft' => 'text',
21 self::CONFIG_KEY_SCHEMA => array(
22 'authorPHID' => array(
23 'columns' => array('authorPHID', 'draftKey'),
24 'unique' => true,
27 ) + parent::getConfiguration();
30 public function replaceOrDelete() {
31 if ($this->draft == '' && !array_filter($this->metadata)) {
32 queryfx(
33 $this->establishConnection('w'),
34 'DELETE FROM %T WHERE authorPHID = %s AND draftKey = %s',
35 $this->getTableName(),
36 $this->authorPHID,
37 $this->draftKey);
38 $this->deleted = true;
39 return $this;
41 return parent::replace();
44 protected function didDelete() {
45 $this->deleted = true;
48 public function isDeleted() {
49 return $this->deleted;
52 public static function newFromUserAndKey(PhabricatorUser $user, $key) {
53 if ($user->getPHID() && strlen($key)) {
54 $draft = id(new PhabricatorDraft())->loadOneWhere(
55 'authorPHID = %s AND draftKey = %s',
56 $user->getPHID(),
57 $key);
58 if ($draft) {
59 return $draft;
63 $draft = new PhabricatorDraft();
64 if ($user->getPHID()) {
65 $draft
66 ->setAuthorPHID($user->getPHID())
67 ->setDraftKey($key);
70 return $draft;
73 public static function buildFromRequest(AphrontRequest $request) {
74 $user = $request->getUser();
75 if (!$user->getPHID()) {
76 return null;
79 if (!$request->getStr('__draft__')) {
80 return null;
83 $draft = id(new PhabricatorDraft())
84 ->setAuthorPHID($user->getPHID())
85 ->setDraftKey($request->getStr('__draft__'));
87 // If this is a preview, add other data. If not, leave the draft empty so
88 // that replaceOrDelete() will delete it.
89 if ($request->isPreviewRequest()) {
90 $other_data = $request->getPassthroughRequestData();
91 unset($other_data['comment']);
93 $draft
94 ->setDraft($request->getStr('comment'))
95 ->setMetadata($other_data);
98 return $draft;