~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/applications/diviner/storage/DivinerLiveSymbol.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • Revision ID: package-import@ubuntu.com-20141023204926-ar20vnfjqwxysrce
Tags: upstream-0~git20141023
ImportĀ upstreamĀ versionĀ 0~git20141023

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class DivinerLiveSymbol extends DivinerDAO
 
4
  implements PhabricatorPolicyInterface, PhabricatorMarkupInterface {
 
5
 
 
6
  protected $bookPHID;
 
7
  protected $context;
 
8
  protected $type;
 
9
  protected $name;
 
10
  protected $atomIndex;
 
11
  protected $graphHash;
 
12
  protected $identityHash;
 
13
  protected $nodeHash;
 
14
 
 
15
  protected $title;
 
16
  protected $titleSlugHash;
 
17
  protected $groupName;
 
18
  protected $summary;
 
19
  protected $isDocumentable = 0;
 
20
 
 
21
  private $book = self::ATTACHABLE;
 
22
  private $atom = self::ATTACHABLE;
 
23
  private $extends = self::ATTACHABLE;
 
24
  private $children = self::ATTACHABLE;
 
25
 
 
26
  public function getConfiguration() {
 
27
    return array(
 
28
      self::CONFIG_AUX_PHID => true,
 
29
      self::CONFIG_TIMESTAMPS => false,
 
30
      self::CONFIG_COLUMN_SCHEMA => array(
 
31
        'context' => 'text255?',
 
32
        'type' => 'text32',
 
33
        'name' => 'text255',
 
34
        'atomIndex' => 'uint32',
 
35
        'identityHash' => 'bytes12',
 
36
        'graphHash' => 'text64?',
 
37
        'title' => 'text?',
 
38
        'titleSlugHash' => 'bytes12?',
 
39
        'groupName' => 'text255?',
 
40
        'summary' => 'text?',
 
41
        'isDocumentable' => 'bool',
 
42
        'nodeHash' => 'text64?',
 
43
      ),
 
44
      self::CONFIG_KEY_SCHEMA => array(
 
45
        'key_phid' => null,
 
46
        'identityHash' => array(
 
47
          'columns' => array('identityHash'),
 
48
          'unique' => true,
 
49
        ),
 
50
        'phid' => array(
 
51
          'columns' => array('phid'),
 
52
          'unique' => true,
 
53
        ),
 
54
        'graphHash' => array(
 
55
          'columns' => array('graphHash'),
 
56
          'unique' => true,
 
57
        ),
 
58
        'nodeHash' => array(
 
59
          'columns' => array('nodeHash'),
 
60
          'unique' => true,
 
61
        ),
 
62
        'bookPHID' => array(
 
63
          'columns' => array(
 
64
            'bookPHID',
 
65
            'type',
 
66
            'name(64)',
 
67
            'context(64)',
 
68
            'atomIndex',
 
69
          ),
 
70
        ),
 
71
        'name' => array(
 
72
          'columns' => array('name(64)'),
 
73
        ),
 
74
        'key_slug' => array(
 
75
          'columns' => array('titleSlugHash'),
 
76
        ),
 
77
      ),
 
78
    ) + parent::getConfiguration();
 
79
  }
 
80
 
 
81
  public function generatePHID() {
 
82
    return PhabricatorPHID::generateNewPHID(DivinerAtomPHIDType::TYPECONST);
 
83
  }
 
84
 
 
85
  public function getBook() {
 
86
    return $this->assertAttached($this->book);
 
87
  }
 
88
 
 
89
  public function attachBook(DivinerLiveBook $book) {
 
90
    $this->book = $book;
 
91
    return $this;
 
92
  }
 
93
 
 
94
  public function getAtom() {
 
95
    return $this->assertAttached($this->atom);
 
96
  }
 
97
 
 
98
  public function attachAtom(DivinerLiveAtom $atom) {
 
99
    $this->atom = DivinerAtom::newFromDictionary($atom->getAtomData());
 
100
    return $this;
 
101
  }
 
102
 
 
103
  public function getURI() {
 
104
    $parts = array(
 
105
      'book',
 
106
      $this->getBook()->getName(),
 
107
      $this->getType(),
 
108
    );
 
109
 
 
110
    if ($this->getContext()) {
 
111
      $parts[] = $this->getContext();
 
112
    }
 
113
 
 
114
    $parts[] = $this->getName();
 
115
 
 
116
    if ($this->getAtomIndex()) {
 
117
      $parts[] = $this->getAtomIndex();
 
118
    }
 
119
 
 
120
    return '/'.implode('/', $parts).'/';
 
121
  }
 
122
 
 
123
  public function getSortKey() {
 
124
    // Sort articles before other types of content. Then, sort atoms in a
 
125
    // case-insensitive way.
 
126
    return sprintf(
 
127
      '%c:%s',
 
128
      ($this->getType() == DivinerAtom::TYPE_ARTICLE ? '0' : '1'),
 
129
      phutil_utf8_strtolower($this->getTitle()));
 
130
  }
 
131
 
 
132
  public function save() {
 
133
 
 
134
    // NOTE: The identity hash is just a sanity check because the unique tuple
 
135
    // on this table is way way too long to fit into a normal UNIQUE KEY. We
 
136
    // don't use it directly, but its existence prevents duplicate records.
 
137
 
 
138
    if (!$this->identityHash) {
 
139
      $this->identityHash = PhabricatorHash::digestForIndex(
 
140
        serialize(
 
141
          array(
 
142
            'bookPHID' => $this->getBookPHID(),
 
143
            'context'  => $this->getContext(),
 
144
            'type'     => $this->getType(),
 
145
            'name'     => $this->getName(),
 
146
            'index'    => $this->getAtomIndex(),
 
147
          )));
 
148
    }
 
149
 
 
150
    return parent::save();
 
151
  }
 
152
 
 
153
  public function getTitle() {
 
154
    $title = parent::getTitle();
 
155
    if (!strlen($title)) {
 
156
      $title = $this->getName();
 
157
    }
 
158
    return $title;
 
159
  }
 
160
 
 
161
  public function setTitle($value) {
 
162
    $this->writeField('title', $value);
 
163
    if (strlen($value)) {
 
164
      $slug = DivinerAtomRef::normalizeTitleString($value);
 
165
      $hash = PhabricatorHash::digestForIndex($slug);
 
166
      $this->titleSlugHash = $hash;
 
167
    } else {
 
168
      $this->titleSlugHash = null;
 
169
    }
 
170
    return $this;
 
171
  }
 
172
 
 
173
  public function attachExtends(array $extends) {
 
174
    assert_instances_of($extends, 'DivinerLiveSymbol');
 
175
    $this->extends = $extends;
 
176
    return $this;
 
177
  }
 
178
 
 
179
  public function getExtends() {
 
180
    return $this->assertAttached($this->extends);
 
181
  }
 
182
 
 
183
  public function attachChildren(array $children) {
 
184
    assert_instances_of($children, 'DivinerLiveSymbol');
 
185
    $this->children = $children;
 
186
    return $this;
 
187
  }
 
188
 
 
189
  public function getChildren() {
 
190
    return $this->assertAttached($this->children);
 
191
  }
 
192
 
 
193
 
 
194
/* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 
195
 
 
196
  public function getCapabilities() {
 
197
    return $this->getBook()->getCapabilities();
 
198
  }
 
199
 
 
200
 
 
201
  public function getPolicy($capability) {
 
202
    return $this->getBook()->getPolicy($capability);
 
203
  }
 
204
 
 
205
 
 
206
  public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 
207
    return $this->getBook()->hasAutomaticCapability($capability, $viewer);
 
208
  }
 
209
 
 
210
  public function describeAutomaticCapability($capability) {
 
211
    return pht('Atoms inherit the policies of the books they are part of.');
 
212
  }
 
213
 
 
214
 
 
215
/* -(  Markup Interface  )--------------------------------------------------- */
 
216
 
 
217
 
 
218
  public function getMarkupFieldKey($field) {
 
219
    return $this->getPHID().':'.$field.':'.$this->getGraphHash();
 
220
  }
 
221
 
 
222
 
 
223
  public function newMarkupEngine($field) {
 
224
    return PhabricatorMarkupEngine::getEngine('diviner');
 
225
  }
 
226
 
 
227
 
 
228
  public function getMarkupText($field) {
 
229
    return $this->getAtom()->getDocblockText();
 
230
  }
 
231
 
 
232
 
 
233
  public function didMarkupText(
 
234
    $field,
 
235
    $output,
 
236
    PhutilMarkupEngine $engine) {
 
237
    return $output;
 
238
  }
 
239
 
 
240
 
 
241
  public function shouldUseMarkupCache($field) {
 
242
    return true;
 
243
  }
 
244
 
 
245
}