You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

263 lines
7.9 KiB

  1. <?php
  2. namespace dokuwiki\ChangeLog;
  3. use dokuwiki\Utf8\PhpString;
  4. /**
  5. * Provides methods for handling of changelog
  6. */
  7. trait ChangeLogTrait
  8. {
  9. /**
  10. * Adds an entry to the changelog file
  11. *
  12. * @return array added log line as revision info
  13. */
  14. abstract public function addLogEntry(array $info, $timestamp = null);
  15. /**
  16. * Parses a changelog line into its components
  17. *
  18. * @param string $line changelog line
  19. * @return array|bool parsed line or false
  20. * @author Ben Coburn <btcoburn@silicodon.net>
  21. *
  22. */
  23. public static function parseLogLine($line)
  24. {
  25. $info = sexplode("\t", rtrim($line, "\n"), 8);
  26. if ($info[3]) { // we need at least the page id to consider it a valid line
  27. return [
  28. 'date' => (int)$info[0], // unix timestamp
  29. 'ip' => $info[1], // IP address (127.0.0.1)
  30. 'type' => $info[2], // log line type
  31. 'id' => $info[3], // page id
  32. 'user' => $info[4], // user name
  33. 'sum' => $info[5], // edit summary (or action reason)
  34. 'extra' => $info[6], // extra data (varies by line type)
  35. 'sizechange' => ($info[7] != '') ? (int)$info[7] : null, // size difference in bytes
  36. ];
  37. } else {
  38. return false;
  39. }
  40. }
  41. /**
  42. * Build a changelog line from its components
  43. *
  44. * @param array $info Revision info structure
  45. * @param int $timestamp log line date (optional)
  46. * @return string changelog line
  47. */
  48. public static function buildLogLine(array &$info, $timestamp = null)
  49. {
  50. $strip = ["\t", "\n"];
  51. $entry = [
  52. 'date' => $timestamp ?? $info['date'],
  53. 'ip' => $info['ip'],
  54. 'type' => str_replace($strip, '', $info['type']),
  55. 'id' => $info['id'],
  56. 'user' => $info['user'],
  57. 'sum' => PhpString::substr(str_replace($strip, '', $info['sum'] ?? ''), 0, 255),
  58. 'extra' => str_replace($strip, '', $info['extra']),
  59. 'sizechange' => $info['sizechange']
  60. ];
  61. $info = $entry;
  62. return implode("\t", $entry) . "\n";
  63. }
  64. /**
  65. * Returns path to changelog
  66. *
  67. * @return string path to file
  68. */
  69. abstract protected function getChangelogFilename();
  70. /**
  71. * Checks if the ID has old revisions
  72. * @return boolean
  73. */
  74. public function hasRevisions()
  75. {
  76. $logfile = $this->getChangelogFilename();
  77. return file_exists($logfile);
  78. }
  79. /** @var int */
  80. protected $chunk_size;
  81. /**
  82. * Set chunk size for file reading
  83. * Chunk size zero let read whole file at once
  84. *
  85. * @param int $chunk_size maximum block size read from file
  86. */
  87. public function setChunkSize($chunk_size)
  88. {
  89. if (!is_numeric($chunk_size)) $chunk_size = 0;
  90. $this->chunk_size = max($chunk_size, 0);
  91. }
  92. /**
  93. * Returns lines from changelog.
  94. * If file larger than $chunk_size, only chunk is read that could contain $rev.
  95. *
  96. * When reference timestamp $rev is outside time range of changelog, readloglines() will return
  97. * lines in first or last chunk, but they obviously does not contain $rev.
  98. *
  99. * @param int $rev revision timestamp
  100. * @return array|false
  101. * if success returns array(fp, array(changeloglines), $head, $tail, $eof)
  102. * where fp only defined for chuck reading, needs closing.
  103. * otherwise false
  104. */
  105. protected function readloglines($rev)
  106. {
  107. $file = $this->getChangelogFilename();
  108. if (!file_exists($file)) {
  109. return false;
  110. }
  111. $fp = null;
  112. $head = 0;
  113. $tail = 0;
  114. $eof = 0;
  115. if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
  116. // read whole file
  117. $lines = file($file);
  118. if ($lines === false) {
  119. return false;
  120. }
  121. } else {
  122. // read by chunk
  123. $fp = fopen($file, 'rb'); // "file pointer"
  124. if ($fp === false) {
  125. return false;
  126. }
  127. fseek($fp, 0, SEEK_END);
  128. $eof = ftell($fp);
  129. $tail = $eof;
  130. // find chunk
  131. while ($tail - $head > $this->chunk_size) {
  132. $finger = $head + (int)(($tail - $head) / 2);
  133. $finger = $this->getNewlinepointer($fp, $finger);
  134. $tmp = fgets($fp);
  135. if ($finger == $head || $finger == $tail) {
  136. break;
  137. }
  138. $info = $this->parseLogLine($tmp);
  139. $finger_rev = $info['date'];
  140. if ($finger_rev > $rev) {
  141. $tail = $finger;
  142. } else {
  143. $head = $finger;
  144. }
  145. }
  146. if ($tail - $head < 1) {
  147. // could not find chunk, assume requested rev is missing
  148. fclose($fp);
  149. return false;
  150. }
  151. $lines = $this->readChunk($fp, $head, $tail);
  152. }
  153. return [$fp, $lines, $head, $tail, $eof];
  154. }
  155. /**
  156. * Read chunk and return array with lines of given chunk.
  157. * Has no check if $head and $tail are really at a new line
  158. *
  159. * @param resource $fp resource file pointer
  160. * @param int $head start point chunk
  161. * @param int $tail end point chunk
  162. * @return array lines read from chunk
  163. */
  164. protected function readChunk($fp, $head, $tail)
  165. {
  166. $chunk = '';
  167. $chunk_size = max($tail - $head, 0); // found chunk size
  168. $got = 0;
  169. fseek($fp, $head);
  170. while ($got < $chunk_size && !feof($fp)) {
  171. $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0));
  172. if ($tmp === false) { //error state
  173. break;
  174. }
  175. $got += strlen($tmp);
  176. $chunk .= $tmp;
  177. }
  178. $lines = explode("\n", $chunk);
  179. array_pop($lines); // remove trailing newline
  180. return $lines;
  181. }
  182. /**
  183. * Set pointer to first new line after $finger and return its position
  184. *
  185. * @param resource $fp file pointer
  186. * @param int $finger a pointer
  187. * @return int pointer
  188. */
  189. protected function getNewlinepointer($fp, $finger)
  190. {
  191. fseek($fp, $finger);
  192. $nl = $finger;
  193. if ($finger > 0) {
  194. fgets($fp); // slip the finger forward to a new line
  195. $nl = ftell($fp);
  196. }
  197. return $nl;
  198. }
  199. /**
  200. * Returns the next lines of the changelog of the chunk before head or after tail
  201. *
  202. * @param resource $fp file pointer
  203. * @param int $head position head of last chunk
  204. * @param int $tail position tail of last chunk
  205. * @param int $direction positive forward, negative backward
  206. * @return array with entries:
  207. * - $lines: changelog lines of read chunk
  208. * - $head: head of chunk
  209. * - $tail: tail of chunk
  210. */
  211. protected function readAdjacentChunk($fp, $head, $tail, $direction)
  212. {
  213. if (!$fp) return [[], $head, $tail];
  214. if ($direction > 0) {
  215. //read forward
  216. $head = $tail;
  217. $tail = $head + (int)($this->chunk_size * (2 / 3));
  218. $tail = $this->getNewlinepointer($fp, $tail);
  219. } else {
  220. //read backward
  221. $tail = $head;
  222. $head = max($tail - $this->chunk_size, 0);
  223. while (true) {
  224. $nl = $this->getNewlinepointer($fp, $head);
  225. // was the chunk big enough? if not, take another bite
  226. if ($nl > 0 && $tail <= $nl) {
  227. $head = max($head - $this->chunk_size, 0);
  228. } else {
  229. $head = $nl;
  230. break;
  231. }
  232. }
  233. }
  234. //load next chunk
  235. $lines = $this->readChunk($fp, $head, $tail);
  236. return [$lines, $head, $tail];
  237. }
  238. }