PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/paste/storage/PhabricatorPaste.php

http://github.com/facebook/phabricator
PHP | 297 lines | 222 code | 64 blank | 11 comment | 12 complexity | a0e241783bfdaa4c84c1edfe627e03b2 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class PhabricatorPaste extends PhabricatorPasteDAO
  3. implements
  4. PhabricatorSubscribableInterface,
  5. PhabricatorTokenReceiverInterface,
  6. PhabricatorFlaggableInterface,
  7. PhabricatorMentionableInterface,
  8. PhabricatorPolicyInterface,
  9. PhabricatorProjectInterface,
  10. PhabricatorDestructibleInterface,
  11. PhabricatorApplicationTransactionInterface,
  12. PhabricatorSpacesInterface,
  13. PhabricatorConduitResultInterface,
  14. PhabricatorFerretInterface,
  15. PhabricatorFulltextInterface {
  16. protected $title;
  17. protected $authorPHID;
  18. protected $filePHID;
  19. protected $language;
  20. protected $parentPHID;
  21. protected $viewPolicy;
  22. protected $editPolicy;
  23. protected $mailKey;
  24. protected $status;
  25. protected $spacePHID;
  26. const STATUS_ACTIVE = 'active';
  27. const STATUS_ARCHIVED = 'archived';
  28. private $content = self::ATTACHABLE;
  29. private $rawContent = self::ATTACHABLE;
  30. private $snippet = self::ATTACHABLE;
  31. public static function initializeNewPaste(PhabricatorUser $actor) {
  32. $app = id(new PhabricatorApplicationQuery())
  33. ->setViewer($actor)
  34. ->withClasses(array('PhabricatorPasteApplication'))
  35. ->executeOne();
  36. $view_policy = $app->getPolicy(PasteDefaultViewCapability::CAPABILITY);
  37. $edit_policy = $app->getPolicy(PasteDefaultEditCapability::CAPABILITY);
  38. return id(new PhabricatorPaste())
  39. ->setTitle('')
  40. ->setStatus(self::STATUS_ACTIVE)
  41. ->setAuthorPHID($actor->getPHID())
  42. ->setViewPolicy($view_policy)
  43. ->setEditPolicy($edit_policy)
  44. ->setSpacePHID($actor->getDefaultSpacePHID())
  45. ->attachRawContent(null);
  46. }
  47. public static function getStatusNameMap() {
  48. return array(
  49. self::STATUS_ACTIVE => pht('Active'),
  50. self::STATUS_ARCHIVED => pht('Archived'),
  51. );
  52. }
  53. public function getURI() {
  54. return '/'.$this->getMonogram();
  55. }
  56. public function getMonogram() {
  57. return 'P'.$this->getID();
  58. }
  59. protected function getConfiguration() {
  60. return array(
  61. self::CONFIG_AUX_PHID => true,
  62. self::CONFIG_COLUMN_SCHEMA => array(
  63. 'status' => 'text32',
  64. 'title' => 'text255',
  65. 'language' => 'text64?',
  66. 'mailKey' => 'bytes20',
  67. 'parentPHID' => 'phid?',
  68. // T6203/NULLABILITY
  69. // Pastes should always have a view policy.
  70. 'viewPolicy' => 'policy?',
  71. ),
  72. self::CONFIG_KEY_SCHEMA => array(
  73. 'parentPHID' => array(
  74. 'columns' => array('parentPHID'),
  75. ),
  76. 'authorPHID' => array(
  77. 'columns' => array('authorPHID'),
  78. ),
  79. 'key_dateCreated' => array(
  80. 'columns' => array('dateCreated'),
  81. ),
  82. 'key_language' => array(
  83. 'columns' => array('language'),
  84. ),
  85. ),
  86. ) + parent::getConfiguration();
  87. }
  88. public function generatePHID() {
  89. return PhabricatorPHID::generateNewPHID(
  90. PhabricatorPastePastePHIDType::TYPECONST);
  91. }
  92. public function isArchived() {
  93. return ($this->getStatus() == self::STATUS_ARCHIVED);
  94. }
  95. public function save() {
  96. if (!$this->getMailKey()) {
  97. $this->setMailKey(Filesystem::readRandomCharacters(20));
  98. }
  99. return parent::save();
  100. }
  101. public function getFullName() {
  102. $title = $this->getTitle();
  103. if (!$title) {
  104. $title = pht('(An Untitled Masterwork)');
  105. }
  106. return 'P'.$this->getID().' '.$title;
  107. }
  108. public function getContent() {
  109. return $this->assertAttached($this->content);
  110. }
  111. public function attachContent($content) {
  112. $this->content = $content;
  113. return $this;
  114. }
  115. public function getRawContent() {
  116. return $this->assertAttached($this->rawContent);
  117. }
  118. public function attachRawContent($raw_content) {
  119. $this->rawContent = $raw_content;
  120. return $this;
  121. }
  122. public function getSnippet() {
  123. return $this->assertAttached($this->snippet);
  124. }
  125. public function attachSnippet(PhabricatorPasteSnippet $snippet) {
  126. $this->snippet = $snippet;
  127. return $this;
  128. }
  129. /* -( PhabricatorSubscribableInterface )----------------------------------- */
  130. public function isAutomaticallySubscribed($phid) {
  131. return ($this->authorPHID == $phid);
  132. }
  133. /* -( PhabricatorTokenReceiverInterface )---------------------------------- */
  134. public function getUsersToNotifyOfTokenGiven() {
  135. return array(
  136. $this->getAuthorPHID(),
  137. );
  138. }
  139. /* -( PhabricatorPolicyInterface )----------------------------------------- */
  140. public function getCapabilities() {
  141. return array(
  142. PhabricatorPolicyCapability::CAN_VIEW,
  143. PhabricatorPolicyCapability::CAN_EDIT,
  144. );
  145. }
  146. public function getPolicy($capability) {
  147. if ($capability == PhabricatorPolicyCapability::CAN_VIEW) {
  148. return $this->viewPolicy;
  149. } else if ($capability == PhabricatorPolicyCapability::CAN_EDIT) {
  150. return $this->editPolicy;
  151. }
  152. return PhabricatorPolicies::POLICY_NOONE;
  153. }
  154. public function hasAutomaticCapability($capability, PhabricatorUser $user) {
  155. return ($user->getPHID() == $this->getAuthorPHID());
  156. }
  157. public function describeAutomaticCapability($capability) {
  158. return pht('The author of a paste can always view and edit it.');
  159. }
  160. /* -( PhabricatorDestructibleInterface )----------------------------------- */
  161. public function destroyObjectPermanently(
  162. PhabricatorDestructionEngine $engine) {
  163. if ($this->filePHID) {
  164. $file = id(new PhabricatorFileQuery())
  165. ->setViewer($engine->getViewer())
  166. ->withPHIDs(array($this->filePHID))
  167. ->executeOne();
  168. if ($file) {
  169. $engine->destroyObject($file);
  170. }
  171. }
  172. $this->delete();
  173. }
  174. /* -( PhabricatorApplicationTransactionInterface )------------------------- */
  175. public function getApplicationTransactionEditor() {
  176. return new PhabricatorPasteEditor();
  177. }
  178. public function getApplicationTransactionTemplate() {
  179. return new PhabricatorPasteTransaction();
  180. }
  181. /* -( PhabricatorSpacesInterface )----------------------------------------- */
  182. public function getSpacePHID() {
  183. return $this->spacePHID;
  184. }
  185. /* -( PhabricatorConduitResultInterface )---------------------------------- */
  186. public function getFieldSpecificationsForConduit() {
  187. return array(
  188. id(new PhabricatorConduitSearchFieldSpecification())
  189. ->setKey('title')
  190. ->setType('string')
  191. ->setDescription(pht('The title of the paste.')),
  192. id(new PhabricatorConduitSearchFieldSpecification())
  193. ->setKey('uri')
  194. ->setType('uri')
  195. ->setDescription(pht('View URI for the paste.')),
  196. id(new PhabricatorConduitSearchFieldSpecification())
  197. ->setKey('authorPHID')
  198. ->setType('phid')
  199. ->setDescription(pht('User PHID of the author.')),
  200. id(new PhabricatorConduitSearchFieldSpecification())
  201. ->setKey('language')
  202. ->setType('string?')
  203. ->setDescription(pht('Language to use for syntax highlighting.')),
  204. id(new PhabricatorConduitSearchFieldSpecification())
  205. ->setKey('status')
  206. ->setType('string')
  207. ->setDescription(pht('Active or archived status of the paste.')),
  208. );
  209. }
  210. public function getFieldValuesForConduit() {
  211. return array(
  212. 'title' => $this->getTitle(),
  213. 'uri' => PhabricatorEnv::getURI($this->getURI()),
  214. 'authorPHID' => $this->getAuthorPHID(),
  215. 'language' => nonempty($this->getLanguage(), null),
  216. 'status' => $this->getStatus(),
  217. );
  218. }
  219. public function getConduitSearchAttachments() {
  220. return array(
  221. id(new PhabricatorPasteContentSearchEngineAttachment())
  222. ->setAttachmentKey('content'),
  223. );
  224. }
  225. /* -( PhabricatorFerretInterface )----------------------------------------- */
  226. public function newFerretEngine() {
  227. return new PhabricatorPasteFerretEngine();
  228. }
  229. /* -( PhabricatorFulltextInterface )--------------------------------------- */
  230. public function newFulltextEngine() {
  231. return new PhabricatorPasteFulltextEngine();
  232. }
  233. }