History.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Class History
  4. *
  5. * Handle the history file tracing events in Shaarli.
  6. * The history is stored as JSON in a file set by 'resource.history' setting.
  7. *
  8. * Available data:
  9. * - event: event key
  10. * - datetime: event date, in ISO8601 format.
  11. * - id: event item identifier (currently only link IDs).
  12. *
  13. * Available event keys:
  14. * - CREATED: new link
  15. * - UPDATED: link updated
  16. * - DELETED: link deleted
  17. * - SETTINGS: the settings have been updated through the UI.
  18. *
  19. * Note: new events are put at the beginning of the file and history array.
  20. */
  21. class History
  22. {
  23. /**
  24. * @var string Action key: a new link has been created.
  25. */
  26. const CREATED = 'CREATED';
  27. /**
  28. * @var string Action key: a link has been updated.
  29. */
  30. const UPDATED = 'UPDATED';
  31. /**
  32. * @var string Action key: a link has been deleted.
  33. */
  34. const DELETED = 'DELETED';
  35. /**
  36. * @var string Action key: settings have been updated.
  37. */
  38. const SETTINGS = 'SETTINGS';
  39. /**
  40. * @var string History file path.
  41. */
  42. protected $historyFilePath;
  43. /**
  44. * @var array History data.
  45. */
  46. protected $history;
  47. /**
  48. * @var int History retention time in seconds (1 month).
  49. */
  50. protected $retentionTime = 2678400;
  51. /**
  52. * History constructor.
  53. *
  54. * @param string $historyFilePath History file path.
  55. * @param int $retentionTime History content rentention time in seconds.
  56. *
  57. * @throws Exception if something goes wrong.
  58. */
  59. public function __construct($historyFilePath, $retentionTime = null)
  60. {
  61. $this->historyFilePath = $historyFilePath;
  62. if ($retentionTime !== null) {
  63. $this->retentionTime = $retentionTime;
  64. }
  65. }
  66. /**
  67. * Initialize: read history file.
  68. *
  69. * Allow lazy loading (don't read the file if it isn't necessary).
  70. */
  71. protected function initialize()
  72. {
  73. $this->check();
  74. $this->read();
  75. }
  76. /**
  77. * Add Event: new link.
  78. *
  79. * @param array $link Link data.
  80. */
  81. public function addLink($link)
  82. {
  83. $this->addEvent(self::CREATED, $link['id']);
  84. }
  85. /**
  86. * Add Event: update existing link.
  87. *
  88. * @param array $link Link data.
  89. */
  90. public function updateLink($link)
  91. {
  92. $this->addEvent(self::UPDATED, $link['id']);
  93. }
  94. /**
  95. * Add Event: delete existing link.
  96. *
  97. * @param array $link Link data.
  98. */
  99. public function deleteLink($link)
  100. {
  101. $this->addEvent(self::DELETED, $link['id']);
  102. }
  103. /**
  104. * Add Event: settings updated.
  105. */
  106. public function updateSettings()
  107. {
  108. $this->addEvent(self::SETTINGS);
  109. }
  110. /**
  111. * Save a new event and write it in the history file.
  112. *
  113. * @param string $status Event key, should be defined as constant.
  114. * @param mixed $id Event item identifier (e.g. link ID).
  115. */
  116. protected function addEvent($status, $id = null)
  117. {
  118. if ($this->history === null) {
  119. $this->initialize();
  120. }
  121. $item = [
  122. 'event' => $status,
  123. 'datetime' => new DateTime(),
  124. 'id' => $id !== null ? $id : '',
  125. ];
  126. $this->history = array_merge([$item], $this->history);
  127. $this->write();
  128. }
  129. /**
  130. * Check that the history file is writable.
  131. * Create the file if it doesn't exist.
  132. *
  133. * @throws Exception if it isn't writable.
  134. */
  135. protected function check()
  136. {
  137. if (! is_file($this->historyFilePath)) {
  138. FileUtils::writeFlatDB($this->historyFilePath, []);
  139. }
  140. if (! is_writable($this->historyFilePath)) {
  141. throw new Exception('History file isn\'t readable or writable');
  142. }
  143. }
  144. /**
  145. * Read JSON history file.
  146. */
  147. protected function read()
  148. {
  149. $this->history = FileUtils::readFlatDB($this->historyFilePath, []);
  150. if ($this->history === false) {
  151. throw new Exception('Could not parse history file');
  152. }
  153. }
  154. /**
  155. * Write JSON history file and delete old entries.
  156. */
  157. protected function write()
  158. {
  159. $comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
  160. foreach ($this->history as $key => $value) {
  161. if ($value['datetime'] < $comparaison) {
  162. unset($this->history[$key]);
  163. }
  164. }
  165. FileUtils::writeFlatDB($this->historyFilePath, array_values($this->history));
  166. }
  167. /**
  168. * Get the History.
  169. *
  170. * @return array
  171. */
  172. public function getHistory()
  173. {
  174. if ($this->history === null) {
  175. $this->initialize();
  176. }
  177. return $this->history;
  178. }
  179. }