/Class/Fdl/Class.UpdateAttributeStatus.php

https://github.com/CircleCode/dynacase-core · PHP · 155 lines · 81 code · 5 blank · 69 comment · 10 complexity · 0592676111172e29403bc0879494d519 MD5 · raw file

  1. <?php
  2. /*
  3. * @author Anakeen
  4. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  5. * @package FDL
  6. */
  7. /**
  8. * update attribut status management
  9. *
  10. * @author Anakeen
  11. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  12. * @package FDL
  13. */
  14. /**
  15. * Format document list to be easily used in
  16. * @class UpdateAttributeStatus
  17. * @code
  18. $newValue=4;
  19. $attrid='TST_ENUM';
  20. $s = new \SearchDoc('', 'TST_UPDTATTR');
  21. $s->setObjectReturn();
  22. $s->setSlice(34);
  23. $dl = new \DocumentList($s);
  24. $ua = new \UpdateAttribute();
  25. $ua->useCollection($dl);
  26. $comment = "coucoux";
  27. $ua->addHistoryComment($comment);
  28. $ua->revise('REvX');
  29. $statusFile= $ua->bgSetValue($attrid, $newValue);
  30. $sua= new UpdateAttributeStatus($statusFile);
  31. while (! $sua->isFinished()) {
  32. print $sua->getStatus();
  33. print ".";
  34. sleep(1);
  35. }
  36. * @endcode
  37. */
  38. class UpdateAttributeStatus
  39. {
  40. private $statusFile = '';
  41. private $content = null;
  42. const statusRunning = 1;
  43. const StatusFinished = 2;
  44. const statusUnknown = 3;
  45. public function __construct($statusFile)
  46. {
  47. $this->statusFile = $statusFile;
  48. if (!file_exists($this->statusFile)) throw new Dcp\Upat\Exception("UPAT0003", $statusFile);
  49. }
  50. private function readStatus()
  51. {
  52. $this->content = file($this->statusFile);
  53. }
  54. /**
  55. * return file status content
  56. * one line by array items
  57. * @return string[]
  58. */
  59. public function getContent()
  60. {
  61. if ($this->content === null) $this->readStatus();
  62. return $this->content;
  63. }
  64. /**
  65. * return global status
  66. * update file status content
  67. * @return int StatusFinished, StatusRunning or statusUnknown
  68. */
  69. public function getStatus()
  70. {
  71. $this->readStatus();
  72. if ($this->content) {
  73. $last = end($this->content);
  74. if (strpos($last, 'END') > 0) return self::StatusFinished;
  75. $first = $this->content[0];
  76. if (strpos($first, 'BEGIN') > 0) return self::statusRunning;
  77. }
  78. return self::statusUnknown;
  79. }
  80. /**
  81. * get lines which match code
  82. * @param string $code
  83. * @return string[]
  84. */
  85. public function getCodeLines($code)
  86. {
  87. if ($this->content === null) $this->readStatus();
  88. $lines = array();
  89. foreach ($this->content as $line) {
  90. if (preg_match(sprintf("/^[0-9T:-]{19} [\w-]* ?%s/u", preg_quote($code, "/")) , $line)) $lines[] = $line;
  91. }
  92. return $lines;
  93. }
  94. /**
  95. * get last message from file status
  96. * @return UpdateAttributeStatusLine
  97. */
  98. public function getLastMessage()
  99. {
  100. $l = new UpdateAttributeStatusLine();
  101. list($l->date, $l->processCode, $l->message) = explode(' ', trim(end($this->content)) , 3);
  102. return $l;
  103. }
  104. /**
  105. * return error messages
  106. * empty string if not
  107. * @return string error messages
  108. */
  109. public function getError()
  110. {
  111. $r = $this->getCodeLines("ERROR");
  112. if ($r) {
  113. return implode("\n", $r);
  114. }
  115. return '';
  116. }
  117. /**
  118. * get all status for each documents
  119. * return null is no processing doduments
  120. * @return null|UpdateAttributeResults[]
  121. */
  122. public function getResults()
  123. {
  124. $r = $this->getCodeLines("logStatusReport PHP");
  125. if ($r) {
  126. $pos = mb_strpos($r[0], ':', 20);
  127. $s = mb_substr($r[0], $pos + 1);
  128. return unserialize($s);
  129. }
  130. return null;
  131. }
  132. /**
  133. * return true is status file contains message that indicates end of processing
  134. * @return bool
  135. */
  136. public function isFinished()
  137. {
  138. return $this->getStatus() == self::StatusFinished;
  139. }
  140. }
  141. class UpdateAttributeStatusLine
  142. {
  143. public $date;
  144. public $processCode;
  145. public $message;
  146. public function __toString()
  147. {
  148. return $this->date . " " . $this->processCode . " " . $this->message;
  149. }
  150. }