PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/herald/storage/transcript/base/HeraldTranscript.php

http://github.com/facebook/phabricator
PHP | 185 lines | 130 code | 35 blank | 20 comment | 4 complexity | 48b8f1e58d81f0a5067dfd029288a19d 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. /*
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. class HeraldTranscript extends HeraldDAO {
  18. protected $id;
  19. protected $phid;
  20. protected $objectTranscript;
  21. protected $ruleTranscripts = array();
  22. protected $conditionTranscripts = array();
  23. protected $applyTranscripts = array();
  24. protected $time;
  25. protected $host;
  26. protected $duration;
  27. protected $objectPHID;
  28. protected $dryRun;
  29. const TABLE_SAVED_HEADER = 'herald_savedheader';
  30. public function getXHeraldRulesHeader() {
  31. $ids = array();
  32. foreach ($this->applyTranscripts as $xscript) {
  33. if ($xscript->getApplied()) {
  34. if ($xscript->getRuleID()) {
  35. $ids[] = $xscript->getRuleID();
  36. }
  37. }
  38. }
  39. if (!$ids) {
  40. return 'none';
  41. }
  42. // A rule may have multiple effects, which will cause it to be listed
  43. // multiple times.
  44. $ids = array_unique($ids);
  45. foreach ($ids as $k => $id) {
  46. $ids[$k] = '<'.$id.'>';
  47. }
  48. return implode(', ', $ids);
  49. }
  50. public static function saveXHeraldRulesHeader($phid, $header) {
  51. // Combine any existing header with the new header, listing all rules
  52. // which have ever triggered for this object.
  53. $header = self::combineXHeraldRulesHeaders(
  54. self::loadXHeraldRulesHeader($phid),
  55. $header);
  56. queryfx(
  57. id(new HeraldTranscript())->establishConnection('w'),
  58. 'INSERT INTO %T (phid, header) VALUES (%s, %s)
  59. ON DUPLICATE KEY UPDATE header = VALUES(header)',
  60. self::TABLE_SAVED_HEADER,
  61. $phid,
  62. $header);
  63. return $header;
  64. }
  65. private static function combineXHeraldRulesHeaders($u, $v) {
  66. $u = preg_split('/[, ]+/', $u);
  67. $v = preg_split('/[, ]+/', $v);
  68. $combined = array_unique(array_filter(array_merge($u, $v)));
  69. return implode(', ', $combined);
  70. }
  71. public static function loadXHeraldRulesHeader($phid) {
  72. $header = queryfx_one(
  73. id(new HeraldTranscript())->establishConnection('r'),
  74. 'SELECT * FROM %T WHERE phid = %s',
  75. self::TABLE_SAVED_HEADER,
  76. $phid);
  77. if ($header) {
  78. return idx($header, 'header');
  79. }
  80. return null;
  81. }
  82. protected function getConfiguration() {
  83. // Ugh. Too much of a mess to deal with.
  84. return array(
  85. self::CONFIG_AUX_PHID => true,
  86. self::CONFIG_TIMESTAMPS => false,
  87. self::CONFIG_SERIALIZATION => array(
  88. 'objectTranscript' => self::SERIALIZATION_PHP,
  89. 'ruleTranscripts' => self::SERIALIZATION_PHP,
  90. 'conditionTranscripts' => self::SERIALIZATION_PHP,
  91. 'applyTranscripts' => self::SERIALIZATION_PHP,
  92. ),
  93. ) + parent::getConfiguration();
  94. }
  95. public function __construct() {
  96. $this->time = time();
  97. $this->host = php_uname('n');
  98. }
  99. public function addApplyTranscript(HeraldApplyTranscript $transcript) {
  100. $this->applyTranscripts[] = $transcript;
  101. return $this;
  102. }
  103. public function getApplyTranscripts() {
  104. return nonempty($this->applyTranscripts, array());
  105. }
  106. public function setDuration($duration) {
  107. $this->duration = $duration;
  108. return $this;
  109. }
  110. public function setObjectTranscript(HeraldObjectTranscript $transcript) {
  111. $this->objectTranscript = $transcript;
  112. return $this;
  113. }
  114. public function getObjectTranscript() {
  115. return $this->objectTranscript;
  116. }
  117. public function addRuleTranscript(HeraldRuleTranscript $transcript) {
  118. $this->ruleTranscripts[$transcript->getRuleID()] = $transcript;
  119. return $this;
  120. }
  121. public function discardDetails() {
  122. $this->applyTranscripts = null;
  123. $this->ruleTranscripts = null;
  124. $this->objectTranscript = null;
  125. $this->conditionTranscripts = null;
  126. }
  127. public function getRuleTranscripts() {
  128. return nonempty($this->ruleTranscripts, array());
  129. }
  130. public function addConditionTranscript(
  131. HeraldConditionTranscript $transcript) {
  132. $rule_id = $transcript->getRuleID();
  133. $cond_id = $transcript->getConditionID();
  134. $this->conditionTranscripts[$rule_id][$cond_id] = $transcript;
  135. return $this;
  136. }
  137. public function getConditionTranscriptsForRule($rule_id) {
  138. return idx($this->conditionTranscripts, $rule_id, array());
  139. }
  140. public function getMetadataMap() {
  141. return array(
  142. 'Run At Epoch' => date('F jS, g:i:s A', $this->time),
  143. 'Run On Host' => $this->host,
  144. 'Run Duration' => (int)(1000 * $this->duration).' ms',
  145. );
  146. }
  147. public function generatePHID() {
  148. return PhabricatorPHID::generateNewPHID('HLXS');
  149. }
  150. }