1<?php 2 3namespace dokuwiki\ChangeLog; 4 5/** 6 * methods for handling of changelog of pages or media files 7 */ 8abstract class ChangeLog 9{ 10 /** @var string */ 11 protected $id; 12 /** @var int */ 13 protected $currentRevision; 14 /** @var int */ 15 protected $chunk_size; 16 /** @var array */ 17 protected $cache; 18 19 /** 20 * Constructor 21 * 22 * @param string $id page id 23 * @param int $chunk_size maximum block size read from file 24 */ 25 public function __construct($id, $chunk_size = 8192) 26 { 27 global $cache_revinfo; 28 29 $this->cache =& $cache_revinfo; 30 if (!isset($this->cache[$id])) { 31 $this->cache[$id] = array(); 32 } 33 34 $this->id = $id; 35 $this->setChunkSize($chunk_size); 36 // set property currentRevision and cache prior to getRevisionInfo($currentRev) call 37 $this->getCurrentRevisionInfo(); 38 } 39 40 /** 41 * Set chunk size for file reading 42 * Chunk size zero let read whole file at once 43 * 44 * @param int $chunk_size maximum block size read from file 45 */ 46 public function setChunkSize($chunk_size) 47 { 48 if (!is_numeric($chunk_size)) $chunk_size = 0; 49 50 $this->chunk_size = (int)max($chunk_size, 0); 51 } 52 53 /** 54 * Returns path to changelog 55 * 56 * @return string path to file 57 */ 58 abstract protected function getChangelogFilename(); 59 60 /** 61 * Returns path to current page/media 62 * 63 * @return string path to file 64 */ 65 abstract protected function getFilename(); 66 67 /** 68 * Get the changelog information for a specific page id and revision (timestamp) 69 * 70 * Adjacent changelog lines are optimistically parsed and cached to speed up 71 * consecutive calls to getRevisionInfo. For large changelog files, only the chunk 72 * containing the requested changelog line is read. 73 * 74 * @param int $rev revision timestamp 75 * @return bool|array false or array with entries: 76 * - date: unix timestamp 77 * - ip: IPv4 address (127.0.0.1) 78 * - type: log line type 79 * - id: page id 80 * - user: user name 81 * - sum: edit summary (or action reason) 82 * - extra: extra data (varies by line type) 83 * - sizechange: change of filesize 84 * 85 * @author Ben Coburn <btcoburn@silicodon.net> 86 * @author Kate Arzamastseva <pshns@ukr.net> 87 */ 88 public function getRevisionInfo($rev) 89 { 90 $rev = max(0, $rev); 91 if (!$rev) return false; 92 93 // check if it's already in the memory cache 94 if (isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) { 95 return $this->cache[$this->id][$rev]; 96 } 97 98 //read lines from changelog 99 list($fp, $lines) = $this->readloglines($rev); 100 if ($fp) { 101 fclose($fp); 102 } 103 if (empty($lines)) return false; 104 105 // parse and cache changelog lines 106 foreach ($lines as $value) { 107 $info = parseChangelogLine($value); 108 if ($info !== false) { 109 $this->cache[$this->id][$info['date']] ??= $info; 110 } 111 } 112 if (!isset($this->cache[$this->id][$rev])) { 113 return false; 114 } 115 return $this->cache[$this->id][$rev]; 116 } 117 118 /** 119 * Return a list of page revisions numbers 120 * 121 * Does not guarantee that the revision exists in the attic, 122 * only that a line with the date exists in the changelog. 123 * By default the current revision is skipped. 124 * 125 * The current revision is automatically skipped when the page exists. 126 * See $INFO['meta']['last_change'] for the current revision. 127 * A negative $first let read the current revision too. 128 * 129 * For efficiency, the log lines are parsed and cached for later 130 * calls to getRevisionInfo. Large changelog files are read 131 * backwards in chunks until the requested number of changelog 132 * lines are recieved. 133 * 134 * @param int $first skip the first n changelog lines 135 * @param int $num number of revisions to return 136 * @return array with the revision timestamps 137 * 138 * @author Ben Coburn <btcoburn@silicodon.net> 139 * @author Kate Arzamastseva <pshns@ukr.net> 140 */ 141 public function getRevisions($first, $num) 142 { 143 $revs = array(); 144 $lines = array(); 145 $count = 0; 146 147 $num = max($num, 0); 148 if ($num == 0) { 149 return $revs; 150 } 151 152 if ($first < 0) { 153 $first = 0; 154 } else { 155 if (file_exists($this->getFilename()) && !$this->isExternalEdition()) { 156 // skip current revision if the page exist and not external edition 157 $first = max($first + 1, 0); 158 } 159 } 160 161 $file = $this->getChangelogFilename(); 162 163 if (!file_exists($file)) { 164 return $revs; 165 } 166 if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 167 // read whole file 168 $lines = file($file); 169 if ($lines === false) { 170 return $revs; 171 } 172 } else { 173 // read chunks backwards 174 $fp = fopen($file, 'rb'); // "file pointer" 175 if ($fp === false) { 176 return $revs; 177 } 178 fseek($fp, 0, SEEK_END); 179 $tail = ftell($fp); 180 181 // chunk backwards 182 $finger = max($tail - $this->chunk_size, 0); 183 while ($count < $num + $first) { 184 $nl = $this->getNewlinepointer($fp, $finger); 185 186 // was the chunk big enough? if not, take another bite 187 if ($nl > 0 && $tail <= $nl) { 188 $finger = max($finger - $this->chunk_size, 0); 189 continue; 190 } else { 191 $finger = $nl; 192 } 193 194 // read chunk 195 $chunk = ''; 196 $read_size = max($tail - $finger, 0); // found chunk size 197 $got = 0; 198 while ($got < $read_size && !feof($fp)) { 199 $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0)); 200 if ($tmp === false) { 201 break; 202 } //error state 203 $got += strlen($tmp); 204 $chunk .= $tmp; 205 } 206 $tmp = explode("\n", $chunk); 207 array_pop($tmp); // remove trailing newline 208 209 // combine with previous chunk 210 $count += count($tmp); 211 $lines = array_merge($tmp, $lines); 212 213 // next chunk 214 if ($finger == 0) { 215 break; 216 } else { // already read all the lines 217 $tail = $finger; 218 $finger = max($tail - $this->chunk_size, 0); 219 } 220 } 221 fclose($fp); 222 } 223 224 // skip parsing extra lines 225 $num = max(min(count($lines) - $first, $num), 0); 226 if ($first > 0 && $num > 0) { 227 $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); 228 } else { 229 if ($first > 0 && $num == 0) { 230 $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); 231 } elseif ($first == 0 && $num > 0) { 232 $lines = array_slice($lines, max(count($lines) - $num, 0)); 233 } 234 } 235 236 // handle lines in reverse order 237 for ($i = count($lines) - 1; $i >= 0; $i--) { 238 $info = parseChangelogLine($lines[$i]); 239 if ($info !== false) { 240 $this->cache[$this->id][$info['date']] ??= $info; 241 $revs[] = $info['date']; 242 } 243 } 244 245 return $revs; 246 } 247 248 /** 249 * Get the nth revision left or right handside for a specific page id and revision (timestamp) 250 * 251 * For large changelog files, only the chunk containing the 252 * reference revision $rev is read and sometimes a next chunck. 253 * 254 * Adjacent changelog lines are optimistically parsed and cached to speed up 255 * consecutive calls to getRevisionInfo. 256 * 257 * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) 258 * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev 259 * @return bool|int 260 * timestamp of the requested revision 261 * otherwise false 262 */ 263 public function getRelativeRevision($rev, $direction) 264 { 265 $rev = max($rev, 0); 266 $direction = (int)$direction; 267 268 //no direction given or last rev, so no follow-up 269 if (!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { 270 return false; 271 } 272 273 //get lines from changelog 274 list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev); 275 if (empty($lines)) return false; 276 277 // look for revisions later/earlier than $rev, when founded count till the wanted revision is reached 278 // also parse and cache changelog lines for getRevisionInfo(). 279 $revcounter = 0; 280 $relativerev = false; 281 $checkotherchunck = true; //always runs once 282 while (!$relativerev && $checkotherchunck) { 283 $info = array(); 284 //parse in normal or reverse order 285 $count = count($lines); 286 if ($direction > 0) { 287 $start = 0; 288 $step = 1; 289 } else { 290 $start = $count - 1; 291 $step = -1; 292 } 293 for ($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { 294 $info = parseChangelogLine($lines[$i]); 295 if ($info !== false) { 296 $this->cache[$this->id][$info['date']] ??= $info; 297 //look for revs older/earlier then reference $rev and select $direction-th one 298 if (($direction > 0 && $info['date'] > $rev) || ($direction < 0 && $info['date'] < $rev)) { 299 $revcounter++; 300 if ($revcounter == abs($direction)) { 301 $relativerev = $info['date']; 302 } 303 } 304 } 305 } 306 307 //true when $rev is found, but not the wanted follow-up. 308 $checkotherchunck = $fp 309 && ($info['date'] == $rev || ($revcounter > 0 && !$relativerev)) 310 && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); 311 312 if ($checkotherchunck) { 313 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction); 314 315 if (empty($lines)) break; 316 } 317 } 318 if ($fp) { 319 fclose($fp); 320 } 321 322 return $relativerev; 323 } 324 325 /** 326 * Returns revisions around rev1 and rev2 327 * When available it returns $max entries for each revision 328 * 329 * @param int $rev1 oldest revision timestamp 330 * @param int $rev2 newest revision timestamp (0 looks up last revision) 331 * @param int $max maximum number of revisions returned 332 * @return array with two arrays with revisions surrounding rev1 respectively rev2 333 */ 334 public function getRevisionsAround($rev1, $rev2, $max = 50) 335 { 336 $max = intval(abs($max) / 2) * 2 + 1; 337 $rev1 = max($rev1, 0); 338 $rev2 = max($rev2, 0); 339 340 if ($rev2) { 341 if ($rev2 < $rev1) { 342 $rev = $rev2; 343 $rev2 = $rev1; 344 $rev1 = $rev; 345 } 346 } else { 347 //empty right side means a removed page. Look up last revision. 348 $rev2 = $this->currentRevision(); 349 } 350 //collect revisions around rev2 351 list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max); 352 353 if (empty($revs2)) return array(array(), array()); 354 355 //collect revisions around rev1 356 $index = array_search($rev1, $allrevs); 357 if ($index === false) { 358 //no overlapping revisions 359 list($revs1, , , , ,) = $this->retrieveRevisionsAround($rev1, $max); 360 if (empty($revs1)) $revs1 = array(); 361 } else { 362 //revisions overlaps, reuse revisions around rev2 363 $lastrev = array_pop($allrevs); //keep last entry that could be external edit 364 $revs1 = $allrevs; 365 while ($head > 0) { 366 for ($i = count($lines) - 1; $i >= 0; $i--) { 367 $info = parseChangelogLine($lines[$i]); 368 if ($info !== false) { 369 $this->cache[$this->id][$info['date']] ??= $info; 370 $revs1[] = $info['date']; 371 $index++; 372 373 if ($index > intval($max / 2)) break 2; 374 } 375 } 376 377 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 378 } 379 sort($revs1); 380 $revs1[] = $lastrev; //push back last entry 381 382 //return wanted selection 383 $revs1 = array_slice($revs1, max($index - intval($max / 2), 0), $max); 384 } 385 386 return array(array_reverse($revs1), array_reverse($revs2)); 387 } 388 389 390 /** 391 * Checks if the ID has old revisons 392 * @return boolean 393 */ 394 public function hasRevisions() { 395 $file = $this->getChangelogFilename(); 396 return file_exists($file); 397 } 398 399 /** 400 * Returns lines from changelog. 401 * If file larger than $chuncksize, only chunck is read that could contain $rev. 402 * 403 * When reference timestamp $rev is outside time range of changelog, readloglines() will return 404 * lines in first or last chunk, but they obviously does not contain $rev. 405 * 406 * @param int $rev revision timestamp 407 * @return array|false 408 * if success returns array(fp, array(changeloglines), $head, $tail, $eof) 409 * where fp only defined for chuck reading, needs closing. 410 * otherwise false 411 */ 412 protected function readloglines($rev) 413 { 414 $file = $this->getChangelogFilename(); 415 416 if (!file_exists($file)) { 417 return false; 418 } 419 420 $fp = null; 421 $head = 0; 422 $tail = 0; 423 $eof = 0; 424 425 if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 426 // read whole file 427 $lines = file($file); 428 if ($lines === false) { 429 return false; 430 } 431 } else { 432 // read by chunk 433 $fp = fopen($file, 'rb'); // "file pointer" 434 if ($fp === false) { 435 return false; 436 } 437 $head = 0; 438 fseek($fp, 0, SEEK_END); 439 $eof = ftell($fp); 440 $tail = $eof; 441 442 // find chunk 443 while ($tail - $head > $this->chunk_size) { 444 $finger = $head + intval(($tail - $head) / 2); 445 $finger = $this->getNewlinepointer($fp, $finger); 446 $tmp = fgets($fp); 447 if ($finger == $head || $finger == $tail) { 448 break; 449 } 450 $info = parseChangelogLine($tmp); 451 $finger_rev = $info['date']; 452 453 if ($finger_rev > $rev) { 454 $tail = $finger; 455 } else { 456 $head = $finger; 457 } 458 } 459 460 if ($tail - $head < 1) { 461 // cound not find chunk, assume requested rev is missing 462 fclose($fp); 463 return false; 464 } 465 466 $lines = $this->readChunk($fp, $head, $tail); 467 } 468 return array( 469 $fp, 470 $lines, 471 $head, 472 $tail, 473 $eof, 474 ); 475 } 476 477 /** 478 * Read chunk and return array with lines of given chunck. 479 * Has no check if $head and $tail are really at a new line 480 * 481 * @param resource $fp resource filepointer 482 * @param int $head start point chunck 483 * @param int $tail end point chunck 484 * @return array lines read from chunck 485 */ 486 protected function readChunk($fp, $head, $tail) 487 { 488 $chunk = ''; 489 $chunk_size = max($tail - $head, 0); // found chunk size 490 $got = 0; 491 fseek($fp, $head); 492 while ($got < $chunk_size && !feof($fp)) { 493 $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); 494 if ($tmp === false) { //error state 495 break; 496 } 497 $got += strlen($tmp); 498 $chunk .= $tmp; 499 } 500 $lines = explode("\n", $chunk); 501 array_pop($lines); // remove trailing newline 502 return $lines; 503 } 504 505 /** 506 * Set pointer to first new line after $finger and return its position 507 * 508 * @param resource $fp filepointer 509 * @param int $finger a pointer 510 * @return int pointer 511 */ 512 protected function getNewlinepointer($fp, $finger) 513 { 514 fseek($fp, $finger); 515 $nl = $finger; 516 if ($finger > 0) { 517 fgets($fp); // slip the finger forward to a new line 518 $nl = ftell($fp); 519 } 520 return $nl; 521 } 522 523 /** 524 * Check whether given revision is the current page 525 * 526 * @param int $rev timestamp of current page 527 * @return bool true if $rev is current revision, otherwise false 528 */ 529 public function isCurrentRevision($rev) 530 { 531 return $rev == $this->currentRevision(); 532 } 533 534 /** 535 * Return an existing revision for a specific date which is 536 * the current one or younger or equal then the date 537 * 538 * @param number $date_at timestamp 539 * @return string revision ('' for current) 540 */ 541 public function getLastRevisionAt($date_at) 542 { 543 //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current 544 if (file_exists($this->getFilename()) && $date_at >= @filemtime($this->getFilename())) { 545 return ''; 546 } else { 547 if ($rev = $this->getRelativeRevision($date_at + 1, -1)) { //+1 to get also the requested date revision 548 return $rev; 549 } else { 550 return false; 551 } 552 } 553 } 554 555 /** 556 * Returns the next lines of the changelog of the chunck before head or after tail 557 * 558 * @param resource $fp filepointer 559 * @param int $head position head of last chunk 560 * @param int $tail position tail of last chunk 561 * @param int $direction positive forward, negative backward 562 * @return array with entries: 563 * - $lines: changelog lines of readed chunk 564 * - $head: head of chunk 565 * - $tail: tail of chunk 566 */ 567 protected function readAdjacentChunk($fp, $head, $tail, $direction) 568 { 569 if (!$fp) return array(array(), $head, $tail); 570 571 if ($direction > 0) { 572 //read forward 573 $head = $tail; 574 $tail = $head + intval($this->chunk_size * (2 / 3)); 575 $tail = $this->getNewlinepointer($fp, $tail); 576 } else { 577 //read backward 578 $tail = $head; 579 $head = max($tail - $this->chunk_size, 0); 580 while (true) { 581 $nl = $this->getNewlinepointer($fp, $head); 582 // was the chunk big enough? if not, take another bite 583 if ($nl > 0 && $tail <= $nl) { 584 $head = max($head - $this->chunk_size, 0); 585 } else { 586 $head = $nl; 587 break; 588 } 589 } 590 } 591 592 //load next chunck 593 $lines = $this->readChunk($fp, $head, $tail); 594 return array($lines, $head, $tail); 595 } 596 597 /** 598 * Collect the $max revisions near to the timestamp $rev 599 * 600 * Ideally, half of retrieved timestamps are older than $rev, another half are newer. 601 * The returned array $requestedrevs may not contain the reference timestamp $rev 602 * when it does not match any revision value recorded in changelog. 603 * 604 * @param int $rev revision timestamp 605 * @param int $max maximum number of revisions to be returned 606 * @return bool|array 607 * return array with entries: 608 * - $requestedrevs: array of with $max revision timestamps 609 * - $revs: all parsed revision timestamps 610 * - $fp: filepointer only defined for chuck reading, needs closing. 611 * - $lines: non-parsed changelog lines before the parsed revisions 612 * - $head: position of first readed changelogline 613 * - $lasttail: position of end of last readed changelogline 614 * otherwise false 615 */ 616 protected function retrieveRevisionsAround($rev, $max) 617 { 618 $revs = array(); 619 $aftercount = $beforecount = 0; 620 621 //get lines from changelog 622 list($fp, $lines, $starthead, $starttail, $eof) = $this->readloglines($rev); 623 if (empty($lines)) return false; 624 625 //parse changelog lines in chunk, and read forward more chunks until $max/2 is reached 626 $head = $starthead; 627 $tail = $starttail; 628 while (count($lines) > 0) { 629 foreach ($lines as $line) { 630 $info = parseChangelogLine($line); 631 if ($info !== false) { 632 $this->cache[$this->id][$info['date']] ??= $info; 633 $revs[] = $info['date']; 634 if ($info['date'] >= $rev) { 635 //count revs after reference $rev 636 $aftercount++; 637 if ($aftercount == 1) $beforecount = count($revs); 638 } 639 //enough revs after reference $rev? 640 if ($aftercount > intval($max / 2)) break 2; 641 } 642 } 643 //retrieve next chunk 644 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1); 645 } 646 $lasttail = $tail; 647 648 // add a possible revision of external edit, create or deletion 649 if ($lasttail == $eof && $aftercount <= intval($max / 2) && $this->isExternalEdition()) { 650 $revs[] = $this->currentRevision; 651 $aftercount++; 652 } 653 654 if ($aftercount == 0) { 655 //given timestamp $rev is newer than the most recent line in chunk 656 return false; //FIXME: or proceed to collect older revisions? 657 } 658 659 //read more chunks backward until $max/2 is reached and total number of revs is equal to $max 660 $lines = array(); 661 $i = 0; 662 if ($aftercount > 0) { 663 $head = $starthead; 664 $tail = $starttail; 665 while ($head > 0) { 666 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 667 668 for ($i = count($lines) - 1; $i >= 0; $i--) { 669 $info = parseChangelogLine($lines[$i]); 670 if ($info !== false) { 671 $this->cache[$this->id][$info['date']] ??= $info; 672 $revs[] = $info['date']; 673 $beforecount++; 674 //enough revs before reference $rev? 675 if ($beforecount > max(intval($max / 2), $max - $aftercount)) break 2; 676 } 677 } 678 } 679 } 680 //keep only non-parsed lines 681 $lines = array_slice($lines, 0, $i); 682 683 sort($revs); 684 685 //trunk desired selection 686 $requestedrevs = array_slice($revs, -$max, $max); 687 688 return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail); 689 } 690 691 /** 692 * Get the current revision information, considering external edit, create or deletion 693 * 694 * The "current" revison is the last timestamp of the page in the context of changelog. 695 * However it is often recognised that is in sight now from the DokuWiki user perspective. 696 * The current page is accessible without any revision identifier (eg. doku.php?id=foo), 697 * but it has unique modification time of the source txt file and kept in changelog. 698 * When the page is deleted by saving blank text in the DokuWiki editor, the deletion 699 * time is to be kept as its revision identifier in the changelog. 700 * 701 * External edit will break consistency between the file and changelog. A page source 702 * file might be modified, created or deleted without using DokuWiki editor, instead 703 * by accessing direct to the file stored in data directory via server console. 704 * Such editions are never recorded in changelog. However after external file edit, 705 * now we can see new "current" content of the edited page! 706 * 707 * A tentative revision should be assigned for the external edition to handle whole 708 * revisions successfully in DokuWiki revision list and diff view interface. 709 * As far as the source file of the edition exists, a unique revision can be decided 710 * using function filemtime(), but it could be unknown if the foo.txt file had deleted 711 * or moved to foo.bak file. 712 * In such case, we assume unknown revision as "last timestamp in chagelog" +1 713 * to ensure that current one should be newer than any revisions in changelog. 714 * Another case of external edit: when foo.bak file moved back to foo.txt, the current 715 * one could become older than latest timestamp in changelog. In this case, we should 716 * assume the revison as "last timestamp in chagelog" +1, instead of its timestamp. 717 * 718 * @return bool|array false when page had never existed or array with entries: 719 * - date: revision identifier (timestamp or last revision +1) 720 * - ip: IPv4 address (127.0.0.1) 721 * - type: log line type 722 * - id: id of page or media 723 * - user: user name 724 * - sum: edit summary (or action reason) 725 * - extra: extra data (varies by line type) 726 * - sizechange: change of filesize 727 * - timestamp: timestamp or 'unknown' (key set only for external edition) 728 * 729 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 730 */ 731 public function getCurrentRevisionInfo() 732 { 733 global $lang; 734 735 if (isset($this->currentRevision)) return $this->getRevisionInfo($this->currentRevision); 736 737 // get revision id from the item file timestamp and chagelog 738 $fileRev = @filemtime($this->getFilename()); // false when the file not exist 739 $lastRev = @$this->getRevisions(-1, 1)[0]; // null when failed 740 741 if (!$fileRev && $lastRev === null) { // has never existed 742 return false; 743 } elseif ($fileRev === $lastRev) { // not external edit 744 $this->currentRevision = $lastRev; 745 $this->cache[$this->id][$this->currentRevision] += [ 746 'current' => true, 747 ]; 748 return $this->getRevisionInfo($this->currentRevision); 749 } 750 751 if (!$fileRev && $lastRev) { // item file does not exist 752 // check consistency against changelog 753 $revInfo = $this->getRevisionInfo($lastRev); 754 if ($revInfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 755 $this->currentRevision = $lastRev; 756 $this->cache[$this->id][$this->currentRevision] += [ 757 'current' => true, 758 ]; 759 return $this->getRevisionInfo($this->currentRevision); 760 } 761 762 // externally deleted 763 $revInfo = [ 764 'date' => $lastRev +1, 765 'ip' => '127.0.0.1', 766 'type' => DOKU_CHANGE_TYPE_DELETE, 767 'id' => $this->id, 768 'user' => '', 769 'sum' => $lang['deleted'] .' - '. $lang['external_edit'], 770 'extra' => '', 771 'sizechange' => -io_getSizeFile($this->getFilename($lastRev)), 772 'current' => true, 773 'timestamp' => 'unknown', 774 ]; 775 776 } elseif ($fileRev) { // item file exist 777 // here, file timestamp is different with last revision in changelog 778 $isJustCreated = !boolval($lastRev); 779 $filesize_new = filesize($this->getFilename()); 780 $filesize_old = $lastRev ? io_getSizeFile($this->getFilename($lastRev)) : 0; 781 $sizechange = $filesize_new - $filesize_old; 782 783 if ($isJustCreated) { // lastRev is null 784 $rev = $timestamp = $fileRev; 785 $sum = $lang['created'] .' - '. $lang['external_edit']; 786 } elseif ($fileRev > $lastRev) { 787 $rev = $timestamp = $fileRev; 788 $sum = $lang['external_edit']; 789 } else { 790 // $fileRev is older than $lastRev, externally reverted an old file 791 $rev = max($fileRev, $lastRev +1); 792 $timestamp = 'unknown'; 793 $sum = $lang['external_edit']; 794 } 795 796 // externally created or edited 797 $revInfo = [ 798 'date' => $rev, 799 'ip' => '127.0.0.1', 800 'type' => $isJustCreated ? DOKU_CHANGE_TYPE_CREATE : DOKU_CHANGE_TYPE_EDIT, 801 'id' => $this->id, 802 'user' => '', 803 'sum' => $sum, 804 'extra' => '', 805 'sizechange' => $sizechange, 806 'current' => true, 807 'timestamp' => $timestamp, 808 ]; 809 } 810 811 // cache current revision information of external edition 812 $this->currentRevision = $revInfo['date']; 813 $this->cache[$this->id][$this->currentRevision] = $revInfo; 814 return $this->getRevisionInfo($this->currentRevision); 815 } 816 817 /** 818 * Checks if the revision is external edition 819 * @return boolean 820 */ 821 public function isExternalEdition($rev =null) 822 { 823 if ($rev === 'current' || $rev === null) { 824 $revInfo = $this->getCurrentRevisionInfo(); 825 } elseif (is_int($rev)) { 826 $revInfo = $this->getRevisionInfo($rev); 827 } elseif (is_array($rev)) { 828 $revInfo = $rev; 829 } 830 return array_key_exists('timestamp', $revInfo); 831 } 832 833 /** 834 * Return the current revision identifer 835 * @return int|false 836 */ 837 public function currentRevision() 838 { 839 if (!isset($this->currentRevision)) { 840 // set ChangeLog::currentRevision property 841 $this->getCurrentRevisionInfo(); 842 } 843 return $this->currentRevision; 844 } 845} 846