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